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);
42 #if QT_VERSION >= 0x040600
43 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
47 ItemInfo AbstractClipItem::info() const
49 ItemInfo info = m_info;
50 info.cropStart = cropStart();
51 info.endPos = endPos();
55 int AbstractClipItem::defaultZValue() const
60 GenTime AbstractClipItem::endPos() const
62 return m_info.startPos + m_info.cropDuration;
65 int AbstractClipItem::track() const
70 GenTime AbstractClipItem::cropStart() const
72 return m_info.cropStart;
75 GenTime AbstractClipItem::cropDuration() const
77 return m_info.cropDuration;
80 void AbstractClipItem::setCropStart(GenTime pos)
82 m_info.cropStart = pos;
85 void AbstractClipItem::updateItem()
87 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
88 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
91 void AbstractClipItem::updateRectGeometry()
93 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
96 void AbstractClipItem::resizeStart(int posx)
98 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
99 if (durationDiff == GenTime()) return;
100 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
102 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
103 durationDiff = GenTime() - cropStart();
104 } else if (durationDiff >= cropDuration()) {
106 if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
109 //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
110 m_info.startPos += durationDiff;
112 if (type() == AVWIDGET) {
113 m_info.cropStart += durationDiff;
115 m_info.cropDuration = m_info.cropDuration - durationDiff;
116 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
117 moveBy(durationDiff.frames(m_fps), 0);
118 //setPos(m_startPos.frames(m_fps), pos().y());
119 if ((int) scenePos().x() != posx) {
120 //kDebug()<<"////// WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
121 GenTime diff = GenTime((int) pos().x() - posx, m_fps);
123 if (type() == AVWIDGET) {
124 m_info.cropStart += diff;
126 m_info.cropDuration = m_info.cropDuration - diff;
127 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
128 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
132 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
133 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
135 /* if (durationDiff < GenTime()) {
136 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
137 for (int i = 0; i < collisionList.size(); ++i) {
138 QGraphicsItem *item = collisionList.at(i);
139 if (item->type() == type() && item->pos().x() < pos().x()) {
140 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
141 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
142 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
143 setPos((m_startPos + diff).frames(m_fps), pos().y());
145 if (type() == AVWIDGET) m_cropStart += diff;
146 m_cropDuration = m_cropDuration - diff;
153 void AbstractClipItem::resizeEnd(int posx)
155 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
156 if (durationDiff == GenTime()) return;
157 if (cropDuration() + durationDiff <= GenTime()) {
158 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
161 m_info.cropDuration += durationDiff;
163 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
164 if (durationDiff > GenTime()) {
165 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
166 for (int i = 0; i < collisionList.size(); ++i) {
167 QGraphicsItem *item = collisionList.at(i);
168 if (item->type() == type() && item->pos().x() > pos().x()) {
169 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
170 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
171 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
172 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
173 m_info.cropDuration = diff;
174 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
181 GenTime AbstractClipItem::startPos() const
183 return m_info.startPos;
186 void AbstractClipItem::setTrack(int track)
188 m_info.track = track;
191 double AbstractClipItem::fps() const
196 void AbstractClipItem::updateFps(double fps)
199 setPos((qreal) startPos().frames(m_fps), pos().y());
200 updateRectGeometry();
203 GenTime AbstractClipItem::maxDuration() const
205 return m_maxDuration;
208 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
210 if (m_keyframes.count() < 2) return;
212 double maxw = br.width() / cropDuration().frames(m_fps);
213 double maxh = br.height() / 100.0 * m_keyframeFactor;
219 // draw line showing default value
220 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
224 y1 = br.bottom() - m_keyframeDefault * maxh;
225 QLineF l(x1, y1, x2, y1);
226 QLineF l2 = painter->matrix().map(l);
227 painter->setPen(QColor(168, 168, 168, 180));
228 painter->drawLine(l2);
229 painter->setPen(QColor(108, 108, 108, 180));
230 painter->drawLine(l2.translated(0, 1));
231 painter->setPen(QColor(Qt::white));
235 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
236 QColor color(Qt::blue);
237 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
238 y1 = br.bottom() - i.value() * maxh;
240 while (i != m_keyframes.constEnd()) {
241 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
242 else color = QColor(Qt::blue);
244 if (i == m_keyframes.constEnd()) break;
245 x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
246 y2 = br.bottom() - i.value() * maxh;
247 QLineF l(x1, y1, x2, y2);
248 l2 = painter->matrix().map(l);
249 painter->drawLine(l2);
251 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
252 painter->fillRect(frame, color);
258 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
259 painter->fillRect(frame, color);
263 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
265 const QRectF br = sceneBoundingRect();
266 double maxw = br.width() / cropDuration().frames(m_fps);
267 double maxh = br.height() / 100.0 * m_keyframeFactor;
268 if (m_keyframes.count() > 1) {
269 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
272 while (i != m_keyframes.constEnd()) {
273 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
274 y1 = br.bottom() - i.value() * maxh;
275 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
276 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
278 } else if (x1 > pos.x()) break;
282 setToolTip(QString());
286 void AbstractClipItem::updateSelectedKeyFrame()
288 if (m_editedKeyframe == -1) return;
289 QRectF br = sceneBoundingRect();
290 double maxw = br.width() / cropDuration().frames(m_fps);
291 double maxh = br.height() / 100.0 * m_keyframeFactor;
292 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
293 m_selectedKeyframe = m_editedKeyframe;
294 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
297 int AbstractClipItem::selectedKeyFramePos() const
299 return m_editedKeyframe;
302 double AbstractClipItem::selectedKeyFrameValue() const
304 return m_keyframes[m_editedKeyframe];
307 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
309 if (!m_keyframes.contains(m_selectedKeyframe)) return;
310 int newpos = (int) pos.frames(m_fps);
311 int start = cropStart().frames(m_fps);
312 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
313 newpos = qMax(newpos, start);
314 newpos = qMin(newpos, end);
315 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
316 // remove kexframe if it is dragged outside
317 m_keyframes.remove(m_selectedKeyframe);
318 m_selectedKeyframe = -1;
322 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
323 // remove kexframe if it is dragged outside
324 m_keyframes.remove(m_selectedKeyframe);
325 m_selectedKeyframe = -1;
329 double newval = qMax(value, 0.0);
330 newval = qMin(newval, 100.0);
331 newval = newval / m_keyframeFactor;
332 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
333 m_keyframes[newpos] = (int) newval;
334 m_selectedKeyframe = newpos;
338 double AbstractClipItem::keyFrameFactor() const
340 return m_keyframeFactor;
343 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
345 QRectF br = sceneBoundingRect();
346 double maxh = 100.0 / br.height() / m_keyframeFactor;
347 int newval = (br.bottom() - value) * maxh;
348 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
349 int newpos = (int) pos.frames(m_fps) ;
350 m_keyframes[newpos] = newval;
351 m_selectedKeyframe = newpos;
355 bool AbstractClipItem::hasKeyFrames() const
357 return !m_keyframes.isEmpty();
360 /*QRect AbstractClipItem::visibleRect() {
362 if (scene()->views().size() > 0) {
363 rectInView = scene()->views()[0]->viewport()->rect();
364 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
365 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
366 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
371 CustomTrackScene* AbstractClipItem::projectScene()
373 if (scene()) return static_cast <CustomTrackScene*>(scene());
377 void AbstractClipItem::setItemLocked(bool locked)
381 setFlag(QGraphicsItem::ItemIsMovable, false);
382 setFlag(QGraphicsItem::ItemIsSelectable, false);
384 setFlag(QGraphicsItem::ItemIsMovable, true);
385 setFlag(QGraphicsItem::ItemIsSelectable, true);
389 bool AbstractClipItem::isItemLocked() const
391 return !(flags() & (QGraphicsItem::ItemIsSelectable));
395 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
397 if (event->modifiers() & Qt::ShiftModifier) {
398 // User want to do a rectangle selection, so ignore the event to pass it to the view
400 } else QGraphicsItem::mousePressEvent(event);