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