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