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 = 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 void AbstractClipItem::setCropStart(GenTime pos) {
64 void AbstractClipItem::resizeStart(int posx) {
65 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
66 if (durationDiff == GenTime()) return;
67 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
69 if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
70 durationDiff = GenTime() - m_cropStart;
71 } else if (durationDiff >= m_cropDuration) {
72 durationDiff = m_cropDuration - GenTime(3, m_fps);
75 m_startPos += durationDiff;
76 if (type() == AVWIDGET) m_cropStart += durationDiff;
77 m_cropDuration = m_cropDuration - durationDiff;
79 setRect(0, 0, m_cropDuration.frames(m_fps) - 0.02, rect().height());
80 setPos((qreal) m_startPos.frames(m_fps), pos().y());
81 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
82 if (durationDiff < GenTime()) {
83 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
84 for (int i = 0; i < collisionList.size(); ++i) {
85 QGraphicsItem *item = collisionList.at(i);
86 if (item->type() == type() && item->pos().x() < pos().x()) {
87 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
88 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
89 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
90 setPos((qreal)(m_startPos + diff).frames(m_fps), pos().y());
92 if (type() == AVWIDGET) m_cropStart += diff;
93 m_cropDuration = m_cropDuration - diff;
100 void AbstractClipItem::resizeEnd(int posx) {
101 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
102 if (durationDiff == GenTime()) return;
103 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
104 if (m_cropDuration + durationDiff <= GenTime()) {
105 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
106 } else if (m_cropStart + m_cropDuration + durationDiff >= maxDuration()) {
107 durationDiff = maxDuration() - m_cropDuration - m_cropStart;
109 m_cropDuration += durationDiff;
110 setRect(0, 0, m_cropDuration.frames(m_fps) - 0.02, rect().height());
111 if (durationDiff > GenTime()) {
112 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
113 for (int i = 0; i < collisionList.size(); ++i) {
114 QGraphicsItem *item = collisionList.at(i);
115 if (item->type() == type() && item->pos().x() > pos().x()) {
116 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
117 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
118 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();
119 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
120 m_cropDuration = diff;
121 setRect(0, 0, (m_cropDuration.frames(m_fps)) - 0.02, rect().height());
128 GenTime AbstractClipItem::duration() const {
129 return m_cropDuration;
132 GenTime AbstractClipItem::startPos() const {
136 void AbstractClipItem::setTrack(int track) {
140 double AbstractClipItem::fps() const {
144 GenTime AbstractClipItem::maxDuration() const {
145 return m_maxDuration;
148 void AbstractClipItem::setMaxDuration(const GenTime &max) {
152 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
153 QPainterPath roundRectPathUpper;
154 double roundingY = 20;
155 double roundingX = 20;
158 while (roundingX > br.width() / 2) {
159 roundingX = roundingX / 2;
160 roundingY = roundingY / 2;
162 int br_endx = (int)(br.x() + br .width() - offset);
163 int br_startx = (int)(br.x() + offset);
164 int br_starty = (int)(br.y());
165 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
166 int br_endy = (int)(br.y() + br.height());
168 roundRectPathUpper.moveTo(br_endx , br_halfy);
169 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
170 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
171 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
172 roundRectPathUpper.lineTo(br_startx , br_halfy);
174 return roundRectPathUpper;
177 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
178 QPainterPath roundRectPathLower;
179 double roundingY = 20;
180 double roundingX = 20;
183 int br_endx = (int)(br.x() + br .width() - offset);
184 int br_startx = (int)(br.x() + offset);
185 int br_starty = (int)(br.y());
186 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
187 int br_endy = (int)(br.y() + br.height() - 1);
189 while (roundingX > br.width() / 2) {
190 roundingX = roundingX / 2;
191 roundingY = roundingY / 2;
193 roundRectPathLower.moveTo(br_startx, br_halfy);
194 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
195 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
196 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
197 roundRectPathLower.lineTo(br_endx , br_halfy);
198 return roundRectPathLower;
201 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
202 if (m_keyframes.count() < 2) return;
204 double maxw = br.width() / m_cropDuration.frames(m_fps);
205 double maxh = br.height() / 100.0 * m_keyframeFactor;
211 // draw line showing default value
215 y1 = br.bottom() - m_keyframeDefault * maxh;
216 QLineF l(x1, y1, x2, y1);
217 QLineF l2 = painter->matrix().map(l);
218 painter->setPen(QColor(168, 168, 168, 180));
219 painter->drawLine(l2);
221 painter->setPen(QColor(108, 108, 108, 180));
222 painter->drawLine(l2);
223 painter->setPen(QColor(Qt::white));
227 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
228 QColor color(Qt::blue);
229 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
230 y1 = br.bottom() - i.value() * maxh;
232 while (i != m_keyframes.constEnd()) {
233 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
234 else color = QColor(Qt::blue);
236 if (i == m_keyframes.constEnd()) break;
237 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
238 y2 = br.bottom() - i.value() * maxh;
239 QLineF l(x1, y1, x2, y2);
240 l2 = painter->matrix().map(l);
241 painter->drawLine(l2);
243 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
248 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
251 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
252 QRectF br = sceneBoundingRect();
253 double maxw = br.width() / m_cropDuration.frames(m_fps);
254 double maxh = br.height() / 100.0 * m_keyframeFactor;
255 if (m_keyframes.count() > 1) {
256 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
259 while (i != m_keyframes.constEnd()) {
260 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
261 y1 = br.bottom() - i.value() * maxh;
262 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
263 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
265 } else if (x1 > pos.x()) break;
269 setToolTip(QString());
273 void AbstractClipItem::updateSelectedKeyFrame() {
274 if (m_editedKeyframe == -1) return;
275 QRectF br = sceneBoundingRect();
276 double maxw = br.width() / m_cropDuration.frames(m_fps);
277 double maxh = br.height() / 100.0 * m_keyframeFactor;
278 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
279 m_selectedKeyframe = m_editedKeyframe;
280 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
283 int AbstractClipItem::selectedKeyFramePos() const {
284 return m_editedKeyframe;
287 double AbstractClipItem::selectedKeyFrameValue() const {
288 return m_keyframes[m_editedKeyframe];
291 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
292 if (!m_keyframes.contains(m_selectedKeyframe)) return;
293 int newpos = (int) pos.frames(m_fps);
294 int start = m_cropStart.frames(m_fps);
295 int end = (m_cropStart + m_cropDuration).frames(m_fps);
296 newpos = qMax(newpos, start);
297 newpos = qMin(newpos, end);
298 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
299 // remove kexframe if it is dragged outside
300 m_keyframes.remove(m_selectedKeyframe);
301 m_selectedKeyframe = -1;
305 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
306 // remove kexframe if it is dragged outside
307 m_keyframes.remove(m_selectedKeyframe);
308 m_selectedKeyframe = -1;
312 double newval = qMax(value, 0.0);
313 newval = qMin(newval, 100.0);
314 newval = newval / m_keyframeFactor;
315 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
316 m_keyframes[newpos] = newval;
317 m_selectedKeyframe = newpos;
321 double AbstractClipItem::keyFrameFactor() const {
322 return m_keyframeFactor;
325 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
326 QRectF br = sceneBoundingRect();
327 double maxh = 100.0 / br.height() / m_keyframeFactor;
328 double newval = (br.bottom() - value) * maxh;
329 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
330 int newpos = (int) pos.frames(m_fps) ;
331 m_keyframes[newpos] = newval;
332 m_selectedKeyframe = newpos;
336 bool AbstractClipItem::hasKeyFrames() const {
337 return !m_keyframes.isEmpty();
340 QRect AbstractClipItem::visibleRect() {
342 if (scene()->views().size() > 0) {
343 rectInView = scene()->views()[0]->viewport()->rect();
344 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
345 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
346 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
351 CustomTrackScene* AbstractClipItem::projectScene() {
352 if (scene()) return static_cast <CustomTrackScene*>(scene());