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) {
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) {
215 double maxw = br.width() / 100.0;
216 double maxh = br.height() / 100.0;
217 if (m_keyframes.count() > 1) {
218 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
223 QColor color(Qt::blue);
224 x1 = br.x() + maxw * i.key();
225 y1 = br.bottom() - i.value() * maxh;
226 while (i != m_keyframes.constEnd()) {
227 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
228 else color = QColor(Qt::blue);
230 if (i == m_keyframes.constEnd()) break;
231 x2 = br.x() + maxw * i.key();
232 y2 = br.bottom() - i.value() * maxh;
233 QLineF l(x1, y1, x2, y2);
234 painter->drawLine(l);
236 painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
241 if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
245 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
247 double maxw = br.width() / 100.0;
248 double maxh = br.height() / 100.0;
249 if (m_keyframes.count() > 1) {
250 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
253 while (i != m_keyframes.constEnd()) {
254 x1 = br.x() + maxw * i.key();
255 y1 = br.bottom() - i.value() * maxh;
256 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
257 setToolTip("[" + QString::number(i.key()) + "x" + QString::number(i.value()) + "]");
259 } else if (x1 > pos.x()) break;
263 setToolTip(QString());
267 void AbstractClipItem::updateSelectedKeyFrame() {
268 if (m_editedKeyframe == -1) return;
270 double maxw = br.width() / 100.0;
271 double maxh = br.height() / 100.0;
272 update(br.x() + maxw * m_selectedKeyframe - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
273 m_selectedKeyframe = m_editedKeyframe;
274 update(br.x() + maxw * m_selectedKeyframe - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
277 void AbstractClipItem::updateKeyFramePos(const QPoint pos) {
278 if (m_selectedKeyframe == -1) return;
280 double maxw = br.width() / 100.0;
281 double maxh = br.height() / 100.0;
282 int newval = (int)((br.bottom() - pos.y()) / maxh);
283 int newpos = (int)((pos.x() - br.x()) / maxw);
284 if (newval < -50 && m_selectedKeyframe != 0 && m_selectedKeyframe != 100) {
285 // remove kexframe if it is dragged outside
286 m_keyframes.remove(m_selectedKeyframe);
287 m_selectedKeyframe = -1;
291 if (newval > 150 && m_selectedKeyframe != 0 && m_selectedKeyframe != 100) {
292 // remove kexframe if it is dragged outside
293 m_keyframes.remove(m_selectedKeyframe);
294 m_selectedKeyframe = -1;
298 if (newval < 0) newval = 0;
299 else if (newval > 100) newval = 100;
300 if (m_selectedKeyframe == 0 || m_selectedKeyframe == 100) {
301 // start and end keyframes should stay in place
302 m_keyframes[m_selectedKeyframe] = newval;
304 m_keyframes.remove(m_selectedKeyframe);
305 m_keyframes[newpos] = newval;
306 m_selectedKeyframe = newpos;
311 void AbstractClipItem::addKeyFrame(const QPoint pos) {
313 double maxw = br.width() / 100.0;
314 double maxh = br.height() / 100.0;
315 int newval = (int)((br.bottom() - pos.y()) / maxh);
316 int newpos = (int)((pos.x() - br.x()) / maxw);
317 m_keyframes[newpos] = newval;
318 m_selectedKeyframe = newpos;
322 bool AbstractClipItem::hasKeyFrames() {
323 return !m_keyframes.isEmpty();
326 QRect AbstractClipItem::visibleRect() {
328 if (scene()->views().size() > 0) {
329 rectInView = scene()->views()[0]->viewport()->rect();
330 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
331 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
332 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();