]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Several fixes for keyframes, fix bug in clip resize start undo and cleanup
[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_track(0),
36         m_editedKeyframe(-1),
37         m_selectedKeyframe(0),
38         m_keyframeFactor(1),
39         m_fps(fps)
40 {
41     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
42     setTrack(info.track);
43     m_startPos = info.startPos;
44     m_cropDuration = info.endPos - info.startPos;
45 }
46
47 ItemInfo AbstractClipItem::info() const
48 {
49     ItemInfo itemInfo;
50     itemInfo.startPos = startPos();
51     itemInfo.endPos = endPos();
52     itemInfo.cropStart = m_cropStart;
53     itemInfo.track = track();
54     return itemInfo;
55 }
56
57 GenTime AbstractClipItem::endPos() const
58 {
59     return m_startPos + m_cropDuration;
60 }
61
62 int AbstractClipItem::track() const
63 {
64     return m_track;
65 }
66
67 GenTime AbstractClipItem::cropStart() const
68 {
69     return m_cropStart;
70 }
71
72 GenTime AbstractClipItem::cropDuration() const
73 {
74     return m_cropDuration;
75 }
76
77 void AbstractClipItem::setCropStart(GenTime pos)
78 {
79     m_cropStart = pos;
80 }
81
82 void AbstractClipItem::updateItem()
83 {
84     m_track = (int)(scenePos().y() / KdenliveSettings::trackheight());
85     m_startPos = GenTime((int) scenePos().x(), m_fps);
86 }
87
88 void AbstractClipItem::updateRectGeometry()
89 {
90     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
91 }
92
93 void AbstractClipItem::resizeStart(int posx, double speed)
94 {
95     GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
96     if (durationDiff == GenTime()) return;
97     //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
98
99     if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
100         durationDiff = GenTime() - cropStart();
101     } else if (durationDiff >= m_cropDuration) {
102         return;
103         if (m_cropDuration > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
104         else return;
105     }
106     //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
107     m_startPos += durationDiff;
108     if (type() == AVWIDGET) m_cropStart += durationDiff * speed;
109     m_cropDuration = m_cropDuration - durationDiff * speed;
110
111     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
112     moveBy(durationDiff.frames(m_fps), 0);
113     //setPos(m_startPos.frames(m_fps), pos().y());
114     if ((int) scenePos().x() != posx) {
115         //kDebug()<<"//////  WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
116         GenTime diff = GenTime((int) pos().x() - posx, m_fps);
117         if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
118         m_cropDuration = m_cropDuration - diff;
119         setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
120         //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
121     }
122
123
124     //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
125     //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
126
127     /*    if (durationDiff < GenTime()) {
128             QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
129             for (int i = 0; i < collisionList.size(); ++i) {
130                 QGraphicsItem *item = collisionList.at(i);
131                 if (item->type() == type() && item->pos().x() < pos().x()) {
132                     kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
133                     GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
134                     setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
135                     setPos((m_startPos + diff).frames(m_fps), pos().y());
136                     m_startPos += diff;
137                     if (type() == AVWIDGET) m_cropStart += diff;
138                     m_cropDuration = m_cropDuration - diff;
139                     break;
140                 }
141             }
142         }*/
143 }
144
145 void AbstractClipItem::resizeEnd(int posx, double speed)
146 {
147     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
148     if (durationDiff == GenTime()) return;
149     //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
150     if (cropDuration() + durationDiff <= GenTime()) {
151         durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
152     }
153     //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
154     m_cropDuration += durationDiff * speed;
155     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
156     if (durationDiff > GenTime()) {
157         QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
158         for (int i = 0; i < collisionList.size(); ++i) {
159             QGraphicsItem *item = collisionList.at(i);
160             if (item->type() == type() && item->pos().x() > pos().x()) {
161                 /*kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
162                 kDebug() << "/////////  CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
163                 kDebug() << "/////////  COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
164                 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
165                 m_cropDuration = diff * speed;
166                 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
167                 break;
168             }
169         }
170     }
171 }
172
173 GenTime AbstractClipItem::startPos() const
174 {
175     return m_startPos;
176 }
177
178 void AbstractClipItem::setTrack(int track)
179 {
180     m_track = track;
181 }
182
183 double AbstractClipItem::fps() const
184 {
185     return m_fps;
186 }
187
188 GenTime AbstractClipItem::maxDuration() const
189 {
190     return m_maxDuration;
191 }
192
193 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
194 {
195     if (m_keyframes.count() < 2) return;
196     QRectF br = rect();
197     double maxw = br.width() / cropDuration().frames(m_fps);
198     double maxh = br.height() / 100.0 * m_keyframeFactor;
199     double x1;
200     double y1;
201     double x2;
202     double y2;
203
204     // draw line showing default value
205     bool active = isSelected() || (parentItem() && parentItem()->isSelected());
206     if (active) {
207         x1 = br.x();
208         x2 = br.right();
209         y1 = br.bottom() - m_keyframeDefault * maxh;
210         QLineF l(x1, y1, x2, y1);
211         QLineF l2 = painter->matrix().map(l);
212         painter->setPen(QColor(168, 168, 168, 180));
213         painter->drawLine(l2);
214         painter->setPen(QColor(108, 108, 108, 180));
215         painter->drawLine(l2.translated(0, 1));
216         painter->setPen(QColor(Qt::white));
217     }
218
219     // draw keyframes
220     QMap<int, int>::const_iterator i = m_keyframes.constBegin();
221     QColor color(Qt::blue);
222     x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
223     y1 = br.bottom() - i.value() * maxh;
224     QLineF l2;
225     while (i != m_keyframes.constEnd()) {
226         if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
227         else color = QColor(Qt::blue);
228         ++i;
229         if (i == m_keyframes.constEnd()) break;
230         x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
231         y2 = br.bottom() - i.value() * maxh;
232         QLineF l(x1, y1, x2, y2);
233         l2 = painter->matrix().map(l);
234         painter->drawLine(l2);
235         if (active) {
236             const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
237             painter->fillRect(frame, color);
238         }
239         x1 = x2;
240         y1 = y2;
241     }
242     if (active) {
243         const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
244         painter->fillRect(frame, color);
245     }
246 }
247
248 int AbstractClipItem::mouseOverKeyFrames(QPointF pos)
249 {
250     QRectF br = sceneBoundingRect();
251     double maxw = br.width() / cropDuration().frames(m_fps);
252     double maxh = br.height() / 100.0 * m_keyframeFactor;
253     if (m_keyframes.count() > 1) {
254         QMap<int, int>::const_iterator i = m_keyframes.constBegin();
255         double x1;
256         double y1;
257         while (i != m_keyframes.constEnd()) {
258             x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
259             y1 = br.bottom() - i.value() * maxh;
260             if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
261                 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
262                 return i.key();
263             } else if (x1 > pos.x()) break;
264             ++i;
265         }
266     }
267     setToolTip(QString());
268     return -1;
269 }
270
271 void AbstractClipItem::updateSelectedKeyFrame()
272 {
273     if (m_editedKeyframe == -1) return;
274     QRectF br = sceneBoundingRect();
275     double maxw = br.width() / cropDuration().frames(m_fps);
276     double maxh = br.height() / 100.0 * m_keyframeFactor;
277     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
278     m_selectedKeyframe = m_editedKeyframe;
279     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
280 }
281
282 int AbstractClipItem::selectedKeyFramePos() const
283 {
284     return m_editedKeyframe;
285 }
286
287 double AbstractClipItem::selectedKeyFrameValue() const
288 {
289     return m_keyframes[m_editedKeyframe];
290 }
291
292 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
293 {
294     if (!m_keyframes.contains(m_selectedKeyframe)) return;
295     int newpos = (int) pos.frames(m_fps);
296     int start = cropStart().frames(m_fps);
297     int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
298     newpos = qMax(newpos, start);
299     newpos = qMin(newpos, end);
300     if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
301         // remove kexframe if it is dragged outside
302         m_keyframes.remove(m_selectedKeyframe);
303         m_selectedKeyframe = -1;
304         update();
305         return;
306     }
307     if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
308         // remove kexframe if it is dragged outside
309         m_keyframes.remove(m_selectedKeyframe);
310         m_selectedKeyframe = -1;
311         update();
312         return;
313     }
314     double newval = qMax(value, 0.0);
315     newval = qMin(newval, 100.0);
316     newval = newval / m_keyframeFactor;
317     if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
318     m_keyframes[newpos] = (int) newval;
319     m_selectedKeyframe = newpos;
320     update();
321 }
322
323 double AbstractClipItem::keyFrameFactor() const
324 {
325     return m_keyframeFactor;
326 }
327
328 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
329 {
330     QRectF br = sceneBoundingRect();
331     double maxh = 100.0 / br.height() / m_keyframeFactor;
332     int newval = (br.bottom() - value) * maxh;
333     //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
334     int newpos = (int) pos.frames(m_fps) ;
335     m_keyframes[newpos] = newval;
336     m_selectedKeyframe = newpos;
337     update();
338 }
339
340 bool AbstractClipItem::hasKeyFrames() const
341 {
342     return !m_keyframes.isEmpty();
343 }
344
345 /*QRect AbstractClipItem::visibleRect() {
346     QRect rectInView;
347     if (scene()->views().size() > 0) {
348         rectInView = scene()->views()[0]->viewport()->rect();
349         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
350         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
351         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
352     }
353     return rectInView;
354 }*/
355
356 CustomTrackScene* AbstractClipItem::projectScene()
357 {
358     if (scene()) return static_cast <CustomTrackScene*>(scene());
359     return NULL;
360 }
361
362 void AbstractClipItem::setItemLocked(bool locked)
363 {
364     if (locked) {
365         setSelected(false);
366         setFlag(QGraphicsItem::ItemIsMovable, false);
367         setFlag(QGraphicsItem::ItemIsSelectable, false);
368     } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
369 }
370
371 bool AbstractClipItem::isItemLocked() const
372 {
373     return !(flags() & (QGraphicsItem::ItemIsSelectable));
374 }
375
376 // virtual
377 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
378 {
379     if (event->modifiers() & Qt::ShiftModifier) {
380         // User want to do a rectangle selection, so ignore the event to pass it to the view
381         event->ignore();
382     } else QGraphicsItem::mousePressEvent(event);
383 }
384