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)
38 , m_editedKeyframe(-1)
39 , m_selectedKeyframe(0)
42 , m_keyframeDefault(0)
45 , m_isMainSelectedClip(false)
47 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
48 #if QT_VERSION >= 0x040600
49 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
50 setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
54 AbstractClipItem::~AbstractClipItem()
58 void AbstractClipItem::closeAnimation()
60 #if QT_VERSION >= 0x040600
61 if (!isEnabled()) return;
63 setFlag(QGraphicsItem::ItemIsSelectable, false);
64 if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
69 QPropertyAnimation *closeAnimation = new QPropertyAnimation(this, "rect");
70 QPropertyAnimation *closeAnimation2 = new QPropertyAnimation(this, "opacity");
71 closeAnimation->setDuration(200);
72 closeAnimation2->setDuration(200);
75 r2.setLeft(r.left() + r.width() / 2);
76 r2.setTop(r.top() + r.height() / 2);
79 closeAnimation->setStartValue(r);
80 closeAnimation->setEndValue(r2);
81 closeAnimation->setEasingCurve(QEasingCurve::InQuad);
82 closeAnimation2->setStartValue(1.0);
83 closeAnimation2->setEndValue(0.0);
84 QParallelAnimationGroup *group = new QParallelAnimationGroup;
85 connect(group, SIGNAL(finished()), this, SLOT(deleteLater()));
86 group->addAnimation(closeAnimation);
87 group->addAnimation(closeAnimation2);
88 group->start(QAbstractAnimation::DeleteWhenStopped);
92 ItemInfo AbstractClipItem::info() const
94 ItemInfo info = m_info;
95 info.cropStart = cropStart();
96 info.endPos = endPos();
100 int AbstractClipItem::defaultZValue() const
105 GenTime AbstractClipItem::endPos() const
107 return m_info.startPos + m_info.cropDuration;
110 int AbstractClipItem::track() const
115 GenTime AbstractClipItem::cropStart() const
117 return m_info.cropStart;
120 GenTime AbstractClipItem::cropDuration() const
122 return m_info.cropDuration;
125 void AbstractClipItem::setCropStart(GenTime pos)
127 m_info.cropStart = pos;
130 void AbstractClipItem::updateItem()
132 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
133 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
136 void AbstractClipItem::updateRectGeometry()
138 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
141 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit, bool /*emitChange*/)
143 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
144 if (durationDiff == GenTime()) return;
146 if (type() == AVWIDGET && hasSizeLimit && (cropStart() + durationDiff < GenTime())) {
147 durationDiff = GenTime() - cropStart();
148 } else if (durationDiff >= cropDuration()) {
150 /*if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
153 m_info.startPos += durationDiff;
155 // set to true if crop from start is negative (possible for color clips, images as they have no size limit)
156 bool negCropStart = false;
157 if (type() == AVWIDGET) {
158 m_info.cropStart += durationDiff;
159 if (m_info.cropStart < GenTime())
163 m_info.cropDuration -= durationDiff;
164 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
165 moveBy(durationDiff.frames(m_fps), 0);
167 if (m_info.startPos != GenTime(posx, m_fps)) {
168 //kDebug() << "////// WARNING, DIFF IN XPOS: " << pos().x() << " == " << m_info.startPos.frames(m_fps);
169 GenTime diff = m_info.startPos - GenTime(posx, m_fps);
171 if (type() == AVWIDGET)
172 m_info.cropStart += diff;
174 m_info.cropDuration -= diff;
175 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
176 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
179 // set crop from start to 0 (isn't relevant as this only happens for color clips, images)
181 m_info.cropStart = GenTime();
183 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
184 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
186 /* if (durationDiff < GenTime()) {
187 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
188 for (int i = 0; i < collisionList.size(); ++i) {
189 QGraphicsItem *item = collisionList.at(i);
190 if (item->type() == type() && item->pos().x() < pos().x()) {
191 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
192 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
193 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
194 setPos((m_startPos + diff).frames(m_fps), pos().y());
196 if (type() == AVWIDGET) m_cropStart += diff;
197 m_cropDuration = m_cropDuration - diff;
204 void AbstractClipItem::resizeEnd(int posx, bool /*emitChange*/)
206 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
207 if (durationDiff == GenTime()) return;
208 if (cropDuration() + durationDiff <= GenTime()) {
209 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
212 m_info.cropDuration += durationDiff;
213 m_info.endPos += durationDiff;
215 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
216 if (durationDiff > GenTime()) {
217 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
218 bool fixItem = false;
219 for (int i = 0; i < collisionList.size(); ++i) {
220 if (!collisionList.at(i)->isEnabled()) continue;
221 QGraphicsItem *item = collisionList.at(i);
222 if (item->type() == type() && item->pos().x() > pos().x()) {
223 //kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
224 //kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
225 //kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
226 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
227 if (fixItem == false || diff < m_info.cropDuration) {
229 m_info.cropDuration = diff;
233 if (fixItem) setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
237 GenTime AbstractClipItem::startPos() const
239 return m_info.startPos;
242 void AbstractClipItem::setTrack(int track)
244 m_info.track = track;
247 double AbstractClipItem::fps() const
252 void AbstractClipItem::updateFps(double fps)
255 setPos((qreal) startPos().frames(m_fps), pos().y());
256 updateRectGeometry();
259 GenTime AbstractClipItem::maxDuration() const
261 return m_maxDuration;
264 void AbstractClipItem::drawKeyFrames(QPainter *painter, const QTransform transformation, bool limitedKeyFrames)
266 if (m_keyframes.count() < 1)
269 double maxw = br.width() / cropDuration().frames(m_fps);
270 double maxh = br.height() / 100.0 * m_keyframeFactor;
271 double start = cropStart().frames(m_fps);
272 double x1, y1, x2, y2;
273 bool antialiasing = painter->renderHints() & QPainter::Antialiasing;
275 // draw line showing default value
276 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
280 if (limitedKeyFrames) {
281 QMap<int, int>::const_iterator end = m_keyframes.constEnd();
283 x2 = x1 + maxw * (end.key() - start);
284 x1 += maxw * (m_keyframes.constBegin().key() - start);
286 y1 = br.bottom() - (m_keyframeDefault - m_keyframeOffset) * maxh;
287 QLineF l(x1, y1, x2, y1);
288 QLineF l2 = transformation.map(l);
289 painter->setPen(QColor(168, 168, 168, 180));
290 painter->drawLine(l2);
291 painter->setPen(QColor(108, 108, 108, 180));
292 painter->drawLine(l2.translated(0, 1));
293 painter->setPen(QColor(Qt::white));
294 painter->setRenderHint(QPainter::Antialiasing);
298 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
299 QColor color(Qt::blue);
301 x1 = br.x() + maxw * (i.key() - start);
302 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
306 // make sure line begins with clip beginning
307 if (!limitedKeyFrames && i.key() != start) {
308 QLineF l(br.x(), y1, x1, y1);
309 l2 = transformation.map(l);
310 painter->drawLine(l2);
312 while (i != m_keyframes.constEnd()) {
313 if (i.key() == m_editedKeyframe)
314 color = QColor(Qt::red);
316 color = QColor(Qt::blue);
318 if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
321 if (m_keyframes.count() == 1) {
325 x2 = br.x() + maxw * (i.key() - start);
326 y2 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
328 QLineF l(x1, y1, x2, y2);
329 l2 = transformation.map(l);
330 painter->drawLine(l2);
332 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
333 painter->fillRect(frame, color);
339 // make sure line ends at clip end
340 if (!limitedKeyFrames && x1 != br.right()) {
341 QLineF l(x1, y1, br.right(), y1);
342 painter->drawLine(transformation.map(l));
345 if (active && m_keyframes.count() > 1) {
346 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
347 painter->fillRect(frame, color);
350 painter->setRenderHint(QPainter::Antialiasing, antialiasing);
353 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
355 const QRectF br = sceneBoundingRect();
356 double maxw = br.width() / cropDuration().frames(m_fps);
357 double maxh = br.height() / 100.0 * m_keyframeFactor;
358 if (m_keyframes.count() > 0) {
359 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
362 while (i != m_keyframes.constEnd()) {
363 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
364 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
365 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
366 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + ']');
368 } else if (x1 > pos.x()) {
374 setToolTip(QString());
378 void AbstractClipItem::updateSelectedKeyFrame()
380 if (m_editedKeyframe == -1)
382 QRectF br = sceneBoundingRect();
383 double maxw = br.width() / cropDuration().frames(m_fps);
384 double maxh = br.height() / 100.0 * m_keyframeFactor;
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);
386 m_selectedKeyframe = m_editedKeyframe;
387 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
390 int AbstractClipItem::editedKeyFramePos() const
392 return m_editedKeyframe;
395 double AbstractClipItem::editedKeyFrameValue() const
397 return m_keyframes.value(m_editedKeyframe);
400 int AbstractClipItem::selectedKeyFramePos() const
402 return m_selectedKeyframe;
405 double AbstractClipItem::selectedKeyFrameValue() const
407 return m_keyframes.value(m_selectedKeyframe);
410 void AbstractClipItem::updateKeyFramePos(const GenTime &pos, const double value)
412 if (!m_keyframes.contains(m_editedKeyframe))
414 int newpos = (int) pos.frames(m_fps);
415 int min = (int) cropStart().frames(m_fps) - 1;
416 int max = (int)(cropStart() + cropDuration()).frames(m_fps);
417 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
418 while (i.key() < m_editedKeyframe) {
419 min = qMax(i.key(), min);
422 i = m_keyframes.constEnd() - 1;
423 while (i.key() > m_editedKeyframe) {
424 max = qMin(i.key(), max);
432 double newval = qMax(value, 0.0);
433 newval = qMin(newval, 100.0);
434 newval = newval / m_keyframeFactor + m_keyframeOffset;
435 if (m_editedKeyframe != newpos)
436 m_keyframes.remove(m_editedKeyframe);
437 m_keyframes[newpos] = (int) newval;
438 m_editedKeyframe = newpos;
442 double AbstractClipItem::keyFrameFactor() const
444 return m_keyframeFactor;
447 int AbstractClipItem::keyFrameNumber() const
449 return m_keyframes.count();
452 int AbstractClipItem::checkForSingleKeyframe()
454 // Check if we have only one keyframe
455 if (!m_keyframes.isEmpty() && m_keyframes.count() == 1) {
456 int min = (int) cropStart().frames(m_fps);
457 int max = (int)(cropStart() + cropDuration()).frames(m_fps) - 1;
458 if (m_keyframes.contains(min)) {
459 // Add keyframe at end of clip to allow inserting a new keframe in between
460 m_keyframes[max] = m_keyframes.value(min);
461 return m_keyframes.value(min);
467 int AbstractClipItem::addKeyFrame(const GenTime &pos, const double value)
469 QRectF br = sceneBoundingRect();
470 double maxh = 100.0 / br.height() / m_keyframeFactor;
471 int newval = (br.bottom() - value) * maxh + m_keyframeOffset;
472 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
473 int newpos = (int) pos.frames(m_fps) ;
474 m_keyframes[newpos] = newval;
475 m_selectedKeyframe = newpos;
480 bool AbstractClipItem::hasKeyFrames() const
482 return !m_keyframes.isEmpty();
485 /*QRect AbstractClipItem::visibleRect() {
487 if (scene()->views().size() > 0) {
488 rectInView = scene()->views()[0]->viewport()->rect();
489 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
490 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
491 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
496 CustomTrackScene* AbstractClipItem::projectScene()
499 return static_cast <CustomTrackScene*>(scene());
503 void AbstractClipItem::setItemLocked(bool locked)
508 setFlag(QGraphicsItem::ItemIsMovable, !locked);
509 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
512 bool AbstractClipItem::isItemLocked() const
514 return !(flags() & (QGraphicsItem::ItemIsSelectable));
518 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
520 if (event->modifiers() & Qt::ShiftModifier) {
521 // User want to do a rectangle selection, so ignore the event to pass it to the view
524 QGraphicsItem::mousePressEvent(event);
528 int AbstractClipItem::itemHeight()
533 int AbstractClipItem::itemOffset()
538 void AbstractClipItem::setMainSelectedClip(bool selected)
540 if (selected == m_isMainSelectedClip) return;
541 m_isMainSelectedClip = selected;
545 bool AbstractClipItem::isMainSelectedClip()
547 return m_isMainSelectedClip;