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