]> git.sesse.net Git - kdenlive/blob - src/abstractclipitem.cpp
* show image thumbnail in marker creation dialog
[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 <KDebug>
22 #include <QGraphicsScene>
23 #include <QGraphicsView>
24 #include <QScrollBar>
25 #include <QToolTip>
26
27 #include "abstractclipitem.h"
28
29 AbstractClipItem::AbstractClipItem(const ItemInfo info, const QRectF& rect, double fps): QGraphicsRectItem(rect), m_track(0), m_fps(fps), m_editedKeyframe(-1), m_selectedKeyframe(0) {
30     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
31     setTrack(info.track);
32     m_startPos = info.startPos;
33     m_cropDuration = info.endPos - info.startPos;
34 }
35
36 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack) {
37     double origX = rect().x();
38     double origY = rect().y();
39     bool success = true;
40     if (x < 0) return;
41     setRect(x * scale, origY + offset, rect().width(), rect().height());
42     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
43     if (collisionList.size() == 0) m_track = newTrack;
44     for (int i = 0; i < collisionList.size(); ++i) {
45         QGraphicsItem *item = collisionList.at(i);
46         if (item->type() == type()) {
47             if (offset == 0) {
48                 QRectF other = ((QGraphicsRectItem *)item)->rect();
49                 if (x < m_startPos.frames(m_fps)) {
50                     kDebug() << "COLLISION, MOVING TO------";
51                     m_startPos = ((AbstractClipItem *)item)->endPos();
52                     origX = m_startPos.frames(m_fps) * scale;
53                 } else if (x > m_startPos.frames(m_fps)) {
54                     //kDebug() << "COLLISION, MOVING TO+++: "<<x<<", CLIP CURR POS: "<<m_startPos.frames(m_fps)<<", COLLIDING START: "<<((AbstractClipItem *)item)->startPos().frames(m_fps);
55                     m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
56                     origX = m_startPos.frames(m_fps) * scale;
57                 }
58             }
59             setRect(origX, origY, rect().width(), rect().height());
60             offset = 0;
61             origX = rect().x();
62             success = false;
63             break;
64         }
65     }
66     if (success) {
67         m_track = newTrack;
68         m_startPos = GenTime(x, m_fps);
69     }
70     /*    QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
71         for (int i = 0; i < childrenList.size(); ++i) {
72           childrenList.at(i)->moveBy(rect().x() - origX , offset);
73         }*/
74 }
75
76 GenTime AbstractClipItem::endPos() const {
77     return m_startPos + m_cropDuration;
78 }
79
80 int AbstractClipItem::track() const {
81     return m_track;
82 }
83
84 GenTime AbstractClipItem::cropStart() const {
85     return m_cropStart;
86 }
87
88 void AbstractClipItem::setCropStart(GenTime pos) {
89     m_cropStart = pos;
90 }
91
92 void AbstractClipItem::resizeStart(int posx, double scale) {
93     GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
94     if (durationDiff == GenTime()) return;
95     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
96
97     if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
98         durationDiff = GenTime() - m_cropStart;
99     } else if (durationDiff >= m_cropDuration) {
100         durationDiff = m_cropDuration - GenTime(3, m_fps);
101     }
102
103     m_startPos += durationDiff;
104     if (type() == AVWIDGET) m_cropStart += durationDiff;
105     m_cropDuration = m_cropDuration - durationDiff;
106     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
107     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
108     for (int i = 0; i < collisionList.size(); ++i) {
109         QGraphicsItem *item = collisionList.at(i);
110         if (item->type() == type()) {
111             GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
112             setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
113             m_startPos += diff;
114             if (type() == AVWIDGET) m_cropStart += diff;
115             m_cropDuration = m_cropDuration - diff;
116             break;
117         }
118     }
119 }
120
121 void AbstractClipItem::resizeEnd(int posx, double scale) {
122     GenTime durationDiff = GenTime(posx, m_fps) - endPos();
123     if (durationDiff == GenTime()) return;
124     //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
125     if (m_cropDuration + durationDiff <= GenTime()) {
126         durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
127     } else if (m_cropStart + m_cropDuration + durationDiff >= m_maxDuration) {
128         durationDiff = m_maxDuration - m_cropDuration - m_cropStart;
129     }
130     m_cropDuration += durationDiff;
131     setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
132     QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
133     for (int i = 0; i < collisionList.size(); ++i) {
134         QGraphicsItem *item = collisionList.at(i);
135         if (item->type() == type()) {
136             GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
137             m_cropDuration = diff;
138             setRect(m_startPos.frames(m_fps) * scale, rect().y(), (m_cropDuration.frames(m_fps)) * scale, rect().height());
139             break;
140         }
141     }
142 }
143
144 GenTime AbstractClipItem::duration() const {
145     return m_cropDuration;
146 }
147
148 GenTime AbstractClipItem::startPos() const {
149     return m_startPos;
150 }
151
152 void AbstractClipItem::setTrack(int track) {
153     m_track = track;
154 }
155
156 double AbstractClipItem::fps() const {
157     return m_fps;
158 }
159
160 GenTime AbstractClipItem::maxDuration() const {
161     return m_maxDuration;
162 }
163
164 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
165     QPainterPath roundRectPathUpper;
166     double roundingY = 20;
167     double roundingX = 20;
168     double offset = 1;
169
170     while (roundingX > br.width() / 2) {
171         roundingX = roundingX / 2;
172         roundingY = roundingY / 2;
173     }
174     int br_endx = (int)(br.x() + br .width() - offset);
175     int br_startx = (int)(br.x() + offset);
176     int br_starty = (int)(br.y());
177     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
178     int br_endy = (int)(br.y() + br.height());
179
180     roundRectPathUpper.moveTo(br_endx  , br_halfy);
181     roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
182     roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
183     roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
184     roundRectPathUpper.lineTo(br_startx , br_halfy);
185
186     return roundRectPathUpper;
187 }
188
189 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
190     QPainterPath roundRectPathLower;
191     double roundingY = 20;
192     double roundingX = 20;
193     double offset = 1;
194
195     int br_endx = (int)(br.x() + br .width() - offset);
196     int br_startx = (int)(br.x() + offset);
197     int br_starty = (int)(br.y());
198     int br_halfy = (int)(br.y() + br.height() / 2 - offset);
199     int br_endy = (int)(br.y() + br.height() - 1);
200
201     while (roundingX > br.width() / 2) {
202         roundingX = roundingX / 2;
203         roundingY = roundingY / 2;
204     }
205     roundRectPathLower.moveTo(br_startx, br_halfy);
206     roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
207     roundRectPathLower.lineTo(br_endx - roundingX  , br_endy);
208     roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
209     roundRectPathLower.lineTo(br_endx  , br_halfy);
210     return roundRectPathLower;
211 }
212
213 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
214     if (m_keyframes.count() < 2) return;
215     QRectF br = rect();
216     double maxw = br.width() / m_cropDuration.frames(m_fps);
217     double maxh = br.height() / 100.0 * m_keyframeFactor;
218     double x1;
219     double y1;
220     double x2;
221     double y2;
222
223     // draw line showing default value
224     if (isSelected()) {
225         x1 = br.x();
226         x1 = br.right();
227         y1 = br.bottom() - m_keyframeDefault * maxh;
228         QLineF l(x1, y1, x2, y1);
229         painter->setPen(QColor(168, 168, 168, 180));
230         painter->drawLine(l);
231         l.translate(0, 1);
232         painter->setPen(QColor(108, 108, 108, 180));
233         painter->drawLine(l);
234         painter->setPen(QColor(Qt::white));
235     }
236
237     // draw keyframes
238     QMap<int, double>::const_iterator i = m_keyframes.constBegin();
239     QColor color(Qt::blue);
240     x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
241     y1 = br.bottom() - i.value() * maxh;
242     while (i != m_keyframes.constEnd()) {
243         if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
244         else color = QColor(Qt::blue);
245         ++i;
246         if (i == m_keyframes.constEnd()) break;
247         x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
248         y2 = br.bottom() - i.value() * maxh;
249         QLineF l(x1, y1, x2, y2);
250         painter->drawLine(l);
251         if (isSelected()) {
252             painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
253         }
254         x1 = x2;
255         y1 = y2;
256     }
257     if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
258 }
259
260 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
261     QRectF br = rect();
262     double maxw = br.width() / m_cropDuration.frames(m_fps);
263     double maxh = br.height() / 100.0 * m_keyframeFactor;
264     if (m_keyframes.count() > 1) {
265         QMap<int, double>::const_iterator i = m_keyframes.constBegin();
266         double x1;
267         double y1;
268         while (i != m_keyframes.constEnd()) {
269             x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
270             y1 = br.bottom() - i.value() * maxh;
271             if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
272                 setToolTip("[" + QString::number(i.key()) + " frames, " + QString::number(i.value(), 'f', 1) + "%]");
273                 return i.key();
274             } else if (x1 > pos.x()) break;
275             ++i;
276         }
277     }
278     setToolTip(QString());
279     return -1;
280 }
281
282 void AbstractClipItem::updateSelectedKeyFrame() {
283     if (m_editedKeyframe == -1) return;
284     QRectF br = rect();
285     double maxw = br.width() / m_cropDuration.frames(m_fps);
286     double maxh = br.height() / 100.0 * m_keyframeFactor;
287     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
288     m_selectedKeyframe = m_editedKeyframe;
289     update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
290 }
291
292 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const int value) {
293     if (!m_keyframes.contains(m_selectedKeyframe)) return;
294     QRectF br = rect();
295     double maxh = 100.0 / br.height();
296     double newval = (br.bottom() - value) * maxh;
297     int newpos = (int) pos.frames(m_fps);
298     int start = m_cropStart.frames(m_fps);
299     int end = (m_cropStart + m_cropDuration).frames(m_fps);
300     newpos = qMax(newpos, start);
301     newpos = qMin(newpos, end);
302     if (newval < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
303         // remove kexframe if it is dragged outside
304         m_keyframes.remove(m_selectedKeyframe);
305         m_selectedKeyframe = -1;
306         update();
307         return;
308     }
309     if (newval > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
310         // remove kexframe if it is dragged outside
311         m_keyframes.remove(m_selectedKeyframe);
312         m_selectedKeyframe = -1;
313         update();
314         return;
315     }
316     newval = qMax(newval, 0.0);
317     newval = qMin(newval, 100.0);
318     newval = newval / m_keyframeFactor;
319     if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
320     m_keyframes[newpos] = newval;
321     m_selectedKeyframe = newpos;
322     update();
323 }
324
325 void AbstractClipItem::addKeyFrame(const GenTime pos, const int value) {
326     QRectF br = rect();
327     double maxh = 100.0 / br.height() / m_keyframeFactor;
328     double newval = (br.bottom() - value) * maxh;
329     int newpos = (int) pos.frames(m_fps) ;
330     m_keyframes[newpos] = newval;
331     m_selectedKeyframe = newpos;
332     update();
333 }
334
335 bool AbstractClipItem::hasKeyFrames() const {
336     return !m_keyframes.isEmpty();
337 }
338
339 QRect AbstractClipItem::visibleRect() {
340     QRect rectInView;
341     if (scene()->views().size() > 0) {
342         rectInView = scene()->views()[0]->viewport()->rect();
343         rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
344         rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
345         //kDebug() << scene()->views()[0]->viewport()->rect() << " " <<  scene()->views()[0]->horizontalScrollBar()->value();
346     }
347     return rectInView;
348 }