]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
start of audio thumbs
[kdenlive] / src / clipitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21
22 #include <QPainter>
23 #include <QTimer>
24 #include <QStyleOptionGraphicsItem>
25 #include <QGraphicsScene>
26
27
28 #include <KDebug>
29
30 #include <mlt++/Mlt.h>
31
32 #include "clipitem.h"
33 #include "renderer.h"
34 #include "kdenlivesettings.h"
35
36 ClipItem::ClipItem(DocClipBase *clip, int track, int startpos, const QRectF & rect, int duration)
37     : QGraphicsRectItem(rect), m_clip(clip), m_resizeMode(NONE), m_grabPoint(0), m_maxTrack(0), m_track(track), m_startPos(startpos), m_hasThumbs(false), startThumbTimer(NULL), endThumbTimer(NULL), m_startFade(0), m_endFade(0), m_effectsCounter(0)
38 {
39   //setToolTip(name);
40   kDebug()<<"*******  CREATING NEW TML CLIP, DUR: "<<duration;
41   m_xml = clip->toXML();
42   m_clipName = clip->name();
43   m_producer = clip->getId();
44   m_clipType = clip->clipType();
45   m_cropStart = 0;
46   m_maxDuration = duration;
47   if (duration != -1) m_cropDuration = duration;
48   else m_cropDuration = m_maxDuration;
49
50 /*
51   m_cropStart = xml.attribute("in", 0).toInt();
52   m_maxDuration = xml.attribute("duration", 0).toInt();
53   if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
54
55   if (duration != -1) m_cropDuration = duration;
56   else m_cropDuration = m_maxDuration;*/
57
58
59   setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemClipsChildrenToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
60
61   setBrush(QColor(100, 100, 150));
62   if (m_clipType == VIDEO || m_clipType == AV) {
63     m_hasThumbs = true;
64     connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
65     connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
66     QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
67
68     startThumbTimer = new QTimer(this);
69     startThumbTimer->setSingleShot(true);
70     connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
71     endThumbTimer = new QTimer(this);
72     endThumbTimer->setSingleShot(true);
73     connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
74
75   }
76   else if (m_clipType == COLOR) {
77     QString colour = m_xml.attribute("colour");
78     colour = colour.replace(0, 2, "#");
79     setBrush(QColor(colour.left(7)));
80   }
81   else if (m_clipType == IMAGE) {
82     m_startPix = KThumb::getImage(KUrl(m_xml.attribute("resource")), 50 * KdenliveSettings::project_display_ratio(), 50);
83   }
84 }
85
86
87 ClipItem::~ClipItem()
88 {
89   if (startThumbTimer) delete startThumbTimer;
90   if (endThumbTimer) delete endThumbTimer;
91 }
92
93 void ClipItem::slotFetchThumbs()
94 {
95   emit getThumb(m_cropStart, m_cropStart + m_cropDuration);
96 }
97
98 void ClipItem::slotGetStartThumb()
99 {
100   emit getThumb(m_cropStart, -1);
101 }
102
103 void ClipItem::slotGetEndThumb()
104 {
105   emit getThumb(-1, m_cropStart + m_cropDuration);
106 }
107
108 void ClipItem::slotThumbReady(int frame, QPixmap pix)
109 {
110   if (frame == m_cropStart) m_startPix = pix;
111   else m_endPix = pix;
112   update();
113 }
114
115 int ClipItem::type () const
116 {
117   return 70000;
118 }
119
120 DocClipBase *ClipItem::baseClip()
121 {
122   return m_clip;
123 }
124
125 QDomElement ClipItem::xml() const
126 {
127   return m_xml;
128 }
129
130 int ClipItem::clipType()
131 {
132   return m_clipType;
133 }
134
135 QString ClipItem::clipName()
136 {
137   return m_clipName;
138 }
139
140 int ClipItem::clipProducer()
141 {
142   return m_producer;
143 }
144
145 int ClipItem::maxDuration()
146 {
147   return m_maxDuration;
148 }
149
150 int ClipItem::duration()
151 {
152   return m_cropDuration;
153 }
154
155 int ClipItem::startPos()
156 {
157   return m_startPos;
158 }
159
160 int ClipItem::cropStart()
161 {
162   return m_cropStart;
163 }
164
165 int ClipItem::endPos()
166 {
167   return m_startPos + m_cropDuration;
168 }
169
170 // virtual 
171  void ClipItem::paint(QPainter *painter,
172                            const QStyleOptionGraphicsItem *option,
173                            QWidget *widget)
174  {
175     QRectF br = rect();
176     painter->setRenderHints(QPainter::Antialiasing);
177     QPainterPath roundRectPathUpper,roundRectPathLower;
178     double roundingY = 20;
179     double roundingX = 20;
180     double offset = 1;
181     painter->setClipRect(option->exposedRect);
182     if (roundingX > br.width() / 2) roundingX = br.width() / 2;
183     //kDebug()<<"-----PAINTING, SCAL: "<<scale<<", height: "<<br.height();
184          roundRectPathUpper.moveTo(br.x() + br .width() - offset, br.y() + br.height()/2 - offset);
185          roundRectPathUpper.arcTo(br.x() + br .width() - roundingX - offset, br.y(), roundingX, roundingY, 0.0, 90.0);
186          roundRectPathUpper.lineTo(br.x() + roundingX, br.y());
187          roundRectPathUpper.arcTo(br.x() + offset, br.y(), roundingX, roundingY, 90.0, 90.0);
188          roundRectPathUpper.lineTo(br.x() + offset, br.y() + br.height()/2 - offset);
189          roundRectPathUpper.closeSubpath();
190          
191          roundRectPathLower.moveTo(br.x() + offset, br.y() + br.height()/2 - offset);
192          roundRectPathLower.arcTo(br.x() + offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 180.0, 90.0);
193          roundRectPathLower.lineTo(br.x() + br .width() - roundingX, br.y() + br.height() - offset);
194          roundRectPathLower.arcTo(br.x() + br .width() - roundingX - offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 270.0, 90.0);
195          roundRectPathLower.lineTo(br.x() + br .width() - offset, br.y()+ br.height()/2 - offset);
196          roundRectPathLower.closeSubpath();
197          
198          painter->setClipPath(roundRectPathUpper.united(roundRectPathLower), Qt::IntersectClip);
199     //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
200     painter->fillRect(br, brush());
201     //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
202
203     // draw thumbnails
204     if (!m_startPix.isNull()) {
205       if (m_clipType == IMAGE) {
206         painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
207         QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
208         painter->drawLine(l);
209       } else {
210         painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
211         QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
212         painter->drawLine(l);
213       }
214
215       painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
216       QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
217       painter->drawLine(l2);
218     }
219          if (m_clipType == AV || m_clipType==AUDIO ){
220                  QPainterPath path= m_clipType==AV ? roundRectPathLower : roundRectPathUpper.united(roundRectPathLower);
221                  painter->fillPath(path,QBrush(QColor(200,200,200,127)));
222                  //for test 
223                  int channels=2;
224                  kDebug() << "audio frames=" << baseClip()->audioFrameChache.size() ;
225                  for (int channel=0;channel<channels;channel++){
226                         QRectF re=path.boundingRect();
227                         int y=re.y()+re.height()*channel/channels+ (re.height()/channels)/2;
228                         painter->drawLine(re.x() , y, re.x() + re.width(), y );
229                  }
230          }
231     // draw start / end fades
232     double scale = br.width() / m_cropDuration;
233     QBrush fades;
234     if (isSelected()) {
235       fades = QBrush(QColor(200, 50, 50, 150));
236     }
237     else fades = QBrush(QColor(200, 200, 200, 200));
238
239     if (m_startFade != 0) {
240       QPainterPath fadeInPath;
241       fadeInPath.moveTo(br.x() - offset, br.y());
242       fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
243       fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
244       fadeInPath.closeSubpath();
245       painter->fillPath(fadeInPath, fades);
246       if (isSelected()) {
247         QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
248         painter->drawLine(l);
249       }
250     }
251     if (m_endFade != 0) {
252       QPainterPath fadeOutPath;
253       fadeOutPath.moveTo(br.x() + br.width(), br.y());
254       fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
255       fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
256       fadeOutPath.closeSubpath();
257       painter->fillPath(fadeOutPath, fades);
258       if (isSelected()) {
259         QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
260         painter->drawLine(l);
261       }
262     }
263
264     QPen pen = painter->pen();
265     pen.setColor(Qt::white);
266     //pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
267     // Draw clip name
268     QString effects = effectNames().join(" / ");
269     if (!effects.isEmpty()) {
270       painter->setPen(pen);
271       QFont font = painter->font();
272       QFont smallFont = font;
273       smallFont.setPointSize(8);
274       painter->setFont(smallFont);
275       QRectF txtBounding = painter->boundingRect(br, Qt::AlignLeft | Qt::AlignTop, " " + effects + " ");
276       painter->fillRect(txtBounding, QBrush(QColor(0,0,0,150)));
277       painter->drawText(txtBounding, Qt::AlignCenter, effects);
278       pen.setColor(Qt::black);
279       painter->setPen(pen);
280       painter->setFont(font);
281     }
282
283     QRectF txtBounding = painter->boundingRect(br, Qt::AlignCenter, " " + m_clipName + " ");
284     painter->fillRect(txtBounding, QBrush(QColor(255,255,255,150)));
285     painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
286
287     pen.setColor(Qt::red);
288     pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
289     if (isSelected()) painter->setPen(pen);
290     painter->setClipRect(option->exposedRect);
291          painter->drawPath(roundRectPathUpper.united(roundRectPathLower));
292     //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
293
294     /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
295     painter->drawRect(recta);
296     painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
297     painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
298     painter->setPen(QPen(Qt::black, 1.0));
299     painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
300     painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
301
302     //QGraphicsRectItem::paint(painter, option, widget);
303     //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
304     //painter->setPen(pen);
305     //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
306     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
307     //painter->drawText(rect(), Qt::AlignCenter, m_name);
308     // painter->drawRect(boundingRect());
309      //painter->drawRoundRect(-10, -10, 20, 20);
310  }
311
312
313 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale)
314 {
315     if (abs(pos.x() - (rect().x() + scale * m_startFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEIN;
316     else if (abs(pos.x() - rect().x()) < 6) return RESIZESTART;
317     else if (abs(pos.x() - (rect().x() + rect().width() - scale * m_endFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEOUT;
318     else if (abs(pos.x() - (rect().x() + rect().width())) < 6) return RESIZEEND;
319     return MOVE;
320 }
321
322 int ClipItem::fadeIn() const
323 {
324   return m_startFade;
325 }
326
327 int ClipItem::fadeOut() const
328 {
329   return m_endFade;
330 }
331
332 void ClipItem::setFadeIn(int pos, double scale)
333 {
334   int oldIn = m_startFade;
335   if (pos < 0) pos = 0;
336   if (pos > m_cropDuration) pos = m_cropDuration / 2;
337   m_startFade = pos;
338   if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height()); 
339   else update(rect().x(), rect().y(), pos * scale, rect().height());
340 }
341
342 void ClipItem::setFadeOut(int pos, double scale)
343 {
344   int oldOut = m_endFade;
345   if (pos < 0) pos = 0;
346   if (pos > m_cropDuration) pos = m_cropDuration / 2;
347   m_endFade = pos;
348   if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height()); 
349   else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
350
351 }
352
353
354 // virtual
355  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
356  {
357     /*m_resizeMode = operationMode(event->pos());
358     if (m_resizeMode == MOVE) {
359       m_maxTrack = scene()->sceneRect().height();
360       m_grabPoint = (int) (event->pos().x() - rect().x());
361     }*/
362     QGraphicsRectItem::mousePressEvent(event);
363  }
364
365 // virtual
366  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
367  {
368     m_resizeMode = NONE;
369     QGraphicsRectItem::mouseReleaseEvent(event);
370  }
371
372  void ClipItem::moveTo(int x, double scale, double offset, int newTrack)
373  {
374   double origX = rect().x();
375   double origY = rect().y();
376   bool success = true;
377   if (x < 0) return;
378   setRect(x * scale, origY + offset, rect().width(), rect().height());
379   QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
380   if (collisionList.size() == 0) m_track = newTrack;
381   for (int i = 0; i < collisionList.size(); ++i) {
382     QGraphicsItem *item = collisionList.at(i);
383     if (item->type() == 70000)
384     {
385         if (offset == 0)
386         {
387           QRectF other = ((QGraphicsRectItem *)item)->rect();
388           if (x < m_startPos) {
389             kDebug()<<"COLLISION, MOVING TO------";
390             m_startPos = ((ClipItem *)item)->endPos() + 1;
391             origX = m_startPos * scale; 
392           }
393           else {
394             kDebug()<<"COLLISION, MOVING TO+++";
395             m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
396             origX = m_startPos * scale; 
397           }
398         }
399         setRect(origX, origY, rect().width(), rect().height());
400         offset = 0;
401         origX = rect().x();
402         success = false;
403         break;
404       }
405     }
406     if (success) {
407         m_track = newTrack;
408         m_startPos = x;
409     }
410 /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
411     for (int i = 0; i < childrenList.size(); ++i) {
412       childrenList.at(i)->moveBy(rect().x() - origX , offset);
413     }*/
414  }
415
416 void ClipItem::resizeStart(int posx, double scale)
417 {
418     int durationDiff = posx - m_startPos;
419     if (durationDiff == 0) return;
420     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
421     if (m_cropStart + durationDiff < 0) {
422       durationDiff = -m_cropStart;
423     }
424     else if (durationDiff >= m_cropDuration) {
425       durationDiff = m_cropDuration - 3;
426     }
427     m_startPos += durationDiff;
428     m_cropStart += durationDiff;
429     m_cropDuration -= durationDiff;
430     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
431     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
432     for (int i = 0; i < collisionList.size(); ++i) {
433       QGraphicsItem *item = collisionList.at(i);
434       if (item->type() == 70000)
435       {
436         int diff = ((ClipItem *)item)->endPos() + 1 - m_startPos;
437         setRect((m_startPos + diff) * scale, rect().y(), (m_cropDuration - diff) * scale, rect().height());
438         m_startPos += diff;
439         m_cropStart += diff;
440         m_cropDuration -= diff;
441         break;
442       }
443     }
444     if (m_hasThumbs) startThumbTimer->start(100);
445 }
446
447 void ClipItem::resizeEnd(int posx, double scale)
448 {
449     int durationDiff = posx - endPos();
450     if (durationDiff == 0) return;
451     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
452     if (m_cropDuration + durationDiff <= 0) {
453       durationDiff = - (m_cropDuration - 3);
454     }
455     else if (m_cropDuration + durationDiff >= m_maxDuration) {
456       durationDiff = m_maxDuration - m_cropDuration;
457     }
458     m_cropDuration += durationDiff;
459     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
460     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
461     for (int i = 0; i < collisionList.size(); ++i) {
462       QGraphicsItem *item = collisionList.at(i);
463       if (item->type() == 70000)
464       {
465         int diff = ((ClipItem *)item)->startPos() - 1 - startPos();
466         m_cropDuration = diff;
467         setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
468         break;
469       }
470     }
471     if (m_hasThumbs) endThumbTimer->start(100);
472 }
473
474 // virtual
475  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
476  {
477  }
478
479 int ClipItem::track()
480 {
481   return  m_track;
482 }
483
484 void ClipItem::setTrack(int track)
485 {
486   m_track = track;
487 }
488
489 int ClipItem::effectsCounter()
490 {
491   return m_effectsCounter++;
492 }
493
494 int ClipItem::effectsCount()
495 {
496   return m_effectList.size();
497 }
498
499 QStringList ClipItem::effectNames()
500 {
501   return m_effectList.effectNames();
502 }
503
504 QDomElement ClipItem::effectAt(int ix)
505 {
506   return m_effectList.at(ix);
507 }
508
509 void ClipItem::setEffectAt(int ix, QDomElement effect)
510 {
511   kDebug()<<"CHange EFFECT AT: "<<ix<<", CURR: "<<m_effectList.at(ix).attribute("tag")<<", NEW: "<<effect.attribute("tag");
512   m_effectList.insert(ix, effect);
513   m_effectList.removeAt(ix + 1);
514   update(boundingRect());
515 }
516
517 QMap <QString, QString> ClipItem::addEffect(QDomElement effect)
518 {
519   QMap <QString, QString> effectParams;
520   m_effectList.append(effect);
521   effectParams["tag"] = effect.attribute("tag");
522   effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
523   QDomNodeList params = effect.elementsByTagName("parameter");
524   for (int i = 0; i < params.count(); i++) {
525     QDomElement e = params.item(i).toElement();
526     if (!e.isNull())
527       effectParams[e.attribute("name")] = e.attribute("value");
528   }
529   update(boundingRect());
530   return effectParams;
531 }
532
533 QMap <QString, QString> ClipItem::getEffectArgs(QDomElement effect)
534 {
535   QMap <QString, QString> effectParams;
536   effectParams["tag"] = effect.attribute("tag");
537   effectParams["kdenlive_ix"] = effect.attribute("kdenlive_ix");
538   QDomNodeList params = effect.elementsByTagName("parameter");
539   for (int i = 0; i < params.count(); i++) {
540     QDomElement e = params.item(i).toElement();
541     if (!e.isNull())
542       effectParams[e.attribute("name")] = e.attribute("value");
543   }
544   return effectParams;
545 }
546
547 void ClipItem::deleteEffect(QString index)
548 {
549   for (int i = 0; i < m_effectList.size(); ++i) {
550     if (m_effectList.at(i).attribute("kdenlive_ix") == index) {
551       m_effectList.removeAt(i);
552       break;
553     }
554   }
555   update(boundingRect());
556 }
557
558
559 // virtual 
560 /*
561 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
562 {
563   int pos = event->x();
564   if (event->modifiers() == Qt::ControlModifier) 
565     setDragMode(QGraphicsView::ScrollHandDrag);
566   else if (event->modifiers() == Qt::ShiftModifier) 
567     setDragMode(QGraphicsView::RubberBandDrag);
568   else {
569     QGraphicsItem * item = itemAt(event->pos());
570     if (item) {
571     }
572     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
573   }
574   kDebug()<<pos;
575   QGraphicsView::mousePressEvent(event);
576 }
577
578 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
579 {
580   QGraphicsView::mouseReleaseEvent(event);
581   setDragMode(QGraphicsView::NoDrag);
582 }
583 */
584
585 #include "clipitem.moc"