]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Fix timeline handling of objects (move them instead of changing their bounding rect
[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 <QGraphicsScene>
22 #include <QGraphicsView>
23 #include <QScrollBar>
24 #include <QToolTip>
25
26 #include <KDebug>
27 #include <KLocale>
28
29 #include "abstractclipitem.h"
30
31 AbstractClipItem::AbstractClipItem(const ItemInfo info, const QRectF& rect, double fps): QGraphicsRectItem(rect), m_track(0), m_fps(fps), m_editedKeyframe(-1), m_selectedKeyframe(0), m_keyframeFactor(1) {
32     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
33     setTrack(info.track);
34     m_startPos = info.startPos;
35     m_cropDuration = info.endPos - info.startPos;
36 }
37
38 ItemInfo AbstractClipItem::info() const {
39     ItemInfo itemInfo;
40     itemInfo.startPos = startPos();
41     itemInfo.endPos = endPos();
42     itemInfo.cropStart = cropStart();
43     itemInfo.track = track();
44     return itemInfo;
45 }
46
47 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack, bool checkCollision) {
48     qreal origX = pos().x();
49     qreal origY = pos().y();
50     bool success = true;
51     if (x < 0) return;
52     //setRect(x * scale, origY + offset, rect().width(), rect().height());
53     //double xoffset = (x -  m_startPos.frames(m_fps)) * scale;// - origX;
54     setPos((qreal) x * scale, (qreal) pos().y() + offset);
55
56     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
57     if (collisionList.size() == 0) m_track = newTrack;
58     if (checkCollision)
59         for (int i = 0; i < collisionList.size(); ++i) {
60             QGraphicsItem *item = collisionList.at(i);
61             if (item->type() == type()) {
62                 if (offset == 0) {
63                     QRectF other = ((QGraphicsRectItem *)item)->rect();
64                     if (x < m_startPos.frames(m_fps)) {
65                         kDebug() << "COLLISION, MOVING TO------";
66                         m_startPos = ((AbstractClipItem *)item)->endPos();
67                         origX = (qreal) m_startPos.frames(m_fps) * scale;
68                     } else if (x > m_startPos.frames(m_fps)) {
69                         //kDebug() << "COLLISION, MOVING TO+++: "<<x<<", CLIP CURR POS: "<<m_startPos.frames(m_fps)<<", COLLIDING START: "<<((AbstractClipItem *)item)->startPos().frames(m_fps);
70                         m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
71                         origX = (qreal) m_startPos.frames(m_fps) * scale;
72                     }
73                 }
74                 setPos(origX, origY);
75                 offset = 0;
76                 success = false;
77                 break;
78             }
79         }
80     if (success) {
81         m_track = newTrack;
82         m_startPos = GenTime(x, m_fps);
83     }
84 }
85
86 GenTime AbstractClipItem::endPos() const {
87     return m_startPos + m_cropDuration;
88 }
89
90 int AbstractClipItem::track() const {
91     return m_track;
92 }
93
94 GenTime AbstractClipItem::cropStart() const {
95     return m_cropStart;
96 }
97
98 void AbstractClipItem::setCropStart(GenTime pos) {
99     m_cropStart = pos;
100 }
101
102 void AbstractClipItem::resizeStart(int posx, double scale) {
103     GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
104     if (durationDiff == GenTime()) return;
105     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
106
107     if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
108         durationDiff = GenTime() - m_cropStart;
109     } else if (durationDiff >= m_cropDuration) {
110         durationDiff = m_cropDuration - GenTime(3, m_fps);
111     }
112
113     m_startPos += durationDiff;
114     if (type() == AVWIDGET) m_cropStart += durationDiff;
115     m_cropDuration = m_cropDuration - durationDiff;
116
117     setRect(0, 0, (qreal) m_cropDuration.frames(m_fps) * scale - .5, rect().height());
118     setPos((qreal) m_startPos.frames(m_fps) * scale, pos().y());
119     //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
120     if (durationDiff < GenTime()) {
121         QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
122         for (int i = 0; i < collisionList.size(); ++i) {
123             QGraphicsItem *item = collisionList.at(i);
124             if (item->type() == type() && item->pos().x() < pos().x()) {
125                 kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
126                 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
127                 setRect(0, 0, (qreal)(m_cropDuration - diff).frames(m_fps) * scale - .5, rect().height());
128                 setPos((qreal)(m_startPos + diff).frames(m_fps) * scale, pos().y());
129                 m_startPos += diff;
130                 if (type() == AVWIDGET) m_cropStart += diff;
131                 m_cropDuration = m_cropDuration - diff;
132                 break;
133             }
134         }
135     }
136 }
137
138 void AbstractClipItem::resizeEnd(int posx, double scale) {
139     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
140     if (durationDiff == GenTime()) return;
141     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
142     if (m_cropDuration + durationDiff <= GenTime()) {
143         durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
144     } else if (m_cropStart + m_cropDuration + durationDiff >= maxDuration()) {
145         durationDiff = maxDuration() - m_cropDuration - m_cropStart;
146     }
147     m_cropDuration += durationDiff;
148     setRect(0, 0, (qreal) m_cropDuration.frames(m_fps) * scale - .5, rect().height());
149     if (durationDiff > GenTime()) {
150         QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
151         for (int i = 0; i < collisionList.size(); ++i) {
152             QGraphicsItem *item = collisionList.at(i);
153             if (item->type() == type() && item->pos().x() > pos().x()) {
154                 kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
155                 kDebug() << "/////////  CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
156                 kDebug() << "/////////  COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
157                 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
158                 m_cropDuration = diff;
159                 setRect(0, 0, (qreal)(m_cropDuration.frames(m_fps)) * scale - .5, rect().height());
160                 break;
161             }
162         }
163     }
164 }
165
166 GenTime AbstractClipItem::duration() const {
167     return m_cropDuration;
168 }
169
170 GenTime AbstractClipItem::startPos() const {
171     return m_startPos;
172 }
173
174 void AbstractClipItem::setTrack(int track) {
175     m_track = track;
176 }
177
178 double AbstractClipItem::fps() const {
179     return m_fps;
180 }
181
182 GenTime AbstractClipItem::maxDuration() const {
183     return m_maxDuration;
184 }
185
186 void AbstractClipItem::setMaxDuration(const GenTime &max) {
187     m_maxDuration = max;
188 }
189
190 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
191     QPainterPath roundRectPathUpper;
192     double roundingY = 20;
193     double roundingX = 20;
194     double offset = 1;
195
196     while (roundingX > br.width() / 2) {
197         roundingX = roundingX / 2;
198         roundingY = roundingY / 2;
199     }
200     int br_endx = (int)(br.x() + br .width() - offset);
201     int br_startx = (int)(br.x() + offset);
202     int br_starty = (int)(br.y());
203     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
204     int br_endy = (int)(br.y() + br.height());
205
206     roundRectPathUpper.moveTo(br_endx  , br_halfy);
207     roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
208     roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
209     roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
210     roundRectPathUpper.lineTo(br_startx , br_halfy);
211
212     return roundRectPathUpper;
213 }
214
215 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
216     QPainterPath roundRectPathLower;
217     double roundingY = 20;
218     double roundingX = 20;
219     double offset = 1;
220
221     int br_endx = (int)(br.x() + br .width() - offset);
222     int br_startx = (int)(br.x() + offset);
223     int br_starty = (int)(br.y());
224     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
225     int br_endy = (int)(br.y() + br.height() - 1);
226
227     while (roundingX > br.width() / 2) {
228         roundingX = roundingX / 2;
229         roundingY = roundingY / 2;
230     }
231     roundRectPathLower.moveTo(br_startx, br_halfy);
232     roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
233     roundRectPathLower.lineTo(br_endx - roundingX  , br_endy);
234     roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
235     roundRectPathLower.lineTo(br_endx  , br_halfy);
236     return roundRectPathLower;
237 }
238
239 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
240     if (m_keyframes.count() < 2) return;
241     QRectF br = rect();
242     double maxw = br.width() / m_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     if (isSelected()) {
251         x1 = br.x();
252         x1 = br.right();
253         y1 = br.bottom() - m_keyframeDefault * maxh;
254         QLineF l(x1, y1, x2, y1);
255         painter->setPen(QColor(168, 168, 168, 180));
256         painter->drawLine(l);
257         l.translate(0, 1);
258         painter->setPen(QColor(108, 108, 108, 180));
259         painter->drawLine(l);
260         painter->setPen(QColor(Qt::white));
261     }
262
263     // draw keyframes
264     QMap<int, double>::const_iterator i = m_keyframes.constBegin();
265     QColor color(Qt::blue);
266     x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
267     y1 = br.bottom() - i.value() * maxh;
268     while (i != m_keyframes.constEnd()) {
269         if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
270         else color = QColor(Qt::blue);
271         ++i;
272         if (i == m_keyframes.constEnd()) break;
273         x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
274         y2 = br.bottom() - i.value() * maxh;
275         QLineF l(x1, y1, x2, y2);
276         painter->drawLine(l);
277         if (isSelected()) {
278             painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
279         }
280         x1 = x2;
281         y1 = y2;
282     }
283     if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
284 }
285
286 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
287     QRectF br = sceneBoundingRect();
288     double maxw = br.width() / m_cropDuration.frames(m_fps);
289     double maxh = br.height() / 100.0 * m_keyframeFactor;
290     if (m_keyframes.count() > 1) {
291         QMap<int, double>::const_iterator i = m_keyframes.constBegin();
292         double x1;
293         double y1;
294         while (i != m_keyframes.constEnd()) {
295             x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
296             y1 = br.bottom() - i.value() * maxh;
297             if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
298                 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
299                 return i.key();
300             } else if (x1 > pos.x()) break;
301             ++i;
302         }
303     }
304     setToolTip(QString());
305     return -1;
306 }
307
308 void AbstractClipItem::updateSelectedKeyFrame() {
309     if (m_editedKeyframe == -1) return;
310     QRectF br = sceneBoundingRect();
311     double maxw = br.width() / m_cropDuration.frames(m_fps);
312     double maxh = br.height() / 100.0 * m_keyframeFactor;
313     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
314     m_selectedKeyframe = m_editedKeyframe;
315     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
316 }
317
318 int AbstractClipItem::selectedKeyFramePos() const {
319     return m_editedKeyframe;
320 }
321
322 double AbstractClipItem::selectedKeyFrameValue() const {
323     return m_keyframes[m_editedKeyframe];
324 }
325
326 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
327     if (!m_keyframes.contains(m_selectedKeyframe)) return;
328     int newpos = (int) pos.frames(m_fps);
329     int start = m_cropStart.frames(m_fps);
330     int end = (m_cropStart + m_cropDuration).frames(m_fps);
331     newpos = qMax(newpos, start);
332     newpos = qMin(newpos, end);
333     if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
334         // remove kexframe if it is dragged outside
335         m_keyframes.remove(m_selectedKeyframe);
336         m_selectedKeyframe = -1;
337         update();
338         return;
339     }
340     if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
341         // remove kexframe if it is dragged outside
342         m_keyframes.remove(m_selectedKeyframe);
343         m_selectedKeyframe = -1;
344         update();
345         return;
346     }
347     double newval = qMax(value, 0.0);
348     newval = qMin(newval, 100.0);
349     newval = newval / m_keyframeFactor;
350     if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
351     m_keyframes[newpos] = newval;
352     m_selectedKeyframe = newpos;
353     update();
354 }
355
356 double AbstractClipItem::keyFrameFactor() const {
357     return m_keyframeFactor;
358 }
359
360 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
361     QRectF br = sceneBoundingRect();
362     double maxh = 100.0 / br.height() / m_keyframeFactor;
363     double newval = (br.bottom() - value) * maxh;
364     kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
365     int newpos = (int) pos.frames(m_fps) ;
366     m_keyframes[newpos] = newval;
367     m_selectedKeyframe = newpos;
368     update();
369 }
370
371 bool AbstractClipItem::hasKeyFrames() const {
372     return !m_keyframes.isEmpty();
373 }
374
375 QRect AbstractClipItem::visibleRect() {
376     QRect rectInView;
377     if (scene()->views().size() > 0) {
378         rectInView = scene()->views()[0]->viewport()->rect();
379         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
380         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
381         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
382     }
383     return rectInView;
384 }