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