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