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"
31 AbstractClipItem::AbstractClipItem(const ItemInfo info, const QRectF& rect, double fps) :
32 QGraphicsRectItem(rect),
35 m_selectedKeyframe(0),
39 setFlags(/*QGraphicsItem::ItemClipsToShape | */QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
41 m_startPos = info.startPos;
42 m_cropDuration = info.endPos - info.startPos;
45 ItemInfo AbstractClipItem::info() const
48 itemInfo.startPos = startPos();
49 itemInfo.endPos = endPos();
50 itemInfo.cropStart = m_cropStart;
51 itemInfo.track = track();
55 GenTime AbstractClipItem::endPos() const
57 return m_startPos + m_cropDuration;
60 int AbstractClipItem::track() const
65 GenTime AbstractClipItem::cropStart() const
70 GenTime AbstractClipItem::cropDuration() const
72 return m_cropDuration;
75 void AbstractClipItem::setCropStart(GenTime pos)
80 void AbstractClipItem::updateItem()
82 m_track = (int)(scenePos().y() / KdenliveSettings::trackheight());
83 m_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, double speed)
93 GenTime durationDiff = GenTime(posx, m_fps) - m_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 >= m_cropDuration) {
101 if (m_cropDuration > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
104 //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
105 m_startPos += durationDiff;
106 if (type() == AVWIDGET) m_cropStart += durationDiff * speed;
107 m_cropDuration = m_cropDuration - durationDiff * speed;
109 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
110 moveBy(durationDiff.frames(m_fps), 0);
111 //setPos(m_startPos.frames(m_fps), pos().y());
112 if ((int) scenePos().x() != posx) {
113 //kDebug()<<"////// WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
114 GenTime diff = GenTime((int) pos().x() - posx, m_fps);
115 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
116 m_cropDuration = m_cropDuration - diff;
117 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
118 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
122 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
123 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
125 /* if (durationDiff < GenTime()) {
126 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
127 for (int i = 0; i < collisionList.size(); ++i) {
128 QGraphicsItem *item = collisionList.at(i);
129 if (item->type() == type() && item->pos().x() < pos().x()) {
130 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
131 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
132 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
133 setPos((m_startPos + diff).frames(m_fps), pos().y());
135 if (type() == AVWIDGET) m_cropStart += diff;
136 m_cropDuration = m_cropDuration - diff;
143 void AbstractClipItem::resizeEnd(int posx, double speed, bool /*updateKeyFrames*/)
145 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
146 if (durationDiff == GenTime()) return;
147 //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
148 if (cropDuration() + durationDiff <= GenTime()) {
149 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
150 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
151 //kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
152 durationDiff = maxDuration() - cropDuration() - cropStart();
154 //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
155 m_cropDuration += durationDiff * speed;
156 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
157 if (durationDiff > GenTime()) {
158 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
159 for (int i = 0; i < collisionList.size(); ++i) {
160 QGraphicsItem *item = collisionList.at(i);
161 if (item->type() == type() && item->pos().x() > pos().x()) {
162 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
163 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
164 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
165 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
166 m_cropDuration = diff * speed;
167 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
174 GenTime AbstractClipItem::duration() const
176 return m_cropDuration;
179 GenTime AbstractClipItem::startPos() const
184 void AbstractClipItem::setTrack(int track)
189 double AbstractClipItem::fps() const
194 GenTime AbstractClipItem::maxDuration() const
196 return m_maxDuration;
199 void AbstractClipItem::setMaxDuration(const GenTime &max)
204 QPainterPath AbstractClipItem::upperRectPart(QRectF br)
206 QPainterPath roundRectPathUpper;
207 double roundingY = 20;
208 double roundingX = 20;
211 while (roundingX > br.width() / 2) {
212 roundingX = roundingX / 2;
213 roundingY = roundingY / 2;
215 int br_endx = (int)(br.x() + br .width() - offset);
216 int br_startx = (int)(br.x() + offset);
217 int br_starty = (int)(br.y());
218 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
220 roundRectPathUpper.moveTo(br_endx , br_halfy);
221 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
222 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
223 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
224 roundRectPathUpper.lineTo(br_startx , br_halfy);
226 return roundRectPathUpper;
229 QPainterPath AbstractClipItem::lowerRectPart(QRectF br)
231 QPainterPath roundRectPathLower;
232 double roundingY = 20;
233 double roundingX = 20;
236 int br_endx = (int)(br.x() + br .width() - offset);
237 int br_startx = (int)(br.x() + offset);
238 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
239 int br_endy = (int)(br.y() + br.height() - 1);
241 while (roundingX > br.width() / 2) {
242 roundingX = roundingX / 2;
243 roundingY = roundingY / 2;
245 roundRectPathLower.moveTo(br_startx, br_halfy);
246 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
247 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
248 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
249 roundRectPathLower.lineTo(br_endx , br_halfy);
250 return roundRectPathLower;
253 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
255 if (m_keyframes.count() < 2) return;
257 double maxw = br.width() / m_cropDuration.frames(m_fps);
258 double maxh = br.height() / 100.0 * m_keyframeFactor;
264 // draw line showing default value
268 y1 = br.bottom() - m_keyframeDefault * maxh;
269 QLineF l(x1, y1, x2, y1);
270 QLineF l2 = painter->matrix().map(l);
271 painter->setPen(QColor(168, 168, 168, 180));
272 painter->drawLine(l2);
274 painter->setPen(QColor(108, 108, 108, 180));
275 painter->drawLine(l2);
276 painter->setPen(QColor(Qt::white));
280 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
281 QColor color(Qt::blue);
282 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
283 y1 = br.bottom() - i.value() * maxh;
285 while (i != m_keyframes.constEnd()) {
286 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
287 else color = QColor(Qt::blue);
289 if (i == m_keyframes.constEnd()) break;
290 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
291 y2 = br.bottom() - i.value() * maxh;
292 QLineF l(x1, y1, x2, y2);
293 l2 = painter->matrix().map(l);
294 painter->drawLine(l2);
296 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
301 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
304 int AbstractClipItem::mouseOverKeyFrames(QPointF pos)
306 QRectF br = sceneBoundingRect();
307 double maxw = br.width() / m_cropDuration.frames(m_fps);
308 double maxh = br.height() / 100.0 * m_keyframeFactor;
309 if (m_keyframes.count() > 1) {
310 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
313 while (i != m_keyframes.constEnd()) {
314 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
315 y1 = br.bottom() - i.value() * maxh;
316 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
317 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
319 } else if (x1 > pos.x()) break;
323 setToolTip(QString());
327 void AbstractClipItem::updateSelectedKeyFrame()
329 if (m_editedKeyframe == -1) return;
330 QRectF br = sceneBoundingRect();
331 double maxw = br.width() / m_cropDuration.frames(m_fps);
332 double maxh = br.height() / 100.0 * m_keyframeFactor;
333 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
334 m_selectedKeyframe = m_editedKeyframe;
335 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
338 int AbstractClipItem::selectedKeyFramePos() const
340 return m_editedKeyframe;
343 double AbstractClipItem::selectedKeyFrameValue() const
345 return m_keyframes[m_editedKeyframe];
348 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
350 if (!m_keyframes.contains(m_selectedKeyframe)) return;
351 int newpos = (int) pos.frames(m_fps);
352 int start = m_cropStart.frames(m_fps);
353 int end = (m_cropStart + m_cropDuration).frames(m_fps);
354 newpos = qMax(newpos, start);
355 newpos = qMin(newpos, end);
356 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
357 // remove kexframe if it is dragged outside
358 m_keyframes.remove(m_selectedKeyframe);
359 m_selectedKeyframe = -1;
363 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
364 // remove kexframe if it is dragged outside
365 m_keyframes.remove(m_selectedKeyframe);
366 m_selectedKeyframe = -1;
370 double newval = qMax(value, 0.0);
371 newval = qMin(newval, 100.0);
372 newval = newval / m_keyframeFactor;
373 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
374 m_keyframes[newpos] = newval;
375 m_selectedKeyframe = newpos;
379 double AbstractClipItem::keyFrameFactor() const
381 return m_keyframeFactor;
384 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
386 QRectF br = sceneBoundingRect();
387 double maxh = 100.0 / br.height() / m_keyframeFactor;
388 double newval = (br.bottom() - value) * maxh;
389 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
390 int newpos = (int) pos.frames(m_fps) ;
391 m_keyframes[newpos] = newval;
392 m_selectedKeyframe = newpos;
396 bool AbstractClipItem::hasKeyFrames() const
398 return !m_keyframes.isEmpty();
401 /*QRect AbstractClipItem::visibleRect() {
403 if (scene()->views().size() > 0) {
404 rectInView = scene()->views()[0]->viewport()->rect();
405 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
406 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
407 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
412 CustomTrackScene* AbstractClipItem::projectScene()
414 if (scene()) return static_cast <CustomTrackScene*>(scene());
418 void AbstractClipItem::setItemLocked(bool locked)
422 setFlag(QGraphicsItem::ItemIsMovable, false);
423 setFlag(QGraphicsItem::ItemIsSelectable, false);
424 } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
427 bool AbstractClipItem::isItemLocked() const
429 return !(flags() & (QGraphicsItem::ItemIsSelectable));