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