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