]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
cppcheck warning--
[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         while (i != m_keyframes.constEnd()) {
361             double x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
362             double y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
363             if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
364                 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + ']');
365                 return i.key();
366             } else if (x1 > pos.x()) {
367                 break;
368             }
369             ++i;
370         }
371     }
372     setToolTip(QString());
373     return -1;
374 }
375
376 void AbstractClipItem::updateSelectedKeyFrame()
377 {
378     if (m_editedKeyframe == -1)
379         return;
380     QRectF br = sceneBoundingRect();
381     double maxw = br.width() / cropDuration().frames(m_fps);
382     double maxh = br.height() / 100.0 * m_keyframeFactor;
383     update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
384     m_selectedKeyframe = m_editedKeyframe;
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 }
387
388 int AbstractClipItem::editedKeyFramePos() const
389 {
390     return m_editedKeyframe;
391 }
392
393 double AbstractClipItem::editedKeyFrameValue() const
394 {
395     return m_keyframes.value(m_editedKeyframe);
396 }
397
398 int AbstractClipItem::selectedKeyFramePos() const
399 {
400     return m_selectedKeyframe;
401 }
402
403 double AbstractClipItem::selectedKeyFrameValue() const
404 {
405     return m_keyframes.value(m_selectedKeyframe);
406 }
407
408 void AbstractClipItem::updateKeyFramePos(const GenTime &pos, const double value)
409 {
410     if (!m_keyframes.contains(m_editedKeyframe))
411         return;
412     int newpos = (int) pos.frames(m_fps);
413     int min = (int) cropStart().frames(m_fps) - 1;
414     int max = (int)(cropStart() + cropDuration()).frames(m_fps);
415     QMap<int, int>::const_iterator i = m_keyframes.constBegin();
416     while (i.key() < m_editedKeyframe) {
417         min = qMax(i.key(), min);
418         ++i;
419     }
420     i = m_keyframes.constEnd() - 1;
421     while (i.key() > m_editedKeyframe) {
422         max = qMin(i.key(), max);
423         --i;
424     }
425     if (newpos <= min)
426         newpos = min + 1;
427     if (newpos >= max)
428         newpos = max - 1;
429
430     double newval = qMax(value, 0.0);
431     newval = qMin(newval, 100.0);
432     newval = newval / m_keyframeFactor + m_keyframeOffset;
433     if (m_editedKeyframe != newpos)
434         m_keyframes.remove(m_editedKeyframe);
435     m_keyframes[newpos] = (int) newval;
436     m_editedKeyframe = newpos;
437     update();
438 }
439
440 double AbstractClipItem::keyFrameFactor() const
441 {
442     return m_keyframeFactor;
443 }
444
445 int AbstractClipItem::keyFrameNumber() const
446 {
447     return m_keyframes.count();
448 }
449
450 int AbstractClipItem::checkForSingleKeyframe()
451 {
452     // Check if we have only one keyframe
453     if (!m_keyframes.isEmpty() && m_keyframes.count() == 1) {
454         int min = (int) cropStart().frames(m_fps);
455         int max = (int)(cropStart() + cropDuration()).frames(m_fps) - 1;
456         if (m_keyframes.contains(min)) {
457             // Add keyframe at end of clip to allow inserting a new keframe in between
458             m_keyframes[max] = m_keyframes.value(min);
459             return m_keyframes.value(min);
460         }
461     }
462     return -1;
463 }
464
465 int AbstractClipItem::addKeyFrame(const GenTime &pos, const double value)
466 {
467     QRectF br = sceneBoundingRect();
468     double maxh = 100.0 / br.height() / m_keyframeFactor;
469     int newval = (br.bottom() - value) * maxh + m_keyframeOffset;
470     //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
471     int newpos = (int) pos.frames(m_fps) ;
472     m_keyframes[newpos] = newval;
473     m_selectedKeyframe = newpos;
474     update();
475     return newval;
476 }
477
478 bool AbstractClipItem::hasKeyFrames() const
479 {
480     return !m_keyframes.isEmpty();
481 }
482
483 /*QRect AbstractClipItem::visibleRect() {
484     QRect rectInView;
485     if (scene()->views().size() > 0) {
486         rectInView = scene()->views()[0]->viewport()->rect();
487         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
488         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
489         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
490     }
491     return rectInView;
492 }*/
493
494 CustomTrackScene* AbstractClipItem::projectScene()
495 {
496     if (scene())
497         return static_cast <CustomTrackScene*>(scene());
498     return NULL;
499 }
500
501 void AbstractClipItem::setItemLocked(bool locked)
502 {
503     if (locked)
504         setSelected(false);
505
506     setFlag(QGraphicsItem::ItemIsMovable, !locked);
507     setFlag(QGraphicsItem::ItemIsSelectable, !locked);
508 }
509
510 bool AbstractClipItem::isItemLocked() const
511 {
512     return !(flags() & (QGraphicsItem::ItemIsSelectable));
513 }
514
515 // virtual
516 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
517 {
518     if (event->modifiers() & Qt::ShiftModifier) {
519         // User want to do a rectangle selection, so ignore the event to pass it to the view
520         event->ignore();
521     } else {
522         QGraphicsItem::mousePressEvent(event);
523     }
524 }
525
526 int AbstractClipItem::itemHeight()
527 {
528     return 0;
529 }
530
531 int AbstractClipItem::itemOffset()
532 {
533     return 0;
534 }
535
536 void AbstractClipItem::setMainSelectedClip(bool selected)
537 {
538     if (selected == m_isMainSelectedClip) return;
539     m_isMainSelectedClip = selected;
540     update();
541 }
542
543 bool AbstractClipItem::isMainSelectedClip()
544 {
545     return m_isMainSelectedClip;
546 }
547
548
549 #include "abstractclipitem.moc"