]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Fix keyframes in timeline cannot be dragged to beginning of clip. Also fixes painting...
[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_fps(fps)
40 #if QT_VERSION >= 0x040600
41         , m_closeAnimation(NULL)
42 #endif
43 {
44     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
45 #if QT_VERSION >= 0x040600
46     setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
47     setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
48 #endif
49 }
50
51 AbstractClipItem::~AbstractClipItem()
52 {
53 #if QT_VERSION >= 0x040600
54     if (m_closeAnimation) delete m_closeAnimation;
55 #endif
56 }
57
58 void AbstractClipItem::closeAnimation()
59 {
60 #if QT_VERSION >= 0x040600
61     if (m_closeAnimation) return;
62     setEnabled(false);
63     m_closeAnimation = new QPropertyAnimation(this, "rect");
64     connect(m_closeAnimation, SIGNAL(finished()), this, SLOT(deleteLater()));
65     m_closeAnimation->setDuration(200);
66     QRectF r = rect();
67     QRectF r2 = r;
68     r2.setLeft(r.left() + r.width() / 2);
69     r2.setTop(r.top() + r.height() / 2);
70     r2.setWidth(1);
71     r2.setHeight(1);
72     m_closeAnimation->setStartValue(r);
73     m_closeAnimation->setEndValue(r2);
74     m_closeAnimation->setEasingCurve(QEasingCurve::InQuad);
75     m_closeAnimation->start();
76 #endif
77 }
78
79 ItemInfo AbstractClipItem::info() const
80 {
81     ItemInfo info = m_info;
82     info.cropStart = cropStart();
83     info.endPos = endPos();
84     return info;
85 }
86
87 int AbstractClipItem::defaultZValue() const
88 {
89     return 2;
90 }
91
92 GenTime AbstractClipItem::endPos() const
93 {
94     return m_info.startPos + m_info.cropDuration;
95 }
96
97 int AbstractClipItem::track() const
98 {
99     return m_info.track;
100 }
101
102 GenTime AbstractClipItem::cropStart() const
103 {
104     return m_info.cropStart;
105 }
106
107 GenTime AbstractClipItem::cropDuration() const
108 {
109     return m_info.cropDuration;
110 }
111
112 void AbstractClipItem::setCropStart(GenTime pos)
113 {
114     m_info.cropStart = pos;
115 }
116
117 void AbstractClipItem::updateItem()
118 {
119     m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
120     m_info.startPos = GenTime((int) scenePos().x(), m_fps);
121 }
122
123 void AbstractClipItem::updateRectGeometry()
124 {
125     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
126 }
127
128 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit)
129 {
130     GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
131     if (durationDiff == GenTime()) return;
132     //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
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         for (int i = 0; i < collisionList.size(); ++i) {
207             if (!collisionList.at(i)->isEnabled()) continue;
208             QGraphicsItem *item = collisionList.at(i);
209             if (item->type() == type() && item->pos().x() > pos().x()) {
210                 kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
211                 kDebug() << "/////////  CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
212                 kDebug() << "/////////  COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
213                 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
214                 m_info.cropDuration = diff;
215                 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
216                 break;
217             }
218         }
219     }
220 }
221
222 GenTime AbstractClipItem::startPos() const
223 {
224     return m_info.startPos;
225 }
226
227 void AbstractClipItem::setTrack(int track)
228 {
229     m_info.track = track;
230 }
231
232 double AbstractClipItem::fps() const
233 {
234     return m_fps;
235 }
236
237 void AbstractClipItem::updateFps(double fps)
238 {
239     m_fps = fps;
240     setPos((qreal) startPos().frames(m_fps), pos().y());
241     updateRectGeometry();
242 }
243
244 GenTime AbstractClipItem::maxDuration() const
245 {
246     return m_maxDuration;
247 }
248
249 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
250 {
251     if (m_keyframes.count() < 1)
252         return;
253     QRectF br = rect();
254     double maxw = br.width() / cropDuration().frames(m_fps);
255     double maxh = br.height() / 100.0 * m_keyframeFactor;
256     double x1;
257     double y1;
258     double x2;
259     double y2;
260
261     // draw line showing default value
262     bool active = isSelected() || (parentItem() && parentItem()->isSelected());
263     if (active) {
264         x1 = br.x();
265         x2 = br.right();
266         y1 = br.bottom() - m_keyframeDefault * maxh;
267         QLineF l(x1, y1, x2, y1);
268         QLineF l2 = painter->matrix().map(l);
269         painter->setPen(QColor(168, 168, 168, 180));
270         painter->drawLine(l2);
271         painter->setPen(QColor(108, 108, 108, 180));
272         painter->drawLine(l2.translated(0, 1));
273         painter->setPen(QColor(Qt::white));
274     }
275
276     // draw keyframes
277     QMap<int, int>::const_iterator i = m_keyframes.constBegin();
278     QColor color(Qt::blue);
279     x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
280     y1 = br.bottom() - i.value() * maxh;
281     QLineF l2;
282     while (i != m_keyframes.constEnd()) {
283         if (i.key() == m_editedKeyframe)
284             color = QColor(Qt::red);
285         else
286             color = QColor(Qt::blue);
287         ++i;
288         if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
289             break;
290
291         if (m_keyframes.count() == 1) {
292             x2 = br.right();
293             y2 = y1;
294         } else {
295             x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
296             y2 = br.bottom() - i.value() * maxh;
297         }
298         QLineF l(x1, y1, x2, y2);
299         l2 = painter->matrix().map(l);
300         painter->drawLine(l2);
301         if (active) {
302             const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
303             painter->fillRect(frame, color);
304         }
305         x1 = x2;
306         y1 = y2;
307     }
308     if (active) {
309         const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
310         painter->fillRect(frame, color);
311     }
312 }
313
314 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
315 {
316     const QRectF br = sceneBoundingRect();
317     double maxw = br.width() / cropDuration().frames(m_fps);
318     double maxh = br.height() / 100.0 * m_keyframeFactor;
319     if (m_keyframes.count() > 0) {
320         QMap<int, int>::const_iterator i = m_keyframes.constBegin();
321         double x1;
322         double y1;
323         while (i != m_keyframes.constEnd()) {
324             x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
325             y1 = br.bottom() - i.value() * maxh;
326             if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
327                 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "]");
328                 return i.key();
329             } else if (x1 > pos.x()) {
330                 break;
331             }
332             ++i;
333         }
334     }
335     setToolTip(QString());
336     return -1;
337 }
338
339 void AbstractClipItem::updateSelectedKeyFrame()
340 {
341     if (m_editedKeyframe == -1)
342         return;
343     QRectF br = sceneBoundingRect();
344     double maxw = br.width() / cropDuration().frames(m_fps);
345     double maxh = br.height() / 100.0 * m_keyframeFactor;
346     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes.value(m_selectedKeyframe) * maxh - 3, 12, 12);
347     m_selectedKeyframe = m_editedKeyframe;
348     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes.value(m_selectedKeyframe) * maxh - 3, 12, 12);
349 }
350
351 int AbstractClipItem::editedKeyFramePos() const
352 {
353     return m_editedKeyframe;
354 }
355
356 double AbstractClipItem::editedKeyFrameValue() const
357 {
358     return m_keyframes.value(m_editedKeyframe);
359 }
360
361 int AbstractClipItem::selectedKeyFramePos() const
362 {
363     return m_selectedKeyframe;
364 }
365
366 double AbstractClipItem::selectedKeyFrameValue() const
367 {
368     return m_keyframes.value(m_selectedKeyframe);
369 }
370
371 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
372 {
373     if (!m_keyframes.contains(m_editedKeyframe))
374         return;
375     int newpos = (int) pos.frames(m_fps);
376     int min = (int) cropStart().frames(m_fps) - 1;
377     int max = (int)(cropStart() + cropDuration()).frames(m_fps);
378     QMap<int, int>::const_iterator i = m_keyframes.constBegin();
379     while (i.key() < m_editedKeyframe) {
380         min = qMax(i.key(), min);
381         ++i;
382     }
383     i = m_keyframes.constEnd() - 1;
384     while (i.key() > m_editedKeyframe) {
385         max = qMin(i.key(), max);
386         --i;
387     }
388     if (newpos <= min)
389         newpos = min + 1;
390     if (newpos >= max)
391         newpos = max - 1;
392
393     double newval = qMax(value, 0.0);
394     newval = qMin(newval, 100.0);
395     newval = newval / m_keyframeFactor;
396     if (m_editedKeyframe != newpos)
397         m_keyframes.remove(m_editedKeyframe);
398     m_keyframes[newpos] = (int) newval;
399     m_editedKeyframe = newpos;
400     update();
401 }
402
403 double AbstractClipItem::keyFrameFactor() const
404 {
405     return m_keyframeFactor;
406 }
407
408 int AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
409 {
410     QRectF br = sceneBoundingRect();
411     double maxh = 100.0 / br.height() / m_keyframeFactor;
412     int newval = (br.bottom() - value) * maxh;
413     //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
414     int newpos = (int) pos.frames(m_fps) ;
415     m_keyframes[newpos] = newval;
416     m_selectedKeyframe = newpos;
417     update();
418     return newval;
419 }
420
421 bool AbstractClipItem::hasKeyFrames() const
422 {
423     return !m_keyframes.isEmpty();
424 }
425
426 /*QRect AbstractClipItem::visibleRect() {
427     QRect rectInView;
428     if (scene()->views().size() > 0) {
429         rectInView = scene()->views()[0]->viewport()->rect();
430         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
431         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
432         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
433     }
434     return rectInView;
435 }*/
436
437 CustomTrackScene* AbstractClipItem::projectScene()
438 {
439     if (scene())
440         return static_cast <CustomTrackScene*>(scene());
441     return NULL;
442 }
443
444 void AbstractClipItem::setItemLocked(bool locked)
445 {
446     if (locked)
447         setSelected(false);
448
449     setFlag(QGraphicsItem::ItemIsMovable, !locked);
450     setFlag(QGraphicsItem::ItemIsSelectable, !locked);
451 }
452
453 bool AbstractClipItem::isItemLocked() const
454 {
455     return !(flags() & (QGraphicsItem::ItemIsSelectable));
456 }
457
458 // virtual
459 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
460 {
461     if (event->modifiers() & Qt::ShiftModifier) {
462         // User want to do a rectangle selection, so ignore the event to pass it to the view
463         event->ignore();
464     } else {
465         QGraphicsItem::mousePressEvent(event);
466     }
467 }
468