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 "abstractclipitem.h"
22 #include "customtrackscene.h"
23 #include "kdenlivesettings.h"
31 AbstractClipItem::AbstractClipItem(const ItemInfo info, const QRectF& rect, double fps) :
33 QGraphicsRectItem(rect),
36 m_selectedKeyframe(0),
40 setFlags(/*QGraphicsItem::ItemClipsToShape | */QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
42 m_startPos = info.startPos;
43 m_cropDuration = info.endPos - info.startPos;
46 ItemInfo AbstractClipItem::info() const
49 itemInfo.startPos = startPos();
50 itemInfo.endPos = endPos();
51 itemInfo.cropStart = m_cropStart;
52 itemInfo.track = track();
56 GenTime AbstractClipItem::endPos() const
58 return m_startPos + m_cropDuration;
61 int AbstractClipItem::track() const
66 GenTime AbstractClipItem::cropStart() const
71 GenTime AbstractClipItem::cropDuration() const
73 return m_cropDuration;
76 void AbstractClipItem::setCropStart(GenTime pos)
81 void AbstractClipItem::updateItem()
83 m_track = (int)(scenePos().y() / KdenliveSettings::trackheight());
84 m_startPos = GenTime((int) scenePos().x(), m_fps);
87 void AbstractClipItem::updateRectGeometry()
89 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
92 void AbstractClipItem::resizeStart(int posx, double speed)
94 GenTime durationDiff = GenTime(posx, m_fps) - m_startPos;
95 if (durationDiff == GenTime()) return;
96 //kDebug() << "-- RESCALE DIFF=" << durationDiff.frames(25) << ", CLIP: " << startPos().frames(25) << "-" << endPos().frames(25);
98 if (type() == AVWIDGET && cropStart() + durationDiff < GenTime()) {
99 durationDiff = GenTime() - cropStart();
100 } else if (durationDiff >= m_cropDuration) {
102 if (m_cropDuration > GenTime(3, m_fps)) durationDiff = GenTime(3, m_fps);
105 //kDebug()<<"// DURATION DIFF: "<<durationDiff.frames(25)<<", POS: "<<pos().x();
106 m_startPos += durationDiff;
107 if (type() == AVWIDGET) m_cropStart += durationDiff * speed;
108 m_cropDuration = m_cropDuration - durationDiff * speed;
110 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
111 moveBy(durationDiff.frames(m_fps), 0);
112 //setPos(m_startPos.frames(m_fps), pos().y());
113 if ((int) scenePos().x() != posx) {
114 //kDebug()<<"////// WARNING, DIFF IN XPOS: "<<pos().x()<<" == "<<m_startPos.frames(m_fps);
115 GenTime diff = GenTime((int) pos().x() - posx, m_fps);
116 if (type() == AVWIDGET) m_cropStart = m_cropStart + diff;
117 m_cropDuration = m_cropDuration - diff;
118 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
119 //kDebug()<<"// NEW START: "<<m_startPos.frames(25)<<", NW DUR: "<<m_cropDuration.frames(25);
123 //kDebug() << "-- NEW CLIP=" << startPos().frames(25) << "-" << endPos().frames(25);
124 //setRect((double) m_startPos.frames(m_fps) * scale, rect().y(), (double) m_cropDuration.frames(m_fps) * scale, rect().height());
126 /* if (durationDiff < GenTime()) {
127 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
128 for (int i = 0; i < collisionList.size(); ++i) {
129 QGraphicsItem *item = collisionList.at(i);
130 if (item->type() == type() && item->pos().x() < pos().x()) {
131 kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
132 GenTime diff = ((AbstractClipItem *)item)->endPos() + GenTime(1, m_fps) - m_startPos;
133 setRect(0, 0, (m_cropDuration - diff).frames(m_fps) - 0.02, rect().height());
134 setPos((m_startPos + diff).frames(m_fps), pos().y());
136 if (type() == AVWIDGET) m_cropStart += diff;
137 m_cropDuration = m_cropDuration - diff;
144 void AbstractClipItem::resizeEnd(int posx, double speed, bool /*updateKeyFrames*/)
146 GenTime durationDiff = GenTime(posx, m_fps) - endPos();
147 if (durationDiff == GenTime()) return;
148 //kDebug() << "// DUR DIFF1:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
149 if (cropDuration() + durationDiff <= GenTime()) {
150 durationDiff = GenTime() - (cropDuration() - GenTime(3, m_fps));
151 } else if (cropStart() + cropDuration() + durationDiff >= maxDuration()) {
152 //kDebug() << "// MAX OVERLOAD:" << cropDuration().frames(25) << " + " << durationDiff.frames(25) << ", MAX:" << maxDuration().frames(25);
153 durationDiff = maxDuration() - cropDuration() - cropStart();
155 //kDebug() << "// DUR DIFF2:" << durationDiff.frames(25) << ", ADJUSTED: " << durationDiff.frames(25) * speed << ", SPED:" << speed;
156 m_cropDuration += durationDiff * speed;
157 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
158 if (durationDiff > GenTime()) {
159 QList <QGraphicsItem *> collisionList = collidingItems(Qt::IntersectsItemBoundingRect);
160 for (int i = 0; i < collisionList.size(); ++i) {
161 QGraphicsItem *item = collisionList.at(i);
162 if (item->type() == type() && item->pos().x() > pos().x()) {
163 /*kDebug() << "///////// COLLISION DETECTED!!!!!!!!!";
164 kDebug() << "///////// CURRENT: " << startPos().frames(25) << "x" << endPos().frames(25) << ", RECT: " << rect() << "-" << pos();
165 kDebug() << "///////// COLLISION: " << ((AbstractClipItem *)item)->startPos().frames(25) << "x" << ((AbstractClipItem *)item)->endPos().frames(25) << ", RECT: " << ((AbstractClipItem *)item)->rect() << "-" << item->pos();*/
166 GenTime diff = ((AbstractClipItem *)item)->startPos() - GenTime(1, m_fps) - startPos();
167 m_cropDuration = diff * speed;
168 setRect(0, 0, cropDuration().frames(m_fps) - 0.02, rect().height());
175 GenTime AbstractClipItem::duration() const
177 return m_cropDuration;
180 GenTime AbstractClipItem::startPos() const
185 void AbstractClipItem::setTrack(int track)
190 double AbstractClipItem::fps() const
195 GenTime AbstractClipItem::maxDuration() const
197 return m_maxDuration;
200 void AbstractClipItem::setMaxDuration(const GenTime &max)
205 QPainterPath AbstractClipItem::upperRectPart(QRectF br)
207 QPainterPath roundRectPathUpper;
208 double roundingY = 20;
209 double roundingX = 20;
212 while (roundingX > br.width() / 2) {
213 roundingX = roundingX / 2;
214 roundingY = roundingY / 2;
216 int br_endx = (int)(br.x() + br .width() - offset);
217 int br_startx = (int)(br.x() + offset);
218 int br_starty = (int)(br.y());
219 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
221 roundRectPathUpper.moveTo(br_endx , br_halfy);
222 roundRectPathUpper.arcTo(br_endx - roundingX , br_starty , roundingX, roundingY, 0.0, 90.0);
223 roundRectPathUpper.lineTo(br_startx + roundingX , br_starty);
224 roundRectPathUpper.arcTo(br_startx , br_starty , roundingX, roundingY, 90.0, 90.0);
225 roundRectPathUpper.lineTo(br_startx , br_halfy);
227 return roundRectPathUpper;
230 QPainterPath AbstractClipItem::lowerRectPart(QRectF br)
232 QPainterPath roundRectPathLower;
233 double roundingY = 20;
234 double roundingX = 20;
237 int br_endx = (int)(br.x() + br .width() - offset);
238 int br_startx = (int)(br.x() + offset);
239 int br_halfy = (int)(br.y() + br.height() / 2 - offset);
240 int br_endy = (int)(br.y() + br.height() - 1);
242 while (roundingX > br.width() / 2) {
243 roundingX = roundingX / 2;
244 roundingY = roundingY / 2;
246 roundRectPathLower.moveTo(br_startx, br_halfy);
247 roundRectPathLower.arcTo(br_startx , br_endy - roundingY , roundingX, roundingY, 180.0, 90.0);
248 roundRectPathLower.lineTo(br_endx - roundingX , br_endy);
249 roundRectPathLower.arcTo(br_endx - roundingX , br_endy - roundingY, roundingX, roundingY, 270.0, 90.0);
250 roundRectPathLower.lineTo(br_endx , br_halfy);
251 return roundRectPathLower;
254 void AbstractClipItem::drawKeyFrames(QPainter *painter, QRectF /*exposedRect*/)
256 if (m_keyframes.count() < 2) return;
258 double maxw = br.width() / m_cropDuration.frames(m_fps);
259 double maxh = br.height() / 100.0 * m_keyframeFactor;
265 // draw line showing default value
269 y1 = br.bottom() - m_keyframeDefault * maxh;
270 QLineF l(x1, y1, x2, y1);
271 QLineF l2 = painter->matrix().map(l);
272 painter->setPen(QColor(168, 168, 168, 180));
273 painter->drawLine(l2);
275 painter->setPen(QColor(108, 108, 108, 180));
276 painter->drawLine(l2);
277 painter->setPen(QColor(Qt::white));
281 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
282 QColor color(Qt::blue);
283 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
284 y1 = br.bottom() - i.value() * maxh;
286 while (i != m_keyframes.constEnd()) {
287 if (i.key() == m_selectedKeyframe) color = QColor(Qt::red);
288 else color = QColor(Qt::blue);
290 if (i == m_keyframes.constEnd()) break;
291 x2 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
292 y2 = br.bottom() - i.value() * maxh;
293 QLineF l(x1, y1, x2, y2);
294 l2 = painter->matrix().map(l);
295 painter->drawLine(l2);
297 painter->fillRect(l2.x1() - 3, l2.y1() - 3, 6, 6, QBrush(color));
302 if (isSelected()) painter->fillRect(l2.x2() - 3, l2.y2() - 3, 6, 6, QBrush(color));
305 int AbstractClipItem::mouseOverKeyFrames(QPointF pos)
307 QRectF br = sceneBoundingRect();
308 double maxw = br.width() / m_cropDuration.frames(m_fps);
309 double maxh = br.height() / 100.0 * m_keyframeFactor;
310 if (m_keyframes.count() > 1) {
311 QMap<int, double>::const_iterator i = m_keyframes.constBegin();
314 while (i != m_keyframes.constEnd()) {
315 x1 = br.x() + maxw * (i.key() - m_cropStart.frames(m_fps));
316 y1 = br.bottom() - i.value() * maxh;
317 if (qAbs(pos.x() - x1) < 6 && qAbs(pos.y() - y1) < 6) {
318 setToolTip('[' + QString::number((GenTime(i.key(), m_fps) - m_cropStart).seconds(), 'f', 2) + i18n("seconds") + ", " + QString::number(i.value(), 'f', 1) + "%]");
320 } else if (x1 > pos.x()) break;
324 setToolTip(QString());
328 void AbstractClipItem::updateSelectedKeyFrame()
330 if (m_editedKeyframe == -1) return;
331 QRectF br = sceneBoundingRect();
332 double maxw = br.width() / m_cropDuration.frames(m_fps);
333 double maxh = br.height() / 100.0 * m_keyframeFactor;
334 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
335 m_selectedKeyframe = m_editedKeyframe;
336 update(br.x() + maxw * (m_selectedKeyframe - m_cropStart.frames(m_fps)) - 3, br.bottom() - m_keyframes[m_selectedKeyframe] * maxh - 3, 12, 12);
339 int AbstractClipItem::selectedKeyFramePos() const
341 return m_editedKeyframe;
344 double AbstractClipItem::selectedKeyFrameValue() const
346 return m_keyframes[m_editedKeyframe];
349 void AbstractClipItem::updateKeyFramePos(const GenTime pos, const double value)
351 if (!m_keyframes.contains(m_selectedKeyframe)) return;
352 int newpos = (int) pos.frames(m_fps);
353 int start = m_cropStart.frames(m_fps);
354 int end = (m_cropStart + m_cropDuration).frames(m_fps);
355 newpos = qMax(newpos, start);
356 newpos = qMin(newpos, end);
357 if (value < -50 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
358 // remove kexframe if it is dragged outside
359 m_keyframes.remove(m_selectedKeyframe);
360 m_selectedKeyframe = -1;
364 if (value > 150 && m_selectedKeyframe != start && m_selectedKeyframe != end) {
365 // remove kexframe if it is dragged outside
366 m_keyframes.remove(m_selectedKeyframe);
367 m_selectedKeyframe = -1;
371 double newval = qMax(value, 0.0);
372 newval = qMin(newval, 100.0);
373 newval = newval / m_keyframeFactor;
374 if (m_selectedKeyframe != newpos) m_keyframes.remove(m_selectedKeyframe);
375 m_keyframes[newpos] = newval;
376 m_selectedKeyframe = newpos;
380 double AbstractClipItem::keyFrameFactor() const
382 return m_keyframeFactor;
385 void AbstractClipItem::addKeyFrame(const GenTime pos, const double value)
387 QRectF br = sceneBoundingRect();
388 double maxh = 100.0 / br.height() / m_keyframeFactor;
389 double newval = (br.bottom() - value) * maxh;
390 kDebug() << "Rect: " << br << "/ SCENE: " << sceneBoundingRect() << ", VALUE: " << value << ", MAX: " << maxh << ", NEWVAL: " << newval;
391 int newpos = (int) pos.frames(m_fps) ;
392 m_keyframes[newpos] = newval;
393 m_selectedKeyframe = newpos;
397 bool AbstractClipItem::hasKeyFrames() const
399 return !m_keyframes.isEmpty();
402 /*QRect AbstractClipItem::visibleRect() {
404 if (scene()->views().size() > 0) {
405 rectInView = scene()->views()[0]->viewport()->rect();
406 rectInView.moveTo(scene()->views()[0]->horizontalScrollBar()->value(), scene()->views()[0]->verticalScrollBar()->value());
407 rectInView.adjust(-10, -10, 10, 10);//make view rect 10 pixel greater on each site, or repaint after scroll event
408 //kDebug() << scene()->views()[0]->viewport()->rect() << " " << scene()->views()[0]->horizontalScrollBar()->value();
413 CustomTrackScene* AbstractClipItem::projectScene()
415 if (scene()) return static_cast <CustomTrackScene*>(scene());
419 void AbstractClipItem::setItemLocked(bool locked)
423 setFlag(QGraphicsItem::ItemIsMovable, false);
424 setFlag(QGraphicsItem::ItemIsSelectable, false);
425 } else setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
428 bool AbstractClipItem::isItemLocked() const
430 return !(flags() & (QGraphicsItem::ItemIsSelectable));