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 ***************************************************************************/
22 #include <QGraphicsScene>
23 #include <QGraphicsView>
27 #include "abstractclipitem.h"
29 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) {
30 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
32 m_startPos = info.startPos;
33 m_cropDuration = info.endPos - info.startPos;
36 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack) {
37 double origX = rect().x();
38 double origY = rect().y();
41 setRect(x * scale, origY + offset, rect().width(), rect().height());
42 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
43 if (collisionList.size() == 0) m_track = newTrack;
44 for (int i = 0; i < collisionList.size(); ++i) {
45 QGraphicsItem *item = collisionList.at(i);
46 if (item->type() == type()) {
48 QRectF other = ((QGraphicsRectItem *)item)->rect();
49 if (x < m_startPos.frames(m_fps)) {
50 kDebug() << "COLLISION, MOVING TO------";
51 m_startPos = ((AbstractClipItem *)item)->endPos();
52 origX = m_startPos.frames(m_fps) * scale;
53 } else if (x > m_startPos.frames(m_fps)) {
54 //kDebug() << "COLLISION, MOVING TO+++: "<<x<<", CLIP CURR POS: "<<m_startPos.frames(m_fps)<<", COLLIDING START: "<<((AbstractClipItem *)item)->startPos().frames(m_fps);
55 m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
56 origX = m_startPos.frames(m_fps) * scale;
59 setRect(origX, origY, rect().width(), rect().height());
68 m_startPos = GenTime(x, m_fps);
70 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
71 for (int i = 0; i < childrenList.size(); ++i) {
72 childrenList.at(i)->moveBy(rect().x() - origX , offset);
76 GenTime AbstractClipItem::endPos() const {
77 return m_startPos + m_cropDuration;
80 int AbstractClipItem::track() const {
84 GenTime AbstractClipItem::cropStart() const {
88 void AbstractClipItem::setCropStart(GenTime pos) {
92 void AbstractClipItem::resizeStart(int posx, double scale) {
93 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
94 if (durationDiff == GenTime()) return;
95 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
97 if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
98 durationDiff = GenTime() - m_cropStart;
99 } else if (durationDiff >= m_cropDuration) {
100 durationDiff = m_cropDuration - GenTime(3, m_fps);
103 m_startPos += durationDiff;
104 if (type() == AVWIDGET) m_cropStart += durationDiff;
105 m_cropDuration = m_cropDuration - durationDiff;
106 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
107 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
108 for (int i = 0; i < collisionList.size(); ++i) {
109 QGraphicsItem *item = collisionList.at(i);
110 if (item->type() == type()) {
111 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
112 setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
114 if (type() == AVWIDGET) m_cropStart += diff;
115 m_cropDuration = m_cropDuration - diff;
121 void AbstractClipItem::resizeEnd(int posx, double scale) {
122 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
123 if (durationDiff == GenTime()) return;
124 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
125 if (m_cropDuration + durationDiff <= GenTime()) {
126 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
127 } else if (m_cropStart + m_cropDuration + durationDiff >= m_maxDuration) {
128 durationDiff = m_maxDuration - m_cropDuration - m_cropStart;
130 m_cropDuration += durationDiff;
131 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
132 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
133 for (int i = 0; i < collisionList.size(); ++i) {
134 QGraphicsItem *item = collisionList.at(i);
135 if (item->type() == type()) {
136 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
137 m_cropDuration = diff;
138 setRect(m_startPos.frames(m_fps) * scale, rect().y(), (m_cropDuration.frames(m_fps)) * scale, rect().height());
144 GenTime AbstractClipItem::duration() const {
145 return m_cropDuration;
148 GenTime AbstractClipItem::startPos() const {
152 void AbstractClipItem::setTrack(int track) {
156 double AbstractClipItem::fps() const {
160 GenTime AbstractClipItem::maxDuration() const {
161 return m_maxDuration;
164 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
165 QPainterPath roundRectPathUpper;
166 double roundingY = 20;
167 double roundingX = 20;
170 while (roundingX > br.width() / 2) {
171 roundingX = roundingX / 2;
172 roundingY = roundingY / 2;
174 int br_endx = (int)(br.x() + br .width() - offset);
175 int br_startx = (int)(br.x() + offset);
176 int br_starty = (int)(br.y());
177 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
178 int br_endy = (int)(br.y() + br.height());
180 roundRectPathUpper.moveTo(br_endx , br_halfy);
181 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
182 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
183 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
184 roundRectPathUpper.lineTo(br_startx , br_halfy);
186 return roundRectPathUpper;
189 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
190 QPainterPath roundRectPathLower;
191 double roundingY = 20;
192 double roundingX = 20;
195 int br_endx = (int)(br.x() + br .width() - offset);
196 int br_startx = (int)(br.x() + offset);
197 int br_starty = (int)(br.y());
198 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
199 int br_endy = (int)(br.y() + br.height() - 1);
201 while (roundingX > br.width() / 2) {
202 roundingX = roundingX / 2;
203 roundingY = roundingY / 2;
205 roundRectPathLower.moveTo(br_startx, br_halfy);
206 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
207 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
208 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
209 roundRectPathLower.lineTo(br_endx , br_halfy);
210 return roundRectPathLower;
213 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
214 if (m_keyframes.count() < 2) return;
216 double maxw = br.width() / m_cropDuration.frames(m_fps);
217 double maxh = br.height() / 100.0 * m_keyframeFactor;
223 // draw line showing default value
227 y1 = br.bottom() - m_keyframeDefault * maxh;
228 QLineF l(x1, y1, x2, y1);
229 painter->setPen(QColor(168, 168, 168, 180));
230 painter->drawLine(l);
232 painter->setPen(QColor(108, 108, 108, 180));
233 painter->drawLine(l);
234 painter->setPen(QColor(Qt::white));
238 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
239 QColor color(Qt::blue);
240 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
241 y1 = br.bottom() - i.value() * maxh;
242 while (i != m_keyframes.constEnd()) {
243 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
244 else color = QColor(Qt::blue);
246 if (i == m_keyframes.constEnd()) break;
247 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
248 y2 = br.bottom() - i.value() * maxh;
249 QLineF l(x1, y1, x2, y2);
250 painter->drawLine(l);
252 painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
257 if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
260 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
262 double maxw = br.width() / m_cropDuration.frames(m_fps);
263 double maxh = br.height() / 100.0 * m_keyframeFactor;
264 if (m_keyframes.count() > 1) {
265 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
268 while (i != m_keyframes.constEnd()) {
269 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
270 y1 = br.bottom() - i.value() * maxh;
271 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
272 setToolTip("[" + QString::number(i.key()) + " frames, " + QString::number(i.value(), 'f', 1) + "%]");
274 } else if (x1 > pos.x()) break;
278 setToolTip(QString());
282 void AbstractClipItem::updateSelectedKeyFrame() {
283 if (m_editedKeyframe == -1) return;
285 double maxw = br.width() / m_cropDuration.frames(m_fps);
286 double maxh = br.height() / 100.0 * m_keyframeFactor;
287 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
288 m_selectedKeyframe = m_editedKeyframe;
289 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
292 int AbstractClipItem::selectedKeyFramePos() const {
293 return m_editedKeyframe;
296 double AbstractClipItem::selectedKeyFrameValue() const {
297 return m_keyframes[m_editedKeyframe];
300 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
301 if (!m_keyframes.contains(m_selectedKeyframe)) return;
302 int newpos = (int) pos.frames(m_fps);
303 int start = m_cropStart.frames(m_fps);
304 int end = (m_cropStart + m_cropDuration).frames(m_fps);
305 newpos = qMax(newpos, start);
306 newpos = qMin(newpos, end);
307 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
308 // remove kexframe if it is dragged outside
309 m_keyframes.remove(m_selectedKeyframe);
310 m_selectedKeyframe = -1;
314 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
315 // remove kexframe if it is dragged outside
316 m_keyframes.remove(m_selectedKeyframe);
317 m_selectedKeyframe = -1;
321 double newval = qMax(value, 0.0);
322 newval = qMin(newval, 100.0);
323 newval = newval / m_keyframeFactor;
324 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
325 m_keyframes[newpos] = newval;
326 m_selectedKeyframe = newpos;
330 double AbstractClipItem::keyFrameFactor() const {
331 return m_keyframeFactor;
334 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
336 double maxh = 100.0 / br.height() / m_keyframeFactor;
337 double newval = (br.bottom() - value) * maxh;
338 int newpos = (int) pos.frames(m_fps) ;
339 m_keyframes[newpos] = newval;
340 m_selectedKeyframe = newpos;
344 bool AbstractClipItem::hasKeyFrames() const {
345 return !m_keyframes.isEmpty();
348 QRect AbstractClipItem::visibleRect() {
350 if (scene()->views().size() > 0) {
351 rectInView = scene()->views()[0]->viewport()->rect();
352 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
353 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
354 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();