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>
34 Transition::Transition(const ItemInfo info, int transitiontrack, double fps, QDomElement params, bool automaticTransition) :
35 AbstractClipItem(info, QRectF(), fps),
36 m_forceTransitionTrack(false),
37 m_automaticTransition(automaticTransition),
39 m_transitionTrack(transitiontrack)
42 m_info.cropDuration = info.endPos - info.startPos;
43 setPos(info.startPos.frames(fps), (qreal)(info.track * KdenliveSettings::trackheight() + itemOffset() + 1));
45 #if QT_VERSION >= 0x040600
46 m_startAnimation = new QPropertyAnimation(this, "rect");
47 m_startAnimation->setDuration(200);
48 QRectF r(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight() / 2);
49 QRectF r2(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal)itemHeight());
50 m_startAnimation->setStartValue(r);
51 m_startAnimation->setEndValue(r2);
52 m_startAnimation->setEasingCurve(QEasingCurve::OutQuad);
53 m_startAnimation->start();
55 setRect(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight());
58 m_info.cropStart = GenTime();
59 m_maxDuration = GenTime(600);
61 if (m_automaticTransition) setBrush(QColor(200, 200, 50, 100));
62 else setBrush(QColor(200, 100, 50, 100));
64 //m_referenceClip = clipa;
65 if (params.isNull()) {
66 m_parameters = MainWindow::transitions.getEffectByTag("luma", "dissolve").cloneNode().toElement();
68 m_parameters = params;
70 if (m_automaticTransition) m_parameters.setAttribute("automatic", 1);
71 else if (m_parameters.attribute("automatic") == "1") m_automaticTransition = true;
72 if (m_parameters.attribute("force_track") == "1") m_forceTransitionTrack = true;
73 m_name = i18n(m_parameters.elementsByTagName("name").item(0).toElement().text().toUtf8().data());
76 //m_referenceClip->addTransition(this);
79 Transition::~Transition()
82 #if QT_VERSION >= 0x040600
83 delete m_startAnimation;
85 if (scene()) scene()->removeItem(this);
88 Transition *Transition::clone()
90 QDomElement xml = toXML().cloneNode().toElement();
91 Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
95 QString Transition::transitionTag() const
97 return m_parameters.attribute("tag");
100 QStringList Transition::transitionInfo() const
103 info << m_name << m_parameters.attribute("tag") << m_parameters.attribute("id");
107 bool Transition::isAutomatic() const
109 return m_automaticTransition;
112 void Transition::setAutomatic(bool automatic)
114 m_automaticTransition = automatic;
116 m_parameters.setAttribute("automatic", 1);
117 setBrush(QColor(200, 200, 50, 150));
119 m_parameters.removeAttribute("automatic");
120 setBrush(QColor(200, 50, 50, 150));
125 void Transition::setTransitionParameters(const QDomElement params)
127 m_parameters = params;
128 if (m_parameters.attribute("force_track") == "1") setForcedTrack(true, m_parameters.attribute("transition_btrack").toInt());
129 else if (m_parameters.attribute("force_track") == "0") setForcedTrack(false, m_parameters.attribute("transition_btrack").toInt());
130 m_name = i18n(m_parameters.elementsByTagName("name").item(0).toElement().text().toUtf8().data());
135 bool Transition::invertedTransition() const
137 return false; //m_parameters.attribute("reverse").toInt();
140 void Transition::setTransitionDirection(bool /*inv*/)
142 //m_parameters.setAttribute("reverse", inv);
145 int Transition::transitionEndTrack() const
147 return m_transitionTrack;
150 void Transition::updateTransitionEndTrack(int newtrack)
152 if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
155 void Transition::setForcedTrack(bool force, int track)
157 m_forceTransitionTrack = force;
158 m_transitionTrack = track;
161 bool Transition::forcedTrack() const
163 return m_forceTransitionTrack;
166 void Transition::paint(QPainter *painter,
167 const QStyleOptionGraphicsItem *option,
170 const QRectF exposed = option->exposedRect;
171 painter->setClipRect(exposed);
172 const QRectF br = rect();
173 const QRectF mapped = painter->matrix().mapRect(br);
175 painter->fillRect(exposed, brush());
177 //int top = (int)(br.y() + br.height() / 2 - 7);
178 QPointF p1(br.x(), br.y() + br.height() / 2 - 7);
179 painter->setMatrixEnabled(false);
180 //painter->drawPixmap(painter->matrix().map(p1) + QPointF(5, 0), transitionPixmap());
181 const QString text = m_name + (m_forceTransitionTrack ? "|>" : QString());
184 QColor frameColor(brush().color().darker());
185 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
186 frameColor = QColor(Qt::red);
188 frameColor.setAlpha(160);
190 const QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + text + ' ');
191 //painter->fillRect(txtBounding2, frameColor);
192 painter->setBrush(frameColor);
193 painter->setPen(Qt::NoPen);
194 painter->drawRoundedRect(txtBounding, 3, 3);
195 painter->setBrush(QBrush(Qt::NoBrush));
197 painter->setPen(Qt::white);
198 painter->drawText(txtBounding, Qt::AlignCenter, text);
201 QPen pen = painter->pen();
202 pen.setColor(frameColor);
203 painter->setPen(pen);
204 painter->setClipping(false);
205 painter->drawRect(painter->matrix().mapRect(rect()));
208 int Transition::type() const
210 return TRANSITIONWIDGET;
214 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
216 if (change == QGraphicsItem::ItemSelectedChange) {
217 if (value.toBool()) setZValue(10);
220 if (change == ItemPositionChange && scene()) {
221 // calculate new position.
222 QPointF newPos = value.toPointF();
223 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
224 xpos = qMax(xpos, 0);
226 int newTrack = newPos.y() / KdenliveSettings::trackheight();
227 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
228 newTrack = qMax(newTrack, 0);
229 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + KdenliveSettings::trackheight() / 3 * 2));
230 // Only one clip is moving
231 QRectF sceneShape = rect();
232 sceneShape.translate(newPos);
233 QList<QGraphicsItem*> items;
234 // TODO: manage transitions in OVERWRITE MODE
235 //if (projectScene()->editMode() == NORMALEDIT)
236 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
237 items.removeAll(this);
239 bool forwardMove = newPos.x() > pos().x();
241 if (!items.isEmpty()) {
242 for (int i = 0; i < items.count(); i++) {
243 if (!items.at(i)->isEnabled()) continue;
244 if (items.at(i)->type() == type()) {
246 QPointF otherPos = items.at(i)->pos();
247 if ((int) otherPos.y() != (int) pos().y()) {
251 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
253 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
258 sceneShape.translate(QPointF(-offset, 0));
259 newPos.setX(newPos.x() - offset);
261 sceneShape.translate(QPointF(offset, 0));
262 newPos.setX(newPos.x() + offset);
264 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
265 subitems.removeAll(this);
266 for (int j = 0; j < subitems.count(); j++) {
267 if (!subitems.at(j)->isEnabled()) continue;
268 if (subitems.at(j)->type() == type()) {
269 // move was not successful, revert to previous pos
270 m_info.startPos = GenTime((int) pos().x(), m_fps);
276 m_info.track = newTrack;
277 m_info.startPos = GenTime((int) newPos.x(), m_fps);
284 m_info.track = newTrack;
285 m_info.startPos = GenTime((int) newPos.x(), m_fps);
286 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
289 return QGraphicsItem::itemChange(change, value);
293 OPERATIONTYPE Transition::operationMode(QPointF pos)
295 if (isItemLocked()) return NONE;
297 const double scale = projectScene()->scale().x();
298 double maximumOffset = 6 / scale;
300 QRectF rect = sceneBoundingRect();
301 if (qAbs((int)(pos.x() - rect.x())) < maximumOffset) return RESIZESTART;
302 else if (qAbs((int)(pos.x() - (rect.right()))) < maximumOffset) return RESIZEEND;
306 int Transition::itemHeight()
308 return (int) (KdenliveSettings::trackheight() / 3 * 2 - 1);
311 int Transition::itemOffset()
313 return (int) (KdenliveSettings::trackheight() / 3 * 2);
316 bool Transition::hasClip(const ClipItem * clip) const
318 if (clip == m_secondClip) return true;
322 bool Transition::belongsToClip(const ClipItem * clip) const
324 if (clip == m_referenceClip) return true;
329 Transition *Transition::clone() {
330 return new Transition::Transition(rect(), m_referenceClip, toXML() , m_fps);
334 Transition *Transition::reparent(ClipItem * clip) {
335 return new Transition::Transition(rect(), clip, toXML(), m_fps, m_referenceClip->startPos());
338 bool Transition::isValid() const
340 return true; //(m_transitionDuration != GenTime());
343 const ClipItem *Transition::referencedClip() const
345 return m_referenceClip;
348 QDomElement Transition::toXML()
350 m_parameters.setAttribute("type", transitionTag());
351 //m_transitionParameters.setAttribute("inverted", invertTransition());
352 m_parameters.setAttribute("transition_atrack", track());
353 m_parameters.setAttribute("transition_btrack", m_transitionTrack);
354 m_parameters.setAttribute("start", startPos().frames(m_fps));
355 m_parameters.setAttribute("end", endPos().frames(m_fps));
356 m_parameters.setAttribute("force_track", m_forceTransitionTrack);
357 m_parameters.setAttribute("automatic", m_automaticTransition);
360 m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
361 m_parameters.setAttribute("clipb_track", transitionEndTrack());
363 return m_parameters.cloneNode().toElement();
366 bool Transition::hasGeometry()
368 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
369 for (int i = 0; i < namenode.count() ; i++) {
370 QDomElement pa = namenode.item(i).toElement();
371 if (pa.attribute("type") == "geometry") return true;
376 int Transition::defaultZValue() const
381 bool Transition::updateKeyframes()
385 bool modified = false;
386 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
387 for (int i = 0; i < namenode.count() ; i++) {
388 pa = namenode.item(i).toElement();
389 if (pa.attribute("type") == "geometry") {
390 keyframes = pa.attribute("value");
394 if (keyframes.isEmpty()) return false;
395 int duration = cropDuration().frames(m_fps) - 1;
396 QStringList values = keyframes.split(";");
399 foreach(const QString &pos, values) {
400 if (!pos.contains('=')) {
404 frame = pos.section('=', 0, 0).toInt();
405 if (frame > duration) {
413 // Check if there is a keyframe at transition end
414 QString prev = values.at(i-1);
416 if (prev.contains('=')) {
417 int previousKeyframe = prev.section('=', 0, 0).toInt();
418 if (previousKeyframe == duration) {
419 // Remove the last keyframes
420 while (values.count() > i) {
427 // Add new keyframe at end and remove last keyframes
428 QString last = values.at(i);
429 last = QString::number(duration) + '=' + last.section('=', 1);
431 while (values.count() > (i + 1)) {
436 pa.setAttribute("value", values.join(";"));