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