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