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