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.track = track();
46 void AbstractClipItem::moveTo(int x, double scale, int offset, int newTrack, bool checkCollision) {
47 double origX = rect().x();
48 double origY = rect().y();
51 setRect(x * scale, origY + offset, rect().width(), rect().height());
52 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
53 if (collisionList.size() == 0) m_track = newTrack;
55 for (int i = 0; i < collisionList.size(); ++i) {
56 QGraphicsItem *item = collisionList.at(i);
57 if (item->type() == type()) {
59 QRectF other = ((QGraphicsRectItem *)item)->rect();
60 if (x < m_startPos.frames(m_fps)) {
61 kDebug() << "COLLISION, MOVING TO------";
62 m_startPos = ((AbstractClipItem *)item)->endPos();
63 origX = m_startPos.frames(m_fps) * scale;
64 } else if (x > m_startPos.frames(m_fps)) {
65 //kDebug() << "COLLISION, MOVING TO+++: "<<x<<", CLIP CURR POS: "<<m_startPos.frames(m_fps)<<", COLLIDING START: "<<((AbstractClipItem *)item)->startPos().frames(m_fps);
66 m_startPos = ((AbstractClipItem *)item)->startPos() - m_cropDuration;
67 origX = m_startPos.frames(m_fps) * scale;
70 setRect(origX, origY, rect().width(), rect().height());
79 m_startPos = GenTime(x, m_fps);
81 /* QList <QGraphicsItem *> childrenList = QGraphicsItem::children();
82 for (int i = 0; i < childrenList.size(); ++i) {
83 childrenList.at(i)->moveBy(rect().x() - origX , offset);
87 GenTime AbstractClipItem::endPos() const {
88 return m_startPos + m_cropDuration;
91 int AbstractClipItem::track() const {
95 GenTime AbstractClipItem::cropStart() const {
99 void AbstractClipItem::setCropStart(GenTime pos) {
103 void AbstractClipItem::resizeStart(int posx, double scale) {
104 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
105 if (durationDiff == GenTime()) return;
106 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
108 if (type() == AVWIDGET && m_cropStart + durationDiff < GenTime()) {
109 durationDiff = GenTime() - m_cropStart;
110 } else if (durationDiff >= m_cropDuration) {
111 durationDiff = m_cropDuration - GenTime(3, m_fps);
114 m_startPos += durationDiff;
115 if (type() == AVWIDGET) m_cropStart += durationDiff;
116 m_cropDuration = m_cropDuration - durationDiff;
117 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
118 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
119 for (int i = 0; i < collisionList.size(); ++i) {
120 QGraphicsItem *item = collisionList.at(i);
121 if (item->type() == type()) {
122 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
123 setRect((m_startPos + diff).frames(m_fps) * scale, rect().y(), (m_cropDuration - diff).frames(m_fps) * scale, rect().height());
125 if (type() == AVWIDGET) m_cropStart += diff;
126 m_cropDuration = m_cropDuration - diff;
132 void AbstractClipItem::resizeEnd(int posx, double scale) {
133 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
134 if (durationDiff == GenTime()) return;
135 //kDebug() << "-- RESCALE: CROP=" << m_cropStart << ", DIFF = " << durationDiff;
136 if (m_cropDuration + durationDiff <= GenTime()) {
137 durationDiff = GenTime() - (m_cropDuration - GenTime(3, m_fps));
138 } else if (m_cropStart + m_cropDuration + durationDiff >= m_maxDuration) {
139 durationDiff = m_maxDuration - m_cropDuration - m_cropStart;
141 m_cropDuration += durationDiff;
142 setRect(m_startPos.frames(m_fps) * scale, rect().y(), m_cropDuration.frames(m_fps) * scale, rect().height());
143 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
144 for (int i = 0; i < collisionList.size(); ++i) {
145 QGraphicsItem *item = collisionList.at(i);
146 if (item->type() == type()) {
147 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
148 m_cropDuration = diff;
149 setRect(m_startPos.frames(m_fps) * scale, rect().y(), (m_cropDuration.frames(m_fps)) * scale, rect().height());
155 GenTime AbstractClipItem::duration() const {
156 return m_cropDuration;
159 GenTime AbstractClipItem::startPos() const {
163 void AbstractClipItem::setTrack(int track) {
167 double AbstractClipItem::fps() const {
171 GenTime AbstractClipItem::maxDuration() const {
172 return m_maxDuration;
175 QPainterPath AbstractClipItem::upperRectPart(QRectF br) {
176 QPainterPath roundRectPathUpper;
177 double roundingY = 20;
178 double roundingX = 20;
181 while (roundingX > br.width() / 2) {
182 roundingX = roundingX / 2;
183 roundingY = roundingY / 2;
185 int br_endx = (int)(br.x() + br .width() - offset);
186 int br_startx = (int)(br.x() + offset);
187 int br_starty = (int)(br.y());
188 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
189 int br_endy = (int)(br.y() + br.height());
191 roundRectPathUpper.moveTo(br_endx , br_halfy);
192 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
193 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
194 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
195 roundRectPathUpper.lineTo(br_startx , br_halfy);
197 return roundRectPathUpper;
200 QPainterPath AbstractClipItem::lowerRectPart(QRectF br) {
201 QPainterPath roundRectPathLower;
202 double roundingY = 20;
203 double roundingX = 20;
206 int br_endx = (int)(br.x() + br .width() - offset);
207 int br_startx = (int)(br.x() + offset);
208 int br_starty = (int)(br.y());
209 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
210 int br_endy = (int)(br.y() + br.height() - 1);
212 while (roundingX > br.width() / 2) {
213 roundingX = roundingX / 2;
214 roundingY = roundingY / 2;
216 roundRectPathLower.moveTo(br_startx, br_halfy);
217 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
218 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
219 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
220 roundRectPathLower.lineTo(br_endx , br_halfy);
221 return roundRectPathLower;
224 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF exposedRect) {
225 if (m_keyframes.count() < 2) return;
227 double maxw = br.width() / m_cropDuration.frames(m_fps);
228 double maxh = br.height() / 100.0 * m_keyframeFactor;
234 // draw line showing default value
238 y1 = br.bottom() - m_keyframeDefault * maxh;
239 QLineF l(x1, y1, x2, y1);
240 painter->setPen(QColor(168, 168, 168, 180));
241 painter->drawLine(l);
243 painter->setPen(QColor(108, 108, 108, 180));
244 painter->drawLine(l);
245 painter->setPen(QColor(Qt::white));
249 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
250 QColor color(Qt::blue);
251 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
252 y1 = br.bottom() - i.value() * maxh;
253 while (i != m_keyframes.constEnd()) {
254 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
255 else color = QColor(Qt::blue);
257 if (i == m_keyframes.constEnd()) break;
258 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
259 y2 = br.bottom() - i.value() * maxh;
260 QLineF l(x1, y1, x2, y2);
261 painter->drawLine(l);
263 painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
268 if (isSelected()) painter->fillRect(x1 - 3, y1 - 3, 6, 6, QBrush(color));
271 int AbstractClipItem::mouseOverKeyFrames(QPointF pos) {
273 double maxw = br.width() / m_cropDuration.frames(m_fps);
274 double maxh = br.height() / 100.0 * m_keyframeFactor;
275 if (m_keyframes.count() > 1) {
276 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
279 while (i != m_keyframes.constEnd()) {
280 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
281 y1 = br.bottom() - i.value() * maxh;
282 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
283 setToolTip("[" + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
285 } else if (x1 > pos.x()) break;
289 setToolTip(QString());
293 void AbstractClipItem::updateSelectedKeyFrame() {
294 if (m_editedKeyframe == -1) return;
296 double maxw = br.width() / m_cropDuration.frames(m_fps);
297 double maxh = br.height() / 100.0 * m_keyframeFactor;
298 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
299 m_selectedKeyframe = m_editedKeyframe;
300 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
303 int AbstractClipItem::selectedKeyFramePos() const {
304 return m_editedKeyframe;
307 double AbstractClipItem::selectedKeyFrameValue() const {
308 return m_keyframes[m_editedKeyframe];
311 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value) {
312 if (!m_keyframes.contains(m_selectedKeyframe)) return;
313 int newpos = (int) pos.frames(m_fps);
314 int start = m_cropStart.frames(m_fps);
315 int end = (m_cropStart + m_cropDuration).frames(m_fps);
316 newpos = qMax(newpos, start);
317 newpos = qMin(newpos, end);
318 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
319 // remove kexframe if it is dragged outside
320 m_keyframes.remove(m_selectedKeyframe);
321 m_selectedKeyframe = -1;
325 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
326 // remove kexframe if it is dragged outside
327 m_keyframes.remove(m_selectedKeyframe);
328 m_selectedKeyframe = -1;
332 double newval = qMax(value, 0.0);
333 newval = qMin(newval, 100.0);
334 newval = newval / m_keyframeFactor;
335 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
336 m_keyframes[newpos] = newval;
337 m_selectedKeyframe = newpos;
341 double AbstractClipItem::keyFrameFactor() const {
342 return m_keyframeFactor;
345 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value) {
347 double maxh = 100.0 / br.height() / m_keyframeFactor;
348 double newval = (br.bottom() - value) * maxh;
349 int newpos = (int) pos.frames(m_fps) ;
350 m_keyframes[newpos] = newval;
351 m_selectedKeyframe = newpos;
355 bool AbstractClipItem::hasKeyFrames() const {
356 return !m_keyframes.isEmpty();
359 QRect AbstractClipItem::visibleRect() {
361 if (scene()->views().size() > 0) {
362 rectInView = scene()->views()[0]->viewport()->rect();
363 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
364 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
365 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();