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