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"
27 #include <KGlobalSettings>
31 #include <QGraphicsSceneMouseEvent>
32 #include <QParallelAnimationGroup>
34 AbstractClipItem::AbstractClipItem(const ItemInfo &info, const QRectF& rect, double fps) :
36 QGraphicsRectItem(rect),
39 m_selectedKeyframe(0),
43 m_isMainSelectedClip(false)
45 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
46 #if QT_VERSION >= 0x040600
47 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
48 setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
52 AbstractClipItem::~AbstractClipItem()
56 void AbstractClipItem::closeAnimation()
58 #if QT_VERSION >= 0x040600
59 if (!isEnabled()) return;
61 setFlag(QGraphicsItem::ItemIsSelectable, false);
62 if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
67 QPropertyAnimation *closeAnimation = new QPropertyAnimation(this, "rect");
68 QPropertyAnimation *closeAnimation2 = new QPropertyAnimation(this, "opacity");
69 closeAnimation->setDuration(200);
70 closeAnimation2->setDuration(200);
73 r2.setLeft(r.left() + r.width() / 2);
74 r2.setTop(r.top() + r.height() / 2);
77 closeAnimation->setStartValue(r);
78 closeAnimation->setEndValue(r2);
79 closeAnimation->setEasingCurve(QEasingCurve::InQuad);
80 closeAnimation2->setStartValue(1.0);
81 closeAnimation2->setEndValue(0.0);
82 QParallelAnimationGroup *group = new QParallelAnimationGroup;
83 connect(group, SIGNAL(finished()), this, SLOT(deleteLater()));
84 group->addAnimation(closeAnimation);
85 group->addAnimation(closeAnimation2);
86 group->start(QAbstractAnimation::DeleteWhenStopped);
90 ItemInfo AbstractClipItem::info() const
92 ItemInfo info = m_info;
93 info.cropStart = cropStart();
94 info.endPos = endPos();
98 int AbstractClipItem::defaultZValue() const
103 GenTime AbstractClipItem::endPos() const
105 return m_info.startPos + m_info.cropDuration;
108 int AbstractClipItem::track() const
113 GenTime AbstractClipItem::cropStart() const
115 return m_info.cropStart;
118 GenTime AbstractClipItem::cropDuration() const
120 return m_info.cropDuration;
123 void AbstractClipItem::setCropStart(GenTime pos)
125 m_info.cropStart = pos;
128 void AbstractClipItem::updateItem()
130 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
131 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
134 void AbstractClipItem::updateRectGeometry()
136 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
139 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit, bool /*emitChange*/)
141 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
142 if (durationDiff == GenTime()) return;
144 if (type() == AVWIDGET && hasSizeLimit && (cropStart() + durationDiff < GenTime())) {
145 durationDiff = GenTime() - cropStart();
146 } else if (durationDiff >= cropDuration()) {
148 /*if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
151 m_info.startPos += durationDiff;
153 // set to true if crop from start is negative (possible for color clips, images as they have no size limit)
154 bool negCropStart = false;
155 if (type() == AVWIDGET) {
156 m_info.cropStart += durationDiff;
157 if (m_info.cropStart < GenTime())
161 m_info.cropDuration -= durationDiff;
162 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
163 moveBy(durationDiff.frames(m_fps), 0);
165 if (m_info.startPos != GenTime(posx, m_fps)) {
166 //kDebug() << "////// WARNING, DIFF IN XPOS: " << pos().x() << " == " << m_info.startPos.frames(m_fps);
167 GenTime diff = m_info.startPos - GenTime(posx, m_fps);
169 if (type() == AVWIDGET)
170 m_info.cropStart += diff;
172 m_info.cropDuration -= diff;
173 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
174 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
177 // set crop from start to 0 (isn't relevant as this only happens for color clips, images)
179 m_info.cropStart = GenTime();
181 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
182 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
184 /* if (durationDiff < GenTime()) {
185 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
186 for (int i = 0; i < collisionList.size(); ++i) {
187 QGraphicsItem *item = collisionList.at(i);
188 if (item->type() == type() && item->pos().x() < pos().x()) {
189 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
190 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
191 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
192 setPos((m_startPos + diff).frames(m_fps), pos().y());
194 if (type() == AVWIDGET) m_cropStart += diff;
195 m_cropDuration = m_cropDuration - diff;
202 void AbstractClipItem::resizeEnd(int posx, bool /*emitChange*/)
204 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
205 if (durationDiff == GenTime()) return;
206 if (cropDuration() + durationDiff <= GenTime()) {
207 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
210 m_info.cropDuration += durationDiff;
211 m_info.endPos += durationDiff;
213 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
214 if (durationDiff > GenTime()) {
215 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
216 bool fixItem = false;
217 for (int i = 0; i < collisionList.size(); ++i) {
218 if (!collisionList.at(i)->isEnabled()) continue;
219 QGraphicsItem *item = collisionList.at(i);
220 if (item->type() == type() && item->pos().x() > pos().x()) {
221 //kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
222 //kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
223 //kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
224 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
225 if (fixItem == false || diff < m_info.cropDuration) {
227 m_info.cropDuration = diff;
231 if (fixItem) setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
235 GenTime AbstractClipItem::startPos() const
237 return m_info.startPos;
240 void AbstractClipItem::setTrack(int track)
242 m_info.track = track;
245 double AbstractClipItem::fps() const
250 void AbstractClipItem::updateFps(double fps)
253 setPos((qreal) startPos().frames(m_fps), pos().y());
254 updateRectGeometry();
257 GenTime AbstractClipItem::maxDuration() const
259 return m_maxDuration;
262 void AbstractClipItem::drawKeyFrames(QPainter *painter, const QTransform transformation, bool limitedKeyFrames)
264 if (m_keyframes.count() < 1)
267 double maxw = br.width() / cropDuration().frames(m_fps);
268 double maxh = br.height() / 100.0 * m_keyframeFactor;
269 double start = cropStart().frames(m_fps);
270 double x1, y1, x2, y2;
271 bool antialiasing = painter->renderHints() & QPainter::Antialiasing;
273 // draw line showing default value
274 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
278 if (limitedKeyFrames) {
279 QMap<int, int>::const_iterator end = m_keyframes.constEnd();
281 x2 = x1 + maxw * (end.key() - start);
282 x1 += maxw * (m_keyframes.constBegin().key() - start);
284 y1 = br.bottom() - (m_keyframeDefault - m_keyframeOffset) * maxh;
285 QLineF l(x1, y1, x2, y1);
286 QLineF l2 = transformation.map(l);
287 painter->setPen(QColor(168, 168, 168, 180));
288 painter->drawLine(l2);
289 painter->setPen(QColor(108, 108, 108, 180));
290 painter->drawLine(l2.translated(0, 1));
291 painter->setPen(QColor(Qt::white));
292 painter->setRenderHint(QPainter::Antialiasing);
296 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
297 QColor color(Qt::blue);
299 x1 = br.x() + maxw * (i.key() - start);
300 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
304 // make sure line begins with clip beginning
305 if (!limitedKeyFrames && i.key() != start) {
306 QLineF l(br.x(), y1, x1, y1);
307 l2 = transformation.map(l);
308 painter->drawLine(l2);
310 while (i != m_keyframes.constEnd()) {
311 if (i.key() == m_editedKeyframe)
312 color = QColor(Qt::red);
314 color = QColor(Qt::blue);
316 if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
319 if (m_keyframes.count() == 1) {
323 x2 = br.x() + maxw * (i.key() - start);
324 y2 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
326 QLineF l(x1, y1, x2, y2);
327 l2 = transformation.map(l);
328 painter->drawLine(l2);
330 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
331 painter->fillRect(frame, color);
337 // make sure line ends at clip end
338 if (!limitedKeyFrames && x1 != br.right()) {
339 QLineF l(x1, y1, br.right(), y1);
340 painter->drawLine(transformation.map(l));
343 if (active && m_keyframes.count() > 1) {
344 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
345 painter->fillRect(frame, color);
348 painter->setRenderHint(QPainter::Antialiasing, antialiasing);
351 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
353 const QRectF br = sceneBoundingRect();
354 double maxw = br.width() / cropDuration().frames(m_fps);
355 double maxh = br.height() / 100.0 * m_keyframeFactor;
356 if (m_keyframes.count() > 0) {
357 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
360 while (i != m_keyframes.constEnd()) {
361 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
362 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
363 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
364 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + ']');
366 } else if (x1 > pos.x()) {
372 setToolTip(QString());
376 void AbstractClipItem::updateSelectedKeyFrame()
378 if (m_editedKeyframe == -1)
380 QRectF br = sceneBoundingRect();
381 double maxw = br.width() / cropDuration().frames(m_fps);
382 double maxh = br.height() / 100.0 * m_keyframeFactor;
383 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
384 m_selectedKeyframe = m_editedKeyframe;
385 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
388 int AbstractClipItem::editedKeyFramePos() const
390 return m_editedKeyframe;
393 double AbstractClipItem::editedKeyFrameValue() const
395 return m_keyframes.value(m_editedKeyframe);
398 int AbstractClipItem::selectedKeyFramePos() const
400 return m_selectedKeyframe;
403 double AbstractClipItem::selectedKeyFrameValue() const
405 return m_keyframes.value(m_selectedKeyframe);
408 void AbstractClipItem::updateKeyFramePos(const GenTime &pos, const double value)
410 if (!m_keyframes.contains(m_editedKeyframe))
412 int newpos = (int) pos.frames(m_fps);
413 int min = (int) cropStart().frames(m_fps) - 1;
414 int max = (int)(cropStart() + cropDuration()).frames(m_fps);
415 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
416 while (i.key() < m_editedKeyframe) {
417 min = qMax(i.key(), min);
420 i = m_keyframes.constEnd() - 1;
421 while (i.key() > m_editedKeyframe) {
422 max = qMin(i.key(), max);
430 double newval = qMax(value, 0.0);
431 newval = qMin(newval, 100.0);
432 newval = newval / m_keyframeFactor + m_keyframeOffset;
433 if (m_editedKeyframe != newpos)
434 m_keyframes.remove(m_editedKeyframe);
435 m_keyframes[newpos] = (int) newval;
436 m_editedKeyframe = newpos;
440 double AbstractClipItem::keyFrameFactor() const
442 return m_keyframeFactor;
445 int AbstractClipItem::keyFrameNumber() const
447 return m_keyframes.count();
450 int AbstractClipItem::addKeyFrame(const GenTime &pos, const double value)
452 QRectF br = sceneBoundingRect();
453 double maxh = 100.0 / br.height() / m_keyframeFactor;
454 int newval = (br.bottom() - value) * maxh + m_keyframeOffset;
455 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
456 int newpos = (int) pos.frames(m_fps) ;
457 m_keyframes[newpos] = newval;
458 m_selectedKeyframe = newpos;
463 bool AbstractClipItem::hasKeyFrames() const
465 return !m_keyframes.isEmpty();
468 /*QRect AbstractClipItem::visibleRect() {
470 if (scene()->views().size() > 0) {
471 rectInView = scene()->views()[0]->viewport()->rect();
472 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
473 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
474 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
479 CustomTrackScene* AbstractClipItem::projectScene()
482 return static_cast <CustomTrackScene*>(scene());
486 void AbstractClipItem::setItemLocked(bool locked)
491 setFlag(QGraphicsItem::ItemIsMovable, !locked);
492 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
495 bool AbstractClipItem::isItemLocked() const
497 return !(flags() & (QGraphicsItem::ItemIsSelectable));
501 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
503 if (event->modifiers() & Qt::ShiftModifier) {
504 // User want to do a rectangle selection, so ignore the event to pass it to the view
507 QGraphicsItem::mousePressEvent(event);
511 int AbstractClipItem::itemHeight()
516 int AbstractClipItem::itemOffset()
521 void AbstractClipItem::setMainSelectedClip(bool selected)
523 if (selected == m_isMainSelectedClip) return;
524 m_isMainSelectedClip = selected;
528 bool AbstractClipItem::isMainSelectedClip()
530 return m_isMainSelectedClip;