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