]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
Don't allow to move clips under 0, small fixes
[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)
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 roundRectPath;
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     roundRectPath.moveTo(br.x() + br .width() - offset, br.y() + roundingY);
185     roundRectPath.arcTo(br.x() + br .width() - roundingX - offset, br.y(), roundingX, roundingY, 0.0, 90.0);
186     roundRectPath.lineTo(br.x() + roundingX, br.y());
187     roundRectPath.arcTo(br.x() + offset, br.y(), roundingX, roundingY, 90.0, 90.0);
188     roundRectPath.lineTo(br.x() + offset, br.y() + br.height() - roundingY);
189     roundRectPath.arcTo(br.x() + offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 180.0, 90.0);
190     roundRectPath.lineTo(br.x() + br .width() - roundingX, br.y() + br.height() - offset);
191     roundRectPath.arcTo(br.x() + br .width() - roundingX - offset, br.y() + br.height() - roundingY - offset, roundingX, roundingY, 270.0, 90.0);
192     roundRectPath.closeSubpath();
193     painter->setClipPath(roundRectPath, Qt::IntersectClip);
194     //painter->fillPath(roundRectPath, brush()); //, QBrush(QColor(Qt::red)));
195     painter->fillRect(br, brush());
196     //painter->fillRect(QRectF(br.x() + br.width() - m_endPix.width(), br.y(), m_endPix.width(), br.height()), QBrush(QColor(Qt::black)));
197
198     // draw thumbnails
199     if (!m_startPix.isNull()) {
200       if (m_clipType == IMAGE) {
201         painter->drawPixmap(QPointF(br.x() + br.width() - m_startPix.width(), br.y()), m_startPix);
202         QLineF l(br.x() + br.width() - m_startPix.width(), br.y(), br.x() + br.width() - m_startPix.width(), br.y() + br.height());
203         painter->drawLine(l);
204       } else {
205         painter->drawPixmap(QPointF(br.x() + br.width() - m_endPix.width(), br.y()), m_endPix);
206         QLineF l(br.x() + br.width() - m_endPix.width(), br.y(), br.x() + br.width() - m_endPix.width(), br.y() + br.height());
207         painter->drawLine(l);
208       }
209
210       painter->drawPixmap(QPointF(br.x(), br.y()), m_startPix);
211       QLineF l2(br.x() + m_startPix.width(), br.y(), br.x() + m_startPix.width(), br.y() + br.height());
212       painter->drawLine(l2);
213     }
214
215     // draw start / end fades
216     double scale = br.width() / m_cropDuration;
217     QBrush fades;
218     if (isSelected()) {
219       fades = QBrush(QColor(200, 50, 50, 150));
220     }
221     else fades = QBrush(QColor(200, 200, 200, 200));
222
223     if (m_startFade != 0) {
224       QPainterPath fadeInPath;
225       fadeInPath.moveTo(br.x() - offset, br.y());
226       fadeInPath.lineTo(br.x() - offset, br.y() + br.height());
227       fadeInPath.lineTo(br.x() + m_startFade * scale, br.y());
228       fadeInPath.closeSubpath();
229       painter->fillPath(fadeInPath, fades);
230       if (isSelected()) {
231         QLineF l(br.x() + m_startFade * scale, br.y(), br.x(), br.y() + br.height());
232         painter->drawLine(l);
233       }
234     }
235     if (m_endFade != 0) {
236       QPainterPath fadeOutPath;
237       fadeOutPath.moveTo(br.x() + br.width(), br.y());
238       fadeOutPath.lineTo(br.x() + br.width(), br.y() + br.height());
239       fadeOutPath.lineTo(br.x() + br.width() - m_endFade * scale, br.y());
240       fadeOutPath.closeSubpath();
241       painter->fillPath(fadeOutPath, fades);
242       if (isSelected()) {
243         QLineF l(br.x() + br.width() - m_endFade * scale, br.y(), br.x() + br.width(), br.y() + br.height());
244         painter->drawLine(l);
245       }
246     }
247
248     painter->setClipRect(option->exposedRect);
249     QPen pen = painter->pen();
250     pen.setColor(Qt::red);
251     pen.setStyle(Qt::DashDotDotLine); //Qt::DotLine);
252
253     // Draw clip name
254     QRectF txtBounding = painter->boundingRect(br, Qt::AlignCenter, " " + m_clipName + " ");
255     painter->fillRect(txtBounding, QBrush(QColor(255,255,255,150)));
256     painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
257
258     if (isSelected()) painter->setPen(pen);
259     painter->drawPath(roundRectPath);
260     //painter->fillRect(QRect(br.x(), br.y(), roundingX, roundingY), QBrush(QColor(Qt::green)));
261
262     /*QRectF recta(rect().x(), rect().y(), scale,rect().height());
263     painter->drawRect(recta);
264     painter->drawLine(rect().x() + 1, rect().y(), rect().x() + 1, rect().y() + rect().height());
265     painter->drawLine(rect().x() + rect().width(), rect().y(), rect().x() + rect().width(), rect().y() + rect().height());
266     painter->setPen(QPen(Qt::black, 1.0));
267     painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
268     painter->drawLine(rect().x(), rect().y() + rect().height(), rect().x() + rect().width(), rect().y() + rect().height());*/
269
270     //QGraphicsRectItem::paint(painter, option, widget);
271     //QPen pen(Qt::green, 1.0 / size.x() + 0.5);
272     //painter->setPen(pen);
273     //painter->drawLine(rect().x(), rect().y(), rect().x() + rect().width(), rect().y());
274     //kDebug()<<"ITEM REPAINT RECT: "<<boundingRect().width();
275     //painter->drawText(rect(), Qt::AlignCenter, m_name);
276     // painter->drawRect(boundingRect());
277      //painter->drawRoundRect(-10, -10, 20, 20);
278  }
279
280
281 OPERATIONTYPE ClipItem::operationMode(QPointF pos, double scale)
282 {
283     if (abs(pos.x() - (rect().x() + scale * m_startFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEIN;
284     else if (abs(pos.x() - rect().x()) < 6) return RESIZESTART;
285     else if (abs(pos.x() - (rect().x() + rect().width() - scale * m_endFade)) < 6 && abs(pos.y() - rect().y()) < 6) return FADEOUT;
286     else if (abs(pos.x() - (rect().x() + rect().width())) < 6) return RESIZEEND;
287     return MOVE;
288 }
289
290 int ClipItem::fadeIn() const
291 {
292   return m_startFade;
293 }
294
295 int ClipItem::fadeOut() const
296 {
297   return m_endFade;
298 }
299
300 void ClipItem::setFadeIn(int pos, double scale)
301 {
302   int oldIn = m_startFade;
303   if (pos < 0) pos = 0;
304   if (pos > m_cropDuration) pos = m_cropDuration / 2;
305   m_startFade = pos;
306   if (oldIn > pos) update(rect().x(), rect().y(), oldIn * scale, rect().height()); 
307   else update(rect().x(), rect().y(), pos * scale, rect().height());
308 }
309
310 void ClipItem::setFadeOut(int pos, double scale)
311 {
312   int oldOut = m_endFade;
313   if (pos < 0) pos = 0;
314   if (pos > m_cropDuration) pos = m_cropDuration / 2;
315   m_endFade = pos;
316   if (oldOut > pos) update(rect().x() + rect().width() - pos * scale, rect().y(), pos * scale, rect().height()); 
317   else update(rect().x() + rect().width() - oldOut * scale, rect().y(), oldOut * scale, rect().height());
318
319 }
320
321
322 // virtual
323  void ClipItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) 
324  {
325     /*m_resizeMode = operationMode(event->pos());
326     if (m_resizeMode == MOVE) {
327       m_maxTrack = scene()->sceneRect().height();
328       m_grabPoint = (int) (event->pos().x() - rect().x());
329     }*/
330     QGraphicsRectItem::mousePressEvent(event);
331  }
332
333 // virtual
334  void ClipItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) 
335  {
336     m_resizeMode = NONE;
337     QGraphicsRectItem::mouseReleaseEvent(event);
338  }
339
340  void ClipItem::moveTo(int x, double scale, double offset, int newTrack)
341  {
342   double origX = rect().x();
343   double origY = rect().y();
344   bool success = true;
345   if (x < 0) return;
346   setRect(x * scale, origY + offset, rect().width(), rect().height());
347   QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
348   if (collisionList.size() == 0) m_track = newTrack;
349   for (int i = 0; i < collisionList.size(); ++i) {
350     QGraphicsItem *item = collisionList.at(i);
351     if (item->type() == 70000)
352     {
353         if (offset == 0)
354         {
355           QRectF other = ((QGraphicsRectItem *)item)->rect();
356           if (x < m_startPos) {
357             kDebug()<<"COLLISION, MOVING TO------";
358             m_startPos = ((ClipItem *)item)->endPos() + 1;
359             origX = m_startPos * scale; 
360           }
361           else {
362             kDebug()<<"COLLISION, MOVING TO+++";
363             m_startPos = ((ClipItem *)item)->startPos() - m_cropDuration;
364             origX = m_startPos * scale; 
365           }
366         }
367         setRect(origX, origY, rect().width(), rect().height());
368         offset = 0;
369         origX = rect().x();
370         success = false;
371         break;
372       }
373     }
374     if (success) {
375         m_track = newTrack;
376         m_startPos = x;
377     }
378 /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
379     for (int i = 0; i < childrenList.size(); ++i) {
380       childrenList.at(i)->moveBy(rect().x() - origX , offset);
381     }*/
382  }
383
384 void ClipItem::resizeStart(int posx, double scale)
385 {
386     int durationDiff = posx - m_startPos;
387     if (durationDiff == 0) return;
388     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
389     if (m_cropStart + durationDiff < 0) {
390       durationDiff = -m_cropStart;
391     }
392     else if (durationDiff >= m_cropDuration) {
393       durationDiff = m_cropDuration - 3;
394     }
395     m_startPos += durationDiff;
396     m_cropStart += durationDiff;
397     m_cropDuration -= durationDiff;
398     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
399     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
400     for (int i = 0; i < collisionList.size(); ++i) {
401       QGraphicsItem *item = collisionList.at(i);
402       if (item->type() == 70000)
403       {
404         int diff = ((ClipItem *)item)->endPos() + 1 - m_startPos;
405         setRect((m_startPos + diff) * scale, rect().y(), (m_cropDuration - diff) * scale, rect().height());
406         m_startPos += diff;
407         m_cropStart += diff;
408         m_cropDuration -= diff;
409         break;
410       }
411     }
412     if (m_hasThumbs) startThumbTimer->start(100);
413 }
414
415 void ClipItem::resizeEnd(int posx, double scale)
416 {
417     int durationDiff = posx - endPos();
418     if (durationDiff == 0) return;
419     kDebug()<<"-- RESCALE: CROP="<<m_cropStart<<", DIFF = "<<durationDiff;
420     if (m_cropDuration + durationDiff <= 0) {
421       durationDiff = - (m_cropDuration - 3);
422     }
423     else if (m_cropDuration + durationDiff >= m_maxDuration) {
424       durationDiff = m_maxDuration - m_cropDuration;
425     }
426     m_cropDuration += durationDiff;
427     setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
428     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
429     for (int i = 0; i < collisionList.size(); ++i) {
430       QGraphicsItem *item = collisionList.at(i);
431       if (item->type() == 70000)
432       {
433         int diff = ((ClipItem *)item)->startPos() - 1 - startPos();
434         m_cropDuration = diff;
435         setRect(m_startPos * scale, rect().y(), m_cropDuration * scale, rect().height());
436         break;
437       }
438     }
439     if (m_hasThumbs) endThumbTimer->start(100);
440 }
441
442 // virtual
443  void ClipItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) 
444  {
445  }
446
447 int ClipItem::track()
448 {
449   return  m_track;
450 }
451
452 void ClipItem::setTrack(int track)
453 {
454   m_track = track;
455 }
456
457
458 // virtual 
459 /*
460 void CustomTrackView::mousePressEvent ( QMouseEvent * event )
461 {
462   int pos = event->x();
463   if (event->modifiers() == Qt::ControlModifier) 
464     setDragMode(QGraphicsView::ScrollHandDrag);
465   else if (event->modifiers() == Qt::ShiftModifier) 
466     setDragMode(QGraphicsView::RubberBandDrag);
467   else {
468     QGraphicsItem * item = itemAt(event->pos());
469     if (item) {
470     }
471     else emit cursorMoved((int) mapToScene(event->x(), 0).x());
472   }
473   kDebug()<<pos;
474   QGraphicsView::mousePressEvent(event);
475 }
476
477 void CustomTrackView::mouseReleaseEvent ( QMouseEvent * event )
478 {
479   QGraphicsView::mouseReleaseEvent(event);
480   setDragMode(QGraphicsView::NoDrag);
481 }
482 */
483
484 #include "clipitem.moc"