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