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),
40 #if QT_VERSION >= 0x040600
41 , m_closeAnimation(NULL)
44 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
45 #if QT_VERSION >= 0x040600
46 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
50 AbstractClipItem::~AbstractClipItem()
52 #if QT_VERSION >= 0x040600
53 if (m_closeAnimation) delete m_closeAnimation;
57 void AbstractClipItem::closeAnimation()
59 #if QT_VERSION >= 0x040600
60 if (m_closeAnimation) return;
62 m_closeAnimation = new QPropertyAnimation(this, "rect");
63 connect(m_closeAnimation, SIGNAL(finished()), this, SLOT(deleteLater()));
64 m_closeAnimation->setDuration(200);
67 r2.setLeft(r.left() + r.width() / 2);
68 r2.setTop(r.top() + r.height() / 2);
71 m_closeAnimation->setStartValue(r);
72 m_closeAnimation->setEndValue(r2);
73 m_closeAnimation->setEasingCurve(QEasingCurve::InQuad);
74 m_closeAnimation->start();
78 ItemInfo AbstractClipItem::info() const
80 ItemInfo info = m_info;
81 info.cropStart = cropStart();
82 info.endPos = endPos();
86 int AbstractClipItem::defaultZValue() const
91 GenTime AbstractClipItem::endPos() const
93 return m_info.startPos + m_info.cropDuration;
96 int AbstractClipItem::track() const
101 GenTime AbstractClipItem::cropStart() const
103 return m_info.cropStart;
106 GenTime AbstractClipItem::cropDuration() const
108 return m_info.cropDuration;
111 void AbstractClipItem::setCropStart(GenTime pos)
113 m_info.cropStart = pos;
116 void AbstractClipItem::updateItem()
118 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
119 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
122 void AbstractClipItem::updateRectGeometry()
124 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
127 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit)
129 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
130 if (durationDiff == GenTime()) return;
131 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
133 if (type() == AVWIDGET) {
134 if (hasSizeLimit && cropStart() + durationDiff < GenTime())
135 durationDiff = GenTime() - cropStart();
136 } else if (durationDiff >= cropDuration()) {
138 if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
141 //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
142 m_info.startPos += durationDiff;
144 if (type() == AVWIDGET) {
145 m_info.cropStart += durationDiff;
148 m_info.cropDuration = m_info.cropDuration - durationDiff;
149 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
150 moveBy(durationDiff.frames(m_fps), 0);
152 if (m_info.startPos != GenTime(posx, m_fps)) {
153 //kDebug()<<"////// WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
154 GenTime diff = m_info.startPos - GenTime((int) posx, m_fps);
156 if (type() == AVWIDGET) {
157 m_info.cropStart += diff;
159 m_info.cropDuration = m_info.cropDuration - diff;
160 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
161 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
165 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
166 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
168 /* if (durationDiff < GenTime()) {
169 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
170 for (int i = 0; i < collisionList.size(); ++i) {
171 QGraphicsItem *item = collisionList.at(i);
172 if (item->type() == type() && item->pos().x() < pos().x()) {
173 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
174 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
175 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
176 setPos((m_startPos + diff).frames(m_fps), pos().y());
178 if (type() == AVWIDGET) m_cropStart += diff;
179 m_cropDuration = m_cropDuration - diff;
186 void AbstractClipItem::resizeEnd(int posx)
188 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
189 if (durationDiff == GenTime()) return;
190 if (cropDuration() + durationDiff <= GenTime()) {
191 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
194 m_info.cropDuration += durationDiff;
196 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
197 if (durationDiff > GenTime()) {
198 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
199 for (int i = 0; i < collisionList.size(); ++i) {
200 if (!collisionList.at(i)->isEnabled()) continue;
201 QGraphicsItem *item = collisionList.at(i);
202 if (item->type() == type() && item->pos().x() > pos().x()) {
203 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
204 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
205 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
206 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
207 m_info.cropDuration = diff;
208 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
215 GenTime AbstractClipItem::startPos() const
217 return m_info.startPos;
220 void AbstractClipItem::setTrack(int track)
222 m_info.track = track;
225 double AbstractClipItem::fps() const
230 void AbstractClipItem::updateFps(double fps)
233 setPos((qreal) startPos().frames(m_fps), pos().y());
234 updateRectGeometry();
237 GenTime AbstractClipItem::maxDuration() const
239 return m_maxDuration;
242 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
244 if (m_keyframes.count() < 1) return;
246 double maxw = br.width() / cropDuration().frames(m_fps);
247 double maxh = br.height() / 100.0 * m_keyframeFactor;
253 // draw line showing default value
254 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
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 painter->setPen(QColor(108, 108, 108, 180));
264 painter->drawLine(l2.translated(0, 1));
265 painter->setPen(QColor(Qt::white));
269 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
270 QColor color(Qt::blue);
271 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
272 y1 = br.bottom() - i.value() * maxh;
274 while (i != m_keyframes.constEnd()) {
275 if (i.key() == m_editedKeyframe) color = QColor(Qt::red);
276 else color = QColor(Qt::blue);
278 if (i == m_keyframes.constEnd() && m_keyframes.count() != 1) {
281 if (m_keyframes.count() == 1) {
285 x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
286 y2 = br.bottom() - i.value() * maxh;
288 QLineF l(x1, y1, x2, y2);
289 l2 = painter->matrix().map(l);
290 painter->drawLine(l2);
292 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
293 painter->fillRect(frame, color);
299 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
300 painter->fillRect(frame, color);
304 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
306 const QRectF br = sceneBoundingRect();
307 double maxw = br.width() / cropDuration().frames(m_fps);
308 double maxh = br.height() / 100.0 * m_keyframeFactor;
309 if (m_keyframes.count() > 0) {
310 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
313 while (i != m_keyframes.constEnd()) {
314 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
315 y1 = br.bottom() - i.value() * maxh;
316 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
317 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
319 } else if (x1 > pos.x()) break;
323 setToolTip(QString());
327 void AbstractClipItem::updateSelectedKeyFrame()
329 if (m_editedKeyframe == -1) return;
330 QRectF br = sceneBoundingRect();
331 double maxw = br.width() / cropDuration().frames(m_fps);
332 double maxh = br.height() / 100.0 * m_keyframeFactor;
333 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
334 m_selectedKeyframe = m_editedKeyframe;
335 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
338 int AbstractClipItem::editedKeyFramePos() const
340 return m_editedKeyframe;
343 double AbstractClipItem::editedKeyFrameValue() const
345 return m_keyframes.value(m_editedKeyframe);
348 int AbstractClipItem::selectedKeyFramePos() const
350 return m_selectedKeyframe;
353 double AbstractClipItem::selectedKeyFrameValue() const
355 return m_keyframes.value(m_selectedKeyframe);
358 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
360 if (!m_keyframes.contains(m_editedKeyframe)) return;
361 int newpos = (int) pos.frames(m_fps);
362 int start = cropStart().frames(m_fps);
363 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
364 newpos = qMax(newpos, start);
365 newpos = qMin(newpos, end);
367 double newval = qMax(value, 0.0);
368 newval = qMin(newval, 100.0);
369 newval = newval / m_keyframeFactor;
370 if (m_editedKeyframe != newpos) m_keyframes.remove(m_editedKeyframe);
371 m_keyframes[newpos] = (int) newval;
372 m_editedKeyframe = newpos;
376 double AbstractClipItem::keyFrameFactor() const
378 return m_keyframeFactor;
381 int AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
383 QRectF br = sceneBoundingRect();
384 double maxh = 100.0 / br.height() / m_keyframeFactor;
385 int newval = (br.bottom() - value) * maxh;
386 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
387 int newpos = (int) pos.frames(m_fps) ;
388 m_keyframes[newpos] = newval;
389 m_selectedKeyframe = newpos;
394 bool AbstractClipItem::hasKeyFrames() const
396 return !m_keyframes.isEmpty();
399 /*QRect AbstractClipItem::visibleRect() {
401 if (scene()->views().size() > 0) {
402 rectInView = scene()->views()[0]->viewport()->rect();
403 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
404 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
405 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
410 CustomTrackScene* AbstractClipItem::projectScene()
412 if (scene()) return static_cast <CustomTrackScene*>(scene());
416 void AbstractClipItem::setItemLocked(bool locked)
420 setFlag(QGraphicsItem::ItemIsMovable, false);
421 setFlag(QGraphicsItem::ItemIsSelectable, false);
423 setFlag(QGraphicsItem::ItemIsMovable, true);
424 setFlag(QGraphicsItem::ItemIsSelectable, true);
428 bool AbstractClipItem::isItemLocked() const
430 return !(flags() & (QGraphicsItem::ItemIsSelectable));
434 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
436 if (event->modifiers() & Qt::ShiftModifier) {
437 // User want to do a rectangle selection, so ignore the event to pass it to the view
439 } else QGraphicsItem::mousePressEvent(event);