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