1 /***************************************************************************
2 transition.cpp - description
4 begin : Tue Jan 24 2006
5 copyright : (C) 2006 by Jean-Baptiste Mardelle
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
18 #include "transition.h"
20 #include "kdenlivesettings.h"
21 #include "customtrackscene.h"
22 #include "mainwindow.h"
29 #include <QDomElement>
31 #include <QStyleOptionGraphicsItem>
32 #if QT_VERSION >= 0x040600
33 #include <QPropertyAnimation>
36 Transition::Transition(const ItemInfo &info, int transitiontrack, double fps, QDomElement params, bool automaticTransition) :
37 AbstractClipItem(info, QRectF(), fps),
38 m_forceTransitionTrack(false),
39 m_automaticTransition(automaticTransition),
41 m_transitionTrack(transitiontrack)
44 m_info.cropDuration = info.endPos - info.startPos;
45 setPos(info.startPos.frames(fps), (int)(info.track * KdenliveSettings::trackheight() + itemOffset() + 1));
47 #if QT_VERSION >= 0x040600
48 if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
50 setRect(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight());
53 QPropertyAnimation *startAnimation = new QPropertyAnimation(this, "rect");
54 startAnimation->setDuration(200);
55 const QRectF r(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight() / 2);
56 const QRectF r2(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal)itemHeight());
57 startAnimation->setStartValue(r);
58 startAnimation->setEndValue(r2);
59 startAnimation->setEasingCurve(QEasingCurve::OutQuad);
60 startAnimation->start(QAbstractAnimation::DeleteWhenStopped);
63 setRect(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight());
66 m_info.cropStart = GenTime();
67 m_maxDuration = GenTime(600);
69 if (m_automaticTransition) setBrush(QColor(200, 200, 50, 180));
70 else setBrush(QColor(200, 100, 50, 180));
72 //m_referenceClip = clipa;
73 if (params.isNull()) {
74 m_parameters = MainWindow::transitions.getEffectByTag("luma", "dissolve").cloneNode().toElement();
76 m_parameters = params;
78 if (m_automaticTransition) m_parameters.setAttribute("automatic", 1);
79 else if (m_parameters.attribute("automatic") == "1") m_automaticTransition = true;
80 if (m_parameters.attribute("force_track") == "1") m_forceTransitionTrack = true;
81 m_name = i18n(m_parameters.firstChildElement("name").text().toUtf8().data());
84 //m_referenceClip->addTransition(this);
87 Transition::~Transition()
90 if (scene()) scene()->removeItem(this);
93 Transition *Transition::clone()
95 QDomElement xml = toXML().cloneNode().toElement();
96 Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
100 QString Transition::transitionTag() const
102 return m_parameters.attribute("tag");
105 QStringList Transition::transitionInfo() const
108 info << m_name << m_parameters.attribute("tag") << m_parameters.attribute("id");
112 bool Transition::isAutomatic() const
114 return m_automaticTransition;
117 void Transition::setAutomatic(bool automatic)
119 m_automaticTransition = automatic;
121 m_parameters.setAttribute("automatic", 1);
122 setBrush(QColor(200, 200, 50, 180));
124 m_parameters.removeAttribute("automatic");
125 setBrush(QColor(200, 100, 50, 180));
130 void Transition::setTransitionParameters(const QDomElement params)
132 m_parameters = params;
133 if (m_parameters.attribute("force_track") == "1") setForcedTrack(true, m_parameters.attribute("transition_btrack").toInt());
134 else if (m_parameters.attribute("force_track") == "0") setForcedTrack(false, m_parameters.attribute("transition_btrack").toInt());
135 m_name = i18n(m_parameters.firstChildElement("name").text().toUtf8().data());
139 int Transition::transitionEndTrack() const
141 return m_transitionTrack;
144 void Transition::updateTransitionEndTrack(int newtrack)
146 if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
149 void Transition::setForcedTrack(bool force, int track)
151 m_forceTransitionTrack = force;
152 m_transitionTrack = track;
155 bool Transition::forcedTrack() const
157 return m_forceTransitionTrack;
160 void Transition::paint(QPainter *painter,
161 const QStyleOptionGraphicsItem *option,
164 const QRectF exposed = option->exposedRect;
165 painter->setClipRect(exposed);
166 const QRectF br = rect();
168 const QRectF mapped = painter->worldTransform().mapRect(br);
170 painter->fillRect(exposed, brush());
172 QPointF p1(br.x(), br.y() + br.height() / 2 - 7);
173 painter->setWorldMatrixEnabled(false);
174 const QString text = m_name + (m_forceTransitionTrack ? "|>" : QString());
177 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
178 framePen.setColor(Qt::red);
179 framePen.setWidthF(2.0);
182 framePen.setColor(brush().color().darker());
185 const QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + text + ' ');
186 painter->setBrush(framePen.color());
187 painter->setPen(Qt::NoPen);
188 painter->drawRoundedRect(txtBounding, 3, 3);
189 painter->setBrush(QBrush(Qt::NoBrush));
191 painter->setPen(Qt::white);
192 painter->drawText(txtBounding, Qt::AlignCenter, text);
195 painter->setPen(framePen);
196 painter->setClipping(false);
197 painter->drawRect(mapped.adjusted(0, 0, -0.5, -0.5));
200 int Transition::type() const
202 return TRANSITIONWIDGET;
206 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
208 if (change == QGraphicsItem::ItemSelectedChange) {
209 if (value.toBool()) setZValue(10);
212 if (change == ItemPositionChange && scene()) {
213 // calculate new position.
214 QPointF newPos = value.toPointF();
215 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
216 xpos = qMax(xpos, 0);
218 int newTrack = newPos.y() / KdenliveSettings::trackheight();
219 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
220 newTrack = qMax(newTrack, 0);
221 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + itemOffset() + 1));
222 // Only one clip is moving
223 QRectF sceneShape = rect();
224 sceneShape.translate(newPos);
225 QList<QGraphicsItem*> items;
226 // TODO: manage transitions in OVERWRITE MODE
227 //if (projectScene()->editMode() == NORMALEDIT)
228 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
229 items.removeAll(this);
231 bool forwardMove = newPos.x() > pos().x();
233 if (!items.isEmpty()) {
234 for (int i = 0; i < items.count(); i++) {
235 if (!items.at(i)->isEnabled()) continue;
236 if (items.at(i)->type() == type()) {
238 QPointF otherPos = items.at(i)->pos();
239 if ((int) otherPos.y() != (int) pos().y()) {
243 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
245 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
250 sceneShape.translate(QPointF(-offset, 0));
251 newPos.setX(newPos.x() - offset);
253 sceneShape.translate(QPointF(offset, 0));
254 newPos.setX(newPos.x() + offset);
256 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
257 subitems.removeAll(this);
258 for (int j = 0; j < subitems.count(); j++) {
259 if (!subitems.at(j)->isEnabled()) continue;
260 if (subitems.at(j)->type() == type()) {
261 // move was not successful, revert to previous pos
262 m_info.startPos = GenTime((int) pos().x(), m_fps);
268 m_info.track = newTrack;
269 m_info.startPos = GenTime((int) newPos.x(), m_fps);
276 m_info.track = newTrack;
277 m_info.startPos = GenTime((int) newPos.x(), m_fps);
278 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
281 return QGraphicsItem::itemChange(change, value);
285 OPERATIONTYPE Transition::operationMode(QPointF pos)
287 if (isItemLocked()) return NONE;
289 const double scale = projectScene()->scale().x();
290 double maximumOffset = 6 / scale;
292 QRectF rect = sceneBoundingRect();
293 if (qAbs((int)(pos.x() - rect.x())) < maximumOffset) return RESIZESTART;
294 else if (qAbs((int)(pos.x() - (rect.right()))) < maximumOffset) return RESIZEEND;
299 int Transition::itemHeight()
301 return (int) (KdenliveSettings::trackheight() / 3 * 2 - 1);
305 int Transition::itemOffset()
307 return (int) (KdenliveSettings::trackheight() / 3 * 2);
310 bool Transition::hasClip(const ClipItem * clip) const
312 if (clip == m_secondClip) return true;
316 bool Transition::belongsToClip(const ClipItem * clip) const
318 if (clip == m_referenceClip) return true;
323 Transition *Transition::clone() {
324 return new Transition::Transition(rect(), m_referenceClip, toXML() , m_fps);
327 const ClipItem *Transition::referencedClip() const
329 return m_referenceClip;
332 QDomElement Transition::toXML()
334 m_parameters.setAttribute("type", transitionTag());
335 //m_transitionParameters.setAttribute("inverted", invertTransition());
336 m_parameters.setAttribute("transition_atrack", track());
337 m_parameters.setAttribute("transition_btrack", m_transitionTrack);
338 m_parameters.setAttribute("start", startPos().frames(m_fps));
339 m_parameters.setAttribute("end", endPos().frames(m_fps));
340 m_parameters.setAttribute("force_track", m_forceTransitionTrack);
341 m_parameters.setAttribute("automatic", m_automaticTransition);
344 m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
345 m_parameters.setAttribute("clipb_track", transitionEndTrack());
347 return m_parameters.cloneNode().toElement();
350 bool Transition::hasGeometry()
352 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
353 for (int i = 0; i < namenode.count() ; i++) {
354 QDomElement pa = namenode.item(i).toElement();
355 if (pa.attribute("type") == "geometry") return true;
360 int Transition::defaultZValue() const
365 bool Transition::updateKeyframes()
369 bool modified = false;
370 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
371 for (int i = 0; i < namenode.count() ; i++) {
372 pa = namenode.item(i).toElement();
373 if (pa.attribute("type") == "geometry") {
374 keyframes = pa.attribute("value");
378 if (keyframes.isEmpty()) return false;
379 int duration = cropDuration().frames(m_fps) - 1;
380 QStringList values = keyframes.split(";");
383 foreach(const QString &pos, values) {
384 if (!pos.contains('=')) {
388 frame = pos.section('=', 0, 0).toInt();
389 if (frame > duration) {
397 // Check if there is a keyframe at transition end
398 QString prev = values.at(i-1);
400 if (prev.contains('=')) {
401 int previousKeyframe = prev.section('=', 0, 0).toInt();
402 if (previousKeyframe == duration) {
403 // Remove the last keyframes
404 while (values.count() > i) {
411 // Add new keyframe at end and remove last keyframes
412 QString last = values.at(i);
413 last = QString::number(duration) + '=' + last.section('=', 1);
415 while (values.count() > (i + 1)) {
420 pa.setAttribute("value", values.join(";"));