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