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