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