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