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