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"
32 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) {
33 setFlags(/*QGraphicsItem::ItemClipsToShape | */QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
35 m_startPos = info.startPos;
36 m_cropDuration = info.endPos - info.startPos;
39 ItemInfo AbstractClipItem::info() const {
41 itemInfo.startPos = startPos();
42 itemInfo.endPos = endPos();
43 itemInfo.cropStart = m_cropStart;
44 itemInfo.track = track();
48 GenTime AbstractClipItem::endPos() const {
49 return m_startPos + m_cropDuration;
52 int AbstractClipItem::track() const {
56 GenTime AbstractClipItem::cropStart() const {
60 GenTime AbstractClipItem::cropDuration() const {
61 return m_cropDuration;
64 void AbstractClipItem::setCropStart(GenTime pos) {
68 void AbstractClipItem::updateRectGeometry() {
69 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
72 void AbstractClipItem::resizeStart(int posx, double speed) {
73 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
74 if (durationDiff == GenTime()) return;
75 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
77 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
78 durationDiff = GenTime() - cropStart();
79 } else if (durationDiff >= m_cropDuration) {
81 if (m_cropDuration > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
85 m_startPos += durationDiff;
86 if (type() == AVWIDGET) m_cropStart += durationDiff * speed;
87 m_cropDuration = m_cropDuration - durationDiff * speed;
89 setPos(m_startPos.frames(m_fps), pos().y());
90 if (pos().x() != m_startPos.frames(m_fps)) {
91 GenTime diff = GenTime(pos().x(), m_fps) - m_startPos;
93 m_cropDuration = m_cropDuration - diff;
94 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
96 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
98 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
99 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
101 /* if (durationDiff < GenTime()) {
102 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
103 for (int i = 0; i < collisionList.size(); ++i) {
104 QGraphicsItem *item = collisionList.at(i);
105 if (item->type() == type() && item->pos().x() < pos().x()) {
106 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
107 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
108 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
109 setPos((m_startPos + diff).frames(m_fps), pos().y());
111 if (type() == AVWIDGET) m_cropStart += diff;
112 m_cropDuration = m_cropDuration - diff;
119 void AbstractClipItem::resizeEnd(int posx, double speed, bool updateKeyFrames) {
120 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
121 if (durationDiff == GenTime()) return;
122 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
123 kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
124 if (cropDuration() + durationDiff <= GenTime()) {
125 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
126 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
127 kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
128 durationDiff = maxDuration() - cropDuration() - cropStart();
130 //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
131 m_cropDuration += durationDiff * speed;
132 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
133 if (durationDiff > GenTime()) {
134 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
135 for (int i = 0; i < collisionList.size(); ++i) {
136 QGraphicsItem *item = collisionList.at(i);
137 if (item->type() == type() && item->pos().x() > pos().x()) {
138 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
139 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
140 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
141 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
142 m_cropDuration = diff;
143 setRect(0, 0, m_cropDuration.frames(m_fps) - 0.02, rect().height());
150 GenTime AbstractClipItem::duration() const {
151 return m_cropDuration;
154 GenTime AbstractClipItem::startPos() const {
158 void AbstractClipItem::setTrack(int track) {
162 double AbstractClipItem::fps() const {
166 GenTime AbstractClipItem::maxDuration() const {
167 return m_maxDuration;
170 void AbstractClipItem::setMaxDuration(const GenTime &max) {
174 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
175 QPainterPath roundRectPathUpper;
176 double roundingY = 20;
177 double roundingX = 20;
180 while (roundingX > br.width() / 2) {
181 roundingX = roundingX / 2;
182 roundingY = roundingY / 2;
184 int br_endx = (int)(br.x() + br .width() - offset);
185 int br_startx = (int)(br.x() + offset);
186 int br_starty = (int)(br.y());
187 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
188 int br_endy = (int)(br.y() + br.height());
190 roundRectPathUpper.moveTo(br_endx , br_halfy);
191 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
192 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
193 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
194 roundRectPathUpper.lineTo(br_startx , br_halfy);
196 return roundRectPathUpper;
199 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
200 QPainterPath roundRectPathLower;
201 double roundingY = 20;
202 double roundingX = 20;
205 int br_endx = (int)(br.x() + br .width() - offset);
206 int br_startx = (int)(br.x() + offset);
207 int br_starty = (int)(br.y());
208 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
209 int br_endy = (int)(br.y() + br.height() - 1);
211 while (roundingX > br.width() / 2) {
212 roundingX = roundingX / 2;
213 roundingY = roundingY / 2;
215 roundRectPathLower.moveTo(br_startx, br_halfy);
216 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
217 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
218 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
219 roundRectPathLower.lineTo(br_endx , br_halfy);
220 return roundRectPathLower;
223 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
224 if (m_keyframes.count() < 2) return;
226 double maxw = br.width() / m_cropDuration.frames(m_fps);
227 double maxh = br.height() / 100.0 * m_keyframeFactor;
233 // draw line showing default value
237 y1 = br.bottom() - m_keyframeDefault * maxh;
238 QLineF l(x1, y1, x2, y1);
239 QLineF l2 = painter->matrix().map(l);
240 painter->setPen(QColor(168, 168, 168, 180));
241 painter->drawLine(l2);
243 painter->setPen(QColor(108, 108, 108, 180));
244 painter->drawLine(l2);
245 painter->setPen(QColor(Qt::white));
249 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
250 QColor color(Qt::blue);
251 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
252 y1 = br.bottom() - i.value() * maxh;
254 while (i != m_keyframes.constEnd()) {
255 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
256 else color = QColor(Qt::blue);
258 if (i == m_keyframes.constEnd()) break;
259 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
260 y2 = br.bottom() - i.value() * maxh;
261 QLineF l(x1, y1, x2, y2);
262 l2 = painter->matrix().map(l);
263 painter->drawLine(l2);
265 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
270 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
273 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
274 QRectF br = sceneBoundingRect();
275 double maxw = br.width() / m_cropDuration.frames(m_fps);
276 double maxh = br.height() / 100.0 * m_keyframeFactor;
277 if (m_keyframes.count() > 1) {
278 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
281 while (i != m_keyframes.constEnd()) {
282 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
283 y1 = br.bottom() - i.value() * maxh;
284 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
285 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
287 } else if (x1 > pos.x()) break;
291 setToolTip(QString());
295 void AbstractClipItem::updateSelectedKeyFrame() {
296 if (m_editedKeyframe == -1) return;
297 QRectF br = sceneBoundingRect();
298 double maxw = br.width() / m_cropDuration.frames(m_fps);
299 double maxh = br.height() / 100.0 * m_keyframeFactor;
300 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
301 m_selectedKeyframe = m_editedKeyframe;
302 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
305 int AbstractClipItem::selectedKeyFramePos() const {
306 return m_editedKeyframe;
309 double AbstractClipItem::selectedKeyFrameValue() const {
310 return m_keyframes[m_editedKeyframe];
313 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
314 if (!m_keyframes.contains(m_selectedKeyframe)) return;
315 int newpos = (int) pos.frames(m_fps);
316 int start = m_cropStart.frames(m_fps);
317 int end = (m_cropStart + m_cropDuration).frames(m_fps);
318 newpos = qMax(newpos, start);
319 newpos = qMin(newpos, end);
320 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
321 // remove kexframe if it is dragged outside
322 m_keyframes.remove(m_selectedKeyframe);
323 m_selectedKeyframe = -1;
327 if (value > 150 && 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 double newval = qMax(value, 0.0);
335 newval = qMin(newval, 100.0);
336 newval = newval / m_keyframeFactor;
337 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
338 m_keyframes[newpos] = newval;
339 m_selectedKeyframe = newpos;
343 double AbstractClipItem::keyFrameFactor() const {
344 return m_keyframeFactor;
347 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
348 QRectF br = sceneBoundingRect();
349 double maxh = 100.0 / br.height() / m_keyframeFactor;
350 double newval = (br.bottom() - value) * maxh;
351 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
352 int newpos = (int) pos.frames(m_fps) ;
353 m_keyframes[newpos] = newval;
354 m_selectedKeyframe = newpos;
358 bool AbstractClipItem::hasKeyFrames() const {
359 return !m_keyframes.isEmpty();
362 QRect AbstractClipItem::visibleRect() {
364 if (scene()->views().size() > 0) {
365 rectInView = scene()->views()[0]->viewport()->rect();
366 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
367 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
368 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
373 CustomTrackScene* AbstractClipItem::projectScene() {
374 if (scene()) return static_cast <CustomTrackScene*>(scene());