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