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"
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 = cropStart();
43 itemInfo.track = track();
47 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack, bool checkCollision) {
48 qreal origX = pos().x();
49 qreal origY = pos().y();
52 //setRect(x * scale, origY + offset, rect().width(), rect().height());
53 //double xoffset = (x - m_startPos.frames(m_fps)) * scale;// - origX;
54 setPos((qreal) x * scale, (qreal) pos().y() + offset);
56 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
57 if (collisionList.size() == 0) m_track = newTrack;
59 for (int i = 0; i < collisionList.size(); ++i) {
60 QGraphicsItem *item = collisionList.at(i);
61 if (item->type() == type()) {
63 QRectF other = ((QGraphicsRectItem *)item)->rect();
64 if (x < m_startPos.frames(m_fps)) {
65 kDebug() << "COLLISION, MOVING TO------";
66 m_startPos = ((AbstractClipItem *)item)->endPos();
67 origX = (qreal) m_startPos.frames(m_fps) * scale;
68 } else if (x > m_startPos.frames(m_fps)) {
69 //kDebug() << "COLLISION, MOVING TO+++: "<<x<<", CLIP CURR POS: "<<m_startPos.frames(m_fps)<<", COLLIDING START: "<<((AbstractClipItem *)item)->startPos().frames(m_fps);
70 m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
71 origX = (qreal) m_startPos.frames(m_fps) * scale;
82 m_startPos = GenTime(x, m_fps);
86 GenTime AbstractClipItem::endPos() const {
87 return m_startPos + m_cropDuration;
90 int AbstractClipItem::track() const {
94 GenTime AbstractClipItem::cropStart() const {
98 void AbstractClipItem::setCropStart(GenTime pos) {
102 void AbstractClipItem::resizeStart(int posx, double scale) {
103 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
104 if (durationDiff == GenTime()) return;
105 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
107 if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
108 durationDiff = GenTime() - m_cropStart;
109 } else if (durationDiff >= m_cropDuration) {
110 durationDiff = m_cropDuration - GenTime(3, m_fps);
113 m_startPos += durationDiff;
114 if (type() == AVWIDGET) m_cropStart += durationDiff;
115 m_cropDuration = m_cropDuration - durationDiff;
117 setRect(0, 0, (qreal) m_cropDuration.frames(m_fps) * scale - .5, rect().height());
118 setPos((qreal) m_startPos.frames(m_fps) * scale, pos().y());
119 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
120 if (durationDiff < GenTime()) {
121 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
122 for (int i = 0; i < collisionList.size(); ++i) {
123 QGraphicsItem *item = collisionList.at(i);
124 if (item->type() == type() && item->pos().x() < pos().x()) {
125 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
126 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
127 setRect(0, 0, (qreal)(m_cropDuration - diff).frames(m_fps) * scale - .5, rect().height());
128 setPos((qreal)(m_startPos + diff).frames(m_fps) * scale, pos().y());
130 if (type() == AVWIDGET) m_cropStart += diff;
131 m_cropDuration = m_cropDuration - diff;
138 void AbstractClipItem::resizeEnd(int posx, double scale) {
139 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
140 if (durationDiff == GenTime()) return;
141 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
142 if (m_cropDuration + durationDiff <= GenTime()) {
143 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
144 } else if (m_cropStart + m_cropDuration + durationDiff >= maxDuration()) {
145 durationDiff = maxDuration() - m_cropDuration - m_cropStart;
147 m_cropDuration += durationDiff;
148 setRect(0, 0, (qreal) m_cropDuration.frames(m_fps) * scale - .5, rect().height());
149 if (durationDiff > GenTime()) {
150 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
151 for (int i = 0; i < collisionList.size(); ++i) {
152 QGraphicsItem *item = collisionList.at(i);
153 if (item->type() == type() && item->pos().x() > pos().x()) {
154 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
155 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
156 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
157 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
158 m_cropDuration = diff;
159 setRect(0, 0, (qreal)(m_cropDuration.frames(m_fps)) * scale - .5, rect().height());
166 GenTime AbstractClipItem::duration() const {
167 return m_cropDuration;
170 GenTime AbstractClipItem::startPos() const {
174 void AbstractClipItem::setTrack(int track) {
178 double AbstractClipItem::fps() const {
182 GenTime AbstractClipItem::maxDuration() const {
183 return m_maxDuration;
186 void AbstractClipItem::setMaxDuration(const GenTime &max) {
190 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
191 QPainterPath roundRectPathUpper;
192 double roundingY = 20;
193 double roundingX = 20;
196 while (roundingX > br.width() / 2) {
197 roundingX = roundingX / 2;
198 roundingY = roundingY / 2;
200 int br_endx = (int)(br.x() + br .width() - offset);
201 int br_startx = (int)(br.x() + offset);
202 int br_starty = (int)(br.y());
203 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
204 int br_endy = (int)(br.y() + br.height());
206 roundRectPathUpper.moveTo(br_endx , br_halfy);
207 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
208 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
209 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
210 roundRectPathUpper.lineTo(br_startx , br_halfy);
212 return roundRectPathUpper;
215 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
216 QPainterPath roundRectPathLower;
217 double roundingY = 20;
218 double roundingX = 20;
221 int br_endx = (int)(br.x() + br .width() - offset);
222 int br_startx = (int)(br.x() + offset);
223 int br_starty = (int)(br.y());
224 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
225 int br_endy = (int)(br.y() + br.height() - 1);
227 while (roundingX > br.width() / 2) {
228 roundingX = roundingX / 2;
229 roundingY = roundingY / 2;
231 roundRectPathLower.moveTo(br_startx, br_halfy);
232 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
233 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
234 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
235 roundRectPathLower.lineTo(br_endx , br_halfy);
236 return roundRectPathLower;
239 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
240 if (m_keyframes.count() < 2) return;
242 double maxw = br.width() / m_cropDuration.frames(m_fps);
243 double maxh = br.height() / 100.0 * m_keyframeFactor;
249 // draw line showing default value
253 y1 = br.bottom() - m_keyframeDefault * maxh;
254 QLineF l(x1, y1, x2, y1);
255 painter->setPen(QColor(168, 168, 168, 180));
256 painter->drawLine(l);
258 painter->setPen(QColor(108, 108, 108, 180));
259 painter->drawLine(l);
260 painter->setPen(QColor(Qt::white));
264 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
265 QColor color(Qt::blue);
266 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
267 y1 = br.bottom() - i.value() * maxh;
268 while (i != m_keyframes.constEnd()) {
269 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
270 else color = QColor(Qt::blue);
272 if (i == m_keyframes.constEnd()) break;
273 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
274 y2 = br.bottom() - i.value() * maxh;
275 QLineF l(x1, y1, x2, y2);
276 painter->drawLine(l);
278 painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
283 if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
286 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
287 QRectF br = sceneBoundingRect();
288 double maxw = br.width() / m_cropDuration.frames(m_fps);
289 double maxh = br.height() / 100.0 * m_keyframeFactor;
290 if (m_keyframes.count() > 1) {
291 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
294 while (i != m_keyframes.constEnd()) {
295 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
296 y1 = br.bottom() - i.value() * maxh;
297 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
298 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
300 } else if (x1 > pos.x()) break;
304 setToolTip(QString());
308 void AbstractClipItem::updateSelectedKeyFrame() {
309 if (m_editedKeyframe == -1) return;
310 QRectF br = sceneBoundingRect();
311 double maxw = br.width() / m_cropDuration.frames(m_fps);
312 double maxh = br.height() / 100.0 * m_keyframeFactor;
313 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
314 m_selectedKeyframe = m_editedKeyframe;
315 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
318 int AbstractClipItem::selectedKeyFramePos() const {
319 return m_editedKeyframe;
322 double AbstractClipItem::selectedKeyFrameValue() const {
323 return m_keyframes[m_editedKeyframe];
326 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
327 if (!m_keyframes.contains(m_selectedKeyframe)) return;
328 int newpos = (int) pos.frames(m_fps);
329 int start = m_cropStart.frames(m_fps);
330 int end = (m_cropStart + m_cropDuration).frames(m_fps);
331 newpos = qMax(newpos, start);
332 newpos = qMin(newpos, end);
333 if (value < -50 && 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 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
341 // remove kexframe if it is dragged outside
342 m_keyframes.remove(m_selectedKeyframe);
343 m_selectedKeyframe = -1;
347 double newval = qMax(value, 0.0);
348 newval = qMin(newval, 100.0);
349 newval = newval / m_keyframeFactor;
350 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
351 m_keyframes[newpos] = newval;
352 m_selectedKeyframe = newpos;
356 double AbstractClipItem::keyFrameFactor() const {
357 return m_keyframeFactor;
360 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
361 QRectF br = sceneBoundingRect();
362 double maxh = 100.0 / br.height() / m_keyframeFactor;
363 double newval = (br.bottom() - value) * maxh;
364 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
365 int newpos = (int) pos.frames(m_fps) ;
366 m_keyframes[newpos] = newval;
367 m_selectedKeyframe = newpos;
371 bool AbstractClipItem::hasKeyFrames() const {
372 return !m_keyframes.isEmpty();
375 QRect AbstractClipItem::visibleRect() {
377 if (scene()->views().size() > 0) {
378 rectInView = scene()->views()[0]->viewport()->rect();
379 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
380 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
381 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();