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 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
90 setPos((qreal) m_startPos.frames(m_fps), pos().y());
92 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
93 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
94 if (durationDiff < GenTime()) {
95 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
96 for (int i = 0; i < collisionList.size(); ++i) {
97 QGraphicsItem *item = collisionList.at(i);
98 if (item->type() == type() && item->pos().x() < pos().x()) {
99 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
100 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
101 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
102 setPos((m_startPos + diff).frames(m_fps), pos().y());
104 if (type() == AVWIDGET) m_cropStart += diff;
105 m_cropDuration = m_cropDuration - diff;
112 void AbstractClipItem::resizeEnd(int posx, double speed, bool updateKeyFrames) {
113 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
114 if (durationDiff == GenTime()) return;
115 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
116 kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
117 if (cropDuration() + durationDiff <= GenTime()) {
118 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
119 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
120 kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
121 durationDiff = maxDuration() - cropDuration() - cropStart();
123 kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
124 m_cropDuration += durationDiff * speed;
125 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
126 if (durationDiff > GenTime()) {
127 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
128 for (int i = 0; i < collisionList.size(); ++i) {
129 QGraphicsItem *item = collisionList.at(i);
130 if (item->type() == type() && item->pos().x() > pos().x()) {
131 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
132 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
133 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
134 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
135 m_cropDuration = diff;
136 setRect(0, 0, m_cropDuration.frames(m_fps) - 0.02, rect().height());
143 GenTime AbstractClipItem::duration() const {
144 return m_cropDuration;
147 GenTime AbstractClipItem::startPos() const {
151 void AbstractClipItem::setTrack(int track) {
155 double AbstractClipItem::fps() const {
159 GenTime AbstractClipItem::maxDuration() const {
160 return m_maxDuration;
163 void AbstractClipItem::setMaxDuration(const GenTime &max) {
167 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
168 QPainterPath roundRectPathUpper;
169 double roundingY = 20;
170 double roundingX = 20;
173 while (roundingX > br.width() / 2) {
174 roundingX = roundingX / 2;
175 roundingY = roundingY / 2;
177 int br_endx = (int)(br.x() + br .width() - offset);
178 int br_startx = (int)(br.x() + offset);
179 int br_starty = (int)(br.y());
180 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
181 int br_endy = (int)(br.y() + br.height());
183 roundRectPathUpper.moveTo(br_endx , br_halfy);
184 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
185 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
186 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
187 roundRectPathUpper.lineTo(br_startx , br_halfy);
189 return roundRectPathUpper;
192 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
193 QPainterPath roundRectPathLower;
194 double roundingY = 20;
195 double roundingX = 20;
198 int br_endx = (int)(br.x() + br .width() - offset);
199 int br_startx = (int)(br.x() + offset);
200 int br_starty = (int)(br.y());
201 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
202 int br_endy = (int)(br.y() + br.height() - 1);
204 while (roundingX > br.width() / 2) {
205 roundingX = roundingX / 2;
206 roundingY = roundingY / 2;
208 roundRectPathLower.moveTo(br_startx, br_halfy);
209 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
210 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
211 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
212 roundRectPathLower.lineTo(br_endx , br_halfy);
213 return roundRectPathLower;
216 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
217 if (m_keyframes.count() < 2) return;
219 double maxw = br.width() / m_cropDuration.frames(m_fps);
220 double maxh = br.height() / 100.0 * m_keyframeFactor;
226 // draw line showing default value
230 y1 = br.bottom() - m_keyframeDefault * maxh;
231 QLineF l(x1, y1, x2, y1);
232 QLineF l2 = painter->matrix().map(l);
233 painter->setPen(QColor(168, 168, 168, 180));
234 painter->drawLine(l2);
236 painter->setPen(QColor(108, 108, 108, 180));
237 painter->drawLine(l2);
238 painter->setPen(QColor(Qt::white));
242 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
243 QColor color(Qt::blue);
244 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
245 y1 = br.bottom() - i.value() * maxh;
247 while (i != m_keyframes.constEnd()) {
248 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
249 else color = QColor(Qt::blue);
251 if (i == m_keyframes.constEnd()) break;
252 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
253 y2 = br.bottom() - i.value() * maxh;
254 QLineF l(x1, y1, x2, y2);
255 l2 = painter->matrix().map(l);
256 painter->drawLine(l2);
258 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
263 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
266 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
267 QRectF br = sceneBoundingRect();
268 double maxw = br.width() / m_cropDuration.frames(m_fps);
269 double maxh = br.height() / 100.0 * m_keyframeFactor;
270 if (m_keyframes.count() > 1) {
271 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
274 while (i != m_keyframes.constEnd()) {
275 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
276 y1 = br.bottom() - i.value() * maxh;
277 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
278 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
280 } else if (x1 > pos.x()) break;
284 setToolTip(QString());
288 void AbstractClipItem::updateSelectedKeyFrame() {
289 if (m_editedKeyframe == -1) return;
290 QRectF br = sceneBoundingRect();
291 double maxw = br.width() / m_cropDuration.frames(m_fps);
292 double maxh = br.height() / 100.0 * m_keyframeFactor;
293 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
294 m_selectedKeyframe = m_editedKeyframe;
295 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
298 int AbstractClipItem::selectedKeyFramePos() const {
299 return m_editedKeyframe;
302 double AbstractClipItem::selectedKeyFrameValue() const {
303 return m_keyframes[m_editedKeyframe];
306 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
307 if (!m_keyframes.contains(m_selectedKeyframe)) return;
308 int newpos = (int) pos.frames(m_fps);
309 int start = m_cropStart.frames(m_fps);
310 int end = (m_cropStart + m_cropDuration).frames(m_fps);
311 newpos = qMax(newpos, start);
312 newpos = qMin(newpos, end);
313 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
314 // remove kexframe if it is dragged outside
315 m_keyframes.remove(m_selectedKeyframe);
316 m_selectedKeyframe = -1;
320 if (value > 150 && 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 double newval = qMax(value, 0.0);
328 newval = qMin(newval, 100.0);
329 newval = newval / m_keyframeFactor;
330 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
331 m_keyframes[newpos] = newval;
332 m_selectedKeyframe = newpos;
336 double AbstractClipItem::keyFrameFactor() const {
337 return m_keyframeFactor;
340 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
341 QRectF br = sceneBoundingRect();
342 double maxh = 100.0 / br.height() / m_keyframeFactor;
343 double newval = (br.bottom() - value) * maxh;
344 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
345 int newpos = (int) pos.frames(m_fps) ;
346 m_keyframes[newpos] = newval;
347 m_selectedKeyframe = newpos;
351 bool AbstractClipItem::hasKeyFrames() const {
352 return !m_keyframes.isEmpty();
355 QRect AbstractClipItem::visibleRect() {
357 if (scene()->views().size() > 0) {
358 rectInView = scene()->views()[0]->viewport()->rect();
359 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
360 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
361 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
366 CustomTrackScene* AbstractClipItem::projectScene() {
367 if (scene()) return static_cast <CustomTrackScene*>(scene());