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