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