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),
43 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
44 #if QT_VERSION >= 0x040600
45 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
46 setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
50 AbstractClipItem::~AbstractClipItem()
54 void AbstractClipItem::closeAnimation()
56 #if QT_VERSION >= 0x040600
57 if (!isEnabled()) return;
59 if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
64 QPropertyAnimation *closeAnimation = new QPropertyAnimation(this, "rect");
65 connect(closeAnimation, SIGNAL(finished()), this, SLOT(deleteLater()));
66 closeAnimation->setDuration(200);
69 r2.setLeft(r.left() + r.width() / 2);
70 r2.setTop(r.top() + r.height() / 2);
73 closeAnimation->setStartValue(r);
74 closeAnimation->setEndValue(r2);
75 closeAnimation->setEasingCurve(QEasingCurve::InQuad);
76 closeAnimation->start(QAbstractAnimation::DeleteWhenStopped);
80 ItemInfo AbstractClipItem::info() const
82 ItemInfo info = m_info;
83 info.cropStart = cropStart();
84 info.endPos = endPos();
88 int AbstractClipItem::defaultZValue() const
93 GenTime AbstractClipItem::endPos() const
95 return m_info.startPos + m_info.cropDuration;
98 int AbstractClipItem::track() const
103 GenTime AbstractClipItem::cropStart() const
105 return m_info.cropStart;
108 GenTime AbstractClipItem::cropDuration() const
110 return m_info.cropDuration;
113 void AbstractClipItem::setCropStart(GenTime pos)
115 m_info.cropStart = pos;
118 void AbstractClipItem::updateItem()
120 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
121 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
124 void AbstractClipItem::updateRectGeometry()
126 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
129 void AbstractClipItem::resizeStart(int posx, bool hasSizeLimit)
131 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
132 if (durationDiff == GenTime()) return;
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 // set to true if crop from start is negative (possible for color clips, images as they have no size limit)
144 bool negCropStart = false;
145 if (type() == AVWIDGET) {
146 m_info.cropStart += durationDiff;
147 if (m_info.cropStart < GenTime())
151 m_info.cropDuration -= durationDiff;
152 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
153 moveBy(durationDiff.frames(m_fps), 0);
155 if (m_info.startPos != GenTime(posx, m_fps)) {
156 //kDebug() << "////// WARNING, DIFF IN XPOS: " << pos().x() << " == " << m_info.startPos.frames(m_fps);
157 GenTime diff = m_info.startPos - GenTime(posx, m_fps);
159 if (type() == AVWIDGET)
160 m_info.cropStart += diff;
162 m_info.cropDuration -= diff;
163 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
164 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
167 // set crop from start to 0 (isn't relevant as this only happens for color clips, images)
169 m_info.cropStart = GenTime();
171 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
172 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
174 /* if (durationDiff < GenTime()) {
175 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
176 for (int i = 0; i < collisionList.size(); ++i) {
177 QGraphicsItem *item = collisionList.at(i);
178 if (item->type() == type() && item->pos().x() < pos().x()) {
179 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
180 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
181 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
182 setPos((m_startPos + diff).frames(m_fps), pos().y());
184 if (type() == AVWIDGET) m_cropStart += diff;
185 m_cropDuration = m_cropDuration - diff;
192 void AbstractClipItem::resizeEnd(int posx)
194 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
195 if (durationDiff == GenTime()) return;
196 if (cropDuration() + durationDiff <= GenTime()) {
197 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
200 m_info.cropDuration += durationDiff;
201 m_info.endPos += durationDiff;
203 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
204 if (durationDiff > GenTime()) {
205 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
206 bool fixItem = false;
207 for (int i = 0; i < collisionList.size(); ++i) {
208 if (!collisionList.at(i)->isEnabled()) continue;
209 QGraphicsItem *item = collisionList.at(i);
210 if (item->type() == type() && item->pos().x() > pos().x()) {
211 //kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
212 //kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
213 //kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
214 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
215 if (fixItem == false || diff < m_info.cropDuration) {
217 m_info.cropDuration = diff;
221 if (fixItem) setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
225 GenTime AbstractClipItem::startPos() const
227 return m_info.startPos;
230 void AbstractClipItem::setTrack(int track)
232 m_info.track = track;
235 double AbstractClipItem::fps() const
240 void AbstractClipItem::updateFps(double fps)
243 setPos((qreal) startPos().frames(m_fps), pos().y());
244 updateRectGeometry();
247 GenTime AbstractClipItem::maxDuration() const
249 return m_maxDuration;
252 void AbstractClipItem::drawKeyFrames(QPainter *painter, bool limitedKeyFrames)
254 if (m_keyframes.count() < 1)
257 double maxw = br.width() / cropDuration().frames(m_fps);
258 double maxh = br.height() / 100.0 * m_keyframeFactor;
259 double start = cropStart().frames(m_fps);
260 double x1, y1, x2, y2;
261 bool antialiasing = painter->renderHints() & QPainter::Antialiasing;
263 // draw line showing default value
264 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
268 if (limitedKeyFrames) {
269 QMap<int, int>::const_iterator end = m_keyframes.constEnd();
271 x2 = x1 + maxw * (end.key() - start);
272 x1 += maxw * (m_keyframes.constBegin().key() - start);
274 y1 = br.bottom() - (m_keyframeDefault - m_keyframeOffset) * maxh;
275 QLineF l(x1, y1, x2, y1);
276 QLineF l2 = painter->worldTransform().map(l);
277 painter->setPen(QColor(168, 168, 168, 180));
278 painter->drawLine(l2);
279 painter->setPen(QColor(108, 108, 108, 180));
280 painter->drawLine(l2.translated(0, 1));
281 painter->setPen(QColor(Qt::white));
282 painter->setRenderHint(QPainter::Antialiasing);
286 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
287 QColor color(Qt::blue);
289 x1 = br.x() + maxw * (i.key() - start);
290 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
294 // make sure line begins with clip beginning
295 if (!limitedKeyFrames && i.key() != start) {
296 QLineF l(br.x(), y1, x1, y1);
297 l2 = painter->worldTransform().map(l);
298 painter->drawLine(l2);
300 while (i != m_keyframes.constEnd()) {
301 if (i.key() == m_editedKeyframe)
302 color = QColor(Qt::red);
304 color = QColor(Qt::blue);
306 if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
309 if (m_keyframes.count() == 1) {
313 x2 = br.x() + maxw * (i.key() - start);
314 y2 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
316 QLineF l(x1, y1, x2, y2);
317 l2 = painter->worldTransform().map(l);
318 painter->drawLine(l2);
320 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
321 painter->fillRect(frame, color);
327 // make sure line ends at clip end
328 if (!limitedKeyFrames && x1 != br.right()) {
329 QLineF l(x1, y1, br.right(), y1);
330 painter->drawLine(painter->worldTransform().map(l));
333 if (active && m_keyframes.count() > 1) {
334 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
335 painter->fillRect(frame, color);
338 painter->setRenderHint(QPainter::Antialiasing, antialiasing);
341 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
343 const QRectF br = sceneBoundingRect();
344 double maxw = br.width() / cropDuration().frames(m_fps);
345 double maxh = br.height() / 100.0 * m_keyframeFactor;
346 if (m_keyframes.count() > 0) {
347 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
350 while (i != m_keyframes.constEnd()) {
351 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
352 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
353 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
354 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + ']');
356 } else if (x1 > pos.x()) {
362 setToolTip(QString());
366 void AbstractClipItem::updateSelectedKeyFrame()
368 if (m_editedKeyframe == -1)
370 QRectF br = sceneBoundingRect();
371 double maxw = br.width() / cropDuration().frames(m_fps);
372 double maxh = br.height() / 100.0 * m_keyframeFactor;
373 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
374 m_selectedKeyframe = m_editedKeyframe;
375 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
378 int AbstractClipItem::editedKeyFramePos() const
380 return m_editedKeyframe;
383 double AbstractClipItem::editedKeyFrameValue() const
385 return m_keyframes.value(m_editedKeyframe);
388 int AbstractClipItem::selectedKeyFramePos() const
390 return m_selectedKeyframe;
393 double AbstractClipItem::selectedKeyFrameValue() const
395 return m_keyframes.value(m_selectedKeyframe);
398 void AbstractClipItem::updateKeyFramePos(const GenTime &pos, const double value)
400 if (!m_keyframes.contains(m_editedKeyframe))
402 int newpos = (int) pos.frames(m_fps);
403 int min = (int) cropStart().frames(m_fps) - 1;
404 int max = (int)(cropStart() + cropDuration()).frames(m_fps);
405 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
406 while (i.key() < m_editedKeyframe) {
407 min = qMax(i.key(), min);
410 i = m_keyframes.constEnd() - 1;
411 while (i.key() > m_editedKeyframe) {
412 max = qMin(i.key(), max);
420 double newval = qMax(value, 0.0);
421 newval = qMin(newval, 100.0);
422 newval = newval / m_keyframeFactor + m_keyframeOffset;
423 if (m_editedKeyframe != newpos)
424 m_keyframes.remove(m_editedKeyframe);
425 m_keyframes[newpos] = (int) newval;
426 m_editedKeyframe = newpos;
430 double AbstractClipItem::keyFrameFactor() const
432 return m_keyframeFactor;
435 int AbstractClipItem::keyFrameNumber() const
437 return m_keyframes.count();
440 int AbstractClipItem::addKeyFrame(const GenTime &pos, const double value)
442 QRectF br = sceneBoundingRect();
443 double maxh = 100.0 / br.height() / m_keyframeFactor;
444 int newval = (br.bottom() - value) * maxh + m_keyframeOffset;
445 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
446 int newpos = (int) pos.frames(m_fps) ;
447 m_keyframes[newpos] = newval;
448 m_selectedKeyframe = newpos;
453 bool AbstractClipItem::hasKeyFrames() const
455 return !m_keyframes.isEmpty();
458 /*QRect AbstractClipItem::visibleRect() {
460 if (scene()->views().size() > 0) {
461 rectInView = scene()->views()[0]->viewport()->rect();
462 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
463 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
464 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
469 CustomTrackScene* AbstractClipItem::projectScene()
472 return static_cast <CustomTrackScene*>(scene());
476 void AbstractClipItem::setItemLocked(bool locked)
481 setFlag(QGraphicsItem::ItemIsMovable, !locked);
482 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
485 bool AbstractClipItem::isItemLocked() const
487 return !(flags() & (QGraphicsItem::ItemIsSelectable));
491 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
493 if (event->modifiers() & Qt::ShiftModifier) {
494 // User want to do a rectangle selection, so ignore the event to pass it to the view
497 QGraphicsItem::mousePressEvent(event);
501 int AbstractClipItem::itemHeight()
506 int AbstractClipItem::itemOffset()