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 GenTime AbstractClipItem::endPos() const
57 return m_info.startPos + m_info.cropDuration;
60 int AbstractClipItem::track() const
65 GenTime AbstractClipItem::cropStart() const
67 return m_info.cropStart;
70 GenTime AbstractClipItem::cropDuration() const
72 return m_info.cropDuration;
75 void AbstractClipItem::setCropStart(GenTime pos)
77 m_info.cropStart = pos;
80 void AbstractClipItem::updateItem()
82 m_info.track = (int)(scenePos().y() / KdenliveSettings::trackheight());
83 m_info.startPos = GenTime((int) scenePos().x(), m_fps);
86 void AbstractClipItem::updateRectGeometry()
88 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
91 void AbstractClipItem::resizeStart(int posx)
93 GenTime durationDiff = GenTime(posx, m_fps) - m_info.startPos;
94 if (durationDiff == GenTime()) return;
95 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
97 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
98 durationDiff = GenTime() - cropStart();
99 } else if (durationDiff >= cropDuration()) {
101 if (cropDuration() > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
104 //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
105 m_info.startPos += durationDiff;
107 if (type() == AVWIDGET) {
108 m_info.cropStart += durationDiff;
110 m_info.cropDuration = m_info.cropDuration - durationDiff;
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);
118 if (type() == AVWIDGET) {
119 m_info.cropStart += diff;
121 m_info.cropDuration = m_info.cropDuration - diff;
122 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
123 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
127 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
128 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
130 /* if (durationDiff < GenTime()) {
131 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
132 for (int i = 0; i < collisionList.size(); ++i) {
133 QGraphicsItem *item = collisionList.at(i);
134 if (item->type() == type() && item->pos().x() < pos().x()) {
135 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
136 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
137 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
138 setPos((m_startPos + diff).frames(m_fps), pos().y());
140 if (type() == AVWIDGET) m_cropStart += diff;
141 m_cropDuration = m_cropDuration - diff;
148 void AbstractClipItem::resizeEnd(int posx)
150 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
151 if (durationDiff == GenTime()) return;
152 if (cropDuration() + durationDiff <= GenTime()) {
153 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
156 m_info.cropDuration += durationDiff;
158 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
159 if (durationDiff > GenTime()) {
160 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
161 for (int i = 0; i < collisionList.size(); ++i) {
162 QGraphicsItem *item = collisionList.at(i);
163 if (item->type() == type() && item->pos().x() > pos().x()) {
164 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
165 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
166 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
167 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
168 m_info.cropDuration = diff;
169 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
176 GenTime AbstractClipItem::startPos() const
178 return m_info.startPos;
181 void AbstractClipItem::setTrack(int track)
183 m_info.track = track;
186 double AbstractClipItem::fps() const
191 void AbstractClipItem::updateFps(double fps)
194 setPos((qreal) startPos().frames(m_fps), pos().y());
195 updateRectGeometry();
198 GenTime AbstractClipItem::maxDuration() const
200 return m_maxDuration;
203 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
205 if (m_keyframes.count() < 2) return;
207 double maxw = br.width() / cropDuration().frames(m_fps);
208 double maxh = br.height() / 100.0 * m_keyframeFactor;
214 // draw line showing default value
215 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
219 y1 = br.bottom() - m_keyframeDefault * maxh;
220 QLineF l(x1, y1, x2, y1);
221 QLineF l2 = painter->matrix().map(l);
222 painter->setPen(QColor(168, 168, 168, 180));
223 painter->drawLine(l2);
224 painter->setPen(QColor(108, 108, 108, 180));
225 painter->drawLine(l2.translated(0, 1));
226 painter->setPen(QColor(Qt::white));
230 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
231 QColor color(Qt::blue);
232 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
233 y1 = br.bottom() - i.value() * maxh;
235 while (i != m_keyframes.constEnd()) {
236 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
237 else color = QColor(Qt::blue);
239 if (i == m_keyframes.constEnd()) break;
240 x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
241 y2 = br.bottom() - i.value() * maxh;
242 QLineF l(x1, y1, x2, y2);
243 l2 = painter->matrix().map(l);
244 painter->drawLine(l2);
246 const QRectF frame(l2.x1() - 3, l2.y1() - 3, 6, 6);
247 painter->fillRect(frame, color);
253 const QRectF frame(l2.x2() - 3, l2.y2() - 3, 6, 6);
254 painter->fillRect(frame, color);
258 int AbstractClipItem::mouseOverKeyFrames(QPointF pos, double maxOffset)
260 const QRectF br = sceneBoundingRect();
261 double maxw = br.width() / cropDuration().frames(m_fps);
262 double maxh = br.height() / 100.0 * m_keyframeFactor;
263 if (m_keyframes.count() > 1) {
264 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
267 while (i != m_keyframes.constEnd()) {
268 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
269 y1 = br.bottom() - i.value() * maxh;
270 if (qAbs(pos.x() - x1) < maxOffset && qAbs(pos.y() - y1) < 10) {
271 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
273 } else if (x1 > pos.x()) break;
277 setToolTip(QString());
281 void AbstractClipItem::updateSelectedKeyFrame()
283 if (m_editedKeyframe == -1) return;
284 QRectF br = sceneBoundingRect();
285 double maxw = br.width() / cropDuration().frames(m_fps);
286 double maxh = br.height() / 100.0 * m_keyframeFactor;
287 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
288 m_selectedKeyframe = m_editedKeyframe;
289 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
292 int AbstractClipItem::selectedKeyFramePos() const
294 return m_editedKeyframe;
297 double AbstractClipItem::selectedKeyFrameValue() const
299 return m_keyframes[m_editedKeyframe];
302 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
304 if (!m_keyframes.contains(m_selectedKeyframe)) return;
305 int newpos = (int) pos.frames(m_fps);
306 int start = cropStart().frames(m_fps);
307 int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
308 newpos = qMax(newpos, start);
309 newpos = qMin(newpos, end);
310 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
311 // remove kexframe if it is dragged outside
312 m_keyframes.remove(m_selectedKeyframe);
313 m_selectedKeyframe = -1;
317 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
318 // remove kexframe if it is dragged outside
319 m_keyframes.remove(m_selectedKeyframe);
320 m_selectedKeyframe = -1;
324 double newval = qMax(value, 0.0);
325 newval = qMin(newval, 100.0);
326 newval = newval / m_keyframeFactor;
327 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
328 m_keyframes[newpos] = (int) newval;
329 m_selectedKeyframe = newpos;
333 double AbstractClipItem::keyFrameFactor() const
335 return m_keyframeFactor;
338 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
340 QRectF br = sceneBoundingRect();
341 double maxh = 100.0 / br.height() / m_keyframeFactor;
342 int newval = (br.bottom() - value) * maxh;
343 //kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
344 int newpos = (int) pos.frames(m_fps) ;
345 m_keyframes[newpos] = newval;
346 m_selectedKeyframe = newpos;
350 bool AbstractClipItem::hasKeyFrames() const
352 return !m_keyframes.isEmpty();
355 /*QRect AbstractClipItem::visibleRect() {
357 if (scene()->views().size() > 0) {
358 rectInView = scene()->views()[0]->viewport()->rect();
359 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
360 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
361 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
366 CustomTrackScene* AbstractClipItem::projectScene()
368 if (scene()) return static_cast <CustomTrackScene*>(scene());
372 void AbstractClipItem::setItemLocked(bool locked)
376 setFlag(QGraphicsItem::ItemIsMovable, false);
377 setFlag(QGraphicsItem::ItemIsSelectable, false);
379 setFlag(QGraphicsItem::ItemIsMovable, true);
380 setFlag(QGraphicsItem::ItemIsSelectable, true);
384 bool AbstractClipItem::isItemLocked() const
386 return !(flags() & (QGraphicsItem::ItemIsSelectable));
390 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
392 if (event->modifiers() & Qt::ShiftModifier) {
393 // User want to do a rectangle selection, so ignore the event to pass it to the view
395 } else QGraphicsItem::mousePressEvent(event);