]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Fix several problems with clip crop start, fix undo transition deletion, set monitor...
[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 ItemInfo AbstractClipItem::info() const {
39     ItemInfo itemInfo;
40     itemInfo.startPos = startPos();
41     itemInfo.endPos = endPos();
42     itemInfo.cropStart = cropStart();
43     itemInfo.track = track();
44     return itemInfo;
45 }
46
47 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack, bool checkCollision) {
48     double origX = rect().x();
49     double origY = rect().y();
50     bool success = true;
51     if (x < 0) return;
52     setRect(x * scale, origY + offset, rect().width(), rect().height());
53     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
54     if (collisionList.size() == 0) m_track = newTrack;
55     if (checkCollision)
56         for (int i = 0; i < collisionList.size(); ++i) {
57             QGraphicsItem *item = collisionList.at(i);
58             if (item->type() == type()) {
59                 if (offset == 0) {
60                     QRectF other = ((QGraphicsRectItem *)item)->rect();
61                     if (x < m_startPos.frames(m_fps)) {
62                         kDebug() << "COLLISION, MOVING TO------";
63                         m_startPos = ((AbstractClipItem *)item)->endPos();
64                         origX = m_startPos.frames(m_fps) * scale;
65                     } else if (x > m_startPos.frames(m_fps)) {
66                         //kDebug() << "COLLISION, MOVING TO+++: "<<x<<", CLIP CURR POS: "<<m_startPos.frames(m_fps)<<", COLLIDING START: "<<((AbstractClipItem *)item)->startPos().frames(m_fps);
67                         m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
68                         origX = m_startPos.frames(m_fps) * scale;
69                     }
70                 }
71                 setRect(origX, origY, rect().width(), rect().height());
72                 offset = 0;
73                 origX = rect().x();
74                 success = false;
75                 break;
76             }
77         }
78     if (success) {
79         m_track = newTrack;
80         m_startPos = GenTime(x, m_fps);
81     }
82     /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
83         for (int i = 0; i < childrenList.size(); ++i) {
84           childrenList.at(i)->moveBy(rect().x() - origX , offset);
85         }*/
86 }
87
88 GenTime AbstractClipItem::endPos() const {
89     return m_startPos + m_cropDuration;
90 }
91
92 int AbstractClipItem::track() const {
93     return m_track;
94 }
95
96 GenTime AbstractClipItem::cropStart() const {
97     return m_cropStart;
98 }
99
100 void AbstractClipItem::setCropStart(GenTime pos) {
101     m_cropStart = pos;
102 }
103
104 void AbstractClipItem::resizeStart(int posx, double scale) {
105     GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
106     if (durationDiff == GenTime()) return;
107     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
108
109     if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
110         durationDiff = GenTime() - m_cropStart;
111     } else if (durationDiff >= m_cropDuration) {
112         durationDiff = m_cropDuration - GenTime(3, m_fps);
113     }
114
115     m_startPos += durationDiff;
116     if (type() == AVWIDGET) m_cropStart += durationDiff;
117     m_cropDuration = m_cropDuration - durationDiff;
118     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
119     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
120     for (int i = 0; i < collisionList.size(); ++i) {
121         QGraphicsItem *item = collisionList.at(i);
122         if (item->type() == type()) {
123             GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
124             setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
125             m_startPos += diff;
126             if (type() == AVWIDGET) m_cropStart += diff;
127             m_cropDuration = m_cropDuration - diff;
128             break;
129         }
130     }
131 }
132
133 void AbstractClipItem::resizeEnd(int posx, double scale) {
134     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
135     if (durationDiff == GenTime()) return;
136     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
137     if (m_cropDuration + durationDiff <= GenTime()) {
138         durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
139     } else if (m_cropStart + m_cropDuration + durationDiff >= m_maxDuration) {
140         durationDiff = m_maxDuration - m_cropDuration - m_cropStart;
141     }
142     m_cropDuration += durationDiff;
143     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
144     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
145     for (int i = 0; i < collisionList.size(); ++i) {
146         QGraphicsItem *item = collisionList.at(i);
147         if (item->type() == type()) {
148             GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
149             m_cropDuration = diff;
150             setRect(m_startPos.frames(m_fps) * scale, rect().y(), (m_cropDuration.frames(m_fps)) * scale, rect().height());
151             break;
152         }
153     }
154 }
155
156 GenTime AbstractClipItem::duration() const {
157     return m_cropDuration;
158 }
159
160 GenTime AbstractClipItem::startPos() const {
161     return m_startPos;
162 }
163
164 void AbstractClipItem::setTrack(int track) {
165     m_track = track;
166 }
167
168 double AbstractClipItem::fps() const {
169     return m_fps;
170 }
171
172 GenTime AbstractClipItem::maxDuration() const {
173     return m_maxDuration;
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         x1 = br.right();
239         y1 = br.bottom() - m_keyframeDefault * maxh;
240         QLineF l(x1, y1, x2, y1);
241         painter->setPen(QColor(168, 168, 168, 180));
242         painter->drawLine(l);
243         l.translate(0, 1);
244         painter->setPen(QColor(108, 108, 108, 180));
245         painter->drawLine(l);
246         painter->setPen(QColor(Qt::white));
247     }
248
249     // draw keyframes
250     QMap<int, double>::const_iterator i = m_keyframes.constBegin();
251     QColor color(Qt::blue);
252     x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
253     y1 = br.bottom() - i.value() * maxh;
254     while (i != m_keyframes.constEnd()) {
255         if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
256         else color = QColor(Qt::blue);
257         ++i;
258         if (i == m_keyframes.constEnd()) break;
259         x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
260         y2 = br.bottom() - i.value() * maxh;
261         QLineF l(x1, y1, x2, y2);
262         painter->drawLine(l);
263         if (isSelected()) {
264             painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
265         }
266         x1 = x2;
267         y1 = y2;
268     }
269     if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
270 }
271
272 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
273     QRectF br = rect();
274     double maxw = br.width() / m_cropDuration.frames(m_fps);
275     double maxh = br.height() / 100.0 * m_keyframeFactor;
276     if (m_keyframes.count() > 1) {
277         QMap<int, double>::const_iterator i = m_keyframes.constBegin();
278         double x1;
279         double y1;
280         while (i != m_keyframes.constEnd()) {
281             x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
282             y1 = br.bottom() - i.value() * maxh;
283             if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
284                 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
285                 return i.key();
286             } else if (x1 > pos.x()) break;
287             ++i;
288         }
289     }
290     setToolTip(QString());
291     return -1;
292 }
293
294 void AbstractClipItem::updateSelectedKeyFrame() {
295     if (m_editedKeyframe == -1) return;
296     QRectF br = rect();
297     double maxw = br.width() / m_cropDuration.frames(m_fps);
298     double maxh = br.height() / 100.0 * m_keyframeFactor;
299     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
300     m_selectedKeyframe = m_editedKeyframe;
301     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
302 }
303
304 int AbstractClipItem::selectedKeyFramePos() const {
305     return m_editedKeyframe;
306 }
307
308 double AbstractClipItem::selectedKeyFrameValue() const {
309     return m_keyframes[m_editedKeyframe];
310 }
311
312 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
313     if (!m_keyframes.contains(m_selectedKeyframe)) return;
314     int newpos = (int) pos.frames(m_fps);
315     int start = m_cropStart.frames(m_fps);
316     int end = (m_cropStart + m_cropDuration).frames(m_fps);
317     newpos = qMax(newpos, start);
318     newpos = qMin(newpos, end);
319     if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
320         // remove kexframe if it is dragged outside
321         m_keyframes.remove(m_selectedKeyframe);
322         m_selectedKeyframe = -1;
323         update();
324         return;
325     }
326     if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
327         // remove kexframe if it is dragged outside
328         m_keyframes.remove(m_selectedKeyframe);
329         m_selectedKeyframe = -1;
330         update();
331         return;
332     }
333     double newval = qMax(value, 0.0);
334     newval = qMin(newval, 100.0);
335     newval = newval / m_keyframeFactor;
336     if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
337     m_keyframes[newpos] = newval;
338     m_selectedKeyframe = newpos;
339     update();
340 }
341
342 double AbstractClipItem::keyFrameFactor() const {
343     return m_keyframeFactor;
344 }
345
346 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
347     QRectF br = rect();
348     double maxh = 100.0 / br.height() / m_keyframeFactor;
349     double newval = (br.bottom() - value) * maxh;
350     int newpos = (int) pos.frames(m_fps) ;
351     m_keyframes[newpos] = newval;
352     m_selectedKeyframe = newpos;
353     update();
354 }
355
356 bool AbstractClipItem::hasKeyFrames() const {
357     return !m_keyframes.isEmpty();
358 }
359
360 QRect AbstractClipItem::visibleRect() {
361     QRect rectInView;
362     if (scene()->views().size() > 0) {
363         rectInView = scene()->views()[0]->viewport()->rect();
364         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
365         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
366         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
367     }
368     return rectInView;
369 }