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 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
96 setPos(m_startPos.frames(m_fps), pos().y());
97 if ((int) pos().x() != posx) {
98 //kDebug()<<"////// WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
99 GenTime diff = GenTime((int) pos().x() - posx, m_fps);
100 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
101 m_cropDuration = m_cropDuration - diff;
102 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
103 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
107 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
108 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
110 /* if (durationDiff < GenTime()) {
111 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
112 for (int i = 0; i < collisionList.size(); ++i) {
113 QGraphicsItem *item = collisionList.at(i);
114 if (item->type() == type() && item->pos().x() < pos().x()) {
115 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
116 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
117 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
118 setPos((m_startPos + diff).frames(m_fps), pos().y());
120 if (type() == AVWIDGET) m_cropStart += diff;
121 m_cropDuration = m_cropDuration - diff;
128 void AbstractClipItem::resizeEnd(int posx, double speed, bool updateKeyFrames) {
129 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
130 if (durationDiff == GenTime()) return;
131 //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
132 if (cropDuration() + durationDiff <= GenTime()) {
133 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
134 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
135 //kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
136 durationDiff = maxDuration() - cropDuration() - cropStart();
138 //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
139 m_cropDuration += durationDiff * speed;
140 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
141 if (durationDiff > GenTime()) {
142 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
143 for (int i = 0; i < collisionList.size(); ++i) {
144 QGraphicsItem *item = collisionList.at(i);
145 if (item->type() == type() && item->pos().x() > pos().x()) {
146 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
147 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
148 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
149 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
150 m_cropDuration = diff;
151 setRect(0, 0, m_cropDuration.frames(m_fps) - 0.02, rect().height());
158 GenTime AbstractClipItem::duration() const {
159 return m_cropDuration;
162 GenTime AbstractClipItem::startPos() const {
166 void AbstractClipItem::setTrack(int track) {
170 double AbstractClipItem::fps() const {
174 GenTime AbstractClipItem::maxDuration() const {
175 return m_maxDuration;
178 void AbstractClipItem::setMaxDuration(const GenTime &max) {
182 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
183 QPainterPath roundRectPathUpper;
184 double roundingY = 20;
185 double roundingX = 20;
188 while (roundingX > br.width() / 2) {
189 roundingX = roundingX / 2;
190 roundingY = roundingY / 2;
192 int br_endx = (int)(br.x() + br .width() - offset);
193 int br_startx = (int)(br.x() + offset);
194 int br_starty = (int)(br.y());
195 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
196 int br_endy = (int)(br.y() + br.height());
198 roundRectPathUpper.moveTo(br_endx , br_halfy);
199 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
200 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
201 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
202 roundRectPathUpper.lineTo(br_startx , br_halfy);
204 return roundRectPathUpper;
207 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
208 QPainterPath roundRectPathLower;
209 double roundingY = 20;
210 double roundingX = 20;
213 int br_endx = (int)(br.x() + br .width() - offset);
214 int br_startx = (int)(br.x() + offset);
215 int br_starty = (int)(br.y());
216 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
217 int br_endy = (int)(br.y() + br.height() - 1);
219 while (roundingX > br.width() / 2) {
220 roundingX = roundingX / 2;
221 roundingY = roundingY / 2;
223 roundRectPathLower.moveTo(br_startx, br_halfy);
224 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
225 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
226 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
227 roundRectPathLower.lineTo(br_endx , br_halfy);
228 return roundRectPathLower;
231 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
232 if (m_keyframes.count() < 2) return;
234 double maxw = br.width() / m_cropDuration.frames(m_fps);
235 double maxh = br.height() / 100.0 * m_keyframeFactor;
241 // draw line showing default value
245 y1 = br.bottom() - m_keyframeDefault * maxh;
246 QLineF l(x1, y1, x2, y1);
247 QLineF l2 = painter->matrix().map(l);
248 painter->setPen(QColor(168, 168, 168, 180));
249 painter->drawLine(l2);
251 painter->setPen(QColor(108, 108, 108, 180));
252 painter->drawLine(l2);
253 painter->setPen(QColor(Qt::white));
257 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
258 QColor color(Qt::blue);
259 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
260 y1 = br.bottom() - i.value() * maxh;
262 while (i != m_keyframes.constEnd()) {
263 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
264 else color = QColor(Qt::blue);
266 if (i == m_keyframes.constEnd()) break;
267 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
268 y2 = br.bottom() - i.value() * maxh;
269 QLineF l(x1, y1, x2, y2);
270 l2 = painter->matrix().map(l);
271 painter->drawLine(l2);
273 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
278 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
281 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
282 QRectF br = sceneBoundingRect();
283 double maxw = br.width() / m_cropDuration.frames(m_fps);
284 double maxh = br.height() / 100.0 * m_keyframeFactor;
285 if (m_keyframes.count() > 1) {
286 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
289 while (i != m_keyframes.constEnd()) {
290 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
291 y1 = br.bottom() - i.value() * maxh;
292 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
293 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
295 } else if (x1 > pos.x()) break;
299 setToolTip(QString());
303 void AbstractClipItem::updateSelectedKeyFrame() {
304 if (m_editedKeyframe == -1) return;
305 QRectF br = sceneBoundingRect();
306 double maxw = br.width() / m_cropDuration.frames(m_fps);
307 double maxh = br.height() / 100.0 * m_keyframeFactor;
308 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
309 m_selectedKeyframe = m_editedKeyframe;
310 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
313 int AbstractClipItem::selectedKeyFramePos() const {
314 return m_editedKeyframe;
317 double AbstractClipItem::selectedKeyFrameValue() const {
318 return m_keyframes[m_editedKeyframe];
321 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
322 if (!m_keyframes.contains(m_selectedKeyframe)) return;
323 int newpos = (int) pos.frames(m_fps);
324 int start = m_cropStart.frames(m_fps);
325 int end = (m_cropStart + m_cropDuration).frames(m_fps);
326 newpos = qMax(newpos, start);
327 newpos = qMin(newpos, end);
328 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
329 // remove kexframe if it is dragged outside
330 m_keyframes.remove(m_selectedKeyframe);
331 m_selectedKeyframe = -1;
335 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
336 // remove kexframe if it is dragged outside
337 m_keyframes.remove(m_selectedKeyframe);
338 m_selectedKeyframe = -1;
342 double newval = qMax(value, 0.0);
343 newval = qMin(newval, 100.0);
344 newval = newval / m_keyframeFactor;
345 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
346 m_keyframes[newpos] = newval;
347 m_selectedKeyframe = newpos;
351 double AbstractClipItem::keyFrameFactor() const {
352 return m_keyframeFactor;
355 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
356 QRectF br = sceneBoundingRect();
357 double maxh = 100.0 / br.height() / m_keyframeFactor;
358 double newval = (br.bottom() - value) * maxh;
359 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
360 int newpos = (int) pos.frames(m_fps) ;
361 m_keyframes[newpos] = newval;
362 m_selectedKeyframe = newpos;
366 bool AbstractClipItem::hasKeyFrames() const {
367 return !m_keyframes.isEmpty();
370 QRect AbstractClipItem::visibleRect() {
372 if (scene()->views().size() > 0) {
373 rectInView = scene()->views()[0]->viewport()->rect();
374 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
375 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
376 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
381 CustomTrackScene* AbstractClipItem::projectScene() {
382 if (scene()) return static_cast <CustomTrackScene*>(scene());