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 <QGraphicsScene>
22 #include <QGraphicsView>
29 #include "abstractclipitem.h"
30 #include "customtrackscene.h"
31 #include "kdenlivesettings.h"
33 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) {
34 setFlags(/*QGraphicsItem::ItemClipsToShape | */QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
36 m_startPos = info.startPos;
37 m_cropDuration = info.endPos - info.startPos;
40 ItemInfo AbstractClipItem::info() const {
42 itemInfo.startPos = startPos();
43 itemInfo.endPos = endPos();
44 itemInfo.cropStart = m_cropStart;
45 itemInfo.track = track();
49 GenTime AbstractClipItem::endPos() const {
50 return m_startPos + m_cropDuration;
53 int AbstractClipItem::track() const {
57 GenTime AbstractClipItem::cropStart() const {
61 GenTime AbstractClipItem::cropDuration() const {
62 return m_cropDuration;
65 void AbstractClipItem::setCropStart(GenTime pos) {
69 void AbstractClipItem::updateItem() {
70 m_track = (int)(scenePos().y() / KdenliveSettings::trackheight());
71 m_startPos = GenTime((int) scenePos().x(), m_fps);
74 void AbstractClipItem::updateRectGeometry() {
75 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
78 void AbstractClipItem::resizeStart(int posx, double speed) {
79 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
80 if (durationDiff == GenTime()) return;
81 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
83 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
84 durationDiff = GenTime() - cropStart();
85 } else if (durationDiff >= m_cropDuration) {
87 if (m_cropDuration > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
91 m_startPos += durationDiff;
92 if (type() == AVWIDGET) m_cropStart += durationDiff * speed;
93 m_cropDuration = m_cropDuration - durationDiff * speed;
95 setPos(m_startPos.frames(m_fps), pos().y());
96 if (pos().x() != m_startPos.frames(m_fps)) {
97 GenTime diff = GenTime(pos().x(), m_fps) - m_startPos;
99 m_cropDuration = m_cropDuration - diff;
100 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
102 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
104 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
105 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
107 /* if (durationDiff < GenTime()) {
108 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
109 for (int i = 0; i < collisionList.size(); ++i) {
110 QGraphicsItem *item = collisionList.at(i);
111 if (item->type() == type() && item->pos().x() < pos().x()) {
112 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
113 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
114 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
115 setPos((m_startPos + diff).frames(m_fps), pos().y());
117 if (type() == AVWIDGET) m_cropStart += diff;
118 m_cropDuration = m_cropDuration - diff;
125 void AbstractClipItem::resizeEnd(int posx, double speed, bool updateKeyFrames) {
126 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
127 if (durationDiff == GenTime()) return;
128 //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
129 if (cropDuration() + durationDiff <= GenTime()) {
130 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
131 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
132 kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
133 durationDiff = maxDuration() - cropDuration() - cropStart();
135 //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
136 m_cropDuration += durationDiff * speed;
137 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
138 if (durationDiff > GenTime()) {
139 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
140 for (int i = 0; i < collisionList.size(); ++i) {
141 QGraphicsItem *item = collisionList.at(i);
142 if (item->type() == type() && item->pos().x() > pos().x()) {
143 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
144 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
145 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
146 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
147 m_cropDuration = diff;
148 setRect(0, 0, m_cropDuration.frames(m_fps) - 0.02, rect().height());
155 GenTime AbstractClipItem::duration() const {
156 return m_cropDuration;
159 GenTime AbstractClipItem::startPos() const {
163 void AbstractClipItem::setTrack(int track) {
167 double AbstractClipItem::fps() const {
171 GenTime AbstractClipItem::maxDuration() const {
172 return m_maxDuration;
175 void AbstractClipItem::setMaxDuration(const GenTime &max) {
179 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
180 QPainterPath roundRectPathUpper;
181 double roundingY = 20;
182 double roundingX = 20;
185 while (roundingX > br.width() / 2) {
186 roundingX = roundingX / 2;
187 roundingY = roundingY / 2;
189 int br_endx = (int)(br.x() + br .width() - offset);
190 int br_startx = (int)(br.x() + offset);
191 int br_starty = (int)(br.y());
192 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
193 int br_endy = (int)(br.y() + br.height());
195 roundRectPathUpper.moveTo(br_endx , br_halfy);
196 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
197 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
198 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
199 roundRectPathUpper.lineTo(br_startx , br_halfy);
201 return roundRectPathUpper;
204 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
205 QPainterPath roundRectPathLower;
206 double roundingY = 20;
207 double roundingX = 20;
210 int br_endx = (int)(br.x() + br .width() - offset);
211 int br_startx = (int)(br.x() + offset);
212 int br_starty = (int)(br.y());
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());