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