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