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::ItemClipsToShape | */QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
43 m_startPos = info.startPos;
44 m_cropDuration = info.endPos - info.startPos;
47 ItemInfo AbstractClipItem::info() const
50 itemInfo.startPos = startPos();
51 itemInfo.endPos = endPos();
52 itemInfo.cropStart = m_cropStart;
53 itemInfo.track = track();
57 GenTime AbstractClipItem::endPos() const
59 return m_startPos + m_cropDuration;
62 int AbstractClipItem::track() const
67 GenTime AbstractClipItem::cropStart() const
72 GenTime AbstractClipItem::cropDuration() const
74 return m_cropDuration;
77 void AbstractClipItem::setCropStart(GenTime pos)
82 void AbstractClipItem::updateItem()
84 m_track = (int)(scenePos().y() / KdenliveSettings::trackheight());
85 m_startPos = GenTime((int) scenePos().x(), m_fps);
88 void AbstractClipItem::updateRectGeometry()
90 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
93 void AbstractClipItem::resizeStart(int posx, double speed)
95 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
96 if (durationDiff == GenTime()) return;
97 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
99 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
100 durationDiff = GenTime() - cropStart();
101 } else if (durationDiff >= m_cropDuration) {
103 if (m_cropDuration > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
106 //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
107 m_startPos += durationDiff;
108 if (type() == AVWIDGET) m_cropStart += durationDiff * speed;
109 m_cropDuration = m_cropDuration - durationDiff * speed;
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 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
118 m_cropDuration = m_cropDuration - diff;
119 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
120 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
124 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
125 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
127 /* if (durationDiff < GenTime()) {
128 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
129 for (int i = 0; i < collisionList.size(); ++i) {
130 QGraphicsItem *item = collisionList.at(i);
131 if (item->type() == type() && item->pos().x() < pos().x()) {
132 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
133 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
134 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
135 setPos((m_startPos + diff).frames(m_fps), pos().y());
137 if (type() == AVWIDGET) m_cropStart += diff;
138 m_cropDuration = m_cropDuration - diff;
145 void AbstractClipItem::resizeEnd(int posx, double speed, bool /*updateKeyFrames*/)
147 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
148 if (durationDiff == GenTime()) return;
149 //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
150 if (cropDuration() + durationDiff <= GenTime()) {
151 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
153 //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
154 m_cropDuration += durationDiff * speed;
155 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
156 if (durationDiff > GenTime()) {
157 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
158 for (int i = 0; i < collisionList.size(); ++i) {
159 QGraphicsItem *item = collisionList.at(i);
160 if (item->type() == type() && item->pos().x() > pos().x()) {
161 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
162 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
163 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
164 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
165 m_cropDuration = diff * speed;
166 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
173 GenTime AbstractClipItem::startPos() const
178 void AbstractClipItem::setTrack(int track)
183 double AbstractClipItem::fps() const
188 GenTime AbstractClipItem::maxDuration() const
190 return m_maxDuration;
193 QPainterPath AbstractClipItem::upperRectPart(QRectF br)
195 QPainterPath roundRectPathUpper;
196 double roundingY = 20;
197 double roundingX = 20;
200 while (roundingX > br.width() / 2) {
201 roundingX = roundingX / 2;
202 roundingY = roundingY / 2;
204 int br_endx = (int)(br.x() + br .width() - offset);
205 int br_startx = (int)(br.x() + offset);
206 int br_starty = (int)(br.y());
207 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
209 roundRectPathUpper.moveTo(br_endx , br_halfy);
210 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
211 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
212 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
213 roundRectPathUpper.lineTo(br_startx , br_halfy);
215 return roundRectPathUpper;
218 QPainterPath AbstractClipItem::lowerRectPart(QRectF br)
220 QPainterPath roundRectPathLower;
221 double roundingY = 20;
222 double roundingX = 20;
225 int br_endx = (int)(br.x() + br .width() - offset);
226 int br_startx = (int)(br.x() + offset);
227 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
228 int br_endy = (int)(br.y() + br.height() - 1);
230 while (roundingX > br.width() / 2) {
231 roundingX = roundingX / 2;
232 roundingY = roundingY / 2;
234 roundRectPathLower.moveTo(br_startx, br_halfy);
235 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
236 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
237 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
238 roundRectPathLower.lineTo(br_endx , br_halfy);
239 return roundRectPathLower;
242 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
244 if (m_keyframes.count() < 2) return;
246 double maxw = br.width() / cropDuration().frames(m_fps);
247 double maxh = br.height() / 100.0 * m_keyframeFactor;
253 // draw line showing default value
254 bool active = isSelected() || (parentItem() && parentItem()->isSelected());
258 y1 = br.bottom() - m_keyframeDefault * maxh;
259 QLineF l(x1, y1, x2, y1);
260 QLineF l2 = painter->matrix().map(l);
261 painter->setPen(QColor(168, 168, 168, 180));
262 painter->drawLine(l2);
264 painter->setPen(QColor(108, 108, 108, 180));
265 painter->drawLine(l2);
266 painter->setPen(QColor(Qt::white));
270 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
271 QColor color(Qt::blue);
272 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
273 y1 = br.bottom() - i.value() * maxh;
275 while (i != m_keyframes.constEnd()) {
276 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
277 else color = QColor(Qt::blue);
279 if (i == m_keyframes.constEnd()) break;
280 x2 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
281 y2 = br.bottom() - i.value() * maxh;
282 QLineF l(x1, y1, x2, y2);
283 l2 = painter->matrix().map(l);
284 painter->drawLine(l2);
286 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
291 if (active) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
294 int AbstractClipItem::mouseOverKeyFrames(QPointF pos)
296 QRectF br = sceneBoundingRect();
297 double maxw = br.width() / cropDuration().frames(m_fps);
298 double maxh = br.height() / 100.0 * m_keyframeFactor;
299 if (m_keyframes.count() > 1) {
300 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
303 while (i != m_keyframes.constEnd()) {
304 x1 = br.x() + maxw * (i.key() - cropStart().frames(m_fps));
305 y1 = br.bottom() - i.value() * maxh;
306 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
307 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - cropStart()).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
309 } else if (x1 > pos.x()) break;
313 setToolTip(QString());
317 void AbstractClipItem::updateSelectedKeyFrame()
319 if (m_editedKeyframe == -1) return;
320 QRectF br = sceneBoundingRect();
321 double maxw = br.width() / cropDuration().frames(m_fps);
322 double maxh = br.height() / 100.0 * m_keyframeFactor;
323 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
324 m_selectedKeyframe = m_editedKeyframe;
325 update(br.x() + maxw *(m_selectedKeyframe - cropStart().frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
328 int AbstractClipItem::selectedKeyFramePos() const
330 return m_editedKeyframe;
333 double AbstractClipItem::selectedKeyFrameValue() const
335 return m_keyframes[m_editedKeyframe];
338 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
340 if (!m_keyframes.contains(m_selectedKeyframe)) return;
341 int newpos = (int) pos.frames(m_fps);
342 int start = cropStart().frames(m_fps);
343 int end = (cropStart() + cropDuration()).frames(m_fps);
344 newpos = qMax(newpos, start);
345 newpos = qMin(newpos, end);
346 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
347 // remove kexframe if it is dragged outside
348 m_keyframes.remove(m_selectedKeyframe);
349 m_selectedKeyframe = -1;
353 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
354 // remove kexframe if it is dragged outside
355 m_keyframes.remove(m_selectedKeyframe);
356 m_selectedKeyframe = -1;
360 double newval = qMax(value, 0.0);
361 newval = qMin(newval, 100.0);
362 newval = newval / m_keyframeFactor;
363 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
364 m_keyframes[newpos] = (int) newval;
365 m_selectedKeyframe = newpos;
369 double AbstractClipItem::keyFrameFactor() const
371 return m_keyframeFactor;
374 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
376 QRectF br = sceneBoundingRect();
377 double maxh = 100.0 / br.height() / m_keyframeFactor;
378 int newval = (br.bottom() - value) * maxh;
379 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
380 int newpos = (int) pos.frames(m_fps) ;
381 m_keyframes[newpos] = newval;
382 m_selectedKeyframe = newpos;
386 bool AbstractClipItem::hasKeyFrames() const
388 return !m_keyframes.isEmpty();
391 /*QRect AbstractClipItem::visibleRect() {
393 if (scene()->views().size() > 0) {
394 rectInView = scene()->views()[0]->viewport()->rect();
395 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
396 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
397 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
402 CustomTrackScene* AbstractClipItem::projectScene()
404 if (scene()) return static_cast <CustomTrackScene*>(scene());
408 void AbstractClipItem::setItemLocked(bool locked)
412 setFlag(QGraphicsItem::ItemIsMovable, false);
413 setFlag(QGraphicsItem::ItemIsSelectable, false);
414 } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
417 bool AbstractClipItem::isItemLocked() const
419 return !(flags() & (QGraphicsItem::ItemIsSelectable));
423 void AbstractClipItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
425 if (event->modifiers() & Qt::ShiftModifier) {
426 // User want to do a rectangle selection, so ignore the event to pass it to the view
428 } else QGraphicsItem::mousePressEvent(event);