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"
30 #include <QGraphicsSceneMouseEvent>
32 AbstractClipItem::AbstractClipItem(const ItemInfo info, const QRectF& rect, double fps) :
34 QGraphicsRectItem(rect),
37 m_selectedKeyframe(0),
41 #if QT_VERSION >= 0x040600
42 , m_closeAnimation(NULL)
45 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
46 #if QT_VERSION >= 0x040600
47 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
48 setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
52 AbstractClipItem::~AbstractClipItem()
54 #if QT_VERSION >= 0x040600
55 if (m_closeAnimation) delete m_closeAnimation;
59 void AbstractClipItem::closeAnimation()
61 #if QT_VERSION >= 0x040600
62 if (m_closeAnimation) return;
64 m_closeAnimation = new QPropertyAnimation(this, "rect");
65 connect(m_closeAnimation, SIGNAL(finished()), this, SLOT(deleteLater()));
66 m_closeAnimation->setDuration(200);
69 r2.setLeft(r.left() + r.width() / 2);
70 r2.setTop(r.top() + r.height() / 2);
73 m_closeAnimation->setStartValue(r);
74 m_closeAnimation->setEndValue(r2);
75 m_closeAnimation->setEasingCurve(QEasingCurve::InQuad);
76 m_closeAnimation->start();
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 for (int i = 0; i < collisionList.size(); ++i) {
207 if (!collisionList.at(i)->isEnabled()) continue;
208 QGraphicsItem *item = collisionList.at(i);
209 if (item->type() == type() && item->pos().x() > pos().x()) {
210 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
211 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
212 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
213 GenTime diff = ((AbstractClipItem *)item)->startPos() - startPos();
214 m_info.cropDuration = diff;
215 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
222 GenTime AbstractClipItem::startPos() const
224 return m_info.startPos;
227 void AbstractClipItem::setTrack(int track)
229 m_info.track = track;
232 double AbstractClipItem::fps() const
237 void AbstractClipItem::updateFps(double fps)
240 setPos((qreal) startPos().frames(m_fps), pos().y());
241 updateRectGeometry();
244 GenTime AbstractClipItem::maxDuration() const
246 return m_maxDuration;
249 void AbstractClipItem::drawKeyFrames(QPainter *painter, bool limitedKeyFrames)
251 if (m_keyframes.count() < 1)
254 double maxw = br.width() / cropDuration().frames(m_fps);
255 double maxh = br.height() / 100.0 * m_keyframeFactor;
256 double start = cropStart().frames(m_fps);
257 double x1, y1, x2, y2;
258 bool antialiasing = painter->renderHints() & QPainter::Antialiasing;
260 // draw line showing default value
261 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
265 if (limitedKeyFrames) {
266 QMap<int, int>::const_iterator end = m_keyframes.constEnd();
268 x2 = x1 + maxw * (end.key() - start);
269 x1 += maxw * (m_keyframes.constBegin().key() - start);
271 y1 = br.bottom() - (m_keyframeDefault - m_keyframeOffset) * maxh;
272 QLineF l(x1, y1, x2, y1);
273 QLineF l2 = painter->matrix().map(l);
274 painter->setPen(QColor(168, 168, 168, 180));
275 painter->drawLine(l2);
276 painter->setPen(QColor(108, 108, 108, 180));
277 painter->drawLine(l2.translated(0, 1));
278 painter->setPen(QColor(Qt::white));
279 painter->setRenderHint(QPainter::Antialiasing);
283 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
284 QColor color(Qt::blue);
286 x1 = br.x() + maxw * (i.key() - start);
287 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
291 // make sure line begins with clip beginning
292 if (!limitedKeyFrames && i.key() != start) {
293 QLineF l(br.x(), y1, x1, y1);
294 l2 = painter->matrix().map(l);
295 painter->drawLine(l2);
297 while (i != m_keyframes.constEnd()) {
298 if (i.key() == m_editedKeyframe)
299 color = QColor(Qt::red);
301 color = QColor(Qt::blue);
303 if (i == m_keyframes.constEnd() && m_keyframes.count() != 1)
306 if (m_keyframes.count() == 1) {
310 x2 = br.x() + maxw * (i.key() - start);
311 y2 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
313 QLineF l(x1, y1, x2, y2);
314 l2 = painter->matrix().map(l);
315 painter->drawLine(l2);
317 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
318 painter->fillRect(frame, color);
324 // make sure line ends at clip end
325 if (!limitedKeyFrames && x1 != br.right()) {
326 QLineF l(x1, y1, br.right(), y1);
327 painter->drawLine(painter->matrix().map(l));
330 if (active && m_keyframes.count() > 1) {
331 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
332 painter->fillRect(frame, color);
335 painter->setRenderHint(QPainter::Antialiasing, antialiasing);
338 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
340 const QRectF br = sceneBoundingRect();
341 double maxw = br.width() / cropDuration().frames(m_fps);
342 double maxh = br.height() / 100.0 * m_keyframeFactor;
343 if (m_keyframes.count() > 0) {
344 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
347 while (i != m_keyframes.constEnd()) {
348 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
349 y1 = br.bottom() - (i.value() - m_keyframeOffset) * maxh;
350 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
351 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "]");
353 } else if (x1 > pos.x()) {
359 setToolTip(QString());
363 void AbstractClipItem::updateSelectedKeyFrame()
365 if (m_editedKeyframe == -1)
367 QRectF br = sceneBoundingRect();
368 double maxw = br.width() / cropDuration().frames(m_fps);
369 double maxh = br.height() / 100.0 * m_keyframeFactor;
370 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - (m_keyframes.value(m_selectedKeyframe) - m_keyframeOffset) * maxh - 3, 12, 12);
371 m_selectedKeyframe = m_editedKeyframe;
372 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 int AbstractClipItem::editedKeyFramePos() const
377 return m_editedKeyframe;
380 double AbstractClipItem::editedKeyFrameValue() const
382 return m_keyframes.value(m_editedKeyframe);
385 int AbstractClipItem::selectedKeyFramePos() const
387 return m_selectedKeyframe;
390 double AbstractClipItem::selectedKeyFrameValue() const
392 return m_keyframes.value(m_selectedKeyframe);
395 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
397 if (!m_keyframes.contains(m_editedKeyframe))
399 int newpos = (int) pos.frames(m_fps);
400 int min = (int) cropStart().frames(m_fps) - 1;
401 int max = (int)(cropStart() + cropDuration()).frames(m_fps);
402 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
403 while (i.key() < m_editedKeyframe) {
404 min = qMax(i.key(), min);
407 i = m_keyframes.constEnd() - 1;
408 while (i.key() > m_editedKeyframe) {
409 max = qMin(i.key(), max);
417 double newval = qMax(value, 0.0);
418 newval = qMin(newval, 100.0);
419 newval = newval / m_keyframeFactor + m_keyframeOffset;
420 if (m_editedKeyframe != newpos)
421 m_keyframes.remove(m_editedKeyframe);
422 m_keyframes[newpos] = (int) newval;
423 m_editedKeyframe = newpos;
427 double AbstractClipItem::keyFrameFactor() const
429 return m_keyframeFactor;
432 int AbstractClipItem::keyFrameNumber() const
434 return m_keyframes.count();
437 int AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
439 QRectF br = sceneBoundingRect();
440 double maxh = 100.0 / br.height() / m_keyframeFactor;
441 int newval = (br.bottom() - value) * maxh + m_keyframeOffset;
442 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
443 int newpos = (int) pos.frames(m_fps) ;
444 m_keyframes[newpos] = newval;
445 m_selectedKeyframe = newpos;
450 bool AbstractClipItem::hasKeyFrames() const
452 return !m_keyframes.isEmpty();
455 /*QRect AbstractClipItem::visibleRect() {
457 if (scene()->views().size() > 0) {
458 rectInView = scene()->views()[0]->viewport()->rect();
459 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
460 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
461 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
466 CustomTrackScene* AbstractClipItem::projectScene()
469 return static_cast <CustomTrackScene*>(scene());
473 void AbstractClipItem::setItemLocked(bool locked)
478 setFlag(QGraphicsItem::ItemIsMovable, !locked);
479 setFlag(QGraphicsItem::ItemIsSelectable, !locked);
482 bool AbstractClipItem::isItemLocked() const
484 return !(flags() & (QGraphicsItem::ItemIsSelectable));
488 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
490 if (event->modifiers() & Qt::ShiftModifier) {
491 // User want to do a rectangle selection, so ignore the event to pass it to the view
494 QGraphicsItem::mousePressEvent(event);
498 int AbstractClipItem::itemHeight()
503 int AbstractClipItem::itemOffset()