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 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack) {
39 double origX = rect().x();
40 double origY = rect().y();
43 setRect(x * scale, origY + offset, rect().width(), rect().height());
44 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
45 if (collisionList.size() == 0) m_track = newTrack;
46 for (int i = 0; i < collisionList.size(); ++i) {
47 QGraphicsItem *item = collisionList.at(i);
48 if (item->type() == type()) {
50 QRectF other = ((QGraphicsRectItem *)item)->rect();
51 if (x < m_startPos.frames(m_fps)) {
52 kDebug() << "COLLISION, MOVING TO------";
53 m_startPos = ((AbstractClipItem *)item)->endPos();
54 origX = m_startPos.frames(m_fps) * scale;
55 } else if (x > m_startPos.frames(m_fps)) {
56 //kDebug() << "COLLISION, MOVING TO+++: "<<x<<", CLIP CURR POS: "<<m_startPos.frames(m_fps)<<", COLLIDING START: "<<((AbstractClipItem *)item)->startPos().frames(m_fps);
57 m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
58 origX = m_startPos.frames(m_fps) * scale;
61 setRect(origX, origY, rect().width(), rect().height());
70 m_startPos = GenTime(x, m_fps);
72 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
73 for (int i = 0; i < childrenList.size(); ++i) {
74 childrenList.at(i)->moveBy(rect().x() - origX , offset);
78 GenTime AbstractClipItem::endPos() const {
79 return m_startPos + m_cropDuration;
82 int AbstractClipItem::track() const {
86 GenTime AbstractClipItem::cropStart() const {
90 void AbstractClipItem::setCropStart(GenTime pos) {
94 void AbstractClipItem::resizeStart(int posx, double scale) {
95 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
96 if (durationDiff == GenTime()) return;
97 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
99 if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
100 durationDiff = GenTime() - m_cropStart;
101 } else if (durationDiff >= m_cropDuration) {
102 durationDiff = m_cropDuration - GenTime(3, m_fps);
105 m_startPos += durationDiff;
106 if (type() == AVWIDGET) m_cropStart += durationDiff;
107 m_cropDuration = m_cropDuration - durationDiff;
108 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
109 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
110 for (int i = 0; i < collisionList.size(); ++i) {
111 QGraphicsItem *item = collisionList.at(i);
112 if (item->type() == type()) {
113 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
114 setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
116 if (type() == AVWIDGET) m_cropStart += diff;
117 m_cropDuration = m_cropDuration - diff;
123 void AbstractClipItem::resizeEnd(int posx, double scale) {
124 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
125 if (durationDiff == GenTime()) return;
126 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
127 if (m_cropDuration + durationDiff <= GenTime()) {
128 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
129 } else if (m_cropStart + m_cropDuration + durationDiff >= m_maxDuration) {
130 durationDiff = m_maxDuration - m_cropDuration - m_cropStart;
132 m_cropDuration += durationDiff;
133 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
134 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
135 for (int i = 0; i < collisionList.size(); ++i) {
136 QGraphicsItem *item = collisionList.at(i);
137 if (item->type() == type()) {
138 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
139 m_cropDuration = diff;
140 setRect(m_startPos.frames(m_fps) * scale, rect().y(), (m_cropDuration.frames(m_fps)) * scale, rect().height());
146 GenTime AbstractClipItem::duration() const {
147 return m_cropDuration;
150 GenTime AbstractClipItem::startPos() const {
154 void AbstractClipItem::setTrack(int track) {
158 double AbstractClipItem::fps() const {
162 GenTime AbstractClipItem::maxDuration() const {
163 return m_maxDuration;
166 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
167 QPainterPath roundRectPathUpper;
168 double roundingY = 20;
169 double roundingX = 20;
172 while (roundingX > br.width() / 2) {
173 roundingX = roundingX / 2;
174 roundingY = roundingY / 2;
176 int br_endx = (int)(br.x() + br .width() - offset);
177 int br_startx = (int)(br.x() + offset);
178 int br_starty = (int)(br.y());
179 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
180 int br_endy = (int)(br.y() + br.height());
182 roundRectPathUpper.moveTo(br_endx , br_halfy);
183 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
184 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
185 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
186 roundRectPathUpper.lineTo(br_startx , br_halfy);
188 return roundRectPathUpper;
191 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
192 QPainterPath roundRectPathLower;
193 double roundingY = 20;
194 double roundingX = 20;
197 int br_endx = (int)(br.x() + br .width() - offset);
198 int br_startx = (int)(br.x() + offset);
199 int br_starty = (int)(br.y());
200 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
201 int br_endy = (int)(br.y() + br.height() - 1);
203 while (roundingX > br.width() / 2) {
204 roundingX = roundingX / 2;
205 roundingY = roundingY / 2;
207 roundRectPathLower.moveTo(br_startx, br_halfy);
208 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
209 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
210 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
211 roundRectPathLower.lineTo(br_endx , br_halfy);
212 return roundRectPathLower;
215 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
216 if (m_keyframes.count() < 2) return;
218 double maxw = br.width() / m_cropDuration.frames(m_fps);
219 double maxh = br.height() / 100.0 * m_keyframeFactor;
225 // draw line showing default value
229 y1 = br.bottom() - m_keyframeDefault * maxh;
230 QLineF l(x1, y1, x2, y1);
231 painter->setPen(QColor(168, 168, 168, 180));
232 painter->drawLine(l);
234 painter->setPen(QColor(108, 108, 108, 180));
235 painter->drawLine(l);
236 painter->setPen(QColor(Qt::white));
240 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
241 QColor color(Qt::blue);
242 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
243 y1 = br.bottom() - i.value() * maxh;
244 while (i != m_keyframes.constEnd()) {
245 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
246 else color = QColor(Qt::blue);
248 if (i == m_keyframes.constEnd()) break;
249 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
250 y2 = br.bottom() - i.value() * maxh;
251 QLineF l(x1, y1, x2, y2);
252 painter->drawLine(l);
254 painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
259 if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
262 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
264 double maxw = br.width() / m_cropDuration.frames(m_fps);
265 double maxh = br.height() / 100.0 * m_keyframeFactor;
266 if (m_keyframes.count() > 1) {
267 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
270 while (i != m_keyframes.constEnd()) {
271 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
272 y1 = br.bottom() - i.value() * maxh;
273 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
274 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
276 } else if (x1 > pos.x()) break;
280 setToolTip(QString());
284 void AbstractClipItem::updateSelectedKeyFrame() {
285 if (m_editedKeyframe == -1) return;
287 double maxw = br.width() / m_cropDuration.frames(m_fps);
288 double maxh = br.height() / 100.0 * m_keyframeFactor;
289 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
290 m_selectedKeyframe = m_editedKeyframe;
291 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
294 int AbstractClipItem::selectedKeyFramePos() const {
295 return m_editedKeyframe;
298 double AbstractClipItem::selectedKeyFrameValue() const {
299 return m_keyframes[m_editedKeyframe];
302 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
303 if (!m_keyframes.contains(m_selectedKeyframe)) return;
304 int newpos = (int) pos.frames(m_fps);
305 int start = m_cropStart.frames(m_fps);
306 int end = (m_cropStart + m_cropDuration).frames(m_fps);
307 newpos = qMax(newpos, start);
308 newpos = qMin(newpos, end);
309 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
310 // remove kexframe if it is dragged outside
311 m_keyframes.remove(m_selectedKeyframe);
312 m_selectedKeyframe = -1;
316 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
317 // remove kexframe if it is dragged outside
318 m_keyframes.remove(m_selectedKeyframe);
319 m_selectedKeyframe = -1;
323 double newval = qMax(value, 0.0);
324 newval = qMin(newval, 100.0);
325 newval = newval / m_keyframeFactor;
326 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
327 m_keyframes[newpos] = newval;
328 m_selectedKeyframe = newpos;
332 double AbstractClipItem::keyFrameFactor() const {
333 return m_keyframeFactor;
336 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
338 double maxh = 100.0 / br.height() / m_keyframeFactor;
339 double newval = (br.bottom() - value) * maxh;
340 int newpos = (int) pos.frames(m_fps) ;
341 m_keyframes[newpos] = newval;
342 m_selectedKeyframe = newpos;
346 bool AbstractClipItem::hasKeyFrames() const {
347 return !m_keyframes.isEmpty();
350 QRect AbstractClipItem::visibleRect() {
352 if (scene()->views().size() > 0) {
353 rectInView = scene()->views()[0]->viewport()->rect();
354 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
355 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
356 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();