]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Merge branch 'master' into bugfix/jogshuttle
[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 #include <KGlobalSettings>
28
29 #include <QPainter>
30 #include <QToolTip>
31 #include <QGraphicsSceneMouseEvent>
32 #include <QParallelAnimationGroup>
33
34 AbstractClipItem::AbstractClipItem(const ItemInfo &info, const QRectF& rect, double fps) :
35         QObject(),
36         QGraphicsRectItem(rect),
37         m_info(info),
38         m_editedKeyframe(-1),
39         m_selectedKeyframe(0),
40         m_keyframeFactor(1),
41         m_keyframeOffset(0),
42         m_fps(fps),
43         m_isMainSelectedClip(false)
44 {
45     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
46 #if QT_VERSION >= 0x040600
47     setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
48     setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
49 #endif
50 }
51
52 AbstractClipItem::~AbstractClipItem()
53 {
54 }
55
56 void AbstractClipItem::closeAnimation()
57 {
58 #if QT_VERSION >= 0x040600
59     if (!isEnabled()) return;
60     setEnabled(false);
61     if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
62         // animation disabled
63         deleteLater();
64         return;
65     }
66     QPropertyAnimation *closeAnimation = new QPropertyAnimation(this, "rect");
67     QPropertyAnimation *closeAnimation2 = new QPropertyAnimation(this, "opacity");
68     closeAnimation->setDuration(200);
69     closeAnimation2->setDuration(200);
70     QRectF r = rect();
71     QRectF r2 = r;
72     r2.setLeft(r.left() + r.width() / 2);
73     r2.setTop(r.top() + r.height() / 2);
74     r2.setWidth(1);
75     r2.setHeight(1);
76     closeAnimation->setStartValue(r);
77     closeAnimation->setEndValue(r2);
78     closeAnimation->setEasingCurve(QEasingCurve::InQuad);
79     closeAnimation2->setStartValue(1.0);
80     closeAnimation2->setEndValue(0.0);
81     QParallelAnimationGroup *group = new QParallelAnimationGroup;
82     connect(group, SIGNAL(finished()), this, SLOT(deleteLater()));
83     group->addAnimation(closeAnimation);
84     group->addAnimation(closeAnimation2);
85     group->start(QAbstractAnimation::DeleteWhenStopped);
86 #endif
87 }
88
89 ItemInfo AbstractClipItem::info() const
90 {
91     ItemInfo info = m_info;
92     info.cropStart = cropStart();
93     info.endPos = endPos();
94     return info;
95 }
96
97 int AbstractClipItem::defaultZValue() const
98 {
99     return 2;
100 }
101
102 GenTime AbstractClipItem::endPos() const
103 {
104     return m_info.startPos + m_info.cropDuration;
105 }
106
107 int AbstractClipItem::track() const
108 {
109     return m_info.track;
110 }
111
112 GenTime AbstractClipItem::cropStart() const
113 {
114     return m_info.cropStart;
115 }
116
117 GenTime AbstractClipItem::cropDuration() const
118 {
119     return m_info.cropDuration;
120 }
121
122 void AbstractClipItem::setCropStart(GenTime pos)
123 {
124     m_info.cropStart = pos;
125 }
126
127 void AbstractClipItem::updateItem()
128 {
129     m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
130     m_info.startPos = GenTime((int) scenePos().x(), m_fps);
131 }
132
133 void AbstractClipItem::updateRectGeometry()
134 {
135     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
136 }
137
138 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit, bool /*emitChange*/)
139 {
140     GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
141     if (durationDiff == GenTime()) return;
142
143     if (type() == AVWIDGET && hasSizeLimit && (cropStart() + durationDiff < GenTime())) {
144         durationDiff = GenTime() - cropStart();
145     } else if (durationDiff >= cropDuration()) {
146         return;
147         /*if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
148         else return;*/
149     }
150     m_info.startPos += durationDiff;
151
152     // set to true if crop from start is negative (possible for color clips, images as they have no size limit)
153     bool negCropStart = false;
154     if (type() == AVWIDGET) {
155         m_info.cropStart += durationDiff;
156         if (m_info.cropStart < GenTime())
157             negCropStart = true;
158     }
159
160     m_info.cropDuration -= durationDiff;
161     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
162     moveBy(durationDiff.frames(m_fps), 0);
163
164     if (m_info.startPos != GenTime(posx, m_fps)) {
165         //kDebug() << "//////  WARNING, DIFF IN XPOS: " << pos().x() << " == " << m_info.startPos.frames(m_fps);
166         GenTime diff = m_info.startPos - GenTime(posx, m_fps);
167
168         if (type() == AVWIDGET)
169             m_info.cropStart += diff;
170
171         m_info.cropDuration -= diff;
172         setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
173         //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
174     }
175
176     // set crop from start to 0 (isn't relevant as this only happens for color clips, images)
177     if (negCropStart)
178         m_info.cropStart = GenTime();
179
180     //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
181     //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
182
183     /*    if (durationDiff < GenTime()) {
184             QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
185             for (int i = 0; i < collisionList.size(); ++i) {
186                 QGraphicsItem *item = collisionList.at(i);
187                 if (item->type() == type() && item->pos().x() < pos().x()) {
188                     kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
189                     GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
190                     setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
191                     setPos((m_startPos + diff).frames(m_fps), pos().y());
192                     m_startPos += diff;
193                     if (type() == AVWIDGET) m_cropStart += diff;
194                     m_cropDuration = m_cropDuration - diff;
195                     break;
196                 }
197             }
198         }*/
199 }
200
201 void AbstractClipItem::resizeEnd(int posx, bool /*emitChange*/)
202 {
203     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
204     if (durationDiff == GenTime()) return;
205     if (cropDuration() + durationDiff <= GenTime()) {
206         durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
207     }
208
209     m_info.cropDuration += durationDiff;
210     m_info.endPos += durationDiff;
211
212     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
213     if (durationDiff > GenTime()) {
214         QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
215         bool fixItem = false;
216         for (int i = 0; i < collisionList.size(); ++i) {
217             if (!collisionList.at(i)->isEnabled()) continue;
218             QGraphicsItem *item = collisionList.at(i);
219             if (item->type() == type() && item->pos().x() > pos().x()) {
220                 //kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
221                 //kDebug() << "/////////  CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
222                 //kDebug() << "/////////  COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
223                 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
224                 if (fixItem == false || diff < m_info.cropDuration) {
225                     fixItem = true;
226                     m_info.cropDuration = diff;
227                 }
228             }
229         }
230         if (fixItem) setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
231     }
232 }
233
234 GenTime AbstractClipItem::startPos() const
235 {
236     return m_info.startPos;
237 }
238
239 void AbstractClipItem::setTrack(int track)
240 {
241     m_info.track = track;
242 }
243
244 double AbstractClipItem::fps() const
245 {
246     return m_fps;
247 }
248
249 void AbstractClipItem::updateFps(double fps)
250 {
251     m_fps = fps;
252     setPos((qreal) startPos().frames(m_fps), pos().y());
253     updateRectGeometry();
254 }
255
256 GenTime AbstractClipItem::maxDuration() const
257 {
258     return m_maxDuration;
259 }
260
261 void AbstractClipItem::drawKeyFrames(QPainter *painter, bool limitedKeyFrames)
262 {
263     if (m_keyframes.count() < 1)
264         return;
265     QRectF br = rect();
266     double maxw = br.width() / cropDuration().frames(m_fps);
267     double maxh = br.height() / 100.0 * m_keyframeFactor;
268     double start = cropStart().frames(m_fps);
269     double x1, y1, x2, y2;
270     bool antialiasing = painter->renderHints() & QPainter::Antialiasing;
271
272     // draw line showing default value
273     bool active = isSelected() || (parentItem() && parentItem()->isSelected());
274     if (active) {
275         x1 = br.x();
276         x2 = br.right();
277         if (limitedKeyFrames) {
278             QMap<int, int>::const_iterator end = m_keyframes.constEnd();
279             end--;
280             x2 = x1 + maxw * (end.key() - start);
281             x1 += maxw * (m_keyframes.constBegin().key() - start);
282         }
283         y1 = br.bottom() - (m_keyframeDefault - m_keyframeOffset) * maxh;
284         QLineF l(x1, y1, x2, y1);
285         QLineF l2 = painter->worldTransform().map(l);
286         painter->setPen(QColor(168, 168, 168, 180));
287         painter->drawLine(l2);
288         painter->setPen(QColor(108, 108, 108, 180));
289         painter->drawLine(l2.translated(0, 1));
290         painter->setPen(QColor(Qt::white));
291         painter->setRenderHint(QPainter::Antialiasing);
292     }
293
294     // draw keyframes
295     QMap<int, int>::const_iterator i = m_keyframes.constBegin();
296     QColor color(Qt::blue);
297     QLineF l2;
298     x1 = br.x() + maxw * (i.key() - start);
299     y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
300
301
302
303     // make sure line begins with clip beginning
304     if (!limitedKeyFrames && i.key() != start) {
305         QLineF l(br.x(), y1, x1, y1);
306         l2 = painter->worldTransform().map(l);
307         painter->drawLine(l2);
308     }
309     while (i != m_keyframes.constEnd()) {
310         if (i.key() == m_editedKeyframe)
311             color = QColor(Qt::red);
312         else
313             color = QColor(Qt::blue);
314         ++i;
315         if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
316             break;
317
318         if (m_keyframes.count() == 1) {
319             x2 = br.right();
320             y2 = y1;
321         } else {
322             x2 = br.x() + maxw * (i.key() - start);
323             y2 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
324         }
325         QLineF l(x1, y1, x2, y2);
326         l2 = painter->worldTransform().map(l);
327         painter->drawLine(l2);
328         if (active) {
329             const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
330             painter->fillRect(frame, color);
331         }
332         x1 = x2;
333         y1 = y2;
334     }
335
336     // make sure line ends at clip end
337     if (!limitedKeyFrames && x1 != br.right()) {
338         QLineF l(x1, y1, br.right(), y1);
339         painter->drawLine(painter->worldTransform().map(l));
340     }
341
342     if (active && m_keyframes.count() > 1) {
343         const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
344         painter->fillRect(frame, color);
345     }
346
347     painter->setRenderHint(QPainter::Antialiasing, antialiasing);
348 }
349
350 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
351 {
352     const QRectF br = sceneBoundingRect();
353     double maxw = br.width() / cropDuration().frames(m_fps);
354     double maxh = br.height() / 100.0 * m_keyframeFactor;
355     if (m_keyframes.count() > 0) {
356         QMap<int, int>::const_iterator i = m_keyframes.constBegin();
357         double x1;
358         double y1;
359         while (i != m_keyframes.constEnd()) {
360             x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
361             y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
362             if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
363                 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + ']');
364                 return i.key();
365             } else if (x1 > pos.x()) {
366                 break;
367             }
368             ++i;
369         }
370     }
371     setToolTip(QString());
372     return -1;
373 }
374
375 void AbstractClipItem::updateSelectedKeyFrame()
376 {
377     if (m_editedKeyframe == -1)
378         return;
379     QRectF br = sceneBoundingRect();
380     double maxw = br.width() / cropDuration().frames(m_fps);
381     double maxh = br.height() / 100.0 * m_keyframeFactor;
382     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
383     m_selectedKeyframe = m_editedKeyframe;
384     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
385 }
386
387 int AbstractClipItem::editedKeyFramePos() const
388 {
389     return m_editedKeyframe;
390 }
391
392 double AbstractClipItem::editedKeyFrameValue() const
393 {
394     return m_keyframes.value(m_editedKeyframe);
395 }
396
397 int AbstractClipItem::selectedKeyFramePos() const
398 {
399     return m_selectedKeyframe;
400 }
401
402 double AbstractClipItem::selectedKeyFrameValue() const
403 {
404     return m_keyframes.value(m_selectedKeyframe);
405 }
406
407 void AbstractClipItem::updateKeyFramePos(const GenTime &pos, const double value)
408 {
409     if (!m_keyframes.contains(m_editedKeyframe))
410         return;
411     int newpos = (int) pos.frames(m_fps);
412     int min = (int) cropStart().frames(m_fps) - 1;
413     int max = (int)(cropStart() + cropDuration()).frames(m_fps);
414     QMap<int, int>::const_iterator i = m_keyframes.constBegin();
415     while (i.key() < m_editedKeyframe) {
416         min = qMax(i.key(), min);
417         ++i;
418     }
419     i = m_keyframes.constEnd() - 1;
420     while (i.key() > m_editedKeyframe) {
421         max = qMin(i.key(), max);
422         --i;
423     }
424     if (newpos <= min)
425         newpos = min + 1;
426     if (newpos >= max)
427         newpos = max - 1;
428
429     double newval = qMax(value, 0.0);
430     newval = qMin(newval, 100.0);
431     newval = newval / m_keyframeFactor + m_keyframeOffset;
432     if (m_editedKeyframe != newpos)
433         m_keyframes.remove(m_editedKeyframe);
434     m_keyframes[newpos] = (int) newval;
435     m_editedKeyframe = newpos;
436     update();
437 }
438
439 double AbstractClipItem::keyFrameFactor() const
440 {
441     return m_keyframeFactor;
442 }
443
444 int AbstractClipItem::keyFrameNumber() const
445 {
446     return m_keyframes.count();
447 }
448
449 int AbstractClipItem::addKeyFrame(const GenTime &pos, const double value)
450 {
451     QRectF br = sceneBoundingRect();
452     double maxh = 100.0 / br.height() / m_keyframeFactor;
453     int newval = (br.bottom() - value) * maxh + m_keyframeOffset;
454     //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
455     int newpos = (int) pos.frames(m_fps) ;
456     m_keyframes[newpos] = newval;
457     m_selectedKeyframe = newpos;
458     update();
459     return newval;
460 }
461
462 bool AbstractClipItem::hasKeyFrames() const
463 {
464     return !m_keyframes.isEmpty();
465 }
466
467 /*QRect AbstractClipItem::visibleRect() {
468     QRect rectInView;
469     if (scene()->views().size() > 0) {
470         rectInView = scene()->views()[0]->viewport()->rect();
471         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
472         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
473         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
474     }
475     return rectInView;
476 }*/
477
478 CustomTrackScene* AbstractClipItem::projectScene()
479 {
480     if (scene())
481         return static_cast <CustomTrackScene*>(scene());
482     return NULL;
483 }
484
485 void AbstractClipItem::setItemLocked(bool locked)
486 {
487     if (locked)
488         setSelected(false);
489
490     setFlag(QGraphicsItem::ItemIsMovable, !locked);
491     setFlag(QGraphicsItem::ItemIsSelectable, !locked);
492 }
493
494 bool AbstractClipItem::isItemLocked() const
495 {
496     return !(flags() & (QGraphicsItem::ItemIsSelectable));
497 }
498
499 // virtual
500 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
501 {
502     if (event->modifiers() & Qt::ShiftModifier) {
503         // User want to do a rectangle selection, so ignore the event to pass it to the view
504         event->ignore();
505     } else {
506         QGraphicsItem::mousePressEvent(event);
507     }
508 }
509
510 int AbstractClipItem::itemHeight()
511 {
512     return 0;
513 }
514
515 int AbstractClipItem::itemOffset()
516 {
517     return 0;
518 }
519
520 void AbstractClipItem::setMainSelectedClip(bool selected)
521 {
522     if (selected == m_isMainSelectedClip) return;
523     m_isMainSelectedClip = selected;
524     update();
525 }
526
527 bool AbstractClipItem::isMainSelectedClip()
528 {
529     return m_isMainSelectedClip;
530 }
531