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(m_startPos.frames(m_fps), pos().y());
92 if (pos().x() != m_startPos.frames(m_fps)) {
93 GenTime diff = GenTime(pos().x(), m_fps) - m_startPos;
95 m_cropDuration = m_cropDuration - diff;
96 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
97 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
100 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
101 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
103 if (durationDiff < GenTime()) {
104 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
105 for (int i = 0; i < collisionList.size(); ++i) {
106 QGraphicsItem *item = collisionList.at(i);
107 if (item->type() == type() && item->pos().x() < pos().x()) {
108 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
109 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
110 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
111 setPos((m_startPos + diff).frames(m_fps), pos().y());
113 if (type() == AVWIDGET) m_cropStart += diff;
114 m_cropDuration = m_cropDuration - diff;
121 void AbstractClipItem::resizeEnd(int posx, double speed, bool updateKeyFrames) {
122 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
123 if (durationDiff == GenTime()) return;
124 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
125 kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
126 if (cropDuration() + durationDiff <= GenTime()) {
127 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
128 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
129 kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
130 durationDiff = maxDuration() - cropDuration() - cropStart();
132 kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
133 m_cropDuration += durationDiff * speed;
134 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
135 if (durationDiff > GenTime()) {
136 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
137 for (int i = 0; i < collisionList.size(); ++i) {
138 QGraphicsItem *item = collisionList.at(i);
139 if (item->type() == type() && item->pos().x() > pos().x()) {
140 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
141 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
142 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
143 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
144 m_cropDuration = diff;
145 setRect(0, 0, m_cropDuration.frames(m_fps) - 0.02, rect().height());
152 GenTime AbstractClipItem::duration() const {
153 return m_cropDuration;
156 GenTime AbstractClipItem::startPos() const {
160 void AbstractClipItem::setTrack(int track) {
164 double AbstractClipItem::fps() const {
168 GenTime AbstractClipItem::maxDuration() const {
169 return m_maxDuration;
172 void AbstractClipItem::setMaxDuration(const GenTime &max) {
176 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
177 QPainterPath roundRectPathUpper;
178 double roundingY = 20;
179 double roundingX = 20;
182 while (roundingX > br.width() / 2) {
183 roundingX = roundingX / 2;
184 roundingY = roundingY / 2;
186 int br_endx = (int)(br.x() + br .width() - offset);
187 int br_startx = (int)(br.x() + offset);
188 int br_starty = (int)(br.y());
189 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
190 int br_endy = (int)(br.y() + br.height());
192 roundRectPathUpper.moveTo(br_endx , br_halfy);
193 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
194 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
195 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
196 roundRectPathUpper.lineTo(br_startx , br_halfy);
198 return roundRectPathUpper;
201 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
202 QPainterPath roundRectPathLower;
203 double roundingY = 20;
204 double roundingX = 20;
207 int br_endx = (int)(br.x() + br .width() - offset);
208 int br_startx = (int)(br.x() + offset);
209 int br_starty = (int)(br.y());
210 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
211 int br_endy = (int)(br.y() + br.height() - 1);
213 while (roundingX > br.width() / 2) {
214 roundingX = roundingX / 2;
215 roundingY = roundingY / 2;
217 roundRectPathLower.moveTo(br_startx, br_halfy);
218 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
219 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
220 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
221 roundRectPathLower.lineTo(br_endx , br_halfy);
222 return roundRectPathLower;
225 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
226 if (m_keyframes.count() < 2) return;
228 double maxw = br.width() / m_cropDuration.frames(m_fps);
229 double maxh = br.height() / 100.0 * m_keyframeFactor;
235 // draw line showing default value
239 y1 = br.bottom() - m_keyframeDefault * maxh;
240 QLineF l(x1, y1, x2, y1);
241 QLineF l2 = painter->matrix().map(l);
242 painter->setPen(QColor(168, 168, 168, 180));
243 painter->drawLine(l2);
245 painter->setPen(QColor(108, 108, 108, 180));
246 painter->drawLine(l2);
247 painter->setPen(QColor(Qt::white));
251 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
252 QColor color(Qt::blue);
253 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
254 y1 = br.bottom() - i.value() * maxh;
256 while (i != m_keyframes.constEnd()) {
257 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
258 else color = QColor(Qt::blue);
260 if (i == m_keyframes.constEnd()) break;
261 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
262 y2 = br.bottom() - i.value() * maxh;
263 QLineF l(x1, y1, x2, y2);
264 l2 = painter->matrix().map(l);
265 painter->drawLine(l2);
267 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
272 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
275 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
276 QRectF br = sceneBoundingRect();
277 double maxw = br.width() / m_cropDuration.frames(m_fps);
278 double maxh = br.height() / 100.0 * m_keyframeFactor;
279 if (m_keyframes.count() > 1) {
280 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
283 while (i != m_keyframes.constEnd()) {
284 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
285 y1 = br.bottom() - i.value() * maxh;
286 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
287 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
289 } else if (x1 > pos.x()) break;
293 setToolTip(QString());
297 void AbstractClipItem::updateSelectedKeyFrame() {
298 if (m_editedKeyframe == -1) return;
299 QRectF br = sceneBoundingRect();
300 double maxw = br.width() / m_cropDuration.frames(m_fps);
301 double maxh = br.height() / 100.0 * m_keyframeFactor;
302 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
303 m_selectedKeyframe = m_editedKeyframe;
304 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
307 int AbstractClipItem::selectedKeyFramePos() const {
308 return m_editedKeyframe;
311 double AbstractClipItem::selectedKeyFrameValue() const {
312 return m_keyframes[m_editedKeyframe];
315 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
316 if (!m_keyframes.contains(m_selectedKeyframe)) return;
317 int newpos = (int) pos.frames(m_fps);
318 int start = m_cropStart.frames(m_fps);
319 int end = (m_cropStart + m_cropDuration).frames(m_fps);
320 newpos = qMax(newpos, start);
321 newpos = qMin(newpos, end);
322 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
323 // remove kexframe if it is dragged outside
324 m_keyframes.remove(m_selectedKeyframe);
325 m_selectedKeyframe = -1;
329 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
330 // remove kexframe if it is dragged outside
331 m_keyframes.remove(m_selectedKeyframe);
332 m_selectedKeyframe = -1;
336 double newval = qMax(value, 0.0);
337 newval = qMin(newval, 100.0);
338 newval = newval / m_keyframeFactor;
339 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
340 m_keyframes[newpos] = newval;
341 m_selectedKeyframe = newpos;
345 double AbstractClipItem::keyFrameFactor() const {
346 return m_keyframeFactor;
349 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
350 QRectF br = sceneBoundingRect();
351 double maxh = 100.0 / br.height() / m_keyframeFactor;
352 double newval = (br.bottom() - value) * maxh;
353 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
354 int newpos = (int) pos.frames(m_fps) ;
355 m_keyframes[newpos] = newval;
356 m_selectedKeyframe = newpos;
360 bool AbstractClipItem::hasKeyFrames() const {
361 return !m_keyframes.isEmpty();
364 QRect AbstractClipItem::visibleRect() {
366 if (scene()->views().size() > 0) {
367 rectInView = scene()->views()[0]->viewport()->rect();
368 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
369 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
370 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
375 CustomTrackScene* AbstractClipItem::projectScene() {
376 if (scene()) return static_cast <CustomTrackScene*>(scene());