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