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