]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
Comment out names of unused parameters [PATCH by Ray Lehtiniemi]
[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
195     roundRectPathUpper.moveTo(br_endx  , br_halfy);
196     roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
197     roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
198     roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
199     roundRectPathUpper.lineTo(br_startx , br_halfy);
200
201     return roundRectPathUpper;
202 }
203
204 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
205     QPainterPath roundRectPathLower;
206     double roundingY = 20;
207     double roundingX = 20;
208     double offset = 1;
209
210     int br_endx = (int)(br.x() + br .width() - offset);
211     int br_startx = (int)(br.x() + offset);
212     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
213     int br_endy = (int)(br.y() + br.height() - 1);
214
215     while (roundingX > br.width() / 2) {
216         roundingX = roundingX / 2;
217         roundingY = roundingY / 2;
218     }
219     roundRectPathLower.moveTo(br_startx, br_halfy);
220     roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
221     roundRectPathLower.lineTo(br_endx - roundingX  , br_endy);
222     roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
223     roundRectPathLower.lineTo(br_endx  , br_halfy);
224     return roundRectPathLower;
225 }
226
227 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/) {
228     if (m_keyframes.count() < 2) return;
229     QRectF br = rect();
230     double maxw = br.width() / m_cropDuration.frames(m_fps);
231     double maxh = br.height() / 100.0 * m_keyframeFactor;
232     double x1;
233     double y1;
234     double x2;
235     double y2;
236
237     // draw line showing default value
238     if (isSelected()) {
239         x1 = br.x();
240         x2 = br.right();
241         y1 = br.bottom() - m_keyframeDefault * maxh;
242         QLineF l(x1, y1, x2, y1);
243         QLineF l2 = painter->matrix().map(l);
244         painter->setPen(QColor(168, 168, 168, 180));
245         painter->drawLine(l2);
246         l2.translate(0, 1);
247         painter->setPen(QColor(108, 108, 108, 180));
248         painter->drawLine(l2);
249         painter->setPen(QColor(Qt::white));
250     }
251
252     // draw keyframes
253     QMap<int, double>::const_iterator i = m_keyframes.constBegin();
254     QColor color(Qt::blue);
255     x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
256     y1 = br.bottom() - i.value() * maxh;
257     QLineF l2;
258     while (i != m_keyframes.constEnd()) {
259         if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
260         else color = QColor(Qt::blue);
261         ++i;
262         if (i == m_keyframes.constEnd()) break;
263         x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
264         y2 = br.bottom() - i.value() * maxh;
265         QLineF l(x1, y1, x2, y2);
266         l2 = painter->matrix().map(l);
267         painter->drawLine(l2);
268         if (isSelected()) {
269             painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
270         }
271         x1 = x2;
272         y1 = y2;
273     }
274     if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
275 }
276
277 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
278     QRectF br = sceneBoundingRect();
279     double maxw = br.width() / m_cropDuration.frames(m_fps);
280     double maxh = br.height() / 100.0 * m_keyframeFactor;
281     if (m_keyframes.count() > 1) {
282         QMap<int, double>::const_iterator i = m_keyframes.constBegin();
283         double x1;
284         double y1;
285         while (i != m_keyframes.constEnd()) {
286             x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
287             y1 = br.bottom() - i.value() * maxh;
288             if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
289                 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
290                 return i.key();
291             } else if (x1 > pos.x()) break;
292             ++i;
293         }
294     }
295     setToolTip(QString());
296     return -1;
297 }
298
299 void AbstractClipItem::updateSelectedKeyFrame() {
300     if (m_editedKeyframe == -1) return;
301     QRectF br = sceneBoundingRect();
302     double maxw = br.width() / m_cropDuration.frames(m_fps);
303     double maxh = br.height() / 100.0 * m_keyframeFactor;
304     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
305     m_selectedKeyframe = m_editedKeyframe;
306     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
307 }
308
309 int AbstractClipItem::selectedKeyFramePos() const {
310     return m_editedKeyframe;
311 }
312
313 double AbstractClipItem::selectedKeyFrameValue() const {
314     return m_keyframes[m_editedKeyframe];
315 }
316
317 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
318     if (!m_keyframes.contains(m_selectedKeyframe)) return;
319     int newpos = (int) pos.frames(m_fps);
320     int start = m_cropStart.frames(m_fps);
321     int end = (m_cropStart + m_cropDuration).frames(m_fps);
322     newpos = qMax(newpos, start);
323     newpos = qMin(newpos, end);
324     if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
325         // remove kexframe if it is dragged outside
326         m_keyframes.remove(m_selectedKeyframe);
327         m_selectedKeyframe = -1;
328         update();
329         return;
330     }
331     if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
332         // remove kexframe if it is dragged outside
333         m_keyframes.remove(m_selectedKeyframe);
334         m_selectedKeyframe = -1;
335         update();
336         return;
337     }
338     double newval = qMax(value, 0.0);
339     newval = qMin(newval, 100.0);
340     newval = newval / m_keyframeFactor;
341     if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
342     m_keyframes[newpos] = newval;
343     m_selectedKeyframe = newpos;
344     update();
345 }
346
347 double AbstractClipItem::keyFrameFactor() const {
348     return m_keyframeFactor;
349 }
350
351 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
352     QRectF br = sceneBoundingRect();
353     double maxh = 100.0 / br.height() / m_keyframeFactor;
354     double newval = (br.bottom() - value) * maxh;
355     kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
356     int newpos = (int) pos.frames(m_fps) ;
357     m_keyframes[newpos] = newval;
358     m_selectedKeyframe = newpos;
359     update();
360 }
361
362 bool AbstractClipItem::hasKeyFrames() const {
363     return !m_keyframes.isEmpty();
364 }
365
366 /*QRect AbstractClipItem::visibleRect() {
367     QRect rectInView;
368     if (scene()->views().size() > 0) {
369         rectInView = scene()->views()[0]->viewport()->rect();
370         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
371         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
372         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
373     }
374     return rectInView;
375 }*/
376
377 CustomTrackScene* AbstractClipItem::projectScene() {
378     if (scene()) return static_cast <CustomTrackScene*>(scene());
379     return NULL;
380 }
381
382 void AbstractClipItem::setItemLocked(bool locked) {
383     if (locked) {
384         setSelected(false);
385         setFlag(QGraphicsItem::ItemIsMovable, false);
386         setFlag(QGraphicsItem::ItemIsSelectable, false);
387     } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
388 }
389
390 bool AbstractClipItem::isItemLocked() const {
391     return !(flags() & (QGraphicsItem::ItemIsSelectable));
392 }
393