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