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