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