]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
put code for painterpath back in, to choose which to take
[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
27
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)
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)) , this, SLOT (slotPrepareAudioThumb(double,QPainterPath)));
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   update();
118 }
119
120 int ClipItem::type () const
121 {
122   return 70000;
123 }
124
125 DocClipBase *ClipItem::baseClip()
126 {
127   return m_clip;
128 }
129
130 QDomElement ClipItem::xml() const
131 {
132   return m_xml;
133 }
134
135 int ClipItem::clipType()
136 {
137   return m_clipType;
138 }
139
140 QString ClipItem::clipName()
141 {
142   return m_clipName;
143 }
144
145 int ClipItem::clipProducer()
146 {
147   return m_producer;
148 }
149
150 int ClipItem::maxDuration()
151 {
152   return m_maxDuration;
153 }
154
155 int ClipItem::duration()
156 {
157   return m_cropDuration;
158 }
159
160 int ClipItem::startPos()
161 {
162   return m_startPos;
163 }
164
165 int ClipItem::cropStart()
166 {
167   return m_cropStart;
168 }
169
170 int ClipItem::endPos()
171 {
172   return m_startPos + m_cropDuration;
173 }
174
175 // virtual 
176  void ClipItem::paint(QPainter *painter,
177                            const QStyleOptionGraphicsItem *option,
178                            QWidget *widget)
179  {
180     QRectF br = rect();
181     painter->setRenderHints(QPainter::Antialiasing);
182     QPainterPath roundRectPathUpper,roundRectPathLower;
183     double roundingY = 20;
184     double roundingX = 20;
185     double offset = 1;
186     painter->setClipRect(option->exposedRect);
187     if (roundingX > br.width() / 2) roundingX = br.width() / 2;
188     //kDebug()<<"-----PAINTING, SCAL: "<<scale<<", height: "<<br.height();
189          roundRectPathUpper.moveTo(br.x() + br .width() - offset, br.y() + br.height()/2 - offset);
190          roundRectPathUpper.arcTo(br.x() + br .width() - roundingX - offset, br.y(), roundingX, roundingY, 0.0, 90.0);
191          roundRectPathUpper.lineTo(br.x() + roundingX, br.y());
192          roundRectPathUpper.arcTo(br.x() + offset, br.y(), roundingX, roundingY, 90.0, 90.0);
193          roundRectPathUpper.lineTo(br.x() + offset, br.y() + br.height()/2 - offset);
194          roundRectPathUpper.closeSubpath();
195          
196          roundRectPathLower.moveTo(br.x() + offset, br.y() + br.height()/2 - offset);
197          roundRectPathLower.arcTo(br.x() + offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 180.0, 90.0);
198          roundRectPathLower.lineTo(br.x() + br .width() - roundingX, br.y() + br.height() - offset);
199          roundRectPathLower.arcTo(br.x() + br .width() - roundingX - offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 270.0, 90.0);
200          roundRectPathLower.lineTo(br.x() + br .width() - offset, br.y()+ br.height()/2 - offset);
201          roundRectPathLower.closeSubpath();
202          
203          painter->setClipPath(roundRectPathUpper.united(roundRectPathLower), Qt::IntersectClip);
204     //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
205     painter->fillRect(br, brush());
206     //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
207
208     // draw thumbnails
209     if (!m_startPix.isNull()) {
210       if (m_clipType == IMAGE) {
211         painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
212         QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
213         painter->drawLine(l);
214       } else {
215         painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
216         QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
217         painter->drawLine(l);
218       }
219
220       painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
221       QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
222       painter->drawLine(l2);
223     }
224          if (m_clipType == AV || m_clipType==AUDIO ){
225                  QPainterPath path= m_clipType==AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
226                  painter->fillPath(path,QBrush(QColor(200,200,200,127)));
227                  
228                  int channels=2;
229                  double pixelForOneFrame=(double)br.width()/duration();
230                  if (framePixelWidth!=pixelForOneFrame){
231                          emit prepareAudioThumb(pixelForOneFrame,path);
232         
233                          /* re.x() changeds every time on move, so the path has to be rebuild every time
234                                 QRectF re=path.boundingRect();
235
236                          if ( !baseClip()->audioFrameChache.isEmpty()){
237
238                                  QMap<int,QPainterPath > positiveChannelPaths;
239                                  QMap<int,QPainterPath > negativeChannelPaths;
240
241                                  QPen audiopen;
242                                  audiopen.setWidth(0);
243                                  painter->setPen(audiopen);
244
245                                  for (int i=0;i<channels;i++){
246                                          
247                                          positiveChannelPaths[i].moveTo(re.x(),re.y()+re.height()*i/channels+ (re.y()+re.height()/channels)/2);
248                                          negativeChannelPaths[i].moveTo(re.x(),re.y()+re.height()*i/channels+ (re.y()+re.height()/channels)/2);
249                                  }
250                                  
251                                  for (int samples=re.x();samples<re.x()+re.width();samples++){
252                                          double frame=(double)(samples-re.x())/pixelForOneFrame;
253                                          int sample=(frame-(int)(frame))*20 ;// AUDIO_FRAME_SIZE
254                                          if (frame<0 || sample< 0 || sample>19 )
255                                                  continue;
256                                          QMap<int,QByteArray> frame_channel_data=baseClip()->audioFrameChache[(int)frame];
257                                          
258                                          for (int channel=0;channel<channels && frame_channel_data[channel].size()> 0;channel++){
259                                                  int y=re.y()+re.height()*channel/channels+ (re.height()/channels)/2;
260                                                  positiveChannelPaths[channel].lineTo(samples,y+( (int)frame_channel_data[channel][sample] -127/2 )  * (re.height()/channels) / 64 );   
261                                                  negativeChannelPaths[channel].lineTo(samples,y-( (int)frame_channel_data[channel][sample] -127/2 )  * (re.height()/channels) / 64 );
262                                          }
263                                  }
264                                  for (int i=0;i<channels;i++){
265                                          if (pixelForOneFrame<10){
266                                                  channelPaths[i]=positiveChannelPaths[i].united(negativeChannelPaths[i]);
267                                          }else{
268                                                  channelPaths[i]=positiveChannelPaths[i];
269                                          }
270                                  }
271                                  audioThumbWasDrawn=true;
272                                  framePixelWidth=pixelForOneFrame;
273                          }
274                          */
275                  }
276
277                  if (!audioThumbCachePic.isNull() )
278                         painter->drawPixmap(path.boundingRect().x(),path.boundingRect().y(),audioThumbCachePic);
279                 /*if ( channelPaths.size() )
280                 for (int i=0;i<channels;i++){
281                         painter->drawPath(channelPaths[i]);
282                 }*/
283         }
284          
285          
286          
287     // draw start / end fades
288     double scale = br.width() / m_cropDuration;
289     QBrush fades;
290     if (isSelected()) {
291       fades = QBrush(QColor(200, 50, 50, 150));
292     }
293     else fades = QBrush(QColor(200, 200, 200, 200));
294
295     if (m_startFade != 0) {
296       QPainterPath fadeInPath;
297       fadeInPath.moveTo(br.x() - offset, br.y());
298       fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
299       fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
300       fadeInPath.closeSubpath();
301       painter->fillPath(fadeInPath, fades);
302       if (isSelected()) {
303         QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
304         painter->drawLine(l);
305       }
306     }
307     if (m_endFade != 0) {
308       QPainterPath fadeOutPath;
309       fadeOutPath.moveTo(br.x() + br.width(), br.y());
310       fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
311       fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
312       fadeOutPath.closeSubpath();
313       painter->fillPath(fadeOutPath, fades);
314       if (isSelected()) {
315         QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
316         painter->drawLine(l);
317       }
318     }
319
320     QPen pen = painter->pen();
321     pen.setColor(Qt::white);
322     //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
323     // Draw clip name
324     QString effects = effectNames().join(" / ");
325     if (!effects.isEmpty()) {
326       painter->setPen(pen);
327       QFont font = painter->font();
328       QFont smallFont = font;
329       smallFont.setPointSize(8);
330       painter->setFont(smallFont);
331       QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
332       painter->fillRect(txtBounding, QBrush(QColor(0,0,0,150)));
333       painter->drawText(txtBounding, Qt::AlignCenter, effects);
334       pen.setColor(Qt::black);
335       painter->setPen(pen);
336       painter->setFont(font);
337     }
338
339     QRectF txtBounding = painter->boundingRect(br, Qt::AlignCenter, " " + m_clipName + " ");
340     painter->fillRect(txtBounding, QBrush(QColor(255,255,255,150)));
341     painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
342
343     pen.setColor(Qt::red);
344     pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
345     if (isSelected()) painter->setPen(pen);
346     painter->setClipRect(option->exposedRect);
347          painter->drawPath(roundRectPathUpper.united(roundRectPathLower));
348     //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
349
350     /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
351     painter->drawRect(recta);
352     painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
353     painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
354     painter->setPen(QPen(Qt::black, 1.0));
355     painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
356     painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
357
358     //QGraphicsRectItem::paint(painter, option, widget);
359     //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
360     //painter->setPen(pen);
361     //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
362     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
363     //painter->drawText(rect(), Qt::AlignCenter, m_name);
364     // painter->drawRect(boundingRect());
365      //painter->drawRoundRect(-10, -10, 20, 20);
366  }
367
368
369 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale)
370 {
371     if (abs(pos.x() - (rect().x() + scale * m_startFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEIN;
372     else if (abs(pos.x() - rect().x()) < 6) return RESIZESTART;
373     else if (abs(pos.x() - (rect().x() + rect().width() - scale * m_endFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEOUT;
374     else if (abs(pos.x() - (rect().x() + rect().width())) < 6) return RESIZEEND;
375     return MOVE;
376 }
377
378 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame,QPainterPath path){
379         int channels=2;
380         
381         QRectF re=path.boundingRect();
382         if (audioThumbCachePic.isNull() || framePixelWidth!=pixelForOneFrame){
383                 audioThumbCachePic=QPixmap(re.width(),re.height());
384                 audioThumbCachePic.fill(QColor(200,200,200,127));
385         }
386         if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
387                 QMap<int,QPainterPath > positiveChannelPaths;
388                 QMap<int,QPainterPath > negativeChannelPaths;
389                 QPainter pixpainter(&audioThumbCachePic);
390                 QPen audiopen;
391                 audiopen.setWidth(0);
392                 pixpainter.setPen(audiopen);
393                 pixpainter.setRenderHint(QPainter::Antialiasing,true);
394                 for (int i=0;i<channels;i++){
395                         
396                         positiveChannelPaths[i].moveTo(0,0+audioThumbCachePic.height()*i/channels+ (audioThumbCachePic.height()/channels)/2);
397                         negativeChannelPaths[i].moveTo(0,0+audioThumbCachePic.height()*i/channels+ (audioThumbCachePic.height()/channels)/2);
398                 }
399                 
400                 for (int samples=0;samples<audioThumbCachePic.width();samples++){
401                         double frame=(double)(samples-0)/pixelForOneFrame;
402                         int sample=(frame-(int)(frame))*20 ;// AUDIO_FRAME_SIZE
403                         if (frame<0 || sample< 0 || sample>19 )
404                                 continue;
405                         QMap<int,QByteArray> frame_channel_data=baseClip()->audioFrameChache[(int)frame];
406                         
407                         for (int channel=0;channel<channels && frame_channel_data[channel].size()> 0;channel++){
408                                 
409                                 int y=audioThumbCachePic.height()*channel/channels+ (/*re.height()*/audioThumbCachePic.height()/channels)/2;
410                                 
411                                 positiveChannelPaths[channel].lineTo(samples,0.1+y+( (int)frame_channel_data[channel][sample] -127/2 )  * (audioThumbCachePic.height()/channels) / 64 );        
412                                 negativeChannelPaths[channel].lineTo(samples,0.1+y-( (int)frame_channel_data[channel][sample] -127/2 )  * (audioThumbCachePic.height()/channels) / 64 );
413                         }
414                 }
415                 for (int i=0;i<channels;i++){
416                         if (pixelForOneFrame<10){
417                                 pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
418                                 pixpainter.setBrush(QBrush(QColor(200,200,100,200)));
419                                 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
420                         }else
421                                 pixpainter.drawPath(positiveChannelPaths[i]);
422                 }
423                 audioThumbWasDrawn=true;
424                 framePixelWidth=pixelForOneFrame;
425         }
426 }
427
428 int ClipItem::fadeIn() const
429 {
430   return m_startFade;
431 }
432
433 int ClipItem::fadeOut() const
434 {
435   return m_endFade;
436 }
437
438 void ClipItem::setFadeIn(int pos, double scale)
439 {
440   int oldIn = m_startFade;
441   if (pos < 0) pos = 0;
442   if (pos > m_cropDuration) pos = m_cropDuration / 2;
443   m_startFade = pos;
444   if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height()); 
445   else update(rect().x(), rect().y(), pos * scale, rect().height());
446 }
447
448 void ClipItem::setFadeOut(int pos, double scale)
449 {
450   int oldOut = m_endFade;
451   if (pos < 0) pos = 0;
452   if (pos > m_cropDuration) pos = m_cropDuration / 2;
453   m_endFade = pos;
454   if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height()); 
455   else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
456
457 }
458
459
460 // virtual
461  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
462  {
463     /*m_resizeMode = operationMode(event->pos());
464     if (m_resizeMode == MOVE) {
465       m_maxTrack = scene()->sceneRect().height();
466       m_grabPoint = (int) (event->pos().x() - rect().x());
467     }*/
468     QGraphicsRectItem::mousePressEvent(event);
469  }
470
471 // virtual
472  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
473  {
474     m_resizeMode = NONE;
475     QGraphicsRectItem::mouseReleaseEvent(event);
476  }
477
478  void ClipItem::moveTo(int x, double scale, double offset, int newTrack)
479  {
480   double origX = rect().x();
481   double origY = rect().y();
482   bool success = true;
483   if (x < 0) return;
484   setRect(x * scale, origY + offset, rect().width(), rect().height());
485   QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
486   if (collisionList.size() == 0) m_track = newTrack;
487   for (int i = 0; i < collisionList.size(); ++i) {
488     QGraphicsItem *item = collisionList.at(i);
489     if (item->type() == 70000)
490     {
491         if (offset == 0)
492         {
493           QRectF other = ((QGraphicsRectItem *)item)->rect();
494           if (x < m_startPos) {
495             kDebug()<<"COLLISION, MOVING TO------";
496             m_startPos = ((ClipItem *)item)->endPos() + 1;
497             origX = m_startPos * scale; 
498           }
499           else {
500             kDebug()<<"COLLISION, MOVING TO+++";
501             m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
502             origX = m_startPos * scale; 
503           }
504         }
505         setRect(origX, origY, rect().width(), rect().height());
506         offset = 0;
507         origX = rect().x();
508         success = false;
509         break;
510       }
511     }
512     if (success) {
513         m_track = newTrack;
514         m_startPos = x;
515     }
516 /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
517     for (int i = 0; i < childrenList.size(); ++i) {
518       childrenList.at(i)->moveBy(rect().x() - origX , offset);
519     }*/
520  }
521
522 void ClipItem::resizeStart(int posx, double scale)
523 {
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     }
530     else if (durationDiff >= m_cropDuration) {
531       durationDiff = m_cropDuration - 3;
532     }
533     m_startPos += durationDiff;
534     m_cropStart += durationDiff;
535     m_cropDuration -= durationDiff;
536     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
537     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
538     for (int i = 0; i < collisionList.size(); ++i) {
539       QGraphicsItem *item = collisionList.at(i);
540       if (item->type() == 70000)
541       {
542         int diff = ((ClipItem *)item)->endPos() + 1 - m_startPos;
543         setRect((m_startPos + diff) * scale, rect().y(), (m_cropDuration - diff) * scale, rect().height());
544         m_startPos += diff;
545         m_cropStart += diff;
546         m_cropDuration -= diff;
547         break;
548       }
549     }
550     if (m_hasThumbs) startThumbTimer->start(100);
551 }
552
553 void ClipItem::resizeEnd(int posx, double scale)
554 {
555     int durationDiff = posx - endPos();
556     if (durationDiff == 0) return;
557     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
558     if (m_cropDuration + durationDiff <= 0) {
559       durationDiff = - (m_cropDuration - 3);
560     }
561     else if (m_cropDuration + durationDiff >= m_maxDuration) {
562       durationDiff = m_maxDuration - m_cropDuration;
563     }
564     m_cropDuration += durationDiff;
565     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
566     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
567     for (int i = 0; i < collisionList.size(); ++i) {
568       QGraphicsItem *item = collisionList.at(i);
569       if (item->type() == 70000)
570       {
571         int diff = ((ClipItem *)item)->startPos() - 1 - startPos();
572         m_cropDuration = diff;
573         setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
574         break;
575       }
576     }
577     if (m_hasThumbs) endThumbTimer->start(100);
578 }
579
580 // virtual
581  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
582  {
583  }
584
585 int ClipItem::track()
586 {
587   return  m_track;
588 }
589
590 void ClipItem::setTrack(int track)
591 {
592   m_track = track;
593 }
594
595 int ClipItem::effectsCounter()
596 {
597   return m_effectsCounter++;
598 }
599
600 int ClipItem::effectsCount()
601 {
602   return m_effectList.size();
603 }
604
605 QStringList ClipItem::effectNames()
606 {
607   return m_effectList.effectNames();
608 }
609
610 QDomElement ClipItem::effectAt(int ix)
611 {
612   return m_effectList.at(ix);
613 }
614
615 void ClipItem::setEffectAt(int ix, QDomElement effect)
616 {
617   kDebug()<<"CHange EFFECT AT: "<<ix<<", CURR: "<<m_effectList.at(ix).attribute("tag")<<", NEW: "<<effect.attribute("tag");
618   m_effectList.insert(ix, effect);
619   m_effectList.removeAt(ix + 1);
620   update(boundingRect());
621 }
622
623 QMap <QString, QString> ClipItem::addEffect(QDomElement effect)
624 {
625   QMap <QString, QString> effectParams;
626   m_effectList.append(effect);
627   effectParams["tag"] = effect.attribute("tag");
628   effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
629   QDomNodeList params = effect.elementsByTagName("parameter");
630   for (int i = 0; i < params.count(); i++) {
631     QDomElement e = params.item(i).toElement();
632     if (!e.isNull())
633       effectParams[e.attribute("name")] = e.attribute("value");
634   }
635   update(boundingRect());
636   return effectParams;
637 }
638
639 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect)
640 {
641   QMap <QString, QString> effectParams;
642   effectParams["tag"] = effect.attribute("tag");
643   effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
644   QDomNodeList params = effect.elementsByTagName("parameter");
645   for (int i = 0; i < params.count(); i++) {
646     QDomElement e = params.item(i).toElement();
647     if (!e.isNull())
648       effectParams[e.attribute("name")] = e.attribute("value");
649   }
650   return effectParams;
651 }
652
653 void ClipItem::deleteEffect(QString index)
654 {
655   for (int i = 0; i < m_effectList.size(); ++i) {
656     if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
657       m_effectList.removeAt(i);
658       break;
659     }
660   }
661   update(boundingRect());
662 }
663
664
665 // virtual 
666 /*
667 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
668 {
669   int pos = event->x();
670   if (event->modifiers() == Qt::ControlModifier) 
671     setDragMode(QGraphicsView::ScrollHandDrag);
672   else if (event->modifiers() == Qt::ShiftModifier) 
673     setDragMode(QGraphicsView::RubberBandDrag);
674   else {
675     QGraphicsItem * item = itemAt(event->pos());
676     if (item) {
677     }
678     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
679   }
680   kDebug()<<pos;
681   QGraphicsView::mousePressEvent(event);
682 }
683
684 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
685 {
686   QGraphicsView::mouseReleaseEvent(event);
687   setDragMode(QGraphicsView::NoDrag);
688 }
689 */
690
691 #include "clipitem.moc"