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