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