]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Fix possible crash on track deletion: http://kdenlive.org/mantis/view.php?id=2967
[kdenlive] / src / abstractclipitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Marco Gittler (g.marco@freenet.de)              *
3  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
19  ***************************************************************************/
20
21 #include "abstractclipitem.h"
22 #include "customtrackscene.h"
23 #include "kdenlivesettings.h"
24
25 #include <KDebug>
26 #include <KLocale>
27 #include <KGlobalSettings>
28
29 #include <QPainter>
30 #include <QToolTip>
31 #include <QGraphicsSceneMouseEvent>
32 #include <QParallelAnimationGroup>
33
34 AbstractClipItem::AbstractClipItem(const ItemInfo &info, const QRectF& rect, double fps) :
35         QObject(),
36         QGraphicsRectItem(rect),
37         m_info(info),
38         m_editedKeyframe(-1),
39         m_selectedKeyframe(0),
40         m_keyframeFactor(1),
41         m_keyframeOffset(0),
42         m_fps(fps),
43         m_isMainSelectedClip(false)
44 {
45     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
46 #if QT_VERSION >= 0x040600
47     setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
48     setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
49 #endif
50 }
51
52 AbstractClipItem::~AbstractClipItem()
53 {
54 }
55
56 void AbstractClipItem::closeAnimation()
57 {
58 #if QT_VERSION >= 0x040600
59     if (!isEnabled()) return;
60     setEnabled(false);
61     setFlag(QGraphicsItem::ItemIsSelectable, false);
62     if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
63         // animation disabled
64         deleteLater();
65         return;
66     }
67     QPropertyAnimation *closeAnimation = new QPropertyAnimation(this, "rect");
68     QPropertyAnimation *closeAnimation2 = new QPropertyAnimation(this, "opacity");
69     closeAnimation->setDuration(200);
70     closeAnimation2->setDuration(200);
71     QRectF r = rect();
72     QRectF r2 = r;
73     r2.setLeft(r.left() + r.width() / 2);
74     r2.setTop(r.top() + r.height() / 2);
75     r2.setWidth(1);
76     r2.setHeight(1);
77     closeAnimation->setStartValue(r);
78     closeAnimation->setEndValue(r2);
79     closeAnimation->setEasingCurve(QEasingCurve::InQuad);
80     closeAnimation2->setStartValue(1.0);
81     closeAnimation2->setEndValue(0.0);
82     QParallelAnimationGroup *group = new QParallelAnimationGroup;
83     connect(group, SIGNAL(finished()), this, SLOT(deleteLater()));
84     group->addAnimation(closeAnimation);
85     group->addAnimation(closeAnimation2);
86     group->start(QAbstractAnimation::DeleteWhenStopped);
87 #endif
88 }
89
90 ItemInfo AbstractClipItem::info() const
91 {
92     ItemInfo info = m_info;
93     info.cropStart = cropStart();
94     info.endPos = endPos();
95     return info;
96 }
97
98 int AbstractClipItem::defaultZValue() const
99 {
100     return 2;
101 }
102
103 GenTime AbstractClipItem::endPos() const
104 {
105     return m_info.startPos + m_info.cropDuration;
106 }
107
108 int AbstractClipItem::track() const
109 {
110     return m_info.track;
111 }
112
113 GenTime AbstractClipItem::cropStart() const
114 {
115     return m_info.cropStart;
116 }
117
118 GenTime AbstractClipItem::cropDuration() const
119 {
120     return m_info.cropDuration;
121 }
122
123 void AbstractClipItem::setCropStart(GenTime pos)
124 {
125     m_info.cropStart = pos;
126 }
127
128 void AbstractClipItem::updateItem()
129 {
130     m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
131     m_info.startPos = GenTime((int) scenePos().x(), m_fps);
132 }
133
134 void AbstractClipItem::updateRectGeometry()
135 {
136     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
137 }
138
139 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit, bool /*emitChange*/)
140 {
141     GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
142     if (durationDiff == GenTime()) return;
143
144     if (type() == AVWIDGET && hasSizeLimit && (cropStart() + durationDiff < GenTime())) {
145         durationDiff = GenTime() - cropStart();
146     } else if (durationDiff >= cropDuration()) {
147         return;
148         /*if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
149         else return;*/
150     }
151     m_info.startPos += durationDiff;
152
153     // set to true if crop from start is negative (possible for color clips, images as they have no size limit)
154     bool negCropStart = false;
155     if (type() == AVWIDGET) {
156         m_info.cropStart += durationDiff;
157         if (m_info.cropStart < GenTime())
158             negCropStart = true;
159     }
160
161     m_info.cropDuration -= durationDiff;
162     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
163     moveBy(durationDiff.frames(m_fps), 0);
164
165     if (m_info.startPos != GenTime(posx, m_fps)) {
166         //kDebug() << "//////  WARNING, DIFF IN XPOS: " << pos().x() << " == " << m_info.startPos.frames(m_fps);
167         GenTime diff = m_info.startPos - GenTime(posx, m_fps);
168
169         if (type() == AVWIDGET)
170             m_info.cropStart += diff;
171
172         m_info.cropDuration -= diff;
173         setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
174         //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
175     }
176
177     // set crop from start to 0 (isn't relevant as this only happens for color clips, images)
178     if (negCropStart)
179         m_info.cropStart = GenTime();
180
181     //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
182     //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
183
184     /*    if (durationDiff < GenTime()) {
185             QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
186             for (int i = 0; i < collisionList.size(); ++i) {
187                 QGraphicsItem *item = collisionList.at(i);
188                 if (item->type() == type() && item->pos().x() < pos().x()) {
189                     kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
190                     GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
191                     setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
192                     setPos((m_startPos + diff).frames(m_fps), pos().y());
193                     m_startPos += diff;
194                     if (type() == AVWIDGET) m_cropStart += diff;
195                     m_cropDuration = m_cropDuration - diff;
196                     break;
197                 }
198             }
199         }*/
200 }
201
202 void AbstractClipItem::resizeEnd(int posx, bool /*emitChange*/)
203 {
204     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
205     if (durationDiff == GenTime()) return;
206     if (cropDuration() + durationDiff <= GenTime()) {
207         durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
208     }
209
210     m_info.cropDuration += durationDiff;
211     m_info.endPos += durationDiff;
212
213     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
214     if (durationDiff > GenTime()) {
215         QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
216         bool fixItem = false;
217         for (int i = 0; i < collisionList.size(); ++i) {
218             if (!collisionList.at(i)->isEnabled()) continue;
219             QGraphicsItem *item = collisionList.at(i);
220             if (item->type() == type() && item->pos().x() > pos().x()) {
221                 //kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
222                 //kDebug() << "/////////  CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
223                 //kDebug() << "/////////  COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
224                 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
225                 if (fixItem == false || diff < m_info.cropDuration) {
226                     fixItem = true;
227                     m_info.cropDuration = diff;
228                 }
229             }
230         }
231         if (fixItem) setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
232     }
233 }
234
235 GenTime AbstractClipItem::startPos() const
236 {
237     return m_info.startPos;
238 }
239
240 void AbstractClipItem::setTrack(int track)
241 {
242     m_info.track = track;
243 }
244
245 double AbstractClipItem::fps() const
246 {
247     return m_fps;
248 }
249
250 void AbstractClipItem::updateFps(double fps)
251 {
252     m_fps = fps;
253     setPos((qreal) startPos().frames(m_fps), pos().y());
254     updateRectGeometry();
255 }
256
257 GenTime AbstractClipItem::maxDuration() const
258 {
259     return m_maxDuration;
260 }
261
262 void AbstractClipItem::drawKeyFrames(QPainter *painter, const QTransform transformation, bool limitedKeyFrames)
263 {
264     if (m_keyframes.count() < 1)
265         return;
266     QRectF br = rect();
267     double maxw = br.width() / cropDuration().frames(m_fps);
268     double maxh = br.height() / 100.0 * m_keyframeFactor;
269     double start = cropStart().frames(m_fps);
270     double x1, y1, x2, y2;
271     bool antialiasing = painter->renderHints() & QPainter::Antialiasing;
272
273     // draw line showing default value
274     bool active = isSelected() || (parentItem() && parentItem()->isSelected());
275     if (active) {
276         x1 = br.x();
277         x2 = br.right();
278         if (limitedKeyFrames) {
279             QMap<int, int>::const_iterator end = m_keyframes.constEnd();
280             end--;
281             x2 = x1 + maxw * (end.key() - start);
282             x1 += maxw * (m_keyframes.constBegin().key() - start);
283         }
284         y1 = br.bottom() - (m_keyframeDefault - m_keyframeOffset) * maxh;
285         QLineF l(x1, y1, x2, y1);
286         QLineF l2 = transformation.map(l);
287         painter->setPen(QColor(168, 168, 168, 180));
288         painter->drawLine(l2);
289         painter->setPen(QColor(108, 108, 108, 180));
290         painter->drawLine(l2.translated(0, 1));
291         painter->setPen(QColor(Qt::white));
292         painter->setRenderHint(QPainter::Antialiasing);
293     }
294
295     // draw keyframes
296     QMap<int, int>::const_iterator i = m_keyframes.constBegin();
297     QColor color(Qt::blue);
298     QLineF l2;
299     x1 = br.x() + maxw * (i.key() - start);
300     y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
301
302
303
304     // make sure line begins with clip beginning
305     if (!limitedKeyFrames && i.key() != start) {
306         QLineF l(br.x(), y1, x1, y1);
307         l2 = transformation.map(l);
308         painter->drawLine(l2);
309     }
310     while (i != m_keyframes.constEnd()) {
311         if (i.key() == m_editedKeyframe)
312             color = QColor(Qt::red);
313         else
314             color = QColor(Qt::blue);
315         ++i;
316         if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
317             break;
318
319         if (m_keyframes.count() == 1) {
320             x2 = br.right();
321             y2 = y1;
322         } else {
323             x2 = br.x() + maxw * (i.key() - start);
324             y2 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
325         }
326         QLineF l(x1, y1, x2, y2);
327         l2 = transformation.map(l);
328         painter->drawLine(l2);
329         if (active) {
330             const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
331             painter->fillRect(frame, color);
332         }
333         x1 = x2;
334         y1 = y2;
335     }
336
337     // make sure line ends at clip end
338     if (!limitedKeyFrames && x1 != br.right()) {
339         QLineF l(x1, y1, br.right(), y1);
340         painter->drawLine(transformation.map(l));
341     }
342
343     if (active && m_keyframes.count() > 1) {
344         const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
345         painter->fillRect(frame, color);
346     }
347
348     painter->setRenderHint(QPainter::Antialiasing, antialiasing);
349 }
350
351 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
352 {
353     const QRectF br = sceneBoundingRect();
354     double maxw = br.width() / cropDuration().frames(m_fps);
355     double maxh = br.height() / 100.0 * m_keyframeFactor;
356     if (m_keyframes.count() > 0) {
357         QMap<int, int>::const_iterator i = m_keyframes.constBegin();
358         double x1;
359         double y1;
360         while (i != m_keyframes.constEnd()) {
361             x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
362             y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
363             if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
364                 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + ']');
365                 return i.key();
366             } else if (x1 > pos.x()) {
367                 break;
368             }
369             ++i;
370         }
371     }
372     setToolTip(QString());
373     return -1;
374 }
375
376 void AbstractClipItem::updateSelectedKeyFrame()
377 {
378     if (m_editedKeyframe == -1)
379         return;
380     QRectF br = sceneBoundingRect();
381     double maxw = br.width() / cropDuration().frames(m_fps);
382     double maxh = br.height() / 100.0 * m_keyframeFactor;
383     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
384     m_selectedKeyframe = m_editedKeyframe;
385     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
386 }
387
388 int AbstractClipItem::editedKeyFramePos() const
389 {
390     return m_editedKeyframe;
391 }
392
393 double AbstractClipItem::editedKeyFrameValue() const
394 {
395     return m_keyframes.value(m_editedKeyframe);
396 }
397
398 int AbstractClipItem::selectedKeyFramePos() const
399 {
400     return m_selectedKeyframe;
401 }
402
403 double AbstractClipItem::selectedKeyFrameValue() const
404 {
405     return m_keyframes.value(m_selectedKeyframe);
406 }
407
408 void AbstractClipItem::updateKeyFramePos(const GenTime &pos, const double value)
409 {
410     if (!m_keyframes.contains(m_editedKeyframe))
411         return;
412     int newpos = (int) pos.frames(m_fps);
413     int min = (int) cropStart().frames(m_fps) - 1;
414     int max = (int)(cropStart() + cropDuration()).frames(m_fps);
415     QMap<int, int>::const_iterator i = m_keyframes.constBegin();
416     while (i.key() < m_editedKeyframe) {
417         min = qMax(i.key(), min);
418         ++i;
419     }
420     i = m_keyframes.constEnd() - 1;
421     while (i.key() > m_editedKeyframe) {
422         max = qMin(i.key(), max);
423         --i;
424     }
425     if (newpos <= min)
426         newpos = min + 1;
427     if (newpos >= max)
428         newpos = max - 1;
429
430     double newval = qMax(value, 0.0);
431     newval = qMin(newval, 100.0);
432     newval = newval / m_keyframeFactor + m_keyframeOffset;
433     if (m_editedKeyframe != newpos)
434         m_keyframes.remove(m_editedKeyframe);
435     m_keyframes[newpos] = (int) newval;
436     m_editedKeyframe = newpos;
437     update();
438 }
439
440 double AbstractClipItem::keyFrameFactor() const
441 {
442     return m_keyframeFactor;
443 }
444
445 int AbstractClipItem::keyFrameNumber() const
446 {
447     return m_keyframes.count();
448 }
449
450 int AbstractClipItem::addKeyFrame(const GenTime &pos, const double value)
451 {
452     QRectF br = sceneBoundingRect();
453     double maxh = 100.0 / br.height() / m_keyframeFactor;
454     int newval = (br.bottom() - value) * maxh + m_keyframeOffset;
455     //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
456     int newpos = (int) pos.frames(m_fps) ;
457     m_keyframes[newpos] = newval;
458     m_selectedKeyframe = newpos;
459     update();
460     return newval;
461 }
462
463 bool AbstractClipItem::hasKeyFrames() const
464 {
465     return !m_keyframes.isEmpty();
466 }
467
468 /*QRect AbstractClipItem::visibleRect() {
469     QRect rectInView;
470     if (scene()->views().size() > 0) {
471         rectInView = scene()->views()[0]->viewport()->rect();
472         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
473         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
474         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
475     }
476     return rectInView;
477 }*/
478
479 CustomTrackScene* AbstractClipItem::projectScene()
480 {
481     if (scene())
482         return static_cast <CustomTrackScene*>(scene());
483     return NULL;
484 }
485
486 void AbstractClipItem::setItemLocked(bool locked)
487 {
488     if (locked)
489         setSelected(false);
490
491     setFlag(QGraphicsItem::ItemIsMovable, !locked);
492     setFlag(QGraphicsItem::ItemIsSelectable, !locked);
493 }
494
495 bool AbstractClipItem::isItemLocked() const
496 {
497     return !(flags() & (QGraphicsItem::ItemIsSelectable));
498 }
499
500 // virtual
501 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
502 {
503     if (event->modifiers() & Qt::ShiftModifier) {
504         // User want to do a rectangle selection, so ignore the event to pass it to the view
505         event->ignore();
506     } else {
507         QGraphicsItem::mousePressEvent(event);
508     }
509 }
510
511 int AbstractClipItem::itemHeight()
512 {
513     return 0;
514 }
515
516 int AbstractClipItem::itemOffset()
517 {
518     return 0;
519 }
520
521 void AbstractClipItem::setMainSelectedClip(bool selected)
522 {
523     if (selected == m_isMainSelectedClip) return;
524     m_isMainSelectedClip = selected;
525     update();
526 }
527
528 bool AbstractClipItem::isMainSelectedClip()
529 {
530     return m_isMainSelectedClip;
531 }
532