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 #include <QPropertyAnimation>
35 Transition::Transition(const ItemInfo &info, int transitiontrack, double fps, QDomElement params, bool automaticTransition) :
36 AbstractClipItem(info, QRectF(), fps),
37 m_forceTransitionTrack(false),
38 m_automaticTransition(automaticTransition),
40 m_transitionTrack(transitiontrack)
43 m_info.cropDuration = info.endPos - info.startPos;
44 setPos(info.startPos.frames(fps), (int)(info.track * KdenliveSettings::trackheight() + itemOffset() + 1));
46 #if QT_VERSION >= 0x040600
47 if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
49 setRect(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight());
52 QPropertyAnimation *startAnimation = new QPropertyAnimation(this, "rect");
53 startAnimation->setDuration(200);
54 const QRectF r(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight() / 2);
55 const QRectF r2(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal)itemHeight());
56 startAnimation->setStartValue(r);
57 startAnimation->setEndValue(r2);
58 startAnimation->setEasingCurve(QEasingCurve::OutQuad);
59 startAnimation->start(QAbstractAnimation::DeleteWhenStopped);
62 setRect(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight());
65 m_info.cropStart = GenTime();
66 m_maxDuration = GenTime(600);
68 if (m_automaticTransition) setBrush(QColor(200, 200, 50, 180));
69 else setBrush(QColor(200, 100, 50, 180));
71 //m_referenceClip = clipa;
72 if (params.isNull()) {
73 m_parameters = MainWindow::transitions.getEffectByTag("luma", "dissolve").cloneNode().toElement();
75 m_parameters = params;
77 if (m_automaticTransition) m_parameters.setAttribute("automatic", 1);
78 else if (m_parameters.attribute("automatic") == "1") m_automaticTransition = true;
79 if (m_parameters.attribute("force_track") == "1") m_forceTransitionTrack = true;
80 m_name = i18n(m_parameters.firstChildElement("name").text().toUtf8().data());
83 //m_referenceClip->addTransition(this);
86 Transition::~Transition()
89 if (scene()) scene()->removeItem(this);
92 Transition *Transition::clone()
94 QDomElement xml = toXML().cloneNode().toElement();
95 Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
99 QString Transition::transitionTag() const
101 return m_parameters.attribute("tag");
104 QStringList Transition::transitionInfo() const
107 info << m_name << m_parameters.attribute("tag") << m_parameters.attribute("id");
111 bool Transition::isAutomatic() const
113 return m_automaticTransition;
116 void Transition::setAutomatic(bool automatic)
118 m_automaticTransition = automatic;
120 m_parameters.setAttribute("automatic", 1);
121 setBrush(QColor(200, 200, 50, 180));
123 m_parameters.removeAttribute("automatic");
124 setBrush(QColor(200, 100, 50, 180));
129 void Transition::setTransitionParameters(const QDomElement params)
131 m_parameters = params;
132 if (m_parameters.attribute("force_track") == "1") setForcedTrack(true, m_parameters.attribute("transition_btrack").toInt());
133 else if (m_parameters.attribute("force_track") == "0") setForcedTrack(false, m_parameters.attribute("transition_btrack").toInt());
134 m_name = i18n(m_parameters.firstChildElement("name").text().toUtf8().data());
139 bool Transition::invertedTransition() const
141 return false; //m_parameters.attribute("reverse").toInt();
144 void Transition::setTransitionDirection(bool /*inv*/)
146 //m_parameters.setAttribute("reverse", inv);
149 int Transition::transitionEndTrack() const
151 return m_transitionTrack;
154 void Transition::updateTransitionEndTrack(int newtrack)
156 if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
159 void Transition::setForcedTrack(bool force, int track)
161 m_forceTransitionTrack = force;
162 m_transitionTrack = track;
165 bool Transition::forcedTrack() const
167 return m_forceTransitionTrack;
170 void Transition::paint(QPainter *painter,
171 const QStyleOptionGraphicsItem *option,
174 const QRectF exposed = option->exposedRect;
175 painter->setClipRect(exposed);
176 const QRectF br = rect();
178 const QRectF mapped = painter->worldTransform().mapRect(br);
180 painter->fillRect(exposed, brush());
182 QPointF p1(br.x(), br.y() + br.height() / 2 - 7);
183 painter->setWorldMatrixEnabled(false);
184 const QString text = m_name + (m_forceTransitionTrack ? "|>" : QString());
187 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
188 framePen.setColor(Qt::red);
189 framePen.setWidthF(2.0);
192 framePen.setColor(brush().color().darker());
195 const QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + text + ' ');
196 painter->setBrush(framePen.color());
197 painter->setPen(Qt::NoPen);
198 painter->drawRoundedRect(txtBounding, 3, 3);
199 painter->setBrush(QBrush(Qt::NoBrush));
201 painter->setPen(Qt::white);
202 painter->drawText(txtBounding, Qt::AlignCenter, text);
205 painter->setPen(framePen);
206 painter->setClipping(false);
207 painter->drawRect(mapped.adjusted(0, 0, -0.5, -0.5));
210 int Transition::type() const
212 return TRANSITIONWIDGET;
216 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
218 if (change == QGraphicsItem::ItemSelectedChange) {
219 if (value.toBool()) setZValue(10);
222 if (change == ItemPositionChange && scene()) {
223 // calculate new position.
224 QPointF newPos = value.toPointF();
225 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
226 xpos = qMax(xpos, 0);
228 int newTrack = newPos.y() / KdenliveSettings::trackheight();
229 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
230 newTrack = qMax(newTrack, 0);
231 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + itemOffset() + 1));
232 // Only one clip is moving
233 QRectF sceneShape = rect();
234 sceneShape.translate(newPos);
235 QList<QGraphicsItem*> items;
236 // TODO: manage transitions in OVERWRITE MODE
237 //if (projectScene()->editMode() == NORMALEDIT)
238 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
239 items.removeAll(this);
241 bool forwardMove = newPos.x() > pos().x();
243 if (!items.isEmpty()) {
244 for (int i = 0; i < items.count(); i++) {
245 if (!items.at(i)->isEnabled()) continue;
246 if (items.at(i)->type() == type()) {
248 QPointF otherPos = items.at(i)->pos();
249 if ((int) otherPos.y() != (int) pos().y()) {
253 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
255 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
260 sceneShape.translate(QPointF(-offset, 0));
261 newPos.setX(newPos.x() - offset);
263 sceneShape.translate(QPointF(offset, 0));
264 newPos.setX(newPos.x() + offset);
266 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
267 subitems.removeAll(this);
268 for (int j = 0; j < subitems.count(); j++) {
269 if (!subitems.at(j)->isEnabled()) continue;
270 if (subitems.at(j)->type() == type()) {
271 // move was not successful, revert to previous pos
272 m_info.startPos = GenTime((int) pos().x(), m_fps);
278 m_info.track = newTrack;
279 m_info.startPos = GenTime((int) newPos.x(), m_fps);
286 m_info.track = newTrack;
287 m_info.startPos = GenTime((int) newPos.x(), m_fps);
288 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
291 return QGraphicsItem::itemChange(change, value);
295 OPERATIONTYPE Transition::operationMode(QPointF pos)
297 if (isItemLocked()) return NONE;
299 const double scale = projectScene()->scale().x();
300 double maximumOffset = 6 / scale;
302 QRectF rect = sceneBoundingRect();
303 if (qAbs((int)(pos.x() - rect.x())) < maximumOffset) return RESIZESTART;
304 else if (qAbs((int)(pos.x() - (rect.right()))) < maximumOffset) return RESIZEEND;
308 int Transition::itemHeight()
310 return (int) (KdenliveSettings::trackheight() / 3 * 2 - 1);
313 int Transition::itemOffset()
315 return (int) (KdenliveSettings::trackheight() / 3 * 2);
318 bool Transition::hasClip(const ClipItem * clip) const
320 if (clip == m_secondClip) return true;
324 bool Transition::belongsToClip(const ClipItem * clip) const
326 if (clip == m_referenceClip) return true;
331 Transition *Transition::clone() {
332 return new Transition::Transition(rect(), m_referenceClip, toXML() , m_fps);
336 Transition *Transition::reparent(ClipItem * clip) {
337 return new Transition::Transition(rect(), clip, toXML(), m_fps, m_referenceClip->startPos());
340 bool Transition::isValid() const
342 return true; //(m_transitionDuration != GenTime());
345 const ClipItem *Transition::referencedClip() const
347 return m_referenceClip;
350 QDomElement Transition::toXML()
352 m_parameters.setAttribute("type", transitionTag());
353 //m_transitionParameters.setAttribute("inverted", invertTransition());
354 m_parameters.setAttribute("transition_atrack", track());
355 m_parameters.setAttribute("transition_btrack", m_transitionTrack);
356 m_parameters.setAttribute("start", startPos().frames(m_fps));
357 m_parameters.setAttribute("end", endPos().frames(m_fps));
358 m_parameters.setAttribute("force_track", m_forceTransitionTrack);
359 m_parameters.setAttribute("automatic", m_automaticTransition);
362 m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
363 m_parameters.setAttribute("clipb_track", transitionEndTrack());
365 return m_parameters.cloneNode().toElement();
368 bool Transition::hasGeometry()
370 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
371 for (int i = 0; i < namenode.count() ; i++) {
372 QDomElement pa = namenode.item(i).toElement();
373 if (pa.attribute("type") == "geometry") return true;
378 int Transition::defaultZValue() const
383 bool Transition::updateKeyframes()
387 bool modified = false;
388 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
389 for (int i = 0; i < namenode.count() ; i++) {
390 pa = namenode.item(i).toElement();
391 if (pa.attribute("type") == "geometry") {
392 keyframes = pa.attribute("value");
396 if (keyframes.isEmpty()) return false;
397 int duration = cropDuration().frames(m_fps) - 1;
398 QStringList values = keyframes.split(";");
401 foreach(const QString &pos, values) {
402 if (!pos.contains('=')) {
406 frame = pos.section('=', 0, 0).toInt();
407 if (frame > duration) {
415 // Check if there is a keyframe at transition end
416 QString prev = values.at(i-1);
418 if (prev.contains('=')) {
419 int previousKeyframe = prev.section('=', 0, 0).toInt();
420 if (previousKeyframe == duration) {
421 // Remove the last keyframes
422 while (values.count() > i) {
429 // Add new keyframe at end and remove last keyframes
430 QString last = values.at(i);
431 last = QString::number(duration) + '=' + last.section('=', 1);
433 while (values.count() > (i + 1)) {
438 pa.setAttribute("value", values.join(";"));