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