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