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