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