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