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