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>
33 AbstractClipItem::AbstractClipItem(const ItemInfo &info, const QRectF& rect, double fps) :
35 QGraphicsRectItem(rect),
38 m_selectedKeyframe(0),
42 m_isMainSelectedClip(false)
44 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
45 #if QT_VERSION >= 0x040600
46 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
47 setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
51 AbstractClipItem::~AbstractClipItem()
55 void AbstractClipItem::closeAnimation()
57 #if QT_VERSION >= 0x040600
58 if (!isEnabled()) return;
60 if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
65 QPropertyAnimation *closeAnimation = new QPropertyAnimation(this, "rect");
66 connect(closeAnimation, SIGNAL(finished()), this, SLOT(deleteLater()));
67 closeAnimation->setDuration(200);
70 r2.setLeft(r.left() + r.width() / 2);
71 r2.setTop(r.top() + r.height() / 2);
74 closeAnimation->setStartValue(r);
75 closeAnimation->setEndValue(r2);
76 closeAnimation->setEasingCurve(QEasingCurve::InQuad);
77 closeAnimation->start(QAbstractAnimation::DeleteWhenStopped);
81 ItemInfo AbstractClipItem::info() const
83 ItemInfo info = m_info;
84 info.cropStart = cropStart();
85 info.endPos = endPos();
89 int AbstractClipItem::defaultZValue() const
94 GenTime AbstractClipItem::endPos() const
96 return m_info.startPos + m_info.cropDuration;
99 int AbstractClipItem::track() const
104 GenTime AbstractClipItem::cropStart() const
106 return m_info.cropStart;
109 GenTime AbstractClipItem::cropDuration() const
111 return m_info.cropDuration;
114 void AbstractClipItem::setCropStart(GenTime pos)
116 m_info.cropStart = pos;
119 void AbstractClipItem::updateItem()
121 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
122 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
125 void AbstractClipItem::updateRectGeometry()
127 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
130 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit, bool /*emitChange*/)
132 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
133 if (durationDiff == GenTime()) return;
135 if (type() == AVWIDGET && hasSizeLimit && (cropStart() + durationDiff < GenTime())) {
136 durationDiff = GenTime() - cropStart();
137 } else if (durationDiff >= cropDuration()) {
139 /*if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
142 m_info.startPos += durationDiff;
144 // set to true if crop from start is negative (possible for color clips, images as they have no size limit)
145 bool negCropStart = false;
146 if (type() == AVWIDGET) {
147 m_info.cropStart += durationDiff;
148 if (m_info.cropStart < GenTime())
152 m_info.cropDuration -= durationDiff;
153 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
154 moveBy(durationDiff.frames(m_fps), 0);
156 if (m_info.startPos != GenTime(posx, m_fps)) {
157 //kDebug() << "////// WARNING, DIFF IN XPOS: " << pos().x() << " == " << m_info.startPos.frames(m_fps);
158 GenTime diff = m_info.startPos - GenTime(posx, m_fps);
160 if (type() == AVWIDGET)
161 m_info.cropStart += diff;
163 m_info.cropDuration -= diff;
164 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
165 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
168 // set crop from start to 0 (isn't relevant as this only happens for color clips, images)
170 m_info.cropStart = GenTime();
172 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
173 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
175 /* if (durationDiff < GenTime()) {
176 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
177 for (int i = 0; i < collisionList.size(); ++i) {
178 QGraphicsItem *item = collisionList.at(i);
179 if (item->type() == type() && item->pos().x() < pos().x()) {
180 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
181 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
182 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
183 setPos((m_startPos + diff).frames(m_fps), pos().y());
185 if (type() == AVWIDGET) m_cropStart += diff;
186 m_cropDuration = m_cropDuration - diff;
193 void AbstractClipItem::resizeEnd(int posx, bool /*emitChange*/)
195 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
196 if (durationDiff == GenTime()) return;
197 if (cropDuration() + durationDiff <= GenTime()) {
198 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
201 m_info.cropDuration += durationDiff;
202 m_info.endPos += durationDiff;
204 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
205 if (durationDiff > GenTime()) {
206 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
207 bool fixItem = false;
208 for (int i = 0; i < collisionList.size(); ++i) {
209 if (!collisionList.at(i)->isEnabled()) continue;
210 QGraphicsItem *item = collisionList.at(i);
211 if (item->type() == type() && item->pos().x() > pos().x()) {
212 //kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
213 //kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
214 //kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
215 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
216 if (fixItem == false || diff < m_info.cropDuration) {
218 m_info.cropDuration = diff;
222 if (fixItem) setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
226 GenTime AbstractClipItem::startPos() const
228 return m_info.startPos;
231 void AbstractClipItem::setTrack(int track)
233 m_info.track = track;
236 double AbstractClipItem::fps() const
241 void AbstractClipItem::updateFps(double fps)
244 setPos((qreal) startPos().frames(m_fps), pos().y());
245 updateRectGeometry();
248 GenTime AbstractClipItem::maxDuration() const
250 return m_maxDuration;
253 void AbstractClipItem::drawKeyFrames(QPainter *painter, bool limitedKeyFrames)
255 if (m_keyframes.count() < 1)
258 double maxw = br.width() / cropDuration().frames(m_fps);
259 double maxh = br.height() / 100.0 * m_keyframeFactor;
260 double start = cropStart().frames(m_fps);
261 double x1, y1, x2, y2;
262 bool antialiasing = painter->renderHints() & QPainter::Antialiasing;
264 // draw line showing default value
265 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
269 if (limitedKeyFrames) {
270 QMap<int, int>::const_iterator end = m_keyframes.constEnd();
272 x2 = x1 + maxw * (end.key() - start);
273 x1 += maxw * (m_keyframes.constBegin().key() - start);
275 y1 = br.bottom() - (m_keyframeDefault - m_keyframeOffset) * maxh;
276 QLineF l(x1, y1, x2, y1);
277 QLineF l2 = painter->worldTransform().map(l);
278 painter->setPen(QColor(168, 168, 168, 180));
279 painter->drawLine(l2);
280 painter->setPen(QColor(108, 108, 108, 180));
281 painter->drawLine(l2.translated(0, 1));
282 painter->setPen(QColor(Qt::white));
283 painter->setRenderHint(QPainter::Antialiasing);
287 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
288 QColor color(Qt::blue);
290 x1 = br.x() + maxw * (i.key() - start);
291 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
295 // make sure line begins with clip beginning
296 if (!limitedKeyFrames && i.key() != start) {
297 QLineF l(br.x(), y1, x1, y1);
298 l2 = painter->worldTransform().map(l);
299 painter->drawLine(l2);
301 while (i != m_keyframes.constEnd()) {
302 if (i.key() == m_editedKeyframe)
303 color = QColor(Qt::red);
305 color = QColor(Qt::blue);
307 if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
310 if (m_keyframes.count() == 1) {
314 x2 = br.x() + maxw * (i.key() - start);
315 y2 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
317 QLineF l(x1, y1, x2, y2);
318 l2 = painter->worldTransform().map(l);
319 painter->drawLine(l2);
321 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
322 painter->fillRect(frame, color);
328 // make sure line ends at clip end
329 if (!limitedKeyFrames && x1 != br.right()) {
330 QLineF l(x1, y1, br.right(), y1);
331 painter->drawLine(painter->worldTransform().map(l));
334 if (active && m_keyframes.count() > 1) {
335 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
336 painter->fillRect(frame, color);
339 painter->setRenderHint(QPainter::Antialiasing, antialiasing);
342 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
344 const QRectF br = sceneBoundingRect();
345 double maxw = br.width() / cropDuration().frames(m_fps);
346 double maxh = br.height() / 100.0 * m_keyframeFactor;
347 if (m_keyframes.count() > 0) {
348 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
351 while (i != m_keyframes.constEnd()) {
352 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
353 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
354 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
355 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + ']');
357 } else if (x1 > pos.x()) {
363 setToolTip(QString());
367 void AbstractClipItem::updateSelectedKeyFrame()
369 if (m_editedKeyframe == -1)
371 QRectF br = sceneBoundingRect();
372 double maxw = br.width() / cropDuration().frames(m_fps);
373 double maxh = br.height() / 100.0 * m_keyframeFactor;
374 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
375 m_selectedKeyframe = m_editedKeyframe;
376 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
379 int AbstractClipItem::editedKeyFramePos() const
381 return m_editedKeyframe;
384 double AbstractClipItem::editedKeyFrameValue() const
386 return m_keyframes.value(m_editedKeyframe);
389 int AbstractClipItem::selectedKeyFramePos() const
391 return m_selectedKeyframe;
394 double AbstractClipItem::selectedKeyFrameValue() const
396 return m_keyframes.value(m_selectedKeyframe);
399 void AbstractClipItem::updateKeyFramePos(const GenTime &pos, const double value)
401 if (!m_keyframes.contains(m_editedKeyframe))
403 int newpos = (int) pos.frames(m_fps);
404 int min = (int) cropStart().frames(m_fps) - 1;
405 int max = (int)(cropStart() + cropDuration()).frames(m_fps);
406 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
407 while (i.key() < m_editedKeyframe) {
408 min = qMax(i.key(), min);
411 i = m_keyframes.constEnd() - 1;
412 while (i.key() > m_editedKeyframe) {
413 max = qMin(i.key(), max);
421 double newval = qMax(value, 0.0);
422 newval = qMin(newval, 100.0);
423 newval = newval / m_keyframeFactor + m_keyframeOffset;
424 if (m_editedKeyframe != newpos)
425 m_keyframes.remove(m_editedKeyframe);
426 m_keyframes[newpos] = (int) newval;
427 m_editedKeyframe = newpos;
431 double AbstractClipItem::keyFrameFactor() const
433 return m_keyframeFactor;
436 int AbstractClipItem::keyFrameNumber() const
438 return m_keyframes.count();
441 int AbstractClipItem::addKeyFrame(const GenTime &pos, const double value)
443 QRectF br = sceneBoundingRect();
444 double maxh = 100.0 / br.height() / m_keyframeFactor;
445 int newval = (br.bottom() - value) * maxh + m_keyframeOffset;
446 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
447 int newpos = (int) pos.frames(m_fps) ;
448 m_keyframes[newpos] = newval;
449 m_selectedKeyframe = newpos;
454 bool AbstractClipItem::hasKeyFrames() const
456 return !m_keyframes.isEmpty();
459 /*QRect AbstractClipItem::visibleRect() {
461 if (scene()->views().size() > 0) {
462 rectInView = scene()->views()[0]->viewport()->rect();
463 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
464 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
465 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
470 CustomTrackScene* AbstractClipItem::projectScene()
473 return static_cast <CustomTrackScene*>(scene());
477 void AbstractClipItem::setItemLocked(bool locked)
482 setFlag(QGraphicsItem::ItemIsMovable, !locked);
483 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
486 bool AbstractClipItem::isItemLocked() const
488 return !(flags() & (QGraphicsItem::ItemIsSelectable));
492 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
494 if (event->modifiers() & Qt::ShiftModifier) {
495 // User want to do a rectangle selection, so ignore the event to pass it to the view
498 QGraphicsItem::mousePressEvent(event);
502 int AbstractClipItem::itemHeight()
507 int AbstractClipItem::itemOffset()
512 void AbstractClipItem::setMainSelectedClip(bool selected)
514 if (selected == m_isMainSelectedClip) return;
515 m_isMainSelectedClip = selected;
519 bool AbstractClipItem::isMainSelectedClip()
521 return m_isMainSelectedClip;