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