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