]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
zoom into scene
[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 ||true) && 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+200);//200 more for less missing parts before repaint after scrolling
252
253                  for (int startCache=startpixel-startpixel%100; startCache < endpixel+300;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     // draw start / end fades
261     double scale = br.width() / m_cropDuration;
262     QBrush fades;
263     if (isSelected()) {
264       fades = QBrush(QColor(200, 50, 50, 150));
265     }
266     else fades = QBrush(QColor(200, 200, 200, 200));
267
268     if (m_startFade != 0) {
269       QPainterPath fadeInPath;
270       fadeInPath.moveTo(br.x() - offset, br.y());
271       fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
272       fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
273       fadeInPath.closeSubpath();
274       painter->fillPath(fadeInPath, fades);
275       if (isSelected()) {
276         QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
277         painter->drawLine(l);
278       }
279     }
280     if (m_endFade != 0) {
281       QPainterPath fadeOutPath;
282       fadeOutPath.moveTo(br.x() + br.width(), br.y());
283       fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
284       fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
285       fadeOutPath.closeSubpath();
286       painter->fillPath(fadeOutPath, fades);
287       if (isSelected()) {
288         QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
289         painter->drawLine(l);
290       }
291     }
292
293     QPen pen = painter->pen();
294     pen.setColor(Qt::white);
295     //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
296     // Draw clip name
297     QString effects = effectNames().join(" / ");
298     if (!effects.isEmpty()) {
299       painter->setPen(pen);
300       QFont font = painter->font();
301       QFont smallFont = font;
302       smallFont.setPointSize(8);
303       painter->setFont(smallFont);
304       QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
305       painter->fillRect(txtBounding, QBrush(QColor(0,0,0,150)));
306       painter->drawText(txtBounding, Qt::AlignCenter, effects);
307       pen.setColor(Qt::black);
308       painter->setPen(pen);
309       painter->setFont(font);
310     }
311
312     pen.setColor(Qt::red);
313     //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
314     if (isSelected()) painter->setPen(pen);
315          painter->setClipRect(option->exposedRect);
316          painter->drawPath(roundRectPathUpper.united(roundRectPathLower).intersected(clippath));
317
318     QRectF txtBounding = painter->boundingRect(br, Qt::AlignCenter, " " + m_clipName + " ");
319     painter->fillRect(txtBounding, QBrush(QColor(255,255,255,150)));
320     painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
321
322
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                         bool fullAreaDraw=pixelForOneFrame<10;
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                         int channelHeight=audioThumbCachePic[startCache].height()/channels;
380                         
381                         for (int i=0;i<channels;i++){
382                                 
383                                 positiveChannelPaths[i].moveTo(0,channelHeight*i+ channelHeight/2);
384                                 negativeChannelPaths[i].moveTo(0,channelHeight*i+ channelHeight/2);
385                         }
386                         
387                         for (int samples=0;samples<=100;samples++){
388                                 double frame=(double)(samples+startCache-0)/pixelForOneFrame;
389                                 int sample=(frame-(int)(frame))*20 ;// AUDIO_FRAME_SIZE
390                                 if (frame<0 || sample< 0 || sample>19 )
391                                         continue;
392                                 QMap<int,QByteArray> frame_channel_data=baseClip()->audioFrameChache[(int)frame];
393                                 
394                                 for (int channel=0;channel<channels && frame_channel_data[channel].size()> 0;channel++){
395                                         
396                                         int y=channelHeight*channel+ channelHeight/2;
397                                         int delta=(int)(frame_channel_data[channel][sample] -127/2 )  * channelHeight/ 64;
398                                         if (fullAreaDraw){
399                                                 positiveChannelPaths[channel].lineTo(samples,0.1+y+qAbs( delta ));      
400                                                 negativeChannelPaths[channel].lineTo(samples,0.1+y-qAbs( delta ));
401                                         }else{
402                                                 positiveChannelPaths[channel].lineTo(samples,0.1+y+delta);      
403                                                 negativeChannelPaths[channel].lineTo(samples,0.1+y-delta);
404                                         }
405                                 }
406                         }
407                         for (int i=0;i<channels;i++){
408                                 if (fullAreaDraw){
409                                         pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
410                                         pixpainter.setBrush(QBrush(QColor(200,200,100,200)));
411                                         pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
412                                 }else
413                                         pixpainter.drawPath(positiveChannelPaths[i]);
414                         }
415                 }
416                 //audioThumbWasDrawn=true;
417                 framePixelWidth=pixelForOneFrame;
418                         
419         //}
420 }
421
422 int ClipItem::fadeIn() const
423 {
424   return m_startFade;
425 }
426
427 int ClipItem::fadeOut() const
428 {
429   return m_endFade;
430 }
431
432 void ClipItem::setFadeIn(int pos, double scale)
433 {
434   int oldIn = m_startFade;
435   if (pos < 0) pos = 0;
436   if (pos > m_cropDuration) pos = m_cropDuration / 2;
437   m_startFade = pos;
438   if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height()); 
439   else update(rect().x(), rect().y(), pos * scale, rect().height());
440 }
441
442 void ClipItem::setFadeOut(int pos, double scale)
443 {
444   int oldOut = m_endFade;
445   if (pos < 0) pos = 0;
446   if (pos > m_cropDuration) pos = m_cropDuration / 2;
447   m_endFade = pos;
448   if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height()); 
449   else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
450
451 }
452
453
454 // virtual
455  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
456  {
457     /*m_resizeMode = operationMode(event->pos());
458     if (m_resizeMode == MOVE) {
459       m_maxTrack = scene()->sceneRect().height();
460       m_grabPoint = (int) (event->pos().x() - rect().x());
461     }*/
462     QGraphicsRectItem::mousePressEvent(event);
463  }
464
465 // virtual
466  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
467  {
468     m_resizeMode = NONE;
469     QGraphicsRectItem::mouseReleaseEvent(event);
470  }
471
472  void ClipItem::moveTo(int x, double scale, double offset, int newTrack)
473  {
474   double origX = rect().x();
475   double origY = rect().y();
476   bool success = true;
477   if (x < 0) return;
478   setRect(x * scale, origY + offset, rect().width(), rect().height());
479   QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
480   if (collisionList.size() == 0) m_track = newTrack;
481   for (int i = 0; i < collisionList.size(); ++i) {
482     QGraphicsItem *item = collisionList.at(i);
483     if (item->type() == 70000)
484     {
485         if (offset == 0)
486         {
487           QRectF other = ((QGraphicsRectItem *)item)->rect();
488           if (x < m_startPos) {
489             kDebug()<<"COLLISION, MOVING TO------";
490             m_startPos = ((ClipItem *)item)->endPos() + 1;
491             origX = m_startPos * scale; 
492           }
493           else {
494             kDebug()<<"COLLISION, MOVING TO+++";
495             m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
496             origX = m_startPos * scale; 
497           }
498         }
499         setRect(origX, origY, rect().width(), rect().height());
500         offset = 0;
501         origX = rect().x();
502         success = false;
503         break;
504       }
505     }
506     if (success) {
507         m_track = newTrack;
508         m_startPos = x;
509     }
510 /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
511     for (int i = 0; i < childrenList.size(); ++i) {
512       childrenList.at(i)->moveBy(rect().x() - origX , offset);
513     }*/
514  }
515
516 void ClipItem::resizeStart(int posx, double scale)
517 {
518     int durationDiff = posx - m_startPos;
519     if (durationDiff == 0) return;
520     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
521     if (m_cropStart + durationDiff < 0) {
522       durationDiff = -m_cropStart;
523     }
524     else if (durationDiff >= m_cropDuration) {
525       durationDiff = m_cropDuration - 3;
526     }
527     m_startPos += durationDiff;
528     m_cropStart += durationDiff;
529     m_cropDuration -= durationDiff;
530     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
531     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
532     for (int i = 0; i < collisionList.size(); ++i) {
533       QGraphicsItem *item = collisionList.at(i);
534       if (item->type() == 70000)
535       {
536         int diff = ((ClipItem *)item)->endPos() + 1 - m_startPos;
537         setRect((m_startPos + diff) * scale, rect().y(), (m_cropDuration - diff) * scale, rect().height());
538         m_startPos += diff;
539         m_cropStart += diff;
540         m_cropDuration -= diff;
541         break;
542       }
543     }
544     if (m_hasThumbs) startThumbTimer->start(100);
545 }
546
547 void ClipItem::resizeEnd(int posx, double scale)
548 {
549     int durationDiff = posx - endPos();
550     if (durationDiff == 0) return;
551     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
552     if (m_cropDuration + durationDiff <= 0) {
553       durationDiff = - (m_cropDuration - 3);
554     }
555     else if (m_cropDuration + durationDiff >= m_maxDuration) {
556       durationDiff = m_maxDuration - m_cropDuration;
557     }
558     m_cropDuration += durationDiff;
559     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
560     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
561     for (int i = 0; i < collisionList.size(); ++i) {
562       QGraphicsItem *item = collisionList.at(i);
563       if (item->type() == 70000)
564       {
565         int diff = ((ClipItem *)item)->startPos() - 1 - startPos();
566         m_cropDuration = diff;
567         setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
568         break;
569       }
570     }
571     if (m_hasThumbs) endThumbTimer->start(100);
572 }
573
574 // virtual
575  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
576  {
577  }
578
579 int ClipItem::track()
580 {
581   return  m_track;
582 }
583
584 void ClipItem::setTrack(int track)
585 {
586   m_track = track;
587 }
588
589 int ClipItem::effectsCounter()
590 {
591   return m_effectsCounter++;
592 }
593
594 int ClipItem::effectsCount()
595 {
596   return m_effectList.size();
597 }
598
599 QStringList ClipItem::effectNames()
600 {
601   return m_effectList.effectNames();
602 }
603
604 QDomElement ClipItem::effectAt(int ix)
605 {
606   return m_effectList.at(ix);
607 }
608
609 void ClipItem::setEffectAt(int ix, QDomElement effect)
610 {
611   kDebug()<<"CHange EFFECT AT: "<<ix<<", CURR: "<<m_effectList.at(ix).attribute("tag")<<", NEW: "<<effect.attribute("tag");
612   m_effectList.insert(ix, effect);
613   m_effectList.removeAt(ix + 1);
614   update(boundingRect());
615 }
616
617 QMap <QString, QString> ClipItem::addEffect(QDomElement effect)
618 {
619   QMap <QString, QString> effectParams;
620   m_effectList.append(effect);
621   effectParams["tag"] = effect.attribute("tag");
622   effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
623   QDomNodeList params = effect.elementsByTagName("parameter");
624   for (int i = 0; i < params.count(); i++) {
625     QDomElement e = params.item(i).toElement();
626     if (!e.isNull())
627       effectParams[e.attribute("name")] = e.attribute("value");
628   }
629   update(boundingRect());
630   return effectParams;
631 }
632
633 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect)
634 {
635   QMap <QString, QString> effectParams;
636   effectParams["tag"] = effect.attribute("tag");
637   effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
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.isNull())
642       effectParams[e.attribute("name")] = e.attribute("value");
643   }
644   return effectParams;
645 }
646
647 void ClipItem::deleteEffect(QString index)
648 {
649   for (int i = 0; i < m_effectList.size(); ++i) {
650     if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
651       m_effectList.removeAt(i);
652       break;
653     }
654   }
655   update(boundingRect());
656 }
657
658
659 // virtual 
660 /*
661 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
662 {
663   int pos = event->x();
664   if (event->modifiers() == Qt::ControlModifier) 
665     setDragMode(QGraphicsView::ScrollHandDrag);
666   else if (event->modifiers() == Qt::ShiftModifier) 
667     setDragMode(QGraphicsView::RubberBandDrag);
668   else {
669     QGraphicsItem * item = itemAt(event->pos());
670     if (item) {
671     }
672     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
673   }
674   kDebug()<<pos;
675   QGraphicsView::mousePressEvent(event);
676 }
677
678 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
679 {
680   QGraphicsView::mouseReleaseEvent(event);
681   setDragMode(QGraphicsView::NoDrag);
682 }
683 */
684
685 #include "clipitem.moc"