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 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
44 ItemInfo AbstractClipItem::info() const
46 ItemInfo info = m_info;
47 info.cropStart = cropStart();
48 info.endPos = endPos();
52 GenTime AbstractClipItem::endPos() const
54 return m_info.startPos + m_info.cropDuration;
57 int AbstractClipItem::track() const
62 GenTime AbstractClipItem::cropStart() const
64 return m_info.cropStart;
67 GenTime AbstractClipItem::cropDuration() const
69 return m_info.cropDuration;
72 void AbstractClipItem::setCropStart(GenTime pos)
74 m_info.cropStart = pos;
77 void AbstractClipItem::updateItem()
79 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
80 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
83 void AbstractClipItem::updateRectGeometry()
85 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
88 void AbstractClipItem::resizeStart(int posx, double speed)
90 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
91 if (durationDiff == GenTime()) return;
92 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
94 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
95 durationDiff = GenTime() - cropStart();
96 } else if (durationDiff >= cropDuration()) {
98 if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
101 //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
102 m_info.startPos += durationDiff;
103 GenTime originalDiff = GenTime((int)(durationDiff.frames(m_fps) * speed + 0.5), m_fps);
105 if (type() == AVWIDGET) {
106 m_info.cropStart += durationDiff;
107 m_info.originalcropStart += originalDiff;
109 m_info.cropDuration = m_info.cropDuration - originalDiff;
111 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
112 moveBy(durationDiff.frames(m_fps), 0);
113 //setPos(m_startPos.frames(m_fps), pos().y());
114 if ((int) scenePos().x() != posx) {
115 //kDebug()<<"////// WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
116 GenTime diff = GenTime((int) pos().x() - posx, m_fps);
117 GenTime originalDiff = GenTime((int)(diff.frames(m_fps) * speed + 0.5), m_fps);
118 if (type() == AVWIDGET) {
119 m_info.cropStart += diff;
120 m_info.originalcropStart += originalDiff;
122 m_info.cropDuration = m_info.cropDuration - originalDiff;
123 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
124 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
128 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
129 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
131 /* if (durationDiff < GenTime()) {
132 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
133 for (int i = 0; i < collisionList.size(); ++i) {
134 QGraphicsItem *item = collisionList.at(i);
135 if (item->type() == type() && item->pos().x() < pos().x()) {
136 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
137 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
138 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
139 setPos((m_startPos + diff).frames(m_fps), pos().y());
141 if (type() == AVWIDGET) m_cropStart += diff;
142 m_cropDuration = m_cropDuration - diff;
149 void AbstractClipItem::resizeEnd(int posx, double speed)
151 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
152 if (durationDiff == GenTime()) return;
153 //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
154 if (cropDuration() + durationDiff <= GenTime()) {
155 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
157 //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
158 m_info.cropDuration += GenTime((int)(durationDiff.frames(m_fps) * speed + 0.5), m_fps);
159 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
160 if (durationDiff > GenTime()) {
161 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
162 for (int i = 0; i < collisionList.size(); ++i) {
163 QGraphicsItem *item = collisionList.at(i);
164 if (item->type() == type() && item->pos().x() > pos().x()) {
165 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
166 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
167 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
168 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
169 m_info.cropDuration = GenTime((int)(diff.frames(m_fps) * speed + 0.5), m_fps);
170 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
177 GenTime AbstractClipItem::startPos() const
179 return m_info.startPos;
182 void AbstractClipItem::setTrack(int track)
184 m_info.track = track;
187 double AbstractClipItem::fps() const
192 void AbstractClipItem::updateFps(double fps)
195 setPos((qreal) startPos().frames(m_fps), pos().y());
196 updateRectGeometry();
199 GenTime AbstractClipItem::maxDuration() const
201 return m_maxDuration;
204 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
206 if (m_keyframes.count() < 2) return;
208 double maxw = br.width() / cropDuration().frames(m_fps);
209 double maxh = br.height() / 100.0 * m_keyframeFactor;
215 // draw line showing default value
216 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
220 y1 = br.bottom() - m_keyframeDefault * maxh;
221 QLineF l(x1, y1, x2, y1);
222 QLineF l2 = painter->matrix().map(l);
223 painter->setPen(QColor(168, 168, 168, 180));
224 painter->drawLine(l2);
225 painter->setPen(QColor(108, 108, 108, 180));
226 painter->drawLine(l2.translated(0, 1));
227 painter->setPen(QColor(Qt::white));
231 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
232 QColor color(Qt::blue);
233 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
234 y1 = br.bottom() - i.value() * maxh;
236 while (i != m_keyframes.constEnd()) {
237 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
238 else color = QColor(Qt::blue);
240 if (i == m_keyframes.constEnd()) break;
241 x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
242 y2 = br.bottom() - i.value() * maxh;
243 QLineF l(x1, y1, x2, y2);
244 l2 = painter->matrix().map(l);
245 painter->drawLine(l2);
247 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
248 painter->fillRect(frame, color);
254 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
255 painter->fillRect(frame, color);
259 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
261 const QRectF br = sceneBoundingRect();
262 double maxw = br.width() / cropDuration().frames(m_fps);
263 double maxh = br.height() / 100.0 * m_keyframeFactor;
264 if (m_keyframes.count() > 1) {
265 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
268 while (i != m_keyframes.constEnd()) {
269 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
270 y1 = br.bottom() - i.value() * maxh;
271 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
272 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
274 } else if (x1 > pos.x()) break;
278 setToolTip(QString());
282 void AbstractClipItem::updateSelectedKeyFrame()
284 if (m_editedKeyframe == -1) return;
285 QRectF br = sceneBoundingRect();
286 double maxw = br.width() / cropDuration().frames(m_fps);
287 double maxh = br.height() / 100.0 * m_keyframeFactor;
288 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
289 m_selectedKeyframe = m_editedKeyframe;
290 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
293 int AbstractClipItem::selectedKeyFramePos() const
295 return m_editedKeyframe;
298 double AbstractClipItem::selectedKeyFrameValue() const
300 return m_keyframes[m_editedKeyframe];
303 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
305 if (!m_keyframes.contains(m_selectedKeyframe)) return;
306 int newpos = (int) pos.frames(m_fps);
307 int start = cropStart().frames(m_fps);
308 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
309 newpos = qMax(newpos, start);
310 newpos = qMin(newpos, end);
311 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
312 // remove kexframe if it is dragged outside
313 m_keyframes.remove(m_selectedKeyframe);
314 m_selectedKeyframe = -1;
318 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
319 // remove kexframe if it is dragged outside
320 m_keyframes.remove(m_selectedKeyframe);
321 m_selectedKeyframe = -1;
325 double newval = qMax(value, 0.0);
326 newval = qMin(newval, 100.0);
327 newval = newval / m_keyframeFactor;
328 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
329 m_keyframes[newpos] = (int) newval;
330 m_selectedKeyframe = newpos;
334 double AbstractClipItem::keyFrameFactor() const
336 return m_keyframeFactor;
339 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
341 QRectF br = sceneBoundingRect();
342 double maxh = 100.0 / br.height() / m_keyframeFactor;
343 int newval = (br.bottom() - value) * maxh;
344 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
345 int newpos = (int) pos.frames(m_fps) ;
346 m_keyframes[newpos] = newval;
347 m_selectedKeyframe = newpos;
351 bool AbstractClipItem::hasKeyFrames() const
353 return !m_keyframes.isEmpty();
356 /*QRect AbstractClipItem::visibleRect() {
358 if (scene()->views().size() > 0) {
359 rectInView = scene()->views()[0]->viewport()->rect();
360 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
361 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
362 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
367 CustomTrackScene* AbstractClipItem::projectScene()
369 if (scene()) return static_cast <CustomTrackScene*>(scene());
373 void AbstractClipItem::setItemLocked(bool locked)
377 setFlag(QGraphicsItem::ItemIsMovable, false);
378 setFlag(QGraphicsItem::ItemIsSelectable, false);
379 } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
382 bool AbstractClipItem::isItemLocked() const
384 return !(flags() & (QGraphicsItem::ItemIsSelectable));
388 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
390 if (event->modifiers() & Qt::ShiftModifier) {
391 // User want to do a rectangle selection, so ignore the event to pass it to the view
393 } else QGraphicsItem::mousePressEvent(event);