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