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