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: CROP=" << m_cropStart.frames(25) << " - " << cropStart().frames(25);
77 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
78 durationDiff = GenTime() - cropStart();
79 } else if (durationDiff >= m_cropDuration) {
80 durationDiff = m_cropDuration - GenTime(3, m_fps);
83 m_startPos += durationDiff;
84 if (type() == AVWIDGET) m_cropStart += durationDiff * speed;
85 m_cropDuration = m_cropDuration - durationDiff * speed;
87 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
88 setPos((qreal) m_startPos.frames(m_fps), pos().y());
89 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
90 if (durationDiff < GenTime()) {
91 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
92 for (int i = 0; i < collisionList.size(); ++i) {
93 QGraphicsItem *item = collisionList.at(i);
94 if (item->type() == type() && item->pos().x() < pos().x()) {
95 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
96 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
97 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
98 setPos((qreal)(m_startPos + diff).frames(m_fps), pos().y());
100 if (type() == AVWIDGET) m_cropStart += diff;
101 m_cropDuration = m_cropDuration - diff;
108 void AbstractClipItem::resizeEnd(int posx, double speed, bool updateKeyFrames) {
109 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
110 if (durationDiff == GenTime()) return;
111 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
112 kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
113 if (cropDuration() + durationDiff <= GenTime()) {
114 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
115 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
116 kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
117 durationDiff = maxDuration() - cropDuration() - cropStart();
119 kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
120 m_cropDuration += durationDiff * speed;
121 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
122 if (durationDiff > GenTime()) {
123 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
124 for (int i = 0; i < collisionList.size(); ++i) {
125 QGraphicsItem *item = collisionList.at(i);
126 if (item->type() == type() && item->pos().x() > pos().x()) {
127 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
128 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
129 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
130 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
131 m_cropDuration = diff;
132 setRect(0, 0, (m_cropDuration.frames(m_fps)) - 0.02, rect().height());
139 GenTime AbstractClipItem::duration() const {
140 return m_cropDuration;
143 GenTime AbstractClipItem::startPos() const {
147 void AbstractClipItem::setTrack(int track) {
151 double AbstractClipItem::fps() const {
155 GenTime AbstractClipItem::maxDuration() const {
156 return m_maxDuration;
159 void AbstractClipItem::setMaxDuration(const GenTime &max) {
163 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
164 QPainterPath roundRectPathUpper;
165 double roundingY = 20;
166 double roundingX = 20;
169 while (roundingX > br.width() / 2) {
170 roundingX = roundingX / 2;
171 roundingY = roundingY / 2;
173 int br_endx = (int)(br.x() + br .width() - offset);
174 int br_startx = (int)(br.x() + offset);
175 int br_starty = (int)(br.y());
176 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
177 int br_endy = (int)(br.y() + br.height());
179 roundRectPathUpper.moveTo(br_endx , br_halfy);
180 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
181 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
182 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
183 roundRectPathUpper.lineTo(br_startx , br_halfy);
185 return roundRectPathUpper;
188 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
189 QPainterPath roundRectPathLower;
190 double roundingY = 20;
191 double roundingX = 20;
194 int br_endx = (int)(br.x() + br .width() - offset);
195 int br_startx = (int)(br.x() + offset);
196 int br_starty = (int)(br.y());
197 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
198 int br_endy = (int)(br.y() + br.height() - 1);
200 while (roundingX > br.width() / 2) {
201 roundingX = roundingX / 2;
202 roundingY = roundingY / 2;
204 roundRectPathLower.moveTo(br_startx, br_halfy);
205 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
206 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
207 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
208 roundRectPathLower.lineTo(br_endx , br_halfy);
209 return roundRectPathLower;
212 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
213 if (m_keyframes.count() < 2) return;
215 double maxw = br.width() / m_cropDuration.frames(m_fps);
216 double maxh = br.height() / 100.0 * m_keyframeFactor;
222 // draw line showing default value
226 y1 = br.bottom() - m_keyframeDefault * maxh;
227 QLineF l(x1, y1, x2, y1);
228 QLineF l2 = painter->matrix().map(l);
229 painter->setPen(QColor(168, 168, 168, 180));
230 painter->drawLine(l2);
232 painter->setPen(QColor(108, 108, 108, 180));
233 painter->drawLine(l2);
234 painter->setPen(QColor(Qt::white));
238 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
239 QColor color(Qt::blue);
240 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
241 y1 = br.bottom() - i.value() * maxh;
243 while (i != m_keyframes.constEnd()) {
244 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
245 else color = QColor(Qt::blue);
247 if (i == m_keyframes.constEnd()) break;
248 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
249 y2 = br.bottom() - i.value() * maxh;
250 QLineF l(x1, y1, x2, y2);
251 l2 = painter->matrix().map(l);
252 painter->drawLine(l2);
254 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
259 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
262 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
263 QRectF br = sceneBoundingRect();
264 double maxw = br.width() / m_cropDuration.frames(m_fps);
265 double maxh = br.height() / 100.0 * m_keyframeFactor;
266 if (m_keyframes.count() > 1) {
267 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
270 while (i != m_keyframes.constEnd()) {
271 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
272 y1 = br.bottom() - i.value() * maxh;
273 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
274 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
276 } else if (x1 > pos.x()) break;
280 setToolTip(QString());
284 void AbstractClipItem::updateSelectedKeyFrame() {
285 if (m_editedKeyframe == -1) return;
286 QRectF br = sceneBoundingRect();
287 double maxw = br.width() / m_cropDuration.frames(m_fps);
288 double maxh = br.height() / 100.0 * m_keyframeFactor;
289 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
290 m_selectedKeyframe = m_editedKeyframe;
291 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
294 int AbstractClipItem::selectedKeyFramePos() const {
295 return m_editedKeyframe;
298 double AbstractClipItem::selectedKeyFrameValue() const {
299 return m_keyframes[m_editedKeyframe];
302 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
303 if (!m_keyframes.contains(m_selectedKeyframe)) return;
304 int newpos = (int) pos.frames(m_fps);
305 int start = m_cropStart.frames(m_fps);
306 int end = (m_cropStart + m_cropDuration).frames(m_fps);
307 newpos = qMax(newpos, start);
308 newpos = qMin(newpos, end);
309 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
310 // remove kexframe if it is dragged outside
311 m_keyframes.remove(m_selectedKeyframe);
312 m_selectedKeyframe = -1;
316 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
317 // remove kexframe if it is dragged outside
318 m_keyframes.remove(m_selectedKeyframe);
319 m_selectedKeyframe = -1;
323 double newval = qMax(value, 0.0);
324 newval = qMin(newval, 100.0);
325 newval = newval / m_keyframeFactor;
326 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
327 m_keyframes[newpos] = newval;
328 m_selectedKeyframe = newpos;
332 double AbstractClipItem::keyFrameFactor() const {
333 return m_keyframeFactor;
336 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
337 QRectF br = sceneBoundingRect();
338 double maxh = 100.0 / br.height() / m_keyframeFactor;
339 double newval = (br.bottom() - value) * maxh;
340 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
341 int newpos = (int) pos.frames(m_fps) ;
342 m_keyframes[newpos] = newval;
343 m_selectedKeyframe = newpos;
347 bool AbstractClipItem::hasKeyFrames() const {
348 return !m_keyframes.isEmpty();
351 QRect AbstractClipItem::visibleRect() {
353 if (scene()->views().size() > 0) {
354 rectInView = scene()->views()[0]->viewport()->rect();
355 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
356 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
357 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
362 CustomTrackScene* AbstractClipItem::projectScene() {
363 if (scene()) return static_cast <CustomTrackScene*>(scene());