]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
* New configuration page to set SDL audio/video driver and audio device
[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 <QGraphicsScene>
22 #include <QGraphicsView>
23 #include <QScrollBar>
24 #include <QToolTip>
25
26 #include <KDebug>
27 #include <KLocale>
28
29 #include "abstractclipitem.h"
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 = cropStart();
43     itemInfo.track = track();
44     return itemInfo;
45 }
46
47 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack, bool checkCollision) {
48     double origX = rect().x();
49     double origY = rect().y();
50     bool success = true;
51     if (x < 0) return;
52     setRect(x * scale, origY + offset, rect().width(), rect().height());
53     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
54     if (collisionList.size() == 0) m_track = newTrack;
55     if (checkCollision)
56         for (int i = 0; i < collisionList.size(); ++i) {
57             QGraphicsItem *item = collisionList.at(i);
58             if (item->type() == type()) {
59                 if (offset == 0) {
60                     QRectF other = ((QGraphicsRectItem *)item)->rect();
61                     if (x < m_startPos.frames(m_fps)) {
62                         kDebug() << "COLLISION, MOVING TO------";
63                         m_startPos = ((AbstractClipItem *)item)->endPos();
64                         origX = m_startPos.frames(m_fps) * scale;
65                     } else if (x > m_startPos.frames(m_fps)) {
66                         //kDebug() << "COLLISION, MOVING TO+++: "<<x<<", CLIP CURR POS: "<<m_startPos.frames(m_fps)<<", COLLIDING START: "<<((AbstractClipItem *)item)->startPos().frames(m_fps);
67                         m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
68                         origX = m_startPos.frames(m_fps) * scale;
69                     }
70                 }
71                 setRect(origX, origY, rect().width(), rect().height());
72                 offset = 0;
73                 origX = rect().x();
74                 success = false;
75                 break;
76             }
77         }
78     if (success) {
79         m_track = newTrack;
80         m_startPos = GenTime(x, m_fps);
81     }
82     /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
83         for (int i = 0; i < childrenList.size(); ++i) {
84           childrenList.at(i)->moveBy(rect().x() - origX , offset);
85         }*/
86 }
87
88 GenTime AbstractClipItem::endPos() const {
89     return m_startPos + m_cropDuration;
90 }
91
92 int AbstractClipItem::track() const {
93     return m_track;
94 }
95
96 GenTime AbstractClipItem::cropStart() const {
97     return m_cropStart;
98 }
99
100 void AbstractClipItem::setCropStart(GenTime pos) {
101     m_cropStart = pos;
102 }
103
104 void AbstractClipItem::resizeStart(int posx, double scale) {
105     GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
106     if (durationDiff == GenTime()) return;
107     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
108
109     if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
110         durationDiff = GenTime() - m_cropStart;
111     } else if (durationDiff >= m_cropDuration) {
112         durationDiff = m_cropDuration - GenTime(3, m_fps);
113     }
114
115     m_startPos += durationDiff;
116     if (type() == AVWIDGET) m_cropStart += durationDiff;
117     m_cropDuration = m_cropDuration - durationDiff;
118     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
119     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
120     for (int i = 0; i < collisionList.size(); ++i) {
121         QGraphicsItem *item = collisionList.at(i);
122         if (item->type() == type()) {
123             GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
124             setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
125             m_startPos += diff;
126             if (type() == AVWIDGET) m_cropStart += diff;
127             m_cropDuration = m_cropDuration - diff;
128             break;
129         }
130     }
131 }
132
133 void AbstractClipItem::resizeEnd(int posx, double scale) {
134     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
135     if (durationDiff == GenTime()) return;
136     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
137     if (m_cropDuration + durationDiff <= GenTime()) {
138         durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
139     } else if (m_cropStart + m_cropDuration + durationDiff >= maxDuration()) {
140         durationDiff = maxDuration() - m_cropDuration - m_cropStart;
141     }
142     m_cropDuration += durationDiff;
143     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
144     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
145     for (int i = 0; i < collisionList.size(); ++i) {
146         QGraphicsItem *item = collisionList.at(i);
147         if (item->type() == type()) {
148             GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
149             m_cropDuration = diff;
150             setRect(m_startPos.frames(m_fps) * scale, rect().y(), (m_cropDuration.frames(m_fps)) * scale, rect().height());
151             break;
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         x1 = br.right();
243         y1 = br.bottom() - m_keyframeDefault * maxh;
244         QLineF l(x1, y1, x2, y1);
245         painter->setPen(QColor(168, 168, 168, 180));
246         painter->drawLine(l);
247         l.translate(0, 1);
248         painter->setPen(QColor(108, 108, 108, 180));
249         painter->drawLine(l);
250         painter->setPen(QColor(Qt::white));
251     }
252
253     // draw keyframes
254     QMap<int, double>::const_iterator i = m_keyframes.constBegin();
255     QColor color(Qt::blue);
256     x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
257     y1 = br.bottom() - i.value() * maxh;
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         painter->drawLine(l);
267         if (isSelected()) {
268             painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
269         }
270         x1 = x2;
271         y1 = y2;
272     }
273     if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
274 }
275
276 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
277     QRectF br = rect();
278     double maxw = br.width() / m_cropDuration.frames(m_fps);
279     double maxh = br.height() / 100.0 * m_keyframeFactor;
280     if (m_keyframes.count() > 1) {
281         QMap<int, double>::const_iterator i = m_keyframes.constBegin();
282         double x1;
283         double y1;
284         while (i != m_keyframes.constEnd()) {
285             x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
286             y1 = br.bottom() - i.value() * maxh;
287             if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
288                 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
289                 return i.key();
290             } else if (x1 > pos.x()) break;
291             ++i;
292         }
293     }
294     setToolTip(QString());
295     return -1;
296 }
297
298 void AbstractClipItem::updateSelectedKeyFrame() {
299     if (m_editedKeyframe == -1) return;
300     QRectF br = rect();
301     double maxw = br.width() / m_cropDuration.frames(m_fps);
302     double maxh = br.height() / 100.0 * m_keyframeFactor;
303     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
304     m_selectedKeyframe = m_editedKeyframe;
305     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
306 }
307
308 int AbstractClipItem::selectedKeyFramePos() const {
309     return m_editedKeyframe;
310 }
311
312 double AbstractClipItem::selectedKeyFrameValue() const {
313     return m_keyframes[m_editedKeyframe];
314 }
315
316 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
317     if (!m_keyframes.contains(m_selectedKeyframe)) return;
318     int newpos = (int) pos.frames(m_fps);
319     int start = m_cropStart.frames(m_fps);
320     int end = (m_cropStart + m_cropDuration).frames(m_fps);
321     newpos = qMax(newpos, start);
322     newpos = qMin(newpos, end);
323     if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
324         // remove kexframe if it is dragged outside
325         m_keyframes.remove(m_selectedKeyframe);
326         m_selectedKeyframe = -1;
327         update();
328         return;
329     }
330     if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
331         // remove kexframe if it is dragged outside
332         m_keyframes.remove(m_selectedKeyframe);
333         m_selectedKeyframe = -1;
334         update();
335         return;
336     }
337     double newval = qMax(value, 0.0);
338     newval = qMin(newval, 100.0);
339     newval = newval / m_keyframeFactor;
340     if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
341     m_keyframes[newpos] = newval;
342     m_selectedKeyframe = newpos;
343     update();
344 }
345
346 double AbstractClipItem::keyFrameFactor() const {
347     return m_keyframeFactor;
348 }
349
350 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
351     QRectF br = rect();
352     double maxh = 100.0 / br.height() / m_keyframeFactor;
353     double newval = (br.bottom() - value) * maxh;
354     int newpos = (int) pos.frames(m_fps) ;
355     m_keyframes[newpos] = newval;
356     m_selectedKeyframe = newpos;
357     update();
358 }
359
360 bool AbstractClipItem::hasKeyFrames() const {
361     return !m_keyframes.isEmpty();
362 }
363
364 QRect AbstractClipItem::visibleRect() {
365     QRect rectInView;
366     if (scene()->views().size() > 0) {
367         rectInView = scene()->views()[0]->viewport()->rect();
368         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
369         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
370         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
371     }
372     return rectInView;
373 }