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);
47 setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
51 AbstractClipItem::~AbstractClipItem()
53 #if QT_VERSION >= 0x040600
54 if (m_closeAnimation) delete m_closeAnimation;
58 void AbstractClipItem::closeAnimation()
60 #if QT_VERSION >= 0x040600
61 if (m_closeAnimation) return;
63 m_closeAnimation = new QPropertyAnimation(this, "rect");
64 connect(m_closeAnimation, SIGNAL(finished()), this, SLOT(deleteLater()));
65 m_closeAnimation->setDuration(200);
68 r2.setLeft(r.left() + r.width() / 2);
69 r2.setTop(r.top() + r.height() / 2);
72 m_closeAnimation->setStartValue(r);
73 m_closeAnimation->setEndValue(r2);
74 m_closeAnimation->setEasingCurve(QEasingCurve::InQuad);
75 m_closeAnimation->start();
79 ItemInfo AbstractClipItem::info() const
81 ItemInfo info = m_info;
82 info.cropStart = cropStart();
83 info.endPos = endPos();
87 int AbstractClipItem::defaultZValue() const
92 GenTime AbstractClipItem::endPos() const
94 return m_info.startPos + m_info.cropDuration;
97 int AbstractClipItem::track() const
102 GenTime AbstractClipItem::cropStart() const
104 return m_info.cropStart;
107 GenTime AbstractClipItem::cropDuration() const
109 return m_info.cropDuration;
112 void AbstractClipItem::setCropStart(GenTime pos)
114 m_info.cropStart = pos;
117 void AbstractClipItem::updateItem()
119 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
120 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
123 void AbstractClipItem::updateRectGeometry()
125 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
128 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit)
130 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
131 if (durationDiff == GenTime()) return;
132 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
134 if (type() == AVWIDGET && 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 m_info.startPos += durationDiff;
143 if (type() == AVWIDGET)
144 m_info.cropStart += durationDiff;
146 m_info.cropDuration -= durationDiff;
147 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
148 moveBy(durationDiff.frames(m_fps), 0);
150 if (m_info.startPos != GenTime(posx, m_fps)) {
151 //kDebug() << "////// WARNING, DIFF IN XPOS: " << pos().x() << " == " << m_info.startPos.frames(m_fps);
152 GenTime diff = m_info.startPos - GenTime(posx, m_fps);
154 if (type() == AVWIDGET)
155 m_info.cropStart += diff;
157 m_info.cropDuration -= diff;
158 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
159 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
163 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
164 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
166 /* if (durationDiff < GenTime()) {
167 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
168 for (int i = 0; i < collisionList.size(); ++i) {
169 QGraphicsItem *item = collisionList.at(i);
170 if (item->type() == type() && item->pos().x() < pos().x()) {
171 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
172 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
173 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
174 setPos((m_startPos + diff).frames(m_fps), pos().y());
176 if (type() == AVWIDGET) m_cropStart += diff;
177 m_cropDuration = m_cropDuration - diff;
184 void AbstractClipItem::resizeEnd(int posx)
186 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
187 if (durationDiff == GenTime()) return;
188 if (cropDuration() + durationDiff <= GenTime()) {
189 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
192 m_info.cropDuration += durationDiff;
194 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
195 if (durationDiff > GenTime()) {
196 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
197 for (int i = 0; i < collisionList.size(); ++i) {
198 if (!collisionList.at(i)->isEnabled()) continue;
199 QGraphicsItem *item = collisionList.at(i);
200 if (item->type() == type() && item->pos().x() > pos().x()) {
201 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
202 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
203 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
204 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
205 m_info.cropDuration = diff;
206 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
213 GenTime AbstractClipItem::startPos() const
215 return m_info.startPos;
218 void AbstractClipItem::setTrack(int track)
220 m_info.track = track;
223 double AbstractClipItem::fps() const
228 void AbstractClipItem::updateFps(double fps)
231 setPos((qreal) startPos().frames(m_fps), pos().y());
232 updateRectGeometry();
235 GenTime AbstractClipItem::maxDuration() const
237 return m_maxDuration;
240 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
242 if (m_keyframes.count() < 1)
245 double maxw = br.width() / cropDuration().frames(m_fps);
246 double maxh = br.height() / 100.0 * m_keyframeFactor;
252 // draw line showing default value
253 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
257 y1 = br.bottom() - m_keyframeDefault * maxh;
258 QLineF l(x1, y1, x2, y1);
259 QLineF l2 = painter->matrix().map(l);
260 painter->setPen(QColor(168, 168, 168, 180));
261 painter->drawLine(l2);
262 painter->setPen(QColor(108, 108, 108, 180));
263 painter->drawLine(l2.translated(0, 1));
264 painter->setPen(QColor(Qt::white));
268 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
269 QColor color(Qt::blue);
270 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
271 y1 = br.bottom() - i.value() * maxh;
273 while (i != m_keyframes.constEnd()) {
274 if (i.key() == m_editedKeyframe)
275 color = QColor(Qt::red);
277 color = QColor(Qt::blue);
279 if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
282 if (m_keyframes.count() == 1) {
286 x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
287 y2 = br.bottom() - i.value() * maxh;
289 QLineF l(x1, y1, x2, y2);
290 l2 = painter->matrix().map(l);
291 painter->drawLine(l2);
293 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
294 painter->fillRect(frame, color);
300 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
301 painter->fillRect(frame, color);
305 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
307 const QRectF br = sceneBoundingRect();
308 double maxw = br.width() / cropDuration().frames(m_fps);
309 double maxh = br.height() / 100.0 * m_keyframeFactor;
310 if (m_keyframes.count() > 0) {
311 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
314 while (i != m_keyframes.constEnd()) {
315 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
316 y1 = br.bottom() - i.value() * maxh;
317 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
318 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
320 } else if (x1 > pos.x()) {
326 setToolTip(QString());
330 void AbstractClipItem::updateSelectedKeyFrame()
332 if (m_editedKeyframe == -1)
334 QRectF br = sceneBoundingRect();
335 double maxw = br.width() / cropDuration().frames(m_fps);
336 double maxh = br.height() / 100.0 * m_keyframeFactor;
337 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
338 m_selectedKeyframe = m_editedKeyframe;
339 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
342 int AbstractClipItem::editedKeyFramePos() const
344 return m_editedKeyframe;
347 double AbstractClipItem::editedKeyFrameValue() const
349 return m_keyframes.value(m_editedKeyframe);
352 int AbstractClipItem::selectedKeyFramePos() const
354 return m_selectedKeyframe;
357 double AbstractClipItem::selectedKeyFrameValue() const
359 return m_keyframes.value(m_selectedKeyframe);
362 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
364 if (!m_keyframes.contains(m_editedKeyframe))
366 int newpos = (int) pos.frames(m_fps);
367 int min = (int) cropStart().frames(m_fps) - 1;
368 int max = (int)(cropStart() + cropDuration()).frames(m_fps);
369 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
370 while (i.key() < m_editedKeyframe) {
371 min = qMax(i.key(), min);
374 i = m_keyframes.constEnd() - 1;
375 while (i.key() > m_editedKeyframe) {
376 max = qMin(i.key(), max);
384 double newval = qMax(value, 0.0);
385 newval = qMin(newval, 100.0);
386 newval = newval / m_keyframeFactor;
387 if (m_editedKeyframe != newpos)
388 m_keyframes.remove(m_editedKeyframe);
389 m_keyframes[newpos] = (int) newval;
390 m_editedKeyframe = newpos;
394 double AbstractClipItem::keyFrameFactor() const
396 return m_keyframeFactor;
399 int AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
401 QRectF br = sceneBoundingRect();
402 double maxh = 100.0 / br.height() / m_keyframeFactor;
403 int newval = (br.bottom() - value) * maxh;
404 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
405 int newpos = (int) pos.frames(m_fps) ;
406 m_keyframes[newpos] = newval;
407 m_selectedKeyframe = newpos;
412 bool AbstractClipItem::hasKeyFrames() const
414 return !m_keyframes.isEmpty();
417 /*QRect AbstractClipItem::visibleRect() {
419 if (scene()->views().size() > 0) {
420 rectInView = scene()->views()[0]->viewport()->rect();
421 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
422 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
423 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
428 CustomTrackScene* AbstractClipItem::projectScene()
431 return static_cast <CustomTrackScene*>(scene());
435 void AbstractClipItem::setItemLocked(bool locked)
440 setFlag(QGraphicsItem::ItemIsMovable, !locked);
441 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
444 bool AbstractClipItem::isItemLocked() const
446 return !(flags() & (QGraphicsItem::ItemIsSelectable));
450 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
452 if (event->modifiers() & Qt::ShiftModifier) {
453 // User want to do a rectangle selection, so ignore the event to pass it to the view
456 QGraphicsItem::mousePressEvent(event);