]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
optimize clip painting
[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, GenTime cropStart, double scale, double fps)
43         : AbstractClipItem(info, QRectF(), fps), 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_startFade(0), m_endFade(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
47     m_clipName = clip->name();
48     m_producer = clip->getId();
49     m_clipType = clip->clipType();
50     m_cropStart = cropStart;
51
52     m_maxDuration = clip->maxDuration();
53     setAcceptDrops(true);
54     audioThumbReady = clip->audioThumbCreated();
55     /*
56       m_cropStart = xml.attribute("in", 0).toInt();
57       m_maxDuration = xml.attribute("duration", 0).toInt();
58       if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
59
60       if (duration != -1) m_cropDuration = duration;
61       else m_cropDuration = m_maxDuration;*/
62
63
64     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
65     setAcceptsHoverEvents(true);
66     connect(this , SIGNAL(prepareAudioThumb(double, QPainterPath, int, int)) , this, SLOT(slotPrepareAudioThumb(double, QPainterPath, int, int)));
67
68     setBrush(QColor(141, 166, 215));
69     if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) {
70         m_hasThumbs = true;
71         connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
72         connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
73         connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
74         QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
75
76         startThumbTimer = new QTimer(this);
77         startThumbTimer->setSingleShot(true);
78         connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
79         endThumbTimer = new QTimer(this);
80         endThumbTimer->setSingleShot(true);
81         connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
82     } else if (m_clipType == COLOR) {
83         QString colour = clip->getProperty("colour");
84         colour = colour.replace(0, 2, "#");
85         setBrush(QColor(colour.left(7)));
86     } else if (m_clipType == IMAGE || m_clipType == TEXT) {
87         m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
88         m_endPix = m_startPix;
89     } else if (m_clipType == AUDIO) {
90         connect(clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
91     }
92 }
93
94
95 ClipItem::~ClipItem() {
96     if (startThumbTimer) delete startThumbTimer;
97     if (endThumbTimer) delete endThumbTimer;
98 }
99
100 void ClipItem::resetThumbs() {
101     slotFetchThumbs();
102     audioThumbCachePic.clear();
103 }
104
105
106 void ClipItem::refreshClip() {
107     m_maxDuration = m_clip->maxDuration();
108     if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW) slotFetchThumbs();
109     else if (m_clipType == COLOR) {
110         QString colour = m_clip->getProperty("colour");
111         colour = colour.replace(0, 2, "#");
112         setBrush(QColor(colour.left(7)));
113     } else if (m_clipType == IMAGE || m_clipType == TEXT) {
114         m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(50 * KdenliveSettings::project_display_ratio()), 50);
115         m_endPix = m_startPix;
116     }
117 }
118
119 void ClipItem::slotFetchThumbs() {
120     m_thumbsRequested += 2;
121     emit getThumb((int)m_cropStart.frames(m_fps), (int)(m_cropStart + m_cropDuration).frames(m_fps));
122 }
123
124 void ClipItem::slotGetStartThumb() {
125     m_thumbsRequested++;
126     emit getThumb((int)m_cropStart.frames(m_fps), -1);
127 }
128
129 void ClipItem::slotGetEndThumb() {
130     m_thumbsRequested++;
131     emit getThumb(-1, (int)(m_cropStart + m_cropDuration).frames(m_fps));
132 }
133
134 void ClipItem::slotThumbReady(int frame, QPixmap pix) {
135     if (m_thumbsRequested == 0) return;
136     if (frame == m_cropStart.frames(m_fps)) m_startPix = pix;
137     else m_endPix = pix;
138     update();
139     m_thumbsRequested--;
140 }
141
142 void ClipItem::slotGotAudioData() {
143     audioThumbReady = true;
144     update();
145 }
146
147 int ClipItem::type() const {
148     return AVWIDGET;
149 }
150
151 DocClipBase *ClipItem::baseClip() {
152     return m_clip;
153 }
154
155 QDomElement ClipItem::xml() const {
156     return m_clip->toXML();
157 }
158
159 int ClipItem::clipType() {
160     return m_clipType;
161 }
162
163 QString ClipItem::clipName() {
164     return m_clipName;
165 }
166
167 int ClipItem::clipProducer() {
168     return m_producer;
169 }
170
171 void ClipItem::flashClip() {
172     if (m_timeLine == 0) {
173         m_timeLine = new QTimeLine(750, this);
174         m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
175         connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
176     }
177     m_timeLine->start();
178 }
179
180 void ClipItem::animate(qreal value) {
181     //m_opacity = value;
182     update();
183 }
184
185 // virtual
186 void ClipItem::paint(QPainter *painter,
187                      const QStyleOptionGraphicsItem *option,
188                      QWidget *widget) {
189     painter->setOpacity(m_opacity);
190     QBrush paintColor = brush();
191     if (isSelected()) paintColor = QBrush(QColor(79, 93, 121));
192     QRectF br = rect();
193     double scale = br.width() / m_cropDuration.frames(m_fps);
194
195         // kDebug()<<"///   EXPOSED RECT: "<<option->exposedRect.x()<<" X "<<option->exposedRect.right();
196         painter->setClipRect(option->exposedRect);
197     int startpixel = (int)option->exposedRect.x() - rect().x();
198
199     if (startpixel < 0)
200         startpixel = 0;
201     int endpixel = (int)option->exposedRect.right() - rect().x();
202     if (endpixel < 0)
203         endpixel = 0;
204
205     //painter->setRenderHints(QPainter::Antialiasing);
206
207     QPainterPath roundRectPathUpper = upperRectPart(br), roundRectPathLower = lowerRectPart(br);
208     
209
210     // build path around clip
211     QPainterPath resultClipPath = roundRectPathUpper.united(roundRectPathLower);
212     painter->fillPath(resultClipPath, paintColor);
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 : resultClipPath;
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     // draw markers
249     QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
250     QList < CommentedTime >::Iterator it = markers.begin();
251     GenTime pos;
252     double framepos;
253     const int markerwidth = 4;
254     QBrush markerBrush;
255     markerBrush = QBrush(QColor(120, 120, 0, 100));
256     QPen pen = painter->pen();
257     pen.setColor(QColor(255, 255, 255, 200));
258     pen.setStyle(Qt::DotLine);
259     painter->setPen(pen);
260     for (; it != markers.end(); ++it) {
261         pos = (*it).time() - cropStart();
262         if (pos > GenTime()) {
263             if (pos > duration()) break;
264             framepos = scale * pos.frames(m_fps);
265             QLineF l(br.x() + framepos, br.y() + 5, br.x() + framepos, br.y() + br.height() - 5);
266             painter->drawLine(l);
267             if (KdenliveSettings::showmarkers()) {
268                 const QRectF txtBounding = painter->boundingRect(br.x() + framepos + 1, br.y() + 10, br.width() - framepos - 2, br.height() - 10, Qt::AlignLeft | Qt::AlignTop, " " + (*it).comment() + " ");
269                 QPainterPath path;
270                 path.addRoundedRect(txtBounding, 3, 3);
271                 painter->fillPath(path, markerBrush);
272                 painter->drawText(txtBounding, Qt::AlignCenter, (*it).comment());
273             }
274             //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
275         }
276     }
277     pen.setColor(Qt::black);
278     pen.setStyle(Qt::SolidLine);
279
280
281
282     // draw start / end fades
283     QBrush fades;
284     if (isSelected()) {
285         fades = QBrush(QColor(200, 50, 50, 150));
286     } else fades = QBrush(QColor(200, 200, 200, 200));
287
288     if (m_startFade != 0) {
289         QPainterPath fadeInPath;
290         fadeInPath.moveTo(br.x() , br.y());
291         fadeInPath.lineTo(br.x() , br.bottom());
292         fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
293         fadeInPath.closeSubpath();
294         painter->fillPath(fadeInPath.intersected(resultClipPath), fades);
295         if (isSelected()) {
296             QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.bottom());
297             painter->drawLine(l);
298         }
299     }
300     if (m_endFade != 0) {
301         QPainterPath fadeOutPath;
302         fadeOutPath.moveTo(br.right(), br.y());
303         fadeOutPath.lineTo(br.right(), br.bottom());
304         fadeOutPath.lineTo(br.right() - m_endFade * scale, br.y());
305         fadeOutPath.closeSubpath();
306         painter->fillPath(fadeOutPath.intersected(resultClipPath), fades);
307         if (isSelected()) {
308             QLineF l(br.right() - m_endFade * scale, br.y(), br.x() + br.width(), br.bottom());
309             painter->drawLine(l);
310         }
311     }
312
313     // Draw effects names
314     if (!m_effectNames.isEmpty() && br.width() > 30) {
315         QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
316         txtBounding.setRight(txtBounding.right() + 15);
317         painter->setPen(Qt::white);
318         QBrush markerBrush(Qt::SolidPattern);
319         if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
320             qreal value = m_timeLine->currentValue();
321             txtBounding.setWidth(txtBounding.width() * value);
322             markerBrush.setColor(QColor(50 + 200 * (1.0 - value), 50, 50, 100 + 50 * value));
323         } else markerBrush.setColor(QColor(50, 50, 50, 150));
324         QPainterPath path;
325         path.addRoundedRect(txtBounding, 4, 4);
326         painter->fillPath(path.intersected(resultClipPath), markerBrush);
327         painter->drawText(txtBounding, Qt::AlignCenter, m_effectNames);
328         painter->setPen(Qt::black);
329     }
330
331     // Draw clip name
332     QRectF txtBounding = painter->boundingRect(br, Qt::AlignHCenter | Qt::AlignTop, " " + m_clipName + " ");
333     //painter->fillRect(txtBounding, QBrush(QColor(255, 255, 255, 150)));
334     painter->setPen(QColor(0, 0, 0, 180));
335     painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
336     txtBounding.translate(QPointF(1, 1));
337     painter->setPen(QColor(255, 255, 255, 255));
338     painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
339     // draw frame around clip
340     if (isSelected()) {
341         pen.setColor(Qt::red);
342         //pen.setWidth(2);
343     } else {
344         pen.setColor(Qt::black);
345         //pen.setWidth(1);
346     }
347     painter->setPen(pen);
348     //painter->setClipRect(option->exposedRect);
349     painter->drawPath(resultClipPath);
350
351     if (m_hover && br.width() > 30) {
352         painter->setBrush(QColor(180, 180, 50, 180)); //gradient);
353
354         // draw transitions handles
355         QPainterPath transitionHandle;
356         const int handle_size = 4;
357         transitionHandle.moveTo(0, 0);
358         transitionHandle.lineTo(handle_size, handle_size);
359         transitionHandle.lineTo(handle_size * 2, 0);
360         transitionHandle.lineTo(handle_size * 3, handle_size);
361         transitionHandle.lineTo(handle_size * 2, handle_size * 2);
362         transitionHandle.lineTo(handle_size * 3, handle_size * 3);
363         transitionHandle.lineTo(0, handle_size * 3);
364         transitionHandle.closeSubpath();
365         int pointy = (int)(br.y() + br.height() / 2);
366         int pointx1 = (int)(br.x() + 10);
367         int pointx2 = (int)(br.x() + br.width() - (10 + handle_size * 3));
368 #if 0
369         painter->setPen(QPen(Qt::black));
370         painter->setBrush(QBrush(QColor(50, 50, 0)));
371 #else
372         /*QRadialGradient gradient(pointx1 + 5, pointy + 5 , 5, 2, 2);
373         gradient.setColorAt(0.2, Qt::white);
374         gradient.setColorAt(0.8, Qt::yellow);
375         gradient.setColorAt(1, Qt::black);*/
376
377 #endif
378         painter->translate(pointx1, pointy);
379         painter->drawPath(transitionHandle); //Ellipse(0, 0 , 10, 10);
380         painter->translate(-pointx1, -pointy);
381
382         /*        QRadialGradient gradient1(pointx2 + 5, pointy + 5 , 5, 2, 2);
383                 gradient1.setColorAt(0.2, Qt::white);
384                 gradient1.setColorAt(0.8, Qt::yellow);
385                 gradient1.setColorAt(1, Qt::black);
386                 painter->setBrush(gradient1);*/
387         painter->translate(pointx2, pointy);
388         QMatrix m;
389         m.scale(-1.0, 1.0);
390         //painter->setMatrix(m);
391         painter->drawPath(transitionHandle); // Ellipse(0, 0, 10, 10);
392         //painter->setMatrix(m);
393         painter->translate(- pointx2, -pointy);
394     }
395 }
396
397
398 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale) {
399     if (qAbs((int)(pos.x() - (rect().x() + scale * m_startFade))) < 6 && qAbs((int)(pos.y() - rect().y())) < 6) return FADEIN;
400     else if (qAbs((int)(pos.x() - rect().x())) < 6) return RESIZESTART;
401     else if (qAbs((int)(pos.x() - (rect().x() + rect().width() - scale * m_endFade))) < 6 && qAbs((int)(pos.y() - rect().y())) < 6) return FADEOUT;
402     else if (qAbs((int)(pos.x() - (rect().x() + rect().width()))) < 6) return RESIZEEND;
403     else if (qAbs((int)(pos.x() - (rect().x() + 16))) < 10 && qAbs((int)(pos.y() - (rect().y() + rect().height() / 2 + 5))) < 8) return TRANSITIONSTART;
404     else if (qAbs((int)(pos.x() - (rect().x() + rect().width() - 21))) < 10 && qAbs((int)(pos.y() - (rect().y() + rect().height() / 2 + 5))) < 8) return TRANSITIONEND;
405
406     return MOVE;
407 }
408
409 QList <GenTime> ClipItem::snapMarkers() {
410     QList < GenTime > snaps;
411     QList < GenTime > markers = baseClip()->snapMarkers();
412     GenTime pos;
413     double framepos;
414
415     for (int i = 0; i < markers.size(); i++) {
416         pos = markers.at(i) - cropStart();
417         if (pos > GenTime()) {
418             if (pos > duration()) break;
419             else snaps.append(pos + startPos());
420         }
421     }
422     return snaps;
423 }
424
425 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, QPainterPath path, int startpixel, int endpixel) {
426     int channels = 2;
427
428     QRectF re = path.boundingRect();
429
430     //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
431
432     for (int startCache = startpixel - startpixel % 100;startCache + 100 < endpixel ;startCache += 100) {
433         //kDebug() << "creating " << startCache;
434         //if (framePixelWidth!=pixelForOneFrame  ||
435         if (framePixelWidth == pixelForOneFrame && audioThumbCachePic.contains(startCache))
436             continue;
437         if (audioThumbCachePic[startCache].isNull() || framePixelWidth != pixelForOneFrame) {
438             audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
439             audioThumbCachePic[startCache].fill(QColor(200, 200, 200, 0));
440         }
441         bool fullAreaDraw = pixelForOneFrame < 10;
442         QMap<int, QPainterPath > positiveChannelPaths;
443         QMap<int, QPainterPath > negativeChannelPaths;
444         QPainter pixpainter(&audioThumbCachePic[startCache]);
445         QPen audiopen;
446         audiopen.setWidth(0);
447         pixpainter.setPen(audiopen);
448         //pixpainter.setRenderHint(QPainter::Antialiasing,true);
449         //pixpainter.drawLine(0,0,100,re.height());
450         int channelHeight = audioThumbCachePic[startCache].height() / channels;
451
452         for (int i = 0;i < channels;i++) {
453
454             positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
455             negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
456         }
457
458         for (int samples = 0;samples <= 100;samples++) {
459             double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
460             int sample = (int)((frame - (int)(frame)) * 20);   // AUDIO_FRAME_SIZE
461             if (frame < 0 || sample < 0 || sample > 19)
462                 continue;
463             QMap<int, QByteArray> frame_channel_data = baseClip()->audioFrameChache[(int)frame];
464
465             for (int channel = 0;channel < channels && frame_channel_data[channel].size() > 0;channel++) {
466
467                 int y = channelHeight * channel + channelHeight / 2;
468                 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2)  * channelHeight / 64;
469                 if (fullAreaDraw) {
470                     positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
471                     negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
472                 } else {
473                     positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
474                     negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
475                 }
476             }
477             for (int channel = 0;channel < channels ;channel++)
478                 if (fullAreaDraw && samples == 100) {
479                     positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
480                     negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
481                     positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
482                     negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
483                 }
484
485         }
486         if (m_clipType != AV) pixpainter.setBrush(QBrush(QColor(200, 200, 100)));
487         else {
488             pixpainter.setPen(QPen(QColor(0, 0, 0)));
489             pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
490         }
491         for (int i = 0;i < channels;i++) {
492             if (fullAreaDraw) {
493                 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
494                 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
495             } else
496                 pixpainter.drawPath(positiveChannelPaths[i]);
497         }
498     }
499     //audioThumbWasDrawn=true;
500     framePixelWidth = pixelForOneFrame;
501
502     //}
503 }
504
505 uint ClipItem::fadeIn() const {
506     return m_startFade;
507 }
508
509 uint ClipItem::fadeOut() const {
510     return m_endFade;
511 }
512
513
514 void ClipItem::setFadeIn(int pos, double scale) {
515     int oldIn = m_startFade;
516     if (pos < 0) pos = 0;
517     if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
518     m_startFade = pos;
519     if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height());
520     else update(rect().x(), rect().y(), pos * scale, rect().height());
521 }
522
523 void ClipItem::setFadeOut(int pos, double scale) {
524     int oldOut = m_endFade;
525     if (pos < 0) pos = 0;
526     if (pos > m_cropDuration.frames(m_fps)) pos = (int)(m_cropDuration.frames(m_fps) / 2);
527     m_endFade = pos;
528     if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height());
529     else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
530
531 }
532
533 // virtual
534 void ClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event) {
535     /*m_resizeMode = operationMode(event->pos());
536     if (m_resizeMode == MOVE) {
537       m_maxTrack = scene()->sceneRect().height();
538       m_grabPoint = (int) (event->pos().x() - rect().x());
539     }*/
540     QGraphicsRectItem::mousePressEvent(event);
541 }
542
543 // virtual
544 void ClipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) {
545     m_resizeMode = NONE;
546     QGraphicsRectItem::mouseReleaseEvent(event);
547 }
548
549 //virtual
550 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *) {
551     m_hover = true;
552     update();
553 }
554
555 //virtual
556 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *) {
557     m_hover = false;
558     update();
559 }
560
561 void ClipItem::resizeStart(int posx, double scale) {
562     AbstractClipItem::resizeStart(posx, scale);
563     if (m_hasThumbs) startThumbTimer->start(100);
564 }
565
566 void ClipItem::resizeEnd(int posx, double scale) {
567     AbstractClipItem::resizeEnd(posx, scale);
568     if (m_hasThumbs) endThumbTimer->start(100);
569 }
570
571 // virtual
572 void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
573 }
574
575 int ClipItem::effectsCounter() {
576     return m_effectsCounter++;
577 }
578
579 int ClipItem::effectsCount() {
580     return m_effectList.size();
581 }
582
583 QStringList ClipItem::effectNames() {
584     return m_effectList.effectNames();
585 }
586
587 QDomElement ClipItem::effectAt(int ix) {
588     return m_effectList.at(ix);
589 }
590
591 void ClipItem::setEffectAt(int ix, QDomElement effect) {
592     kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
593     m_effectList.insert(ix, effect);
594     m_effectList.removeAt(ix + 1);
595     m_effectNames = m_effectList.effectNames().join(" / ");
596     update(boundingRect());
597 }
598
599 QMap <QString, QString> ClipItem::addEffect(QDomElement effect, bool animate) {
600     QMap <QString, QString> effectParams;
601     /*QDomDocument doc;
602     doc.appendChild(doc.importNode(effect, true));
603     kDebug() << "///////  CLIP ADD EFFECT: "<< doc.toString();*/
604     m_effectList.append(effect);
605     effectParams["tag"] = effect.attribute("tag");
606     QString effectId = effect.attribute("id");
607     if (effectId.isEmpty()) effectId = effect.attribute("tag");
608     effectParams["id"] = effectId;
609     effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
610     QString state = effect.attribute("disabled");
611     if (!state.isEmpty()) effectParams["disabled"] = state;
612     QDomNodeList params = effect.elementsByTagName("parameter");
613     int fade = 0;
614     for (int i = 0; i < params.count(); i++) {
615         QDomElement e = params.item(i).toElement();
616         if (!e.isNull()) {
617             if (e.attribute("factor").isEmpty()) {
618                 effectParams[e.attribute("name")] = e.attribute("value");
619                 // check if it is a fade effect
620                 if (effectId == "fadein") {
621                     if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
622                     else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
623                 } else if (effectId == "fadeout") {
624                     if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
625                     else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
626                 }
627             } else {
628                 effectParams[e.attribute("name")] =  QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
629             }
630         }
631     }
632     m_effectNames = m_effectList.effectNames().join(" / ");
633     if (fade > 0) m_startFade = fade;
634     else if (fade < 0) m_endFade = -fade;
635     if (animate) {
636         flashClip();
637     }
638     update(boundingRect());
639     return effectParams;
640 }
641
642 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect) {
643     QMap <QString, QString> effectParams;
644     effectParams["tag"] = effect.attribute("tag");
645     effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
646     effectParams["id"] = effect.attribute("id");
647     QString state = effect.attribute("disabled");
648     if (!state.isEmpty()) effectParams["disabled"] = state;
649     QDomNodeList params = effect.elementsByTagName("parameter");
650     for (int i = 0; i < params.count(); i++) {
651         QDomElement e = params.item(i).toElement();
652         if (e.attribute("namedesc").contains(";")) {
653             QString format = e.attribute("format");
654             QStringList separators = format.split("%d", QString::SkipEmptyParts);
655             QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
656             QString neu;
657             QTextStream txtNeu(&neu);
658             if (values.size() > 0)
659                 txtNeu << (int)values[0].toDouble();
660             for (int i = 0;i < separators.size() && i + 1 < values.size();i++) {
661                 txtNeu << separators[i];
662                 txtNeu << (int)(values[i+1].toDouble());
663             }
664             effectParams["start"] = neu;
665         } else if (!e.isNull()) {
666             if (!e.attribute("factor").isEmpty())
667                 effectParams[e.attribute("name")] =  QString::number(effectParams[e.attribute("name")].toDouble() / e.attribute("factor").toDouble());
668             else effectParams[e.attribute("name")] = e.attribute("value");
669         }
670     }
671     return effectParams;
672 }
673
674 void ClipItem::deleteEffect(QString index) {
675     for (int i = 0; i < m_effectList.size(); ++i) {
676         if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
677             if (m_effectList.at(i).attribute("id") == "fadein") m_startFade = 0;
678             else if (m_effectList.at(i).attribute("id") == "fadeout") m_endFade = 0;
679             m_effectList.removeAt(i);
680             break;
681         }
682     }
683     m_effectNames = m_effectList.effectNames().join(" / ");
684     flashClip();
685     update(boundingRect());
686 }
687
688 //virtual
689 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event) {
690     QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
691     QDomDocument doc;
692     doc.setContent(effects, true);
693     QDomElement e = doc.documentElement();
694     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
695     if (view) view->slotAddEffect(e, m_startPos, track());
696 }
697
698 //virtual
699 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event) {
700     event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
701 }
702
703 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) {
704     Q_UNUSED(event);
705 }
706 void ClipItem::addTransition(Transition* t) {
707     m_transitionsList.append(t);
708     CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
709     QDomDocument doc;
710     QDomElement e = doc.documentElement();
711     //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
712 }
713 // virtual
714 /*
715 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
716 {
717   int pos = event->x();
718   if (event->modifiers() == Qt::ControlModifier)
719     setDragMode(QGraphicsView::ScrollHandDrag);
720   else if (event->modifiers() == Qt::ShiftModifier)
721     setDragMode(QGraphicsView::RubberBandDrag);
722   else {
723     QGraphicsItem * item = itemAt(event->pos());
724     if (item) {
725     }
726     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
727   }
728   kDebug()<<pos;
729   QGraphicsView::mousePressEvent(event);
730 }
731
732 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
733 {
734   QGraphicsView::mouseReleaseEvent(event);
735   setDragMode(QGraphicsView::NoDrag);
736 }
737 */
738
739 #include "clipitem.moc"