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