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 if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
66 QPropertyAnimation *closeAnimation = new QPropertyAnimation(this, "rect");
67 QPropertyAnimation *closeAnimation2 = new QPropertyAnimation(this, "opacity");
68 closeAnimation->setDuration(200);
69 closeAnimation2->setDuration(200);
72 r2.setLeft(r.left() + r.width() / 2);
73 r2.setTop(r.top() + r.height() / 2);
76 closeAnimation->setStartValue(r);
77 closeAnimation->setEndValue(r2);
78 closeAnimation->setEasingCurve(QEasingCurve::InQuad);
79 closeAnimation2->setStartValue(1.0);
80 closeAnimation2->setEndValue(0.0);
81 QParallelAnimationGroup *group = new QParallelAnimationGroup;
82 connect(group, SIGNAL(finished()), this, SLOT(deleteLater()));
83 group->addAnimation(closeAnimation);
84 group->addAnimation(closeAnimation2);
85 group->start(QAbstractAnimation::DeleteWhenStopped);
89 ItemInfo AbstractClipItem::info() const
91 ItemInfo info = m_info;
92 info.cropStart = cropStart();
93 info.endPos = endPos();
97 int AbstractClipItem::defaultZValue() const
102 GenTime AbstractClipItem::endPos() const
104 return m_info.startPos + m_info.cropDuration;
107 int AbstractClipItem::track() const
112 GenTime AbstractClipItem::cropStart() const
114 return m_info.cropStart;
117 GenTime AbstractClipItem::cropDuration() const
119 return m_info.cropDuration;
122 void AbstractClipItem::setCropStart(GenTime pos)
124 m_info.cropStart = pos;
127 void AbstractClipItem::updateItem()
129 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
130 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
133 void AbstractClipItem::updateRectGeometry()
135 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
138 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit, bool /*emitChange*/)
140 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
141 if (durationDiff == GenTime()) return;
143 if (type() == AVWIDGET && hasSizeLimit && (cropStart() + durationDiff < GenTime())) {
144 durationDiff = GenTime() - cropStart();
145 } else if (durationDiff >= cropDuration()) {
147 /*if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
150 m_info.startPos += durationDiff;
152 // set to true if crop from start is negative (possible for color clips, images as they have no size limit)
153 bool negCropStart = false;
154 if (type() == AVWIDGET) {
155 m_info.cropStart += durationDiff;
156 if (m_info.cropStart < GenTime())
160 m_info.cropDuration -= durationDiff;
161 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
162 moveBy(durationDiff.frames(m_fps), 0);
164 if (m_info.startPos != GenTime(posx, m_fps)) {
165 //kDebug() << "////// WARNING, DIFF IN XPOS: " << pos().x() << " == " << m_info.startPos.frames(m_fps);
166 GenTime diff = m_info.startPos - GenTime(posx, m_fps);
168 if (type() == AVWIDGET)
169 m_info.cropStart += diff;
171 m_info.cropDuration -= diff;
172 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
173 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
176 // set crop from start to 0 (isn't relevant as this only happens for color clips, images)
178 m_info.cropStart = GenTime();
180 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
181 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
183 /* if (durationDiff < GenTime()) {
184 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
185 for (int i = 0; i < collisionList.size(); ++i) {
186 QGraphicsItem *item = collisionList.at(i);
187 if (item->type() == type() && item->pos().x() < pos().x()) {
188 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
189 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
190 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
191 setPos((m_startPos + diff).frames(m_fps), pos().y());
193 if (type() == AVWIDGET) m_cropStart += diff;
194 m_cropDuration = m_cropDuration - diff;
201 void AbstractClipItem::resizeEnd(int posx, bool /*emitChange*/)
203 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
204 if (durationDiff == GenTime()) return;
205 if (cropDuration() + durationDiff <= GenTime()) {
206 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
209 m_info.cropDuration += durationDiff;
210 m_info.endPos += durationDiff;
212 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
213 if (durationDiff > GenTime()) {
214 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
215 bool fixItem = false;
216 for (int i = 0; i < collisionList.size(); ++i) {
217 if (!collisionList.at(i)->isEnabled()) continue;
218 QGraphicsItem *item = collisionList.at(i);
219 if (item->type() == type() && item->pos().x() > pos().x()) {
220 //kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
221 //kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
222 //kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
223 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
224 if (fixItem == false || diff < m_info.cropDuration) {
226 m_info.cropDuration = diff;
230 if (fixItem) setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
234 GenTime AbstractClipItem::startPos() const
236 return m_info.startPos;
239 void AbstractClipItem::setTrack(int track)
241 m_info.track = track;
244 double AbstractClipItem::fps() const
249 void AbstractClipItem::updateFps(double fps)
252 setPos((qreal) startPos().frames(m_fps), pos().y());
253 updateRectGeometry();
256 GenTime AbstractClipItem::maxDuration() const
258 return m_maxDuration;
261 void AbstractClipItem::drawKeyFrames(QPainter *painter, bool limitedKeyFrames)
263 if (m_keyframes.count() < 1)
266 double maxw = br.width() / cropDuration().frames(m_fps);
267 double maxh = br.height() / 100.0 * m_keyframeFactor;
268 double start = cropStart().frames(m_fps);
269 double x1, y1, x2, y2;
270 bool antialiasing = painter->renderHints() & QPainter::Antialiasing;
272 // draw line showing default value
273 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
277 if (limitedKeyFrames) {
278 QMap<int, int>::const_iterator end = m_keyframes.constEnd();
280 x2 = x1 + maxw * (end.key() - start);
281 x1 += maxw * (m_keyframes.constBegin().key() - start);
283 y1 = br.bottom() - (m_keyframeDefault - m_keyframeOffset) * maxh;
284 QLineF l(x1, y1, x2, y1);
285 QLineF l2 = painter->worldTransform().map(l);
286 painter->setPen(QColor(168, 168, 168, 180));
287 painter->drawLine(l2);
288 painter->setPen(QColor(108, 108, 108, 180));
289 painter->drawLine(l2.translated(0, 1));
290 painter->setPen(QColor(Qt::white));
291 painter->setRenderHint(QPainter::Antialiasing);
295 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
296 QColor color(Qt::blue);
298 x1 = br.x() + maxw * (i.key() - start);
299 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
303 // make sure line begins with clip beginning
304 if (!limitedKeyFrames && i.key() != start) {
305 QLineF l(br.x(), y1, x1, y1);
306 l2 = painter->worldTransform().map(l);
307 painter->drawLine(l2);
309 while (i != m_keyframes.constEnd()) {
310 if (i.key() == m_editedKeyframe)
311 color = QColor(Qt::red);
313 color = QColor(Qt::blue);
315 if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
318 if (m_keyframes.count() == 1) {
322 x2 = br.x() + maxw * (i.key() - start);
323 y2 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
325 QLineF l(x1, y1, x2, y2);
326 l2 = painter->worldTransform().map(l);
327 painter->drawLine(l2);
329 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
330 painter->fillRect(frame, color);
336 // make sure line ends at clip end
337 if (!limitedKeyFrames && x1 != br.right()) {
338 QLineF l(x1, y1, br.right(), y1);
339 painter->drawLine(painter->worldTransform().map(l));
342 if (active && m_keyframes.count() > 1) {
343 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
344 painter->fillRect(frame, color);
347 painter->setRenderHint(QPainter::Antialiasing, antialiasing);
350 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
352 const QRectF br = sceneBoundingRect();
353 double maxw = br.width() / cropDuration().frames(m_fps);
354 double maxh = br.height() / 100.0 * m_keyframeFactor;
355 if (m_keyframes.count() > 0) {
356 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
359 while (i != m_keyframes.constEnd()) {
360 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
361 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
362 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
363 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + ']');
365 } else if (x1 > pos.x()) {
371 setToolTip(QString());
375 void AbstractClipItem::updateSelectedKeyFrame()
377 if (m_editedKeyframe == -1)
379 QRectF br = sceneBoundingRect();
380 double maxw = br.width() / cropDuration().frames(m_fps);
381 double maxh = br.height() / 100.0 * m_keyframeFactor;
382 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
383 m_selectedKeyframe = m_editedKeyframe;
384 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
387 int AbstractClipItem::editedKeyFramePos() const
389 return m_editedKeyframe;
392 double AbstractClipItem::editedKeyFrameValue() const
394 return m_keyframes.value(m_editedKeyframe);
397 int AbstractClipItem::selectedKeyFramePos() const
399 return m_selectedKeyframe;
402 double AbstractClipItem::selectedKeyFrameValue() const
404 return m_keyframes.value(m_selectedKeyframe);
407 void AbstractClipItem::updateKeyFramePos(const GenTime &pos, const double value)
409 if (!m_keyframes.contains(m_editedKeyframe))
411 int newpos = (int) pos.frames(m_fps);
412 int min = (int) cropStart().frames(m_fps) - 1;
413 int max = (int)(cropStart() + cropDuration()).frames(m_fps);
414 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
415 while (i.key() < m_editedKeyframe) {
416 min = qMax(i.key(), min);
419 i = m_keyframes.constEnd() - 1;
420 while (i.key() > m_editedKeyframe) {
421 max = qMin(i.key(), max);
429 double newval = qMax(value, 0.0);
430 newval = qMin(newval, 100.0);
431 newval = newval / m_keyframeFactor + m_keyframeOffset;
432 if (m_editedKeyframe != newpos)
433 m_keyframes.remove(m_editedKeyframe);
434 m_keyframes[newpos] = (int) newval;
435 m_editedKeyframe = newpos;
439 double AbstractClipItem::keyFrameFactor() const
441 return m_keyframeFactor;
444 int AbstractClipItem::keyFrameNumber() const
446 return m_keyframes.count();
449 int AbstractClipItem::addKeyFrame(const GenTime &pos, const double value)
451 QRectF br = sceneBoundingRect();
452 double maxh = 100.0 / br.height() / m_keyframeFactor;
453 int newval = (br.bottom() - value) * maxh + m_keyframeOffset;
454 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
455 int newpos = (int) pos.frames(m_fps) ;
456 m_keyframes[newpos] = newval;
457 m_selectedKeyframe = newpos;
462 bool AbstractClipItem::hasKeyFrames() const
464 return !m_keyframes.isEmpty();
467 /*QRect AbstractClipItem::visibleRect() {
469 if (scene()->views().size() > 0) {
470 rectInView = scene()->views()[0]->viewport()->rect();
471 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
472 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
473 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
478 CustomTrackScene* AbstractClipItem::projectScene()
481 return static_cast <CustomTrackScene*>(scene());
485 void AbstractClipItem::setItemLocked(bool locked)
490 setFlag(QGraphicsItem::ItemIsMovable, !locked);
491 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
494 bool AbstractClipItem::isItemLocked() const
496 return !(flags() & (QGraphicsItem::ItemIsSelectable));
500 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
502 if (event->modifiers() & Qt::ShiftModifier) {
503 // User want to do a rectangle selection, so ignore the event to pass it to the view
506 QGraphicsItem::mousePressEvent(event);
510 int AbstractClipItem::itemHeight()
515 int AbstractClipItem::itemOffset()
520 void AbstractClipItem::setMainSelectedClip(bool selected)
522 if (selected == m_isMainSelectedClip) return;
523 m_isMainSelectedClip = selected;
527 bool AbstractClipItem::isMainSelectedClip()
529 return m_isMainSelectedClip;