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() + KdenliveSettings::trackheight() / 3 * 2));
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, 1);
49 QRectF r2(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal)(KdenliveSettings::trackheight() / 3 * 2 - 1));
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)(KdenliveSettings::trackheight() / 3 * 2 - 1));
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 = m_parameters.elementsByTagName("name").item(0).toElement().text();
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::transitionName() const
100 QString Transition::transitionTag() const
102 return m_parameters.attribute("tag");
105 bool Transition::isAutomatic() const
107 return m_automaticTransition;
110 void Transition::setAutomatic(bool automatic)
112 m_automaticTransition = automatic;
114 m_parameters.setAttribute("automatic", 1);
115 setBrush(QColor(200, 200, 50, 150));
117 m_parameters.removeAttribute("automatic");
118 setBrush(QColor(200, 50, 50, 150));
123 void Transition::setTransitionParameters(const QDomElement params)
125 m_parameters = params;
126 if (m_parameters.attribute("force_track") == "1") setForcedTrack(true, m_parameters.attribute("transition_btrack").toInt());
127 else if (m_parameters.attribute("force_track") == "0") setForcedTrack(false, m_parameters.attribute("transition_btrack").toInt());
128 m_name = m_parameters.elementsByTagName("name").item(0).toElement().text();
133 bool Transition::invertedTransition() const
135 return false; //m_parameters.attribute("reverse").toInt();
138 void Transition::setTransitionDirection(bool /*inv*/)
140 //m_parameters.setAttribute("reverse", inv);
143 int Transition::transitionEndTrack() const
145 return m_transitionTrack;
148 void Transition::updateTransitionEndTrack(int newtrack)
150 if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
153 void Transition::setForcedTrack(bool force, int track)
155 m_forceTransitionTrack = force;
156 m_transitionTrack = track;
159 bool Transition::forcedTrack() const
161 return m_forceTransitionTrack;
164 void Transition::paint(QPainter *painter,
165 const QStyleOptionGraphicsItem *option,
168 const QRectF exposed = option->exposedRect;
169 painter->setClipRect(exposed);
170 const QRectF br = rect();
171 const QRectF mapped = painter->matrix().mapRect(br);
173 painter->fillRect(exposed, brush());
175 //int top = (int)(br.y() + br.height() / 2 - 7);
176 QPointF p1(br.x(), br.y() + br.height() / 2 - 7);
177 painter->setMatrixEnabled(false);
178 //painter->drawPixmap(painter->matrix().map(p1) + QPointF(5, 0), transitionPixmap());
179 const QString text = m_name + (m_forceTransitionTrack ? "|>" : QString());
182 QColor frameColor(brush().color().darker());
183 if (isSelected() || (parentItem() && parentItem()->isSelected())) {
184 frameColor = QColor(Qt::red);
186 frameColor.setAlpha(160);
188 const QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + text + ' ');
189 //painter->fillRect(txtBounding2, frameColor);
190 painter->setBrush(frameColor);
191 painter->setPen(Qt::NoPen);
192 painter->drawRoundedRect(txtBounding, 3, 3);
193 painter->setBrush(QBrush(Qt::NoBrush));
195 painter->setPen(Qt::white);
196 painter->drawText(txtBounding, Qt::AlignCenter, text);
199 QPen pen = painter->pen();
200 pen.setColor(frameColor);
201 painter->setPen(pen);
202 painter->setClipping(false);
203 painter->drawRect(painter->matrix().mapRect(rect()));
206 int Transition::type() const
208 return TRANSITIONWIDGET;
212 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
214 if (change == QGraphicsItem::ItemSelectedChange) {
215 if (value.toBool()) setZValue(10);
218 if (change == ItemPositionChange && scene()) {
219 // calculate new position.
220 QPointF newPos = value.toPointF();
221 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
222 xpos = qMax(xpos, 0);
224 int newTrack = newPos.y() / KdenliveSettings::trackheight();
225 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
226 newTrack = qMax(newTrack, 0);
227 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + KdenliveSettings::trackheight() / 3 * 2));
228 // Only one clip is moving
229 QRectF sceneShape = rect();
230 sceneShape.translate(newPos);
231 QList<QGraphicsItem*> items;
232 if (projectScene()->editMode() == NORMALEDIT)
233 items = scene()->items(sceneShape, Qt::IntersectsItemShape);
234 items.removeAll(this);
236 if (!items.isEmpty()) {
237 for (int i = 0; i < items.count(); i++) {
238 if (!items.at(i)->isEnabled()) continue;
239 if (items.at(i)->type() == type()) {
240 // Collision! Don't move.
241 //kDebug()<<"/// COLLISION WITH ITEM: "<<items.at(i)->boundingRect()<<", POS: "<<items.at(i)->pos()<<", ME: "<<newPos;
242 QPointF otherPos = items.at(i)->pos();
243 if ((int) otherPos.y() != (int) pos().y()) return pos();
244 //kDebug()<<"//// CURRENT Y: "<<pos().y()<<", COLLIDING Y: "<<otherPos.y();
245 if (pos().x() < otherPos.x()) {
246 int npos = (static_cast < AbstractClipItem* >(items.at(i))->startPos() - m_info.cropDuration).frames(m_fps);
249 // get pos just after colliding clip
250 int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
253 m_info.track = newTrack;
254 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
255 m_info.startPos = GenTime((int) newPos.x(), m_fps);
260 m_info.track = newTrack;
261 m_info.startPos = GenTime((int) newPos.x(), m_fps);
262 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
265 return QGraphicsItem::itemChange(change, value);
269 OPERATIONTYPE Transition::operationMode(QPointF pos)
271 if (isItemLocked()) return NONE;
273 const double scale = projectScene()->scale().x();
274 double maximumOffset = 6 / scale;
276 QRectF rect = sceneBoundingRect();
277 if (qAbs((int)(pos.x() - rect.x())) < maximumOffset) return RESIZESTART;
278 else if (qAbs((int)(pos.x() - (rect.right()))) < maximumOffset) return RESIZEEND;
282 bool Transition::hasClip(const ClipItem * clip) const
284 if (clip == m_secondClip) return true;
288 bool Transition::belongsToClip(const ClipItem * clip) const
290 if (clip == m_referenceClip) return true;
295 Transition *Transition::clone() {
296 return new Transition::Transition(rect(), m_referenceClip, toXML() , m_fps);
300 Transition *Transition::reparent(ClipItem * clip) {
301 return new Transition::Transition(rect(), clip, toXML(), m_fps, m_referenceClip->startPos());
304 bool Transition::isValid() const
306 return true; //(m_transitionDuration != GenTime());
309 const ClipItem *Transition::referencedClip() const
311 return m_referenceClip;
314 QDomElement Transition::toXML()
316 m_parameters.setAttribute("type", transitionTag());
317 //m_transitionParameters.setAttribute("inverted", invertTransition());
318 m_parameters.setAttribute("transition_atrack", track());
319 m_parameters.setAttribute("transition_btrack", m_transitionTrack);
320 m_parameters.setAttribute("start", startPos().frames(m_fps));
321 m_parameters.setAttribute("end", endPos().frames(m_fps));
322 m_parameters.setAttribute("force_track", m_forceTransitionTrack);
325 m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
326 m_parameters.setAttribute("clipb_track", transitionEndTrack());
328 return m_parameters.cloneNode().toElement();
331 bool Transition::hasGeometry()
333 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
334 for (int i = 0; i < namenode.count() ; i++) {
335 QDomElement pa = namenode.item(i).toElement();
336 if (pa.attribute("type") == "geometry") return true;
341 int Transition::defaultZValue() const