]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
Fix transition max duration
[kdenlive] / src / clipitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21
22 #include <QPainter>
23 #include <QTimer>
24 #include <QStyleOptionGraphicsItem>
25 #include <QGraphicsScene>
26 #include <QGraphicsView>
27 #include <QScrollBar>
28 #include <QMimeData>
29 #include <QApplication>
30
31 #include <KDebug>
32
33 #include "clipitem.h"
34 #include "customtrackview.h"
35 #include "renderer.h"
36 #include "docclipbase.h"
37 #include "transition.h"
38 #include "events.h"
39 #include "kdenlivesettings.h"
40 #include "kthumb.h"
41
42 ClipItem::ClipItem(DocClipBase *clip, ItemInfo info, double scale, double fps)
43         : AbstractClipItem(info, QRectF()), m_clip(clip), m_resizeMode(NONE), m_grabPoint(0), m_maxTrack(0), m_hasThumbs(false), startThumbTimer(NULL), endThumbTimer(NULL), m_effectsCounter(1), audioThumbWasDrawn(false), m_opacity(1.0), m_timeLine(0), m_thumbsRequested(0), m_hover(false) {
44     QRectF rect((double) info.startPos.frames(fps) * scale, (double)(info.track * KdenliveSettings::trackheight() + 1), (double)(info.endPos - info.startPos).frames(fps) * scale, (double)(KdenliveSettings::trackheight() - 1));
45     setRect(rect);
46     kDebug() << "/////  NEW CLIP RECT: " << rect;
47     m_fps = fps;
48     m_xml = clip->toXML();
49     m_clipName = clip->name();
50     m_producer = clip->getId();
51     m_clipType = clip->clipType();
52     m_cropStart = GenTime();
53
54     m_maxDuration = m_cropDuration;
55     setAcceptDrops(true);
56     audioThumbReady = clip->audioThumbCreated();
57     /*
58       m_cropStart = xml.attribute("in", 0).toInt();
59       m_maxDuration = xml.attribute("duration", 0).toInt();
60       if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
61
62       if (duration != -1) m_cropDuration = duration;
63       else m_cropDuration = m_maxDuration;*/
64
65
66     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
67     setAcceptsHoverEvents(true);
68     connect(this , SIGNAL(prepareAudioThumb(double, QPainterPath, int, int)) , this, SLOT(slotPrepareAudioThumb(double, QPainterPath, int, int)));
69
70     setBrush(QColor(141, 166, 215));
71     if (m_clipType == VIDEO || m_clipType == AV) {
72         m_hasThumbs = true;
73         connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
74         connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
75         connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
76         QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
77
78         startThumbTimer = new QTimer(this);
79         startThumbTimer->setSingleShot(true);
80         connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
81         endThumbTimer = new QTimer(this);
82         endThumbTimer->setSingleShot(true);
83         connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
84
85     } else if (m_clipType == COLOR) {
86         m_maxDuration = GenTime(10000, m_fps);
87         QString colour = m_xml.attribute("colour");
88         colour = colour.replace(0, 2, "#");
89         setBrush(QColor(colour.left(7)));
90     } else if (m_clipType == IMAGE) {
91         m_maxDuration = GenTime(10000, m_fps);
92         m_startPix = KThumb::getImage(KUrl(m_xml.attribute("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
93     } else if (m_clipType == AUDIO) {
94         connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
95     }
96 }
97
98
99 ClipItem::~ClipItem() {
100     if (startThumbTimer) delete startThumbTimer;
101     if (endThumbTimer) delete endThumbTimer;
102 }
103
104 void ClipItem::resetThumbs() {
105     slotFetchThumbs();
106     audioThumbCachePic.clear();
107 }
108
109 void ClipItem::slotFetchThumbs() {
110     m_thumbsRequested += 2;
111     emit getThumb((int)m_cropStart.frames(m_fps), (int)(m_cropStart + m_cropDuration).frames(m_fps));
112 }
113
114 void ClipItem::slotGetStartThumb() {
115     m_thumbsRequested++;
116     emit getThumb((int)m_cropStart.frames(m_fps), -1);
117 }
118
119 void ClipItem::slotGetEndThumb() {
120     m_thumbsRequested++;
121     emit getThumb(-1, (int)(m_cropStart + m_cropDuration).frames(m_fps));
122 }
123
124 void ClipItem::slotThumbReady(int frame, QPixmap pix) {
125     if (m_thumbsRequested == 0) return;
126     if (frame == m_cropStart.frames(m_fps)) m_startPix = pix;
127     else m_endPix = pix;
128     update();
129     m_thumbsRequested--;
130 }
131
132 void ClipItem::slotGotAudioData() {
133     audioThumbReady = true;
134     update();
135 }
136
137 int ClipItem::type() const {
138     return AVWIDGET;
139 }
140
141 DocClipBase *ClipItem::baseClip() {
142     return m_clip;
143 }
144
145 QDomElement ClipItem::xml() const {
146     return m_xml;
147 }
148
149 int ClipItem::clipType() {
150     return m_clipType;
151 }
152
153 QString ClipItem::clipName() {
154     return m_clipName;
155 }
156
157 int ClipItem::clipProducer() {
158     return m_producer;
159 }
160
161 void ClipItem::flashClip() {
162     if (m_timeLine == 0) {
163         m_timeLine = new QTimeLine(750, this);
164         connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
165     }
166     m_timeLine->start();
167 }
168
169 void ClipItem::animate(qreal value) {
170     m_opacity = value;
171     update();
172 }
173
174 // virtual
175 void ClipItem::paint(QPainter *painter,
176                      const QStyleOptionGraphicsItem *option,
177                      QWidget *widget) {
178     painter->setOpacity(m_opacity);
179     QBrush paintColor = brush();
180
181     if (isSelected()) paintColor = QBrush(QColor(79, 93, 121));
182     QRectF br = rect();
183     double scale = br.width() / m_cropDuration.frames(m_fps);
184     QRect rectInView = visibleRect();//this is the rect that is visible by the user
185
186     if (rectInView.isNull())
187         return;
188     QPainterPath clippath;
189     clippath.addRect(rectInView);
190
191     int startpixel = (int)(rectInView.x() - rect().x()); //start and endpixel that is viewable from rect()
192
193     if (startpixel < 0)
194         startpixel = 0;
195     int endpixel = rectInView.width() + rectInView.x();
196     if (endpixel < 0)
197         endpixel = 0;
198
199     //painter->setRenderHints(QPainter::Antialiasing);
200
201     QPainterPath roundRectPathUpper = upperRectPart(br), roundRectPathLower = lowerRectPart(br);
202
203     painter->setClipRect(option->exposedRect);
204
205     // build path around clip
206
207     QPainterPath resultClipPath = roundRectPathUpper.united(roundRectPathLower);
208
209     painter->setClipPath(resultClipPath.intersected(clippath), Qt::IntersectClip);
210     //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
211     painter->fillRect(br.intersected(rectInView), paintColor);
212     //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
213
214     // draw thumbnails
215     if (!m_startPix.isNull() && KdenliveSettings::videothumbnails()) {
216         if (m_clipType == IMAGE) {
217             painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
218             QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
219             painter->drawLine(l);
220         } else {
221             painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
222             QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
223             painter->drawLine(l);
224         }
225
226         painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
227         QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
228         painter->drawLine(l2);
229     }
230
231     // draw audio thumbnails
232     if ((m_clipType == AV || m_clipType == AUDIO) && audioThumbReady && KdenliveSettings::audiothumbnails()) {
233
234         QPainterPath path = m_clipType == AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
235         if (m_clipType == AV) painter->fillPath(path, QBrush(QColor(200, 200, 200, 140)));
236
237         int channels = 2;
238         if (scale != framePixelWidth)
239             audioThumbCachePic.clear();
240         emit prepareAudioThumb(scale, path, startpixel, endpixel + 200);//200 more for less missing parts before repaint after scrolling
241         int cropLeft = (int)((m_cropStart).frames(m_fps) * scale);
242         for (int startCache = startpixel - startpixel % 100; startCache < endpixel + 300;startCache += 100) {
243             if (audioThumbCachePic.contains(startCache) && !audioThumbCachePic[startCache].isNull())
244                 painter->drawPixmap((int)(roundRectPathUpper.united(roundRectPathLower).boundingRect().x() + startCache - cropLeft), (int)(path.boundingRect().y()), audioThumbCachePic[startCache]);
245         }
246
247     }
248
249     // draw start / end fades
250     QBrush fades;
251     if (isSelected()) {
252         fades = QBrush(QColor(200, 50, 50, 150));
253     } else fades = QBrush(QColor(200, 200, 200, 200));
254
255     if (m_startFade != 0) {
256         QPainterPath fadeInPath;
257         fadeInPath.moveTo(br.x() , br.y());
258         fadeInPath.lineTo(br.x() , br.y() + br.height());
259         fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
260         fadeInPath.closeSubpath();
261         painter->fillPath(fadeInPath, fades);
262         if (isSelected()) {
263             QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
264             painter->drawLine(l);
265         }
266     }
267     if (m_endFade != 0) {
268         QPainterPath fadeOutPath;
269         fadeOutPath.moveTo(br.x() + br.width(), br.y());
270         fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
271         fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
272         fadeOutPath.closeSubpath();
273         painter->fillPath(fadeOutPath, fades);
274         if (isSelected()) {
275             QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
276             painter->drawLine(l);
277         }
278     }
279
280     QPen pen = painter->pen();
281     pen.setColor(Qt::white);
282     //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
283
284     // Draw effects names
285     QString effects = effectNames().join(" / ");
286     if (!effects.isEmpty()) {
287         painter->setPen(pen);
288         QFont font = painter->font();
289         QFont smallFont = font;
290         smallFont.setPointSize(8);
291         painter->setFont(smallFont);
292         QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
293         painter->fillRect(txtBounding, QBrush(QColor(0, 0, 0, 150)));
294         painter->drawText(txtBounding, Qt::AlignCenter, effects);
295         pen.setColor(Qt::black);
296         painter->setPen(pen);
297         painter->setFont(font);
298     }
299
300     // For testing puspose only: draw transitions count
301     {
302         painter->setPen(pen);
303         QFont font = painter->font();
304         QFont smallFont = font;
305         smallFont.setPointSize(8);
306         painter->setFont(smallFont);
307         QString txt = " Transitions: " + QString::number(m_transitionsList.count()) + " ";
308         QRectF txtBoundin = painter->boundingRect(br, Qt::AlignRight | Qt::AlignTop, txt);
309         painter->fillRect(txtBoundin, QBrush(QColor(0, 0, 0, 150)));
310         painter->drawText(txtBoundin, Qt::AlignCenter, txt);
311         pen.setColor(Qt::black);
312         painter->setPen(pen);
313         painter->setFont(font);
314     }
315
316     // Draw clip name
317     QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
318     painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
319     painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
320
321     // draw frame around clip
322     if (isSelected()) {
323         pen.setColor(Qt::red);
324         pen.setWidth(2);
325     } else {
326         pen.setColor(Qt::black);
327         pen.setWidth(1);
328     }
329     painter->setPen(pen);
330     painter->setClipRect(option->exposedRect);
331     painter->drawPath(resultClipPath.intersected(clippath));
332
333     //painter->fillRect(startpixel,0,startpixel+endpixel,(int)br.height(),  QBrush(QColor(255,255,255,150)));
334     //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
335
336     /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
337     painter->drawRect(recta);
338     painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
339     painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
340     painter->setPen(QPen(Qt::black, 1.0));
341     painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
342     painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
343
344     //QGraphicsRectItem::paint(painter, option, widget);
345     //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
346     //painter->setPen(pen);
347     //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
348     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
349     //painter->drawText(rect(), Qt::AlignCenter, m_name);
350     // painter->drawRect(boundingRect());
351     //painter->drawRoundRect(-10, -10, 20, 20);
352     if (m_hover) {
353         int pointy = (int)(br.y() + br.height() / 2 - 5);
354         int pointx1 = (int)(br.x() + 10);
355         int pointx2 = (int)(br.x() + br.width() - 20);
356 #if 0
357         painter->setPen(QPen(Qt::black));
358         painter->setBrush(QBrush(QColor(50, 50, 0)));
359 #else
360         QRadialGradient gradient(pointx1 + 5, pointy + 5 , 5, 2, 2);
361         gradient.setColorAt(0.2, Qt::white);
362         gradient.setColorAt(0.8, Qt::yellow);
363         gradient.setColorAt(1, Qt::black);
364         painter->setBrush(gradient);
365 #endif
366         painter->drawEllipse(pointx1, pointy , 10, 10);
367
368         QRadialGradient gradient1(pointx2 + 5, pointy + 5 , 5, 2, 2);
369         gradient1.setColorAt(0.2, Qt::white);
370         gradient1.setColorAt(0.8, Qt::yellow);
371         gradient1.setColorAt(1, Qt::black);
372         painter->setBrush(gradient1);
373         painter->drawEllipse(pointx2, pointy, 10, 10);
374
375     }
376 }
377
378
379 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
380     if (abs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEIN;
381     else if (abs((int)(pos.x() - rect().x())) < 6) return RESIZESTART;
382     else if (abs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && abs((int)(pos.y() - rect().y())) < 6) return FADEOUT;
383     else if (abs((int)(pos.x() - (rect().x() + rect().width()))) < 6) return RESIZEEND;
384     else if (abs((int)(pos.x() - (rect().x() + 10))) < 6 && abs((int)(pos.y() - (rect().y() + rect().height() / 2 - 5))) < 6) return TRANSITIONSTART;
385     else if (abs((int)(pos.x() - (rect().x() + rect().width() - 20))) < 6 && abs((int)(pos.y() - (rect().y() + rect().height() / 2 - 5))) < 6) return TRANSITIONEND;
386
387     return MOVE;
388 }
389
390 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel) {
391     int channels = 2;
392
393     QRectF re = path.boundingRect();
394
395     //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
396
397     for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel ;startCache += 100) {
398         //kDebug() << "creating " << startCache;
399         //if (framePixelWidth!=pixelForOneFrame  ||
400         if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
401             continue;
402         if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
403             audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
404             audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 0));
405         }
406         bool fullAreaDraw = pixelForOneFrame < 10;
407         QMap<int, QPainterPath > positiveChannelPaths;
408         QMap<int, QPainterPath > negativeChannelPaths;
409         QPainter pixpainter(&audioThumbCachePic[startCache]);
410         QPen audiopen;
411         audiopen.setWidth(0);
412         pixpainter.setPen(audiopen);
413         //pixpainter.setRenderHint(QPainter::Antialiasing,true);
414         //pixpainter.drawLine(0,0,100,re.height());
415         int channelHeight = audioThumbCachePic[startCache].height() / channels;
416
417         for (int i = 0;i < channels;i++) {
418
419             positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
420             negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
421         }
422
423         for (int samples = 0;samples <= 100;samples++) {
424             double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
425             int sample = (int)((frame - (int)(frame)) * 20);   // AUDIO_FRAME_SIZE
426             if (frame < 0 || sample < 0 || sample > 19)
427                 continue;
428             QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
429
430             for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
431
432                 int y = channelHeight * channel + channelHeight / 2;
433                 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2)  * channelHeight / 64;
434                 if (fullAreaDraw) {
435                     positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
436                     negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
437                 } else {
438                     positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
439                     negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
440                 }
441             }
442             for (int channel = 0;channel < channels ;channel++)
443                 if (fullAreaDraw && samples == 100) {
444                     positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
445                     negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
446                     positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
447                     negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
448                 }
449
450         }
451         if (m_clipType != AV) pixpainter.setBrush(QBrush(QColor(200, 200, 100)));
452         else {
453             pixpainter.setPen(QPen(QColor(0, 0, 0)));
454             pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
455         }
456         for (int i = 0;i < channels;i++) {
457             if (fullAreaDraw) {
458                 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
459                 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
460             } else
461                 pixpainter.drawPath(positiveChannelPaths[i]);
462         }
463     }
464     //audioThumbWasDrawn=true;
465     framePixelWidth = pixelForOneFrame;
466
467     //}
468 }
469
470
471
472 void ClipItem::setFadeIn(int pos, double scale) {
473     int oldIn = m_startFade;
474     if (pos < 0) pos = 0;
475     if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
476     m_startFade = pos;
477     if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height());
478     else update(rect().x(), rect().y(), pos * scale, rect().height());
479 }
480
481 void ClipItem::setFadeOut(int pos, double scale) {
482     int oldOut = m_endFade;
483     if (pos < 0) pos = 0;
484     if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
485     m_endFade = pos;
486     if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height());
487     else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
488
489 }
490
491
492 // virtual
493 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
494     /*m_resizeMode = operationMode(event->pos());
495     if (m_resizeMode == MOVE) {
496       m_maxTrack = scene()->sceneRect().height();
497       m_grabPoint = (int) (event->pos().x() - rect().x());
498     }*/
499     QGraphicsRectItem::mousePressEvent(event);
500 }
501
502 // virtual
503 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
504     m_resizeMode = NONE;
505     QGraphicsRectItem::mouseReleaseEvent(event);
506 }
507
508 //virtual
509 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
510     m_hover = true;
511     update();
512 }
513
514 //virtual
515 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
516     m_hover = false;
517     update();
518 }
519
520 void ClipItem::resizeStart(int posx, double scale) {
521     AbstractClipItem::resizeStart(posx, scale);
522     if (m_hasThumbs) startThumbTimer->start(100);
523 }
524
525 void ClipItem::resizeEnd(int posx, double scale) {
526     AbstractClipItem::resizeEnd(posx, scale);
527     if (m_hasThumbs) endThumbTimer->start(100);
528 }
529
530 // virtual
531 void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
532 }
533
534 int ClipItem::effectsCounter() {
535     return m_effectsCounter++;
536 }
537
538 int ClipItem::effectsCount() {
539     return m_effectList.size();
540 }
541
542 QStringList ClipItem::effectNames() {
543     return m_effectList.effectNames();
544 }
545
546 QDomElement ClipItem::effectAt(int ix) {
547     return m_effectList.at(ix);
548 }
549
550 void ClipItem::setEffectAt(int ix, QDomElement effect) {
551     kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
552     m_effectList.insert(ix, effect);
553     m_effectList.removeAt(ix + 1);
554     update(boundingRect());
555 }
556
557 QMap <QString, QString> ClipItem::addEffect(QDomElement effect) {
558     QMap <QString, QString> effectParams;
559     m_effectList.append(effect);
560     effectParams["tag"] = effect.attribute("tag");
561     effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
562     QString state = effect.attribute("disabled");
563     if (!state.isEmpty()) effectParams["disabled"] = state;
564     QDomNodeList params = effect.elementsByTagName("parameter");
565     for (int i = 0; i < params.count(); i++) {
566         QDomElement e = params.item(i).toElement();
567         if (!e.isNull()) {
568             effectParams[e.attribute("name")] = e.attribute("value");
569         }
570         if (!e.attribute("factor").isEmpty()) {
571             effectParams[e.attribute("name")] =  QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
572         }
573     }
574     flashClip();
575     update(boundingRect());
576     return effectParams;
577 }
578
579 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
580     QMap <QString, QString> effectParams;
581     effectParams["tag"] = effect.attribute("tag");
582     effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
583     QString state = effect.attribute("disabled");
584     if (!state.isEmpty()) effectParams["disabled"] = state;
585     QDomNodeList params = effect.elementsByTagName("parameter");
586     for (int i = 0; i < params.count(); i++) {
587         QDomElement e = params.item(i).toElement();
588         if (e.attribute("namedesc").contains(";")) {
589             QString format = e.attribute("format");
590             QStringList separators = format.split("%d", QString::SkipEmptyParts);
591             QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
592             QString neu;
593             QTextStream txtNeu(&neu);
594             if (values.size() > 0)
595                 txtNeu << (int)values[0].toDouble();
596             for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
597                 txtNeu << separators[i];
598                 txtNeu << (int)(values[i+1].toDouble());
599             }
600             effectParams["start"] = neu;
601         } else
602             if (!e.isNull()) {
603                 effectParams[e.attribute("name")] = e.attribute("value");
604             }
605         if (!e.attribute("factor").isEmpty()) {
606             effectParams[e.attribute("name")] =  QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
607         }
608     }
609     return effectParams;
610 }
611
612 void ClipItem::deleteEffect(QString index) {
613     for (int i = 0; i < m_effectList.size(); ++i) {
614         if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
615             m_effectList.removeAt(i);
616             break;
617         }
618     }
619     flashClip();
620     update(boundingRect());
621 }
622
623 //virtual
624 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
625     QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
626     QDomDocument doc;
627     doc.setContent(effects, true);
628     QDomElement e = doc.documentElement();
629     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
630     if (view) view->slotAddEffect(e, m_startPos, track());
631 }
632
633 //virtual
634 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
635     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
636 }
637
638 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
639     Q_UNUSED(event);
640 }
641 void ClipItem::addTransition(Transition* t) {
642     m_transitionsList.append(t);
643     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
644     QDomDocument doc;
645     QDomElement e = doc.documentElement();
646     //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
647 }
648 // virtual
649 /*
650 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
651 {
652   int pos = event->x();
653   if (event->modifiers() == Qt::ControlModifier)
654     setDragMode(QGraphicsView::ScrollHandDrag);
655   else if (event->modifiers() == Qt::ShiftModifier)
656     setDragMode(QGraphicsView::RubberBandDrag);
657   else {
658     QGraphicsItem * item = itemAt(event->pos());
659     if (item) {
660     }
661     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
662   }
663   kDebug()<<pos;
664   QGraphicsView::mousePressEvent(event);
665 }
666
667 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
668 {
669   QGraphicsView::mouseReleaseEvent(event);
670   setDragMode(QGraphicsView::NoDrag);
671 }
672 */
673
674 #include "clipitem.moc"