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 (pos().x() != m_startPos.frames(m_fps)) {
98 GenTime diff = GenTime(pos().x(), m_fps) - m_startPos;
100 m_cropDuration = m_cropDuration - diff;
101 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
102 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
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;
150 setRect(0, 0, m_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);
195 int br_endy = (int)(br.y() + br.height());
197 roundRectPathUpper.moveTo(br_endx , br_halfy);
198 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
199 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
200 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
201 roundRectPathUpper.lineTo(br_startx , br_halfy);
203 return roundRectPathUpper;
206 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
207 QPainterPath roundRectPathLower;
208 double roundingY = 20;
209 double roundingX = 20;
212 int br_endx = (int)(br.x() + br .width() - offset);
213 int br_startx = (int)(br.x() + offset);
214 int br_starty = (int)(br.y());
215 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
216 int br_endy = (int)(br.y() + br.height() - 1);
218 while (roundingX > br.width() / 2) {
219 roundingX = roundingX / 2;
220 roundingY = roundingY / 2;
222 roundRectPathLower.moveTo(br_startx, br_halfy);
223 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
224 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
225 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
226 roundRectPathLower.lineTo(br_endx , br_halfy);
227 return roundRectPathLower;
230 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
231 if (m_keyframes.count() < 2) return;
233 double maxw = br.width() / m_cropDuration.frames(m_fps);
234 double maxh = br.height() / 100.0 * m_keyframeFactor;
240 // draw line showing default value
244 y1 = br.bottom() - m_keyframeDefault * maxh;
245 QLineF l(x1, y1, x2, y1);
246 QLineF l2 = painter->matrix().map(l);
247 painter->setPen(QColor(168, 168, 168, 180));
248 painter->drawLine(l2);
250 painter->setPen(QColor(108, 108, 108, 180));
251 painter->drawLine(l2);
252 painter->setPen(QColor(Qt::white));
256 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
257 QColor color(Qt::blue);
258 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
259 y1 = br.bottom() - i.value() * maxh;
261 while (i != m_keyframes.constEnd()) {
262 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
263 else color = QColor(Qt::blue);
265 if (i == m_keyframes.constEnd()) break;
266 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
267 y2 = br.bottom() - i.value() * maxh;
268 QLineF l(x1, y1, x2, y2);
269 l2 = painter->matrix().map(l);
270 painter->drawLine(l2);
272 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
277 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
280 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
281 QRectF br = sceneBoundingRect();
282 double maxw = br.width() / m_cropDuration.frames(m_fps);
283 double maxh = br.height() / 100.0 * m_keyframeFactor;
284 if (m_keyframes.count() > 1) {
285 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
288 while (i != m_keyframes.constEnd()) {
289 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
290 y1 = br.bottom() - i.value() * maxh;
291 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
292 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
294 } else if (x1 > pos.x()) break;
298 setToolTip(QString());
302 void AbstractClipItem::updateSelectedKeyFrame() {
303 if (m_editedKeyframe == -1) return;
304 QRectF br = sceneBoundingRect();
305 double maxw = br.width() / m_cropDuration.frames(m_fps);
306 double maxh = br.height() / 100.0 * m_keyframeFactor;
307 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
308 m_selectedKeyframe = m_editedKeyframe;
309 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
312 int AbstractClipItem::selectedKeyFramePos() const {
313 return m_editedKeyframe;
316 double AbstractClipItem::selectedKeyFrameValue() const {
317 return m_keyframes[m_editedKeyframe];
320 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
321 if (!m_keyframes.contains(m_selectedKeyframe)) return;
322 int newpos = (int) pos.frames(m_fps);
323 int start = m_cropStart.frames(m_fps);
324 int end = (m_cropStart + m_cropDuration).frames(m_fps);
325 newpos = qMax(newpos, start);
326 newpos = qMin(newpos, end);
327 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
328 // remove kexframe if it is dragged outside
329 m_keyframes.remove(m_selectedKeyframe);
330 m_selectedKeyframe = -1;
334 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
335 // remove kexframe if it is dragged outside
336 m_keyframes.remove(m_selectedKeyframe);
337 m_selectedKeyframe = -1;
341 double newval = qMax(value, 0.0);
342 newval = qMin(newval, 100.0);
343 newval = newval / m_keyframeFactor;
344 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
345 m_keyframes[newpos] = newval;
346 m_selectedKeyframe = newpos;
350 double AbstractClipItem::keyFrameFactor() const {
351 return m_keyframeFactor;
354 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
355 QRectF br = sceneBoundingRect();
356 double maxh = 100.0 / br.height() / m_keyframeFactor;
357 double newval = (br.bottom() - value) * maxh;
358 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
359 int newpos = (int) pos.frames(m_fps) ;
360 m_keyframes[newpos] = newval;
361 m_selectedKeyframe = newpos;
365 bool AbstractClipItem::hasKeyFrames() const {
366 return !m_keyframes.isEmpty();
369 QRect AbstractClipItem::visibleRect() {
371 if (scene()->views().size() > 0) {
372 rectInView = scene()->views()[0]->viewport()->rect();
373 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
374 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
375 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
380 CustomTrackScene* AbstractClipItem::projectScene() {
381 if (scene()) return static_cast <CustomTrackScene*>(scene());