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