1 /***************************************************************************
2 * Copyright (C) 2008 by Marco Gittler (g.marco@freenet.de) *
3 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "abstractclipitem.h"
22 #include "customtrackscene.h"
23 #include "kdenlivesettings.h"
30 #include <QGraphicsSceneMouseEvent>
32 AbstractClipItem::AbstractClipItem(const ItemInfo info, const QRectF& rect, double fps) :
34 QGraphicsRectItem(rect),
37 m_selectedKeyframe(0),
41 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
44 ItemInfo AbstractClipItem::info() const
46 ItemInfo info = m_info;
47 info.cropStart = cropStart();
48 info.endPos = endPos();
52 GenTime AbstractClipItem::endPos() const
54 return m_info.startPos + m_info.cropDuration;
57 int AbstractClipItem::track() const
62 GenTime AbstractClipItem::cropStart() const
64 return m_info.cropStart;
67 GenTime AbstractClipItem::cropDuration() const
69 return m_info.cropDuration;
72 void AbstractClipItem::setCropStart(GenTime pos)
74 m_info.cropStart = pos;
77 void AbstractClipItem::updateItem()
79 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
80 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
83 void AbstractClipItem::updateRectGeometry()
85 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
88 void AbstractClipItem::resizeStart(int posx, double speed)
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);
94 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
95 durationDiff = GenTime() - cropStart();
96 } else if (durationDiff >= cropDuration()) {
98 if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
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), m_fps);
105 if (type() == AVWIDGET) {
106 m_info.cropStart += durationDiff;
107 m_info.originalcropStart += originalDiff;
109 m_info.cropDuration = m_info.cropDuration - originalDiff;
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), m_fps);
119 if (type() == AVWIDGET) {
120 m_info.cropStart += diff;
121 m_info.originalcropStart += originalDiff;
123 m_info.cropDuration = m_info.cropDuration - originalDiff;
124 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
125 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
129 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
130 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
132 /* if (durationDiff < GenTime()) {
133 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
134 for (int i = 0; i < collisionList.size(); ++i) {
135 QGraphicsItem *item = collisionList.at(i);
136 if (item->type() == type() && item->pos().x() < pos().x()) {
137 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
138 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
139 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
140 setPos((m_startPos + diff).frames(m_fps), pos().y());
142 if (type() == AVWIDGET) m_cropStart += diff;
143 m_cropDuration = m_cropDuration - diff;
150 void AbstractClipItem::resizeEnd(int posx, double speed)
152 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
153 if (durationDiff == GenTime()) return;
154 if (cropDuration() + durationDiff <= GenTime()) {
155 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
158 m_info.cropDuration += GenTime((int)(durationDiff.frames(m_fps) * speed), m_fps);
160 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
161 if (durationDiff > GenTime()) {
162 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
163 for (int i = 0; i < collisionList.size(); ++i) {
164 QGraphicsItem *item = collisionList.at(i);
165 if (item->type() == type() && item->pos().x() > pos().x()) {
166 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
167 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
168 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
169 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
170 m_info.cropDuration = GenTime((int)(diff.frames(m_fps) * speed), m_fps);
171 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
178 GenTime AbstractClipItem::startPos() const
180 return m_info.startPos;
183 void AbstractClipItem::setTrack(int track)
185 m_info.track = track;
188 double AbstractClipItem::fps() const
193 void AbstractClipItem::updateFps(double fps)
196 setPos((qreal) startPos().frames(m_fps), pos().y());
197 updateRectGeometry();
200 GenTime AbstractClipItem::maxDuration() const
202 return m_maxDuration;
205 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
207 if (m_keyframes.count() < 2) return;
209 double maxw = br.width() / cropDuration().frames(m_fps);
210 double maxh = br.height() / 100.0 * m_keyframeFactor;
216 // draw line showing default value
217 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
221 y1 = br.bottom() - m_keyframeDefault * maxh;
222 QLineF l(x1, y1, x2, y1);
223 QLineF l2 = painter->matrix().map(l);
224 painter->setPen(QColor(168, 168, 168, 180));
225 painter->drawLine(l2);
226 painter->setPen(QColor(108, 108, 108, 180));
227 painter->drawLine(l2.translated(0, 1));
228 painter->setPen(QColor(Qt::white));
232 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
233 QColor color(Qt::blue);
234 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
235 y1 = br.bottom() - i.value() * maxh;
237 while (i != m_keyframes.constEnd()) {
238 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
239 else color = QColor(Qt::blue);
241 if (i == m_keyframes.constEnd()) break;
242 x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
243 y2 = br.bottom() - i.value() * maxh;
244 QLineF l(x1, y1, x2, y2);
245 l2 = painter->matrix().map(l);
246 painter->drawLine(l2);
248 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
249 painter->fillRect(frame, color);
255 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
256 painter->fillRect(frame, color);
260 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
262 const QRectF br = sceneBoundingRect();
263 double maxw = br.width() / cropDuration().frames(m_fps);
264 double maxh = br.height() / 100.0 * m_keyframeFactor;
265 if (m_keyframes.count() > 1) {
266 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
269 while (i != m_keyframes.constEnd()) {
270 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
271 y1 = br.bottom() - i.value() * maxh;
272 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
273 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
275 } else if (x1 > pos.x()) break;
279 setToolTip(QString());
283 void AbstractClipItem::updateSelectedKeyFrame()
285 if (m_editedKeyframe == -1) return;
286 QRectF br = sceneBoundingRect();
287 double maxw = br.width() / cropDuration().frames(m_fps);
288 double maxh = br.height() / 100.0 * m_keyframeFactor;
289 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
290 m_selectedKeyframe = m_editedKeyframe;
291 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
294 int AbstractClipItem::selectedKeyFramePos() const
296 return m_editedKeyframe;
299 double AbstractClipItem::selectedKeyFrameValue() const
301 return m_keyframes[m_editedKeyframe];
304 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
306 if (!m_keyframes.contains(m_selectedKeyframe)) return;
307 int newpos = (int) pos.frames(m_fps);
308 int start = cropStart().frames(m_fps);
309 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
310 newpos = qMax(newpos, start);
311 newpos = qMin(newpos, end);
312 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
313 // remove kexframe if it is dragged outside
314 m_keyframes.remove(m_selectedKeyframe);
315 m_selectedKeyframe = -1;
319 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
320 // remove kexframe if it is dragged outside
321 m_keyframes.remove(m_selectedKeyframe);
322 m_selectedKeyframe = -1;
326 double newval = qMax(value, 0.0);
327 newval = qMin(newval, 100.0);
328 newval = newval / m_keyframeFactor;
329 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
330 m_keyframes[newpos] = (int) newval;
331 m_selectedKeyframe = newpos;
335 double AbstractClipItem::keyFrameFactor() const
337 return m_keyframeFactor;
340 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
342 QRectF br = sceneBoundingRect();
343 double maxh = 100.0 / br.height() / m_keyframeFactor;
344 int newval = (br.bottom() - value) * maxh;
345 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
346 int newpos = (int) pos.frames(m_fps) ;
347 m_keyframes[newpos] = newval;
348 m_selectedKeyframe = newpos;
352 bool AbstractClipItem::hasKeyFrames() const
354 return !m_keyframes.isEmpty();
357 /*QRect AbstractClipItem::visibleRect() {
359 if (scene()->views().size() > 0) {
360 rectInView = scene()->views()[0]->viewport()->rect();
361 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
362 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
363 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
368 CustomTrackScene* AbstractClipItem::projectScene()
370 if (scene()) return static_cast <CustomTrackScene*>(scene());
374 void AbstractClipItem::setItemLocked(bool locked)
378 setFlag(QGraphicsItem::ItemIsMovable, false);
379 setFlag(QGraphicsItem::ItemIsSelectable, false);
380 } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
383 bool AbstractClipItem::isItemLocked() const
385 return !(flags() & (QGraphicsItem::ItemIsSelectable));
389 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
391 if (event->modifiers() & Qt::ShiftModifier) {
392 // User want to do a rectangle selection, so ignore the event to pass it to the view
394 } else QGraphicsItem::mousePressEvent(event);