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): QGraphicsRectItem(rect), m_track(0), m_fps(fps), m_editedKeyframe(-1), m_selectedKeyframe(0), m_keyframeFactor(1) {
32 setFlags(/*QGraphicsItem::ItemClipsToShape | */QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
34 m_startPos = info.startPos;
35 m_cropDuration = info.endPos - info.startPos;
38 ItemInfo AbstractClipItem::info() const {
40 itemInfo.startPos = startPos();
41 itemInfo.endPos = endPos();
42 itemInfo.cropStart = m_cropStart;
43 itemInfo.track = track();
47 GenTime AbstractClipItem::endPos() const {
48 return m_startPos + m_cropDuration;
51 int AbstractClipItem::track() const {
55 GenTime AbstractClipItem::cropStart() const {
59 GenTime AbstractClipItem::cropDuration() const {
60 return m_cropDuration;
63 void AbstractClipItem::setCropStart(GenTime pos) {
67 void AbstractClipItem::updateItem() {
68 m_track = (int)(scenePos().y() / KdenliveSettings::trackheight());
69 m_startPos = GenTime((int) scenePos().x(), m_fps);
72 void AbstractClipItem::updateRectGeometry() {
73 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
76 void AbstractClipItem::resizeStart(int posx, double speed) {
77 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
78 if (durationDiff == GenTime()) return;
79 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
81 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
82 durationDiff = GenTime() - cropStart();
83 } else if (durationDiff >= m_cropDuration) {
85 if (m_cropDuration > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
89 m_startPos += durationDiff;
90 if (type() == AVWIDGET) m_cropStart += durationDiff * speed;
91 m_cropDuration = m_cropDuration - durationDiff * speed;
93 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
94 setPos(m_startPos.frames(m_fps), pos().y());
95 if ((int) pos().x() != posx) {
96 //kDebug()<<"////// WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
97 GenTime diff = GenTime((int) pos().x() - posx, m_fps);
98 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
99 m_cropDuration = m_cropDuration - diff;
100 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
101 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
105 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
106 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
108 /* if (durationDiff < GenTime()) {
109 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
110 for (int i = 0; i < collisionList.size(); ++i) {
111 QGraphicsItem *item = collisionList.at(i);
112 if (item->type() == type() && item->pos().x() < pos().x()) {
113 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
114 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
115 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
116 setPos((m_startPos + diff).frames(m_fps), pos().y());
118 if (type() == AVWIDGET) m_cropStart += diff;
119 m_cropDuration = m_cropDuration - diff;
126 void AbstractClipItem::resizeEnd(int posx, double speed, bool updateKeyFrames) {
127 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
128 if (durationDiff == GenTime()) return;
129 //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
130 if (cropDuration() + durationDiff <= GenTime()) {
131 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
132 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
133 //kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
134 durationDiff = maxDuration() - cropDuration() - cropStart();
136 //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
137 m_cropDuration += durationDiff * speed;
138 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
139 if (durationDiff > GenTime()) {
140 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
141 for (int i = 0; i < collisionList.size(); ++i) {
142 QGraphicsItem *item = collisionList.at(i);
143 if (item->type() == type() && item->pos().x() > pos().x()) {
144 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
145 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
146 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
147 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
148 m_cropDuration = diff * speed;
149 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
156 GenTime AbstractClipItem::duration() const {
157 return m_cropDuration;
160 GenTime AbstractClipItem::startPos() const {
164 void AbstractClipItem::setTrack(int track) {
168 double AbstractClipItem::fps() const {
172 GenTime AbstractClipItem::maxDuration() const {
173 return m_maxDuration;
176 void AbstractClipItem::setMaxDuration(const GenTime &max) {
180 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
181 QPainterPath roundRectPathUpper;
182 double roundingY = 20;
183 double roundingX = 20;
186 while (roundingX > br.width() / 2) {
187 roundingX = roundingX / 2;
188 roundingY = roundingY / 2;
190 int br_endx = (int)(br.x() + br .width() - offset);
191 int br_startx = (int)(br.x() + offset);
192 int br_starty = (int)(br.y());
193 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
194 int br_endy = (int)(br.y() + br.height());
196 roundRectPathUpper.moveTo(br_endx , br_halfy);
197 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
198 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
199 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
200 roundRectPathUpper.lineTo(br_startx , br_halfy);
202 return roundRectPathUpper;
205 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
206 QPainterPath roundRectPathLower;
207 double roundingY = 20;
208 double roundingX = 20;
211 int br_endx = (int)(br.x() + br .width() - offset);
212 int br_startx = (int)(br.x() + offset);
213 int br_starty = (int)(br.y());
214 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
215 int br_endy = (int)(br.y() + br.height() - 1);
217 while (roundingX > br.width() / 2) {
218 roundingX = roundingX / 2;
219 roundingY = roundingY / 2;
221 roundRectPathLower.moveTo(br_startx, br_halfy);
222 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
223 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
224 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
225 roundRectPathLower.lineTo(br_endx , br_halfy);
226 return roundRectPathLower;
229 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
230 if (m_keyframes.count() < 2) return;
232 double maxw = br.width() / m_cropDuration.frames(m_fps);
233 double maxh = br.height() / 100.0 * m_keyframeFactor;
239 // draw line showing default value
243 y1 = br.bottom() - m_keyframeDefault * maxh;
244 QLineF l(x1, y1, x2, y1);
245 QLineF l2 = painter->matrix().map(l);
246 painter->setPen(QColor(168, 168, 168, 180));
247 painter->drawLine(l2);
249 painter->setPen(QColor(108, 108, 108, 180));
250 painter->drawLine(l2);
251 painter->setPen(QColor(Qt::white));
255 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
256 QColor color(Qt::blue);
257 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
258 y1 = br.bottom() - i.value() * maxh;
260 while (i != m_keyframes.constEnd()) {
261 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
262 else color = QColor(Qt::blue);
264 if (i == m_keyframes.constEnd()) break;
265 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
266 y2 = br.bottom() - i.value() * maxh;
267 QLineF l(x1, y1, x2, y2);
268 l2 = painter->matrix().map(l);
269 painter->drawLine(l2);
271 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
276 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
279 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
280 QRectF br = sceneBoundingRect();
281 double maxw = br.width() / m_cropDuration.frames(m_fps);
282 double maxh = br.height() / 100.0 * m_keyframeFactor;
283 if (m_keyframes.count() > 1) {
284 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
287 while (i != m_keyframes.constEnd()) {
288 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
289 y1 = br.bottom() - i.value() * maxh;
290 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
291 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
293 } else if (x1 > pos.x()) break;
297 setToolTip(QString());
301 void AbstractClipItem::updateSelectedKeyFrame() {
302 if (m_editedKeyframe == -1) return;
303 QRectF br = sceneBoundingRect();
304 double maxw = br.width() / m_cropDuration.frames(m_fps);
305 double maxh = br.height() / 100.0 * m_keyframeFactor;
306 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
307 m_selectedKeyframe = m_editedKeyframe;
308 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
311 int AbstractClipItem::selectedKeyFramePos() const {
312 return m_editedKeyframe;
315 double AbstractClipItem::selectedKeyFrameValue() const {
316 return m_keyframes[m_editedKeyframe];
319 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
320 if (!m_keyframes.contains(m_selectedKeyframe)) return;
321 int newpos = (int) pos.frames(m_fps);
322 int start = m_cropStart.frames(m_fps);
323 int end = (m_cropStart + m_cropDuration).frames(m_fps);
324 newpos = qMax(newpos, start);
325 newpos = qMin(newpos, end);
326 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
327 // remove kexframe if it is dragged outside
328 m_keyframes.remove(m_selectedKeyframe);
329 m_selectedKeyframe = -1;
333 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
334 // remove kexframe if it is dragged outside
335 m_keyframes.remove(m_selectedKeyframe);
336 m_selectedKeyframe = -1;
340 double newval = qMax(value, 0.0);
341 newval = qMin(newval, 100.0);
342 newval = newval / m_keyframeFactor;
343 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
344 m_keyframes[newpos] = newval;
345 m_selectedKeyframe = newpos;
349 double AbstractClipItem::keyFrameFactor() const {
350 return m_keyframeFactor;
353 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
354 QRectF br = sceneBoundingRect();
355 double maxh = 100.0 / br.height() / m_keyframeFactor;
356 double newval = (br.bottom() - value) * maxh;
357 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
358 int newpos = (int) pos.frames(m_fps) ;
359 m_keyframes[newpos] = newval;
360 m_selectedKeyframe = newpos;
364 bool AbstractClipItem::hasKeyFrames() const {
365 return !m_keyframes.isEmpty();
368 /*QRect AbstractClipItem::visibleRect() {
370 if (scene()->views().size() > 0) {
371 rectInView = scene()->views()[0]->viewport()->rect();
372 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
373 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
374 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
379 CustomTrackScene* AbstractClipItem::projectScene() {
380 if (scene()) return static_cast <CustomTrackScene*>(scene());
384 void AbstractClipItem::setItemLocked(bool locked) {
387 setFlag(QGraphicsItem::ItemIsMovable, false);
388 setFlag(QGraphicsItem::ItemIsSelectable, false);
389 } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
392 bool AbstractClipItem::isItemLocked() const {
393 return !(flags() & (QGraphicsItem::ItemIsSelectable));