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"
31 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) {
32 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
34 m_startPos = info.startPos;
35 m_cropDuration = info.endPos - info.startPos;
38 ItemInfo AbstractClipItem::info() const {
40 itemInfo.startPos = startPos();
41 itemInfo.endPos = endPos();
42 itemInfo.cropStart = cropStart();
43 itemInfo.track = track();
47 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack, bool checkCollision) {
48 double origX = rect().x();
49 double origY = rect().y();
52 setRect(x * scale, origY + offset, rect().width(), rect().height());
53 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
54 if (collisionList.size() == 0) m_track = newTrack;
56 for (int i = 0; i < collisionList.size(); ++i) {
57 QGraphicsItem *item = collisionList.at(i);
58 if (item->type() == type()) {
60 QRectF other = ((QGraphicsRectItem *)item)->rect();
61 if (x < m_startPos.frames(m_fps)) {
62 kDebug() << "COLLISION, MOVING TO------";
63 m_startPos = ((AbstractClipItem *)item)->endPos();
64 origX = m_startPos.frames(m_fps) * scale;
65 } else if (x > m_startPos.frames(m_fps)) {
66 //kDebug() << "COLLISION, MOVING TO+++: "<<x<<", CLIP CURR POS: "<<m_startPos.frames(m_fps)<<", COLLIDING START: "<<((AbstractClipItem *)item)->startPos().frames(m_fps);
67 m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
68 origX = m_startPos.frames(m_fps) * scale;
71 setRect(origX, origY, rect().width(), rect().height());
80 m_startPos = GenTime(x, m_fps);
82 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
83 for (int i = 0; i < childrenList.size(); ++i) {
84 childrenList.at(i)->moveBy(rect().x() - origX , offset);
88 GenTime AbstractClipItem::endPos() const {
89 return m_startPos + m_cropDuration;
92 int AbstractClipItem::track() const {
96 GenTime AbstractClipItem::cropStart() const {
100 void AbstractClipItem::setCropStart(GenTime pos) {
104 void AbstractClipItem::resizeStart(int posx, double scale) {
105 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
106 if (durationDiff == GenTime()) return;
107 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
109 if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
110 durationDiff = GenTime() - m_cropStart;
111 } else if (durationDiff >= m_cropDuration) {
112 durationDiff = m_cropDuration - GenTime(3, m_fps);
115 m_startPos += durationDiff;
116 if (type() == AVWIDGET) m_cropStart += durationDiff;
117 m_cropDuration = m_cropDuration - durationDiff;
118 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
119 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
120 for (int i = 0; i < collisionList.size(); ++i) {
121 QGraphicsItem *item = collisionList.at(i);
122 if (item->type() == type()) {
123 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
124 setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
126 if (type() == AVWIDGET) m_cropStart += diff;
127 m_cropDuration = m_cropDuration - diff;
133 void AbstractClipItem::resizeEnd(int posx, double scale) {
134 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
135 if (durationDiff == GenTime()) return;
136 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
137 if (m_cropDuration + durationDiff <= GenTime()) {
138 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
139 } else if (m_cropStart + m_cropDuration + durationDiff >= maxDuration()) {
140 durationDiff = maxDuration() - m_cropDuration - m_cropStart;
142 m_cropDuration += durationDiff;
143 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
144 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
145 for (int i = 0; i < collisionList.size(); ++i) {
146 QGraphicsItem *item = collisionList.at(i);
147 if (item->type() == type()) {
148 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
149 m_cropDuration = diff;
150 setRect(m_startPos.frames(m_fps) * scale, rect().y(), (m_cropDuration.frames(m_fps)) * scale, rect().height());
156 GenTime AbstractClipItem::duration() const {
157 return m_cropDuration;
160 GenTime AbstractClipItem::startPos() const {
164 void AbstractClipItem::setTrack(int track) {
168 double AbstractClipItem::fps() const {
172 GenTime AbstractClipItem::maxDuration() const {
173 return m_maxDuration;
176 void AbstractClipItem::setMaxDuration(const GenTime &max) {
180 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
181 QPainterPath roundRectPathUpper;
182 double roundingY = 20;
183 double roundingX = 20;
186 while (roundingX > br.width() / 2) {
187 roundingX = roundingX / 2;
188 roundingY = roundingY / 2;
190 int br_endx = (int)(br.x() + br .width() - offset);
191 int br_startx = (int)(br.x() + offset);
192 int br_starty = (int)(br.y());
193 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
194 int br_endy = (int)(br.y() + br.height());
196 roundRectPathUpper.moveTo(br_endx , br_halfy);
197 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
198 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
199 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
200 roundRectPathUpper.lineTo(br_startx , br_halfy);
202 return roundRectPathUpper;
205 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
206 QPainterPath roundRectPathLower;
207 double roundingY = 20;
208 double roundingX = 20;
211 int br_endx = (int)(br.x() + br .width() - offset);
212 int br_startx = (int)(br.x() + offset);
213 int br_starty = (int)(br.y());
214 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
215 int br_endy = (int)(br.y() + br.height() - 1);
217 while (roundingX > br.width() / 2) {
218 roundingX = roundingX / 2;
219 roundingY = roundingY / 2;
221 roundRectPathLower.moveTo(br_startx, br_halfy);
222 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
223 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
224 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
225 roundRectPathLower.lineTo(br_endx , br_halfy);
226 return roundRectPathLower;
229 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
230 if (m_keyframes.count() < 2) return;
232 double maxw = br.width() / m_cropDuration.frames(m_fps);
233 double maxh = br.height() / 100.0 * m_keyframeFactor;
239 // draw line showing default value
243 y1 = br.bottom() - m_keyframeDefault * maxh;
244 QLineF l(x1, y1, x2, y1);
245 painter->setPen(QColor(168, 168, 168, 180));
246 painter->drawLine(l);
248 painter->setPen(QColor(108, 108, 108, 180));
249 painter->drawLine(l);
250 painter->setPen(QColor(Qt::white));
254 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
255 QColor color(Qt::blue);
256 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
257 y1 = br.bottom() - i.value() * maxh;
258 while (i != m_keyframes.constEnd()) {
259 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
260 else color = QColor(Qt::blue);
262 if (i == m_keyframes.constEnd()) break;
263 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
264 y2 = br.bottom() - i.value() * maxh;
265 QLineF l(x1, y1, x2, y2);
266 painter->drawLine(l);
268 painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
273 if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
276 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
278 double maxw = br.width() / m_cropDuration.frames(m_fps);
279 double maxh = br.height() / 100.0 * m_keyframeFactor;
280 if (m_keyframes.count() > 1) {
281 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
284 while (i != m_keyframes.constEnd()) {
285 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
286 y1 = br.bottom() - i.value() * maxh;
287 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
288 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
290 } else if (x1 > pos.x()) break;
294 setToolTip(QString());
298 void AbstractClipItem::updateSelectedKeyFrame() {
299 if (m_editedKeyframe == -1) return;
301 double maxw = br.width() / m_cropDuration.frames(m_fps);
302 double maxh = br.height() / 100.0 * m_keyframeFactor;
303 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
304 m_selectedKeyframe = m_editedKeyframe;
305 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
308 int AbstractClipItem::selectedKeyFramePos() const {
309 return m_editedKeyframe;
312 double AbstractClipItem::selectedKeyFrameValue() const {
313 return m_keyframes[m_editedKeyframe];
316 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
317 if (!m_keyframes.contains(m_selectedKeyframe)) return;
318 int newpos = (int) pos.frames(m_fps);
319 int start = m_cropStart.frames(m_fps);
320 int end = (m_cropStart + m_cropDuration).frames(m_fps);
321 newpos = qMax(newpos, start);
322 newpos = qMin(newpos, end);
323 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
324 // remove kexframe if it is dragged outside
325 m_keyframes.remove(m_selectedKeyframe);
326 m_selectedKeyframe = -1;
330 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
331 // remove kexframe if it is dragged outside
332 m_keyframes.remove(m_selectedKeyframe);
333 m_selectedKeyframe = -1;
337 double newval = qMax(value, 0.0);
338 newval = qMin(newval, 100.0);
339 newval = newval / m_keyframeFactor;
340 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
341 m_keyframes[newpos] = newval;
342 m_selectedKeyframe = newpos;
346 double AbstractClipItem::keyFrameFactor() const {
347 return m_keyframeFactor;
350 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
352 double maxh = 100.0 / br.height() / m_keyframeFactor;
353 double newval = (br.bottom() - value) * maxh;
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();