]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Introduce video thumbnail for each frame when zooming at max level (causes slowdown...
[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) {
135         if (hasSizeLimit && cropStart() + durationDiff < GenTime())
136             durationDiff = GenTime() - cropStart();
137     } else if (durationDiff >= cropDuration()) {
138         return;
139         if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
140         else return;
141     }
142     //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
143     m_info.startPos += durationDiff;
144
145     if (type() == AVWIDGET) {
146         m_info.cropStart += durationDiff;
147     }
148
149     m_info.cropDuration = m_info.cropDuration - durationDiff;
150     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
151     moveBy(durationDiff.frames(m_fps), 0);
152
153     if (m_info.startPos != GenTime(posx, m_fps)) {
154         //kDebug()<<"//////  WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
155         GenTime diff = m_info.startPos - GenTime((int) posx, m_fps);
156
157         if (type() == AVWIDGET) {
158             m_info.cropStart += diff;
159         }
160         m_info.cropDuration = m_info.cropDuration - diff;
161         setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
162         //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
163     }
164
165
166     //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
167     //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
168
169     /*    if (durationDiff < GenTime()) {
170             QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
171             for (int i = 0; i < collisionList.size(); ++i) {
172                 QGraphicsItem *item = collisionList.at(i);
173                 if (item->type() == type() && item->pos().x() < pos().x()) {
174                     kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
175                     GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
176                     setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
177                     setPos((m_startPos + diff).frames(m_fps), pos().y());
178                     m_startPos += diff;
179                     if (type() == AVWIDGET) m_cropStart += diff;
180                     m_cropDuration = m_cropDuration - diff;
181                     break;
182                 }
183             }
184         }*/
185 }
186
187 void AbstractClipItem::resizeEnd(int posx)
188 {
189     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
190     if (durationDiff == GenTime()) return;
191     if (cropDuration() + durationDiff <= GenTime()) {
192         durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
193     }
194
195     m_info.cropDuration += durationDiff;
196
197     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
198     if (durationDiff > GenTime()) {
199         QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
200         for (int i = 0; i < collisionList.size(); ++i) {
201             if (!collisionList.at(i)->isEnabled()) continue;
202             QGraphicsItem *item = collisionList.at(i);
203             if (item->type() == type() && item->pos().x() > pos().x()) {
204                 kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
205                 kDebug() << "/////////  CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
206                 kDebug() << "/////////  COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
207                 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
208                 m_info.cropDuration = diff;
209                 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
210                 break;
211             }
212         }
213     }
214 }
215
216 GenTime AbstractClipItem::startPos() const
217 {
218     return m_info.startPos;
219 }
220
221 void AbstractClipItem::setTrack(int track)
222 {
223     m_info.track = track;
224 }
225
226 double AbstractClipItem::fps() const
227 {
228     return m_fps;
229 }
230
231 void AbstractClipItem::updateFps(double fps)
232 {
233     m_fps = fps;
234     setPos((qreal) startPos().frames(m_fps), pos().y());
235     updateRectGeometry();
236 }
237
238 GenTime AbstractClipItem::maxDuration() const
239 {
240     return m_maxDuration;
241 }
242
243 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
244 {
245     if (m_keyframes.count() < 1) return;
246     QRectF br = rect();
247     double maxw = br.width() / cropDuration().frames(m_fps);
248     double maxh = br.height() / 100.0 * m_keyframeFactor;
249     double x1;
250     double y1;
251     double x2;
252     double y2;
253
254     // draw line showing default value
255     bool active = isSelected() || (parentItem() && parentItem()->isSelected());
256     if (active) {
257         x1 = br.x();
258         x2 = br.right();
259         y1 = br.bottom() - m_keyframeDefault * maxh;
260         QLineF l(x1, y1, x2, y1);
261         QLineF l2 = painter->matrix().map(l);
262         painter->setPen(QColor(168, 168, 168, 180));
263         painter->drawLine(l2);
264         painter->setPen(QColor(108, 108, 108, 180));
265         painter->drawLine(l2.translated(0, 1));
266         painter->setPen(QColor(Qt::white));
267     }
268
269     // draw keyframes
270     QMap<int, int>::const_iterator i = m_keyframes.constBegin();
271     QColor color(Qt::blue);
272     x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
273     y1 = br.bottom() - i.value() * maxh;
274     QLineF l2;
275     while (i != m_keyframes.constEnd()) {
276         if (i.key() == m_editedKeyframe) color = QColor(Qt::red);
277         else color = QColor(Qt::blue);
278         ++i;
279         if (i == m_keyframes.constEnd() && m_keyframes.count() != 1) {
280             break;
281         }
282         if (m_keyframes.count() == 1) {
283             x2 = br.right();
284             y2 = y1;
285         } else {
286             x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
287             y2 = br.bottom() - i.value() * maxh;
288         }
289         QLineF l(x1, y1, x2, y2);
290         l2 = painter->matrix().map(l);
291         painter->drawLine(l2);
292         if (active) {
293             const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
294             painter->fillRect(frame, color);
295         }
296         x1 = x2;
297         y1 = y2;
298     }
299     if (active) {
300         const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
301         painter->fillRect(frame, color);
302     }
303 }
304
305 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
306 {
307     const QRectF br = sceneBoundingRect();
308     double maxw = br.width() / cropDuration().frames(m_fps);
309     double maxh = br.height() / 100.0 * m_keyframeFactor;
310     if (m_keyframes.count() > 0) {
311         QMap<int, int>::const_iterator i = m_keyframes.constBegin();
312         double x1;
313         double y1;
314         while (i != m_keyframes.constEnd()) {
315             x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
316             y1 = br.bottom() - i.value() * maxh;
317             if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
318                 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
319                 return i.key();
320             } else if (x1 > pos.x()) break;
321             ++i;
322         }
323     }
324     setToolTip(QString());
325     return -1;
326 }
327
328 void AbstractClipItem::updateSelectedKeyFrame()
329 {
330     if (m_editedKeyframe == -1) return;
331     QRectF br = sceneBoundingRect();
332     double maxw = br.width() / cropDuration().frames(m_fps);
333     double maxh = br.height() / 100.0 * m_keyframeFactor;
334     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
335     m_selectedKeyframe = m_editedKeyframe;
336     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
337 }
338
339 int AbstractClipItem::editedKeyFramePos() const
340 {
341     return m_editedKeyframe;
342 }
343
344 double AbstractClipItem::editedKeyFrameValue() const
345 {
346     return m_keyframes.value(m_editedKeyframe);
347 }
348
349 int AbstractClipItem::selectedKeyFramePos() const
350 {
351     return m_selectedKeyframe;
352 }
353
354 double AbstractClipItem::selectedKeyFrameValue() const
355 {
356     return m_keyframes.value(m_selectedKeyframe);
357 }
358
359 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
360 {
361     if (!m_keyframes.contains(m_editedKeyframe)) return;
362     int newpos = (int) pos.frames(m_fps);
363     int start = cropStart().frames(m_fps);
364     int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
365     newpos = qMax(newpos, start);
366     newpos = qMin(newpos, end);
367
368     double newval = qMax(value, 0.0);
369     newval = qMin(newval, 100.0);
370     newval = newval / m_keyframeFactor;
371     if (m_editedKeyframe != newpos) m_keyframes.remove(m_editedKeyframe);
372     m_keyframes[newpos] = (int) newval;
373     m_editedKeyframe = newpos;
374     update();
375 }
376
377 double AbstractClipItem::keyFrameFactor() const
378 {
379     return m_keyframeFactor;
380 }
381
382 int AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
383 {
384     QRectF br = sceneBoundingRect();
385     double maxh = 100.0 / br.height() / m_keyframeFactor;
386     int newval = (br.bottom() - value) * maxh;
387     //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
388     int newpos = (int) pos.frames(m_fps) ;
389     m_keyframes[newpos] = newval;
390     m_selectedKeyframe = newpos;
391     update();
392     return newval;
393 }
394
395 bool AbstractClipItem::hasKeyFrames() const
396 {
397     return !m_keyframes.isEmpty();
398 }
399
400 /*QRect AbstractClipItem::visibleRect() {
401     QRect rectInView;
402     if (scene()->views().size() > 0) {
403         rectInView = scene()->views()[0]->viewport()->rect();
404         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
405         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
406         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
407     }
408     return rectInView;
409 }*/
410
411 CustomTrackScene* AbstractClipItem::projectScene()
412 {
413     if (scene()) return static_cast <CustomTrackScene*>(scene());
414     return NULL;
415 }
416
417 void AbstractClipItem::setItemLocked(bool locked)
418 {
419     if (locked) {
420         setSelected(false);
421         setFlag(QGraphicsItem::ItemIsMovable, false);
422         setFlag(QGraphicsItem::ItemIsSelectable, false);
423     } else {
424         setFlag(QGraphicsItem::ItemIsMovable, true);
425         setFlag(QGraphicsItem::ItemIsSelectable, true);
426     }
427 }
428
429 bool AbstractClipItem::isItemLocked() const
430 {
431     return !(flags() & (QGraphicsItem::ItemIsSelectable));
432 }
433
434 // virtual
435 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
436 {
437     if (event->modifiers() & Qt::ShiftModifier) {
438         // User want to do a rectangle selection, so ignore the event to pass it to the view
439         event->ignore();
440     } else QGraphicsItem::mousePressEvent(event);
441 }
442