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