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, const QDomElement ¶ms, bool automaticTransition) :
37 AbstractClipItem(info, QRectF(), fps),
38 m_forceTransitionTrack(false),
39 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 Transition::~Transition()
86 if (scene()) scene()->removeItem(this);
89 Transition *Transition::clone()
91 const QDomElement xml = toXML().cloneNode().toElement();
92 Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
96 QString Transition::transitionTag() const
98 return m_parameters.attribute("tag");
101 QStringList Transition::transitionInfo() const
104 info << m_name << m_parameters.attribute("tag") << m_parameters.attribute("id");
108 bool Transition::isAutomatic() const
110 return m_automaticTransition;
113 void Transition::setAutomatic(bool automatic)
115 m_automaticTransition = automatic;
117 m_parameters.setAttribute("automatic", 1);
118 setBrush(QColor(200, 200, 50, 180));
120 m_parameters.removeAttribute("automatic");
121 setBrush(QColor(200, 100, 50, 180));
126 void Transition::setTransitionParameters(const QDomElement ¶ms)
128 if (m_parameters != params) {
129 m_parameters = params;
130 if (m_parameters.attribute("force_track") == "1") setForcedTrack(true, m_parameters.attribute("transition_btrack").toInt());
131 else if (m_parameters.attribute("force_track") == "0") setForcedTrack(false, m_parameters.attribute("transition_btrack").toInt());
132 m_name = i18n(m_parameters.firstChildElement("name").text().toUtf8().data());
137 int Transition::transitionEndTrack() const
139 return m_transitionTrack;
142 void Transition::updateTransitionEndTrack(int newtrack)
144 if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
147 void Transition::setForcedTrack(bool force, int track)
149 m_forceTransitionTrack = force;
150 m_transitionTrack = track;
153 bool Transition::forcedTrack() const
155 return m_forceTransitionTrack;
158 void Transition::paint(QPainter *painter,
159 const QStyleOptionGraphicsItem *option,
162 const QRectF exposed = painter->worldTransform().mapRect(option->exposedRect);
163 const QRectF br = rect();
165 framePen.setWidthF(1.2);
166 const QRectF mapped = painter->worldTransform().mapRect(br);
168 QPointF p1(br.x(), br.y() + br.height() / 2 - 7);
169 painter->setWorldTransform(QTransform());
174 q.addRoundedRect(mapped, 3, 3);
175 painter->setClipPath(p.intersected(q));
176 painter->fillRect(exposed, brush());
177 const QString text = m_name + (m_forceTransitionTrack ? "|>" : QString());
180 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
181 framePen.setColor(scene()->palette().highlight().color());
182 framePen.setColor(Qt::red);
185 framePen.setColor(brush().color().darker());
188 const QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + text + ' ');
189 painter->setBrush(framePen.color());
190 painter->setPen(framePen.color());
191 painter->drawRoundedRect(txtBounding, 3, 3);
192 painter->setBrush(QBrush(Qt::NoBrush));
194 painter->setPen(Qt::white);
195 painter->drawText(txtBounding, Qt::AlignCenter, text);
198 painter->setPen(framePen);
199 painter->setClipping(false);
200 painter->setRenderHint(QPainter::Antialiasing, true);
201 painter->drawRoundedRect(mapped.adjusted(0, 0, -0.5, -0.5), 3, 3);
204 int Transition::type() const
206 return TransitionWidget;
210 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
212 if (change == QGraphicsItem::ItemSelectedChange) {
213 if (value.toBool()) setZValue(10);
216 if (change == ItemPositionChange && scene()) {
217 // calculate new position.
218 QPointF newPos = value.toPointF();
219 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
220 xpos = qMax(xpos, 0);
222 int newTrack = newPos.y() / KdenliveSettings::trackheight();
223 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
224 newTrack = qMax(newTrack, 0);
225 QStringList lockedTracks = property("locked_tracks").toStringList();
226 if (lockedTracks.contains(QString::number(newTrack))) {
227 // Trying to move to a locked track
230 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + itemOffset() + 1));
231 // Only one clip is moving
232 QRectF sceneShape = rect();
233 sceneShape.translate(newPos);
234 QList<QGraphicsItem*> items;
235 // TODO: manage transitions in OVERWRITE MODE
236 //if (projectScene()->editMode() == NORMALEDIT)
237 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
238 items.removeAll(this);
240 bool forwardMove = newPos.x() > pos().x();
242 if (!items.isEmpty()) {
243 for (int i = 0; i < items.count(); ++i) {
244 if (!items.at(i)->isEnabled()) continue;
245 if (items.at(i)->type() == type()) {
247 QPointF otherPos = items.at(i)->pos();
248 if ((int) otherPos.y() != (int) pos().y()) {
252 offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
254 offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
259 sceneShape.translate(QPointF(-offset, 0));
260 newPos.setX(newPos.x() - offset);
262 sceneShape.translate(QPointF(offset, 0));
263 newPos.setX(newPos.x() + offset);
265 QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
266 subitems.removeAll(this);
267 for (int j = 0; j < subitems.count(); j++) {
268 if (!subitems.at(j)->isEnabled()) continue;
269 if (subitems.at(j)->type() == type()) {
270 // move was not successful, revert to previous pos
271 m_info.startPos = GenTime((int) pos().x(), m_fps);
277 m_info.track = newTrack;
278 m_info.startPos = GenTime((int) newPos.x(), m_fps);
285 m_info.track = newTrack;
286 m_info.startPos = GenTime((int) newPos.x(), m_fps);
287 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
290 return QGraphicsItem::itemChange(change, value);
294 OperationType Transition::operationMode(const QPointF &pos)
296 if (isItemLocked()) return None;
298 const double scale = projectScene()->scale().x();
299 double maximumOffset = 6 / scale;
301 QRectF rect = sceneBoundingRect();
302 if (qAbs((int)(pos.x() - rect.x())) < maximumOffset) return ResizeStart;
303 else if (qAbs((int)(pos.x() - (rect.right()))) < maximumOffset) return ResizeEnd;
304 return MoveOperation;
308 int Transition::itemHeight()
310 return (int) (KdenliveSettings::trackheight() / 3 * 2 - 1);
314 int Transition::itemOffset()
316 return (int) (KdenliveSettings::trackheight() / 3 * 2);
320 QDomElement Transition::toXML()
322 m_parameters.setAttribute("type", transitionTag());
323 //m_transitionParameters.setAttribute("inverted", invertTransition());
324 m_parameters.setAttribute("transition_atrack", track());
325 m_parameters.setAttribute("transition_btrack", m_transitionTrack);
326 m_parameters.setAttribute("start", startPos().frames(m_fps));
327 m_parameters.setAttribute("end", endPos().frames(m_fps));
328 m_parameters.setAttribute("force_track", m_forceTransitionTrack);
329 m_parameters.setAttribute("automatic", m_automaticTransition);
330 return m_parameters.cloneNode().toElement();
333 bool Transition::hasGeometry()
335 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
336 for (int i = 0; i < namenode.count() ; ++i) {
337 QDomElement pa = namenode.item(i).toElement();
338 if (pa.attribute("type") == "geometry") return true;
343 int Transition::defaultZValue() const
348 bool Transition::updateKeyframes(int oldEnd)
352 bool modified = false;
353 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
354 for (int i = 0; i < namenode.count() ; ++i) {
355 pa = namenode.item(i).toElement();
356 if (pa.attribute("type") == "geometry") {
357 keyframes = pa.attribute("value");
361 if (keyframes.isEmpty()) return false;
362 int duration = cropDuration().frames(m_fps) - 1;
363 QStringList values = keyframes.split(';');
366 if (oldEnd < duration) {
367 // Transition was expanded, check if we had a keyframe at end position
368 foreach(QString pos, values) {
369 if (!pos.contains('=')) {
373 frame = pos.section('=', 0, 0).toInt();
374 if (frame == oldEnd) {
375 // Move that keyframe to new end
376 values[i] = QString::number(duration) + '=' + pos.section('=', 1);
377 pa.setAttribute("value", values.join(";"));
385 // Transition was shortened, check for out of bounds keyframes
386 foreach(const QString &pos, values) {
387 if (!pos.contains('=')) {
391 frame = pos.section('=', 0, 0).toInt();
392 if (frame > duration) {
401 // Check if there is a keyframe at transition end
402 QString prev = values.at(i-1);
404 if (prev.contains('=')) {
405 int previousKeyframe = prev.section('=', 0, 0).toInt();
406 if (previousKeyframe == duration) {
407 // Remove the last keyframes
408 while (values.count() > i) {
415 // Add new keyframe at end and remove last keyframes
416 QString last = values.at(i);
417 last = QString::number(duration) + '=' + last.section('=', 1);
419 while (values.count() > (i + 1)) {
424 pa.setAttribute("value", values.join(";"));
431 #include "transition.moc"