]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
Implement clipmanager
[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(QDomElement xml, int track, int startpos, const QRectF & rect, int duration)
37     : QGraphicsRectItem(rect), m_xml(xml), 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)
38 {
39   //setToolTip(name);
40
41   m_clipName = xml.attribute("name");
42   if (m_clipName.isEmpty()) m_clipName = KUrl(xml.attribute("resource")).fileName();
43
44   m_producer = xml.attribute("id").toInt();
45
46   m_clipType = (CLIPTYPE) xml.attribute("type").toInt();
47
48   m_cropStart = xml.attribute("in", 0).toInt();
49   m_maxDuration = xml.attribute("duration", 0).toInt();
50   if (m_maxDuration == 0) m_maxDuration = xml.attribute("out", 0).toInt() - m_cropStart;
51
52   if (duration != -1) m_cropDuration = duration;
53   else m_cropDuration = m_maxDuration;
54
55   setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemClipsChildrenToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
56
57   setBrush(QColor(100, 100, 150));
58   if (m_clipType == VIDEO || m_clipType == AV) {
59     m_hasThumbs = true;
60     //connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
61     //connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
62     QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
63
64     startThumbTimer = new QTimer(this);
65     startThumbTimer->setSingleShot(true);
66     connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
67     endThumbTimer = new QTimer(this);
68     endThumbTimer->setSingleShot(true);
69     connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
70
71   }
72   else if (m_clipType == COLOR) {
73     QString colour = xml.attribute("colour");
74     colour = colour.replace(0, 2, "#");
75     setBrush(QColor(colour.left(7)));
76   }
77   else if (m_clipType == IMAGE) {
78     m_startPix = KThumb::getImage(KUrl(xml.attribute("resource")), 50 * KdenliveSettings::project_display_ratio(), 50);
79   }
80 }
81
82 ClipItem::ClipItem(DocClipBase *clip, int track, int startpos, const QRectF & rect, int duration)
83     : 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)
84 {
85   //setToolTip(name);
86   kDebug()<<"*******  CREATING NEW TML CLIP, DUR: "<<duration;
87   m_xml = clip->toXML();
88   m_clipName = clip->name();
89   m_producer = clip->getId();
90   m_clipType = clip->clipType();
91   m_cropStart = 0;
92   m_maxDuration = duration;
93   if (duration != -1) m_cropDuration = duration;
94   else m_cropDuration = m_maxDuration;
95
96   setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemClipsChildrenToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
97
98   setBrush(QColor(100, 100, 150));
99   if (m_clipType == VIDEO || m_clipType == AV) {
100     m_hasThumbs = true;
101     connect(this, SIGNAL(getThumb(int, int)), clip->thumbProducer(), SLOT(extractImage(int, int)));
102     connect(clip->thumbProducer(), SIGNAL(thumbReady(int, QPixmap)), this, SLOT(slotThumbReady(int, QPixmap)));
103     QTimer::singleShot(300, this, SLOT(slotFetchThumbs()));
104
105     startThumbTimer = new QTimer(this);
106     startThumbTimer->setSingleShot(true);
107     connect(startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
108     endThumbTimer = new QTimer(this);
109     endThumbTimer->setSingleShot(true);
110     connect(endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
111
112   }
113   else if (m_clipType == COLOR) {
114     QString colour = m_xml.attribute("colour");
115     colour = colour.replace(0, 2, "#");
116     setBrush(QColor(colour.left(7)));
117   }
118   else if (m_clipType == IMAGE) {
119     m_startPix = KThumb::getImage(KUrl(m_xml.attribute("resource")), 50 * KdenliveSettings::project_display_ratio(), 50);
120   }
121 }
122
123
124 ClipItem::~ClipItem()
125 {
126   if (startThumbTimer) delete startThumbTimer;
127   if (endThumbTimer) delete endThumbTimer;
128 }
129
130 void ClipItem::slotFetchThumbs()
131 {
132   emit getThumb(m_cropStart, m_cropStart + m_cropDuration);
133 }
134
135 void ClipItem::slotGetStartThumb()
136 {
137   emit getThumb(m_cropStart, -1);
138 }
139
140 void ClipItem::slotGetEndThumb()
141 {
142   emit getThumb(-1, m_cropStart + m_cropDuration);
143 }
144
145 void ClipItem::slotThumbReady(int frame, QPixmap pix)
146 {
147   if (frame == m_cropStart) m_startPix = pix;
148   else m_endPix = pix;
149   update();
150 }
151
152 int ClipItem::type () const
153 {
154   return 70000;
155 }
156
157 QDomElement ClipItem::xml() const
158 {
159   return m_xml;
160 }
161
162 int ClipItem::clipType()
163 {
164   return m_clipType;
165 }
166
167 QString ClipItem::clipName()
168 {
169   return m_clipName;
170 }
171
172 int ClipItem::clipProducer()
173 {
174   return m_producer;
175 }
176
177 int ClipItem::maxDuration()
178 {
179   return m_maxDuration;
180 }
181
182 int ClipItem::duration()
183 {
184   return m_cropDuration;
185 }
186
187 int ClipItem::startPos()
188 {
189   return m_startPos;
190 }
191
192 int ClipItem::endPos()
193 {
194   return m_startPos + m_cropDuration;
195 }
196
197 // virtual 
198  void ClipItem::paint(QPainter *painter,
199                            const QStyleOptionGraphicsItem *option,
200                            QWidget *widget)
201  {
202     QRectF br = rect();
203     painter->setRenderHints(QPainter::Antialiasing);
204     QPainterPath roundRectPath;
205     double roundingY = 20;
206     double roundingX = 20;
207     double offset = 1;
208     painter->setClipRect(option->exposedRect);
209     if (roundingX > br.width() / 2) roundingX = br.width() / 2;
210     //kDebug()<<"-----PAINTING, SCAL: "<<scale<<", height: "<<br.height();
211     roundRectPath.moveTo(br.x() + br .width() - offset, br.y() + roundingY);
212     roundRectPath.arcTo(br.x() + br .width() - roundingX - offset, br.y(), roundingX, roundingY, 0.0, 90.0);
213     roundRectPath.lineTo(br.x() + roundingX, br.y());
214     roundRectPath.arcTo(br.x() + offset, br.y(), roundingX, roundingY, 90.0, 90.0);
215     roundRectPath.lineTo(br.x() + offset, br.y() + br.height() - roundingY);
216     roundRectPath.arcTo(br.x() + offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 180.0, 90.0);
217     roundRectPath.lineTo(br.x() + br .width() - roundingX, br.y() + br.height() - offset);
218     roundRectPath.arcTo(br.x() + br .width() - roundingX - offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 270.0, 90.0);
219     roundRectPath.closeSubpath();
220     painter->setClipPath(roundRectPath, Qt::IntersectClip);
221     //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
222     painter->fillRect(br, brush());
223     //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
224
225     // draw thumbnails
226     if (!m_startPix.isNull()) {
227       if (m_clipType == IMAGE) {
228         painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
229         QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
230         painter->drawLine(l);
231       } else {
232         painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
233         QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
234         painter->drawLine(l);
235       }
236
237       painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
238       QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
239       painter->drawLine(l2);
240     }
241
242     // draw start / end fades
243     double scale = br.width() / m_cropDuration;
244     QBrush fades;
245     if (isSelected()) {
246       fades = QBrush(QColor(200, 50, 50, 150));
247     }
248     else fades = QBrush(QColor(200, 200, 200, 200));
249
250     if (m_startFade != 0) {
251       QPainterPath fadeInPath;
252       fadeInPath.moveTo(br.x() - offset, br.y());
253       fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
254       fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
255       fadeInPath.closeSubpath();
256       painter->fillPath(fadeInPath, fades);
257       if (isSelected()) {
258         QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
259         painter->drawLine(l);
260       }
261     }
262     if (m_endFade != 0) {
263       QPainterPath fadeOutPath;
264       fadeOutPath.moveTo(br.x() + br.width(), br.y());
265       fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
266       fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
267       fadeOutPath.closeSubpath();
268       painter->fillPath(fadeOutPath, fades);
269       if (isSelected()) {
270         QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
271         painter->drawLine(l);
272       }
273     }
274
275     painter->setClipRect(option->exposedRect);
276     QPen pen = painter->pen();
277     pen.setColor(Qt::red);
278     pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
279
280     // Draw clip name
281     QRectF txtBounding = painter->boundingRect(br, Qt::AlignCenter, " " + m_clipName + " ");
282     painter->fillRect(txtBounding, QBrush(QColor(255,255,255,150)));
283     painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
284
285     if (isSelected()) painter->setPen(pen);
286     painter->drawPath(roundRectPath);
287     //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
288
289     /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
290     painter->drawRect(recta);
291     painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
292     painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
293     painter->setPen(QPen(Qt::black, 1.0));
294     painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
295     painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
296
297     //QGraphicsRectItem::paint(painter, option, widget);
298     //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
299     //painter->setPen(pen);
300     //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
301     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
302     //painter->drawText(rect(), Qt::AlignCenter, m_name);
303     // painter->drawRect(boundingRect());
304      //painter->drawRoundRect(-10, -10, 20, 20);
305  }
306
307
308 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale)
309 {
310     if (abs(pos.x() - (rect().x() + scale * m_startFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEIN;
311     else if (abs(pos.x() - rect().x()) < 6) return RESIZESTART;
312     else if (abs(pos.x() - (rect().x() + rect().width() - scale * m_endFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEOUT;
313     else if (abs(pos.x() - (rect().x() + rect().width())) < 6) return RESIZEEND;
314     return MOVE;
315 }
316
317 int ClipItem::fadeIn() const
318 {
319   return m_startFade;
320 }
321
322 int ClipItem::fadeOut() const
323 {
324   return m_endFade;
325 }
326
327 void ClipItem::setFadeIn(int pos, double scale)
328 {
329   int oldIn = m_startFade;
330   if (pos < 0) pos = 0;
331   if (pos > m_cropDuration) pos = m_cropDuration / 2;
332   m_startFade = pos;
333   if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height()); 
334   else update(rect().x(), rect().y(), pos * scale, rect().height());
335 }
336
337 void ClipItem::setFadeOut(int pos, double scale)
338 {
339   int oldOut = m_endFade;
340   if (pos < 0) pos = 0;
341   if (pos > m_cropDuration) pos = m_cropDuration / 2;
342   m_endFade = pos;
343   if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height()); 
344   else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
345
346 }
347
348
349 // virtual
350  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
351  {
352     /*m_resizeMode = operationMode(event->pos());
353     if (m_resizeMode == MOVE) {
354       m_maxTrack = scene()->sceneRect().height();
355       m_grabPoint = (int) (event->pos().x() - rect().x());
356     }*/
357     QGraphicsRectItem::mousePressEvent(event);
358  }
359
360 // virtual
361  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
362  {
363     m_resizeMode = NONE;
364     QGraphicsRectItem::mouseReleaseEvent(event);
365  }
366
367  void ClipItem::moveTo(int x, double scale, double offset, int newTrack)
368  {
369   double origX = rect().x();
370   double origY = rect().y();
371   bool success = true;
372   setRect(x * scale, origY + offset, rect().width(), rect().height());
373   QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
374   if (collisionList.size() == 0) m_track = newTrack;
375   for (int i = 0; i < collisionList.size(); ++i) {
376     QGraphicsItem *item = collisionList.at(i);
377     if (item->type() == 70000)
378     {
379         if (offset == 0)
380         {
381           QRectF other = ((QGraphicsRectItem *)item)->rect();
382           if (x < m_startPos) {
383             kDebug()<<"COLLISION, MOVING TO------";
384             m_startPos = ((ClipItem *)item)->endPos() + 1;
385             origX = m_startPos * scale; 
386           }
387           else {
388             kDebug()<<"COLLISION, MOVING TO+++";
389             m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
390             origX = m_startPos * scale; 
391           }
392         }
393         setRect(origX, origY, rect().width(), rect().height());
394         offset = 0;
395         origX = rect().x();
396         success = false;
397         break;
398       }
399     }
400     if (success) {
401         m_track = newTrack;
402         m_startPos = x;
403     }
404 /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
405     for (int i = 0; i < childrenList.size(); ++i) {
406       childrenList.at(i)->moveBy(rect().x() - origX , offset);
407     }*/
408  }
409
410 void ClipItem::resizeStart(int posx, double scale)
411 {
412     int durationDiff = posx - m_startPos;
413     if (durationDiff == 0) return;
414     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
415     if (m_cropStart + durationDiff < 0) {
416       durationDiff = -m_cropStart;
417     }
418     else if (durationDiff >= m_cropDuration) {
419       durationDiff = m_cropDuration - 3;
420     }
421     m_startPos += durationDiff;
422     m_cropStart += durationDiff;
423     m_cropDuration -= durationDiff;
424     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
425     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
426     for (int i = 0; i < collisionList.size(); ++i) {
427       QGraphicsItem *item = collisionList.at(i);
428       if (item->type() == 70000)
429       {
430         int diff = ((ClipItem *)item)->endPos() + 1 - m_startPos;
431         setRect((m_startPos + diff) * scale, rect().y(), (m_cropDuration - diff) * scale, rect().height());
432         m_startPos += diff;
433         m_cropStart += diff;
434         m_cropDuration -= diff;
435         break;
436       }
437     }
438     if (m_hasThumbs) startThumbTimer->start(100);
439 }
440
441 void ClipItem::resizeEnd(int posx, double scale)
442 {
443     int durationDiff = posx - endPos();
444     if (durationDiff == 0) return;
445     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
446     if (m_cropDuration + durationDiff <= 0) {
447       durationDiff = - (m_cropDuration - 3);
448     }
449     else if (m_cropDuration + durationDiff >= m_maxDuration) {
450       durationDiff = m_maxDuration - m_cropDuration;
451     }
452     m_cropDuration += durationDiff;
453     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
454     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
455     for (int i = 0; i < collisionList.size(); ++i) {
456       QGraphicsItem *item = collisionList.at(i);
457       if (item->type() == 70000)
458       {
459         int diff = ((ClipItem *)item)->startPos() - 1 - startPos();
460         m_cropDuration = diff;
461         setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
462         break;
463       }
464     }
465     if (m_hasThumbs) endThumbTimer->start(100);
466 }
467
468 // virtual
469  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
470  {
471  }
472
473 int ClipItem::track()
474 {
475   return  m_track;
476 }
477
478 void ClipItem::setTrack(int track)
479 {
480   m_track = track;
481 }
482
483
484 // virtual 
485 /*
486 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
487 {
488   int pos = event->x();
489   if (event->modifiers() == Qt::ControlModifier) 
490     setDragMode(QGraphicsView::ScrollHandDrag);
491   else if (event->modifiers() == Qt::ShiftModifier) 
492     setDragMode(QGraphicsView::RubberBandDrag);
493   else {
494     QGraphicsItem * item = itemAt(event->pos());
495     if (item) {
496     }
497     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
498   }
499   kDebug()<<pos;
500   QGraphicsView::mousePressEvent(event);
501 }
502
503 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
504 {
505   QGraphicsView::mouseReleaseEvent(event);
506   setDragMode(QGraphicsView::NoDrag);
507 }
508 */
509
510 #include "clipitem.moc"