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