]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Fix several issues with clips with speed changed:
[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 "abstractclipitem.h"
22 #include "customtrackscene.h"
23 #include "kdenlivesettings.h"
24
25 #include <KDebug>
26 #include <KLocale>
27
28 #include <QPainter>
29 #include <QToolTip>
30
31 AbstractClipItem::AbstractClipItem(const ItemInfo info, const QRectF& rect, double fps) :
32         QObject(),
33         QGraphicsRectItem(rect),
34         m_track(0),
35         m_editedKeyframe(-1),
36         m_selectedKeyframe(0),
37         m_keyframeFactor(1),
38         m_fps(fps)
39 {
40     setFlags(/*QGraphicsItem::ItemClipsToShape | */QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
41     setTrack(info.track);
42     m_startPos = info.startPos;
43     m_cropDuration = info.endPos - info.startPos;
44 }
45
46 ItemInfo AbstractClipItem::info() const
47 {
48     ItemInfo itemInfo;
49     itemInfo.startPos = startPos();
50     itemInfo.endPos = endPos();
51     itemInfo.cropStart = m_cropStart;
52     itemInfo.track = track();
53     return itemInfo;
54 }
55
56 GenTime AbstractClipItem::endPos() const
57 {
58     return m_startPos + m_cropDuration;
59 }
60
61 int AbstractClipItem::track() const
62 {
63     return m_track;
64 }
65
66 GenTime AbstractClipItem::cropStart() const
67 {
68     return m_cropStart;
69 }
70
71 GenTime AbstractClipItem::cropDuration() const
72 {
73     return m_cropDuration;
74 }
75
76 void AbstractClipItem::setCropStart(GenTime pos)
77 {
78     m_cropStart = pos;
79 }
80
81 void AbstractClipItem::updateItem()
82 {
83     m_track = (int)(scenePos().y() / KdenliveSettings::trackheight());
84     m_startPos = GenTime((int) scenePos().x(), m_fps);
85 }
86
87 void AbstractClipItem::updateRectGeometry()
88 {
89     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
90 }
91
92 void AbstractClipItem::resizeStart(int posx, double speed)
93 {
94     GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
95     if (durationDiff == GenTime()) return;
96     //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
97
98     if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
99         durationDiff = GenTime() - cropStart();
100     } else if (durationDiff >= m_cropDuration) {
101         return;
102         if (m_cropDuration > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
103         else return;
104     }
105     //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
106     m_startPos += durationDiff;
107     if (type() == AVWIDGET) m_cropStart += durationDiff * speed;
108     m_cropDuration = m_cropDuration - durationDiff * speed;
109
110     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
111     moveBy(durationDiff.frames(m_fps), 0);
112     //setPos(m_startPos.frames(m_fps), pos().y());
113     if ((int) scenePos().x() != posx) {
114         //kDebug()<<"//////  WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
115         GenTime diff = GenTime((int) pos().x() - posx, m_fps);
116         if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
117         m_cropDuration = m_cropDuration - diff;
118         setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
119         //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
120     }
121
122
123     //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
124     //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
125
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                     GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
133                     setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
134                     setPos((m_startPos + diff).frames(m_fps), pos().y());
135                     m_startPos += diff;
136                     if (type() == AVWIDGET) m_cropStart += diff;
137                     m_cropDuration = m_cropDuration - diff;
138                     break;
139                 }
140             }
141         }*/
142 }
143
144 void AbstractClipItem::resizeEnd(int posx, double speed, bool /*updateKeyFrames*/)
145 {
146     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
147     if (durationDiff == GenTime()) return;
148     //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
149     if (cropDuration() + durationDiff <= GenTime()) {
150         durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
151     }
152     //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
153     m_cropDuration += durationDiff * speed;
154     setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
155     if (durationDiff > GenTime()) {
156         QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
157         for (int i = 0; i < collisionList.size(); ++i) {
158             QGraphicsItem *item = collisionList.at(i);
159             if (item->type() == type() && item->pos().x() > pos().x()) {
160                 /*kDebug() << "/////////  COLLISION DETECTED!!!!!!!!!";
161                 kDebug() << "/////////  CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
162                 kDebug() << "/////////  COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
163                 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
164                 m_cropDuration = diff * speed;
165                 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
166                 break;
167             }
168         }
169     }
170 }
171
172 GenTime AbstractClipItem::startPos() const
173 {
174     return m_startPos;
175 }
176
177 void AbstractClipItem::setTrack(int track)
178 {
179     m_track = track;
180 }
181
182 double AbstractClipItem::fps() const
183 {
184     return m_fps;
185 }
186
187 GenTime AbstractClipItem::maxDuration() const
188 {
189     return m_maxDuration;
190 }
191
192 QPainterPath AbstractClipItem::upperRectPart(QRectF br)
193 {
194     QPainterPath roundRectPathUpper;
195     double roundingY = 20;
196     double roundingX = 20;
197     double offset = 1;
198
199     while (roundingX > br.width() / 2) {
200         roundingX = roundingX / 2;
201         roundingY = roundingY / 2;
202     }
203     int br_endx = (int)(br.x() + br .width() - offset);
204     int br_startx = (int)(br.x() + offset);
205     int br_starty = (int)(br.y());
206     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
207
208     roundRectPathUpper.moveTo(br_endx  , br_halfy);
209     roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
210     roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
211     roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
212     roundRectPathUpper.lineTo(br_startx , br_halfy);
213
214     return roundRectPathUpper;
215 }
216
217 QPainterPath AbstractClipItem::lowerRectPart(QRectF br)
218 {
219     QPainterPath roundRectPathLower;
220     double roundingY = 20;
221     double roundingX = 20;
222     double offset = 1;
223
224     int br_endx = (int)(br.x() + br .width() - offset);
225     int br_startx = (int)(br.x() + offset);
226     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
227     int br_endy = (int)(br.y() + br.height() - 1);
228
229     while (roundingX > br.width() / 2) {
230         roundingX = roundingX / 2;
231         roundingY = roundingY / 2;
232     }
233     roundRectPathLower.moveTo(br_startx, br_halfy);
234     roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
235     roundRectPathLower.lineTo(br_endx - roundingX  , br_endy);
236     roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
237     roundRectPathLower.lineTo(br_endx  , br_halfy);
238     return roundRectPathLower;
239 }
240
241 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
242 {
243     if (m_keyframes.count() < 2) return;
244     QRectF br = rect();
245     double maxw = br.width() / m_cropDuration.frames(m_fps);
246     double maxh = br.height() / 100.0 * m_keyframeFactor;
247     double x1;
248     double y1;
249     double x2;
250     double y2;
251
252     // draw line showing default value
253     bool active = isSelected() || (parentItem() && parentItem()->isSelected());
254     if (active) {
255         x1 = br.x();
256         x2 = br.right();
257         y1 = br.bottom() - m_keyframeDefault * maxh;
258         QLineF l(x1, y1, x2, y1);
259         QLineF l2 = painter->matrix().map(l);
260         painter->setPen(QColor(168, 168, 168, 180));
261         painter->drawLine(l2);
262         l2.translate(0, 1);
263         painter->setPen(QColor(108, 108, 108, 180));
264         painter->drawLine(l2);
265         painter->setPen(QColor(Qt::white));
266     }
267
268     // draw keyframes
269     QMap<int, double>::const_iterator i = m_keyframes.constBegin();
270     QColor color(Qt::blue);
271     x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
272     y1 = br.bottom() - i.value() * maxh;
273     QLineF l2;
274     while (i != m_keyframes.constEnd()) {
275         if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
276         else color = QColor(Qt::blue);
277         ++i;
278         if (i == m_keyframes.constEnd()) break;
279         x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
280         y2 = br.bottom() - i.value() * maxh;
281         QLineF l(x1, y1, x2, y2);
282         l2 = painter->matrix().map(l);
283         painter->drawLine(l2);
284         if (active) {
285             painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
286         }
287         x1 = x2;
288         y1 = y2;
289     }
290     if (active) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
291 }
292
293 int AbstractClipItem::mouseOverKeyFrames(QPointF pos)
294 {
295     QRectF br = sceneBoundingRect();
296     double maxw = br.width() / m_cropDuration.frames(m_fps);
297     double maxh = br.height() / 100.0 * m_keyframeFactor;
298     if (m_keyframes.count() > 1) {
299         QMap<int, double>::const_iterator i = m_keyframes.constBegin();
300         double x1;
301         double y1;
302         while (i != m_keyframes.constEnd()) {
303             x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
304             y1 = br.bottom() - i.value() * maxh;
305             if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
306                 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
307                 return i.key();
308             } else if (x1 > pos.x()) break;
309             ++i;
310         }
311     }
312     setToolTip(QString());
313     return -1;
314 }
315
316 void AbstractClipItem::updateSelectedKeyFrame()
317 {
318     if (m_editedKeyframe == -1) return;
319     QRectF br = sceneBoundingRect();
320     double maxw = br.width() / m_cropDuration.frames(m_fps);
321     double maxh = br.height() / 100.0 * m_keyframeFactor;
322     update(br.x() + maxw *(m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
323     m_selectedKeyframe = m_editedKeyframe;
324     update(br.x() + maxw *(m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
325 }
326
327 int AbstractClipItem::selectedKeyFramePos() const
328 {
329     return m_editedKeyframe;
330 }
331
332 double AbstractClipItem::selectedKeyFrameValue() const
333 {
334     return m_keyframes[m_editedKeyframe];
335 }
336
337 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
338 {
339     if (!m_keyframes.contains(m_selectedKeyframe)) return;
340     int newpos = (int) pos.frames(m_fps);
341     int start = m_cropStart.frames(m_fps);
342     int end = (m_cropStart + m_cropDuration).frames(m_fps);
343     newpos = qMax(newpos, start);
344     newpos = qMin(newpos, end);
345     if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
346         // remove kexframe if it is dragged outside
347         m_keyframes.remove(m_selectedKeyframe);
348         m_selectedKeyframe = -1;
349         update();
350         return;
351     }
352     if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
353         // remove kexframe if it is dragged outside
354         m_keyframes.remove(m_selectedKeyframe);
355         m_selectedKeyframe = -1;
356         update();
357         return;
358     }
359     double newval = qMax(value, 0.0);
360     newval = qMin(newval, 100.0);
361     newval = newval / m_keyframeFactor;
362     if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
363     m_keyframes[newpos] = newval;
364     m_selectedKeyframe = newpos;
365     update();
366 }
367
368 double AbstractClipItem::keyFrameFactor() const
369 {
370     return m_keyframeFactor;
371 }
372
373 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
374 {
375     QRectF br = sceneBoundingRect();
376     double maxh = 100.0 / br.height() / m_keyframeFactor;
377     double newval = (br.bottom() - value) * maxh;
378     kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
379     int newpos = (int) pos.frames(m_fps) ;
380     m_keyframes[newpos] = newval;
381     m_selectedKeyframe = newpos;
382     update();
383 }
384
385 bool AbstractClipItem::hasKeyFrames() const
386 {
387     return !m_keyframes.isEmpty();
388 }
389
390 /*QRect AbstractClipItem::visibleRect() {
391     QRect rectInView;
392     if (scene()->views().size() > 0) {
393         rectInView = scene()->views()[0]->viewport()->rect();
394         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
395         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
396         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
397     }
398     return rectInView;
399 }*/
400
401 CustomTrackScene* AbstractClipItem::projectScene()
402 {
403     if (scene()) return static_cast <CustomTrackScene*>(scene());
404     return NULL;
405 }
406
407 void AbstractClipItem::setItemLocked(bool locked)
408 {
409     if (locked) {
410         setSelected(false);
411         setFlag(QGraphicsItem::ItemIsMovable, false);
412         setFlag(QGraphicsItem::ItemIsSelectable, false);
413     } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
414 }
415
416 bool AbstractClipItem::isItemLocked() const
417 {
418     return !(flags() & (QGraphicsItem::ItemIsSelectable));
419 }
420