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);
88 //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
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 moveBy(durationDiff.frames(m_fps), 0);
95 //setPos(m_startPos.frames(m_fps), pos().y());
96 if ((int) scenePos().x() != posx) {
97 //kDebug()<<"////// WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
98 GenTime diff = GenTime((int) pos().x() - posx, m_fps);
99 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
100 m_cropDuration = m_cropDuration - diff;
101 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
102 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
106 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
107 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
109 /* if (durationDiff < GenTime()) {
110 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
111 for (int i = 0; i < collisionList.size(); ++i) {
112 QGraphicsItem *item = collisionList.at(i);
113 if (item->type() == type() && item->pos().x() < pos().x()) {
114 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
115 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
116 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
117 setPos((m_startPos + diff).frames(m_fps), pos().y());
119 if (type() == AVWIDGET) m_cropStart += diff;
120 m_cropDuration = m_cropDuration - diff;
127 void AbstractClipItem::resizeEnd(int posx, double speed, bool /*updateKeyFrames*/) {
128 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
129 if (durationDiff == GenTime()) return;
130 //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
131 if (cropDuration() + durationDiff <= GenTime()) {
132 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
133 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
134 //kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
135 durationDiff = maxDuration() - cropDuration() - cropStart();
137 //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
138 m_cropDuration += durationDiff * speed;
139 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
140 if (durationDiff > GenTime()) {
141 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
142 for (int i = 0; i < collisionList.size(); ++i) {
143 QGraphicsItem *item = collisionList.at(i);
144 if (item->type() == type() && item->pos().x() > pos().x()) {
145 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
146 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
147 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
148 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
149 m_cropDuration = diff * speed;
150 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
157 GenTime AbstractClipItem::duration() const {
158 return m_cropDuration;
161 GenTime AbstractClipItem::startPos() const {
165 void AbstractClipItem::setTrack(int track) {
169 double AbstractClipItem::fps() const {
173 GenTime AbstractClipItem::maxDuration() const {
174 return m_maxDuration;
177 void AbstractClipItem::setMaxDuration(const GenTime &max) {
181 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
182 QPainterPath roundRectPathUpper;
183 double roundingY = 20;
184 double roundingX = 20;
187 while (roundingX > br.width() / 2) {
188 roundingX = roundingX / 2;
189 roundingY = roundingY / 2;
191 int br_endx = (int)(br.x() + br .width() - offset);
192 int br_startx = (int)(br.x() + offset);
193 int br_starty = (int)(br.y());
194 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
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_halfy = (int)(br.y() + br.height() / 2 - offset);
214 int br_endy = (int)(br.y() + br.height() - 1);
216 while (roundingX > br.width() / 2) {
217 roundingX = roundingX / 2;
218 roundingY = roundingY / 2;
220 roundRectPathLower.moveTo(br_startx, br_halfy);
221 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
222 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
223 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
224 roundRectPathLower.lineTo(br_endx , br_halfy);
225 return roundRectPathLower;
228 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/) {
229 if (m_keyframes.count() < 2) return;
231 double maxw = br.width() / m_cropDuration.frames(m_fps);
232 double maxh = br.height() / 100.0 * m_keyframeFactor;
238 // draw line showing default value
242 y1 = br.bottom() - m_keyframeDefault * maxh;
243 QLineF l(x1, y1, x2, y1);
244 QLineF l2 = painter->matrix().map(l);
245 painter->setPen(QColor(168, 168, 168, 180));
246 painter->drawLine(l2);
248 painter->setPen(QColor(108, 108, 108, 180));
249 painter->drawLine(l2);
250 painter->setPen(QColor(Qt::white));
254 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
255 QColor color(Qt::blue);
256 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
257 y1 = br.bottom() - i.value() * maxh;
259 while (i != m_keyframes.constEnd()) {
260 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
261 else color = QColor(Qt::blue);
263 if (i == m_keyframes.constEnd()) break;
264 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
265 y2 = br.bottom() - i.value() * maxh;
266 QLineF l(x1, y1, x2, y2);
267 l2 = painter->matrix().map(l);
268 painter->drawLine(l2);
270 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
275 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
278 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
279 QRectF br = sceneBoundingRect();
280 double maxw = br.width() / m_cropDuration.frames(m_fps);
281 double maxh = br.height() / 100.0 * m_keyframeFactor;
282 if (m_keyframes.count() > 1) {
283 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
286 while (i != m_keyframes.constEnd()) {
287 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
288 y1 = br.bottom() - i.value() * maxh;
289 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
290 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
292 } else if (x1 > pos.x()) break;
296 setToolTip(QString());
300 void AbstractClipItem::updateSelectedKeyFrame() {
301 if (m_editedKeyframe == -1) return;
302 QRectF br = sceneBoundingRect();
303 double maxw = br.width() / m_cropDuration.frames(m_fps);
304 double maxh = br.height() / 100.0 * m_keyframeFactor;
305 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
306 m_selectedKeyframe = m_editedKeyframe;
307 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
310 int AbstractClipItem::selectedKeyFramePos() const {
311 return m_editedKeyframe;
314 double AbstractClipItem::selectedKeyFrameValue() const {
315 return m_keyframes[m_editedKeyframe];
318 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
319 if (!m_keyframes.contains(m_selectedKeyframe)) return;
320 int newpos = (int) pos.frames(m_fps);
321 int start = m_cropStart.frames(m_fps);
322 int end = (m_cropStart + m_cropDuration).frames(m_fps);
323 newpos = qMax(newpos, start);
324 newpos = qMin(newpos, end);
325 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
326 // remove kexframe if it is dragged outside
327 m_keyframes.remove(m_selectedKeyframe);
328 m_selectedKeyframe = -1;
332 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
333 // remove kexframe if it is dragged outside
334 m_keyframes.remove(m_selectedKeyframe);
335 m_selectedKeyframe = -1;
339 double newval = qMax(value, 0.0);
340 newval = qMin(newval, 100.0);
341 newval = newval / m_keyframeFactor;
342 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
343 m_keyframes[newpos] = newval;
344 m_selectedKeyframe = newpos;
348 double AbstractClipItem::keyFrameFactor() const {
349 return m_keyframeFactor;
352 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
353 QRectF br = sceneBoundingRect();
354 double maxh = 100.0 / br.height() / m_keyframeFactor;
355 double newval = (br.bottom() - value) * maxh;
356 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
357 int newpos = (int) pos.frames(m_fps) ;
358 m_keyframes[newpos] = newval;
359 m_selectedKeyframe = newpos;
363 bool AbstractClipItem::hasKeyFrames() const {
364 return !m_keyframes.isEmpty();
367 /*QRect AbstractClipItem::visibleRect() {
369 if (scene()->views().size() > 0) {
370 rectInView = scene()->views()[0]->viewport()->rect();
371 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
372 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
373 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
378 CustomTrackScene* AbstractClipItem::projectScene() {
379 if (scene()) return static_cast <CustomTrackScene*>(scene());
383 void AbstractClipItem::setItemLocked(bool locked) {
386 setFlag(QGraphicsItem::ItemIsMovable, false);
387 setFlag(QGraphicsItem::ItemIsSelectable, false);
388 } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
391 bool AbstractClipItem::isItemLocked() const {
392 return !(flags() & (QGraphicsItem::ItemIsSelectable));