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)
40 setRect(0, 0, (info.endPos - info.startPos).frames(fps) - 0.02, (qreal)(KdenliveSettings::trackheight() / 3 * 2 - 1));
41 setPos(info.startPos.frames(fps), (qreal)(info.track * KdenliveSettings::trackheight() + KdenliveSettings::trackheight() / 3 * 2));
43 m_transitionTrack = transitiontrack;
45 m_cropStart = GenTime();
46 m_maxDuration = GenTime(10000, fps);
48 if (m_automaticTransition) setBrush(QColor(200, 200, 50, 100));
49 else setBrush(QColor(200, 100, 50, 100));
51 //m_referenceClip = clipa;
52 if (params.isNull()) {
53 m_parameters = MainWindow::transitions.getEffectByName("Luma").cloneNode().toElement();
55 m_parameters = params;
57 if (m_automaticTransition) m_parameters.setAttribute("automatic", 1);
58 else if (m_parameters.attribute("automatic") == "1") m_automaticTransition = true;
59 if (m_parameters.attribute("force_track") == "1") m_forceTransitionTrack = true;
60 m_name = m_parameters.elementsByTagName("name").item(0).toElement().text();
62 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
64 //m_referenceClip->addTransition(this);
67 Transition::~Transition()
71 Transition *Transition::clone()
73 QDomElement xml = toXML().cloneNode().toElement();
74 Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
78 QString Transition::transitionName() const
83 QString Transition::transitionTag() const
85 return m_parameters.attribute("tag");
88 bool Transition::isAutomatic() const
90 return m_automaticTransition;
93 void Transition::setAutomatic(bool automatic)
95 m_automaticTransition = automatic;
97 m_parameters.setAttribute("automatic", 1);
98 setBrush(QColor(200, 200, 50, 150));
100 m_parameters.removeAttribute("automatic");
101 setBrush(QColor(200, 50, 50, 150));
106 void Transition::setTransitionParameters(const QDomElement params)
108 m_parameters = params;
109 if (m_parameters.attribute("force_track") == "1") setForcedTrack(true, m_parameters.attribute("transition_btrack").toInt());
110 else if (m_parameters.attribute("force_track") == "0") setForcedTrack(false, m_parameters.attribute("transition_btrack").toInt());
111 m_name = m_parameters.elementsByTagName("name").item(0).toElement().text();
116 bool Transition::invertedTransition() const
118 return false; //m_parameters.attribute("reverse").toInt();
121 QPixmap Transition::transitionPixmap() const
124 QString tag = transitionTag();
126 if (invertedTransition()) icon = KIcon("kdenlive_trans_up");
127 else icon = KIcon("kdenlive_trans_down");
128 } else if (tag == "composite") {
129 icon = KIcon("kdenlive_trans_wiper");
130 } else if (tag == "lumafile") {
131 icon = KIcon("kdenlive_trans_luma");
132 } else icon = KIcon("kdenlive_trans_pip");
133 return icon.pixmap(QSize(15, 15));
137 void Transition::setTransitionDirection(bool /*inv*/)
139 //m_parameters.setAttribute("reverse", inv);
142 int Transition::transitionEndTrack() const
144 return m_transitionTrack;
147 void Transition::updateTransitionEndTrack(int newtrack)
149 if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
152 void Transition::setForcedTrack(bool force, int track)
154 m_forceTransitionTrack = force;
155 m_transitionTrack = track;
158 bool Transition::forcedTrack() const
160 return m_forceTransitionTrack;
163 void Transition::paint(QPainter *painter,
164 const QStyleOptionGraphicsItem *option,
167 const double scale = option->matrix.m11();
168 QRectF exposed = option->exposedRect;
169 painter->setClipRect(exposed);
171 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 QString text = transitionName();
180 if (m_forceTransitionTrack) text.append("|>");
181 QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + text + ' ');
182 painter->fillRect(txtBounding, QBrush(QColor(50, 50, 0, 150)));
183 txtBounding.translate(QPointF(1, 1));
184 painter->setPen(QColor(255, 255, 255, 255));
185 painter->drawText(txtBounding, Qt::AlignCenter, text);
187 /* painter->setPen(QColor(0, 0, 0, 180));
188 top += painter->fontInfo().pixelSize();
189 QPointF p2(br.x(), top);
190 painter->drawText(painter->matrix().map(p2) + QPointF(26, 1), transitionName());
191 painter->setPen(QColor(255, 255, 255, 180));
192 QPointF p3(br.x(), top);
193 painter->drawText(painter->matrix().map(p3) + QPointF(25, 0), transitionName());*/
194 painter->setMatrixEnabled(true);
195 QPen pen = painter->pen();
197 pen.setColor(Qt::red);
200 pen.setColor(Qt::black);
203 // expand clip rect to allow correct painting of clip border
204 exposed.setRight(exposed.right() + 1 / scale + 0.5);
205 exposed.setBottom(exposed.bottom() + 1);
206 painter->setClipRect(exposed);
207 painter->setPen(pen);
208 painter->drawRect(br);
211 int Transition::type() const
213 return TRANSITIONWIDGET;
217 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
219 if (change == ItemPositionChange && scene()) {
220 // calculate new position.
221 QPointF newPos = value.toPointF();
222 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
223 xpos = qMax(xpos, 0);
225 int newTrack = newPos.y() / KdenliveSettings::trackheight();
226 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
227 newTrack = qMax(newTrack, 0);
228 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + KdenliveSettings::trackheight() / 3 * 2));
229 // Only one clip is moving
230 QRectF sceneShape = rect();
231 sceneShape.translate(newPos);
232 QList<QGraphicsItem*> items = scene()->items(sceneShape, Qt::IntersectsItemShape);
233 items.removeAll(this);
235 if (!items.isEmpty()) {
236 for (int i = 0; i < items.count(); i++) {
237 if (items.at(i)->type() == type()) {
238 // Collision! Don't move.
239 //kDebug()<<"/// COLLISION WITH ITEM: "<<items.at(i)->boundingRect()<<", POS: "<<items.at(i)->pos()<<", ME: "<<newPos;
240 QPointF otherPos = items.at(i)->pos();
241 if ((int) otherPos.y() != (int) pos().y()) return pos();
242 //kDebug()<<"//// CURRENT Y: "<<pos().y()<<", COLLIDING Y: "<<otherPos.y();
243 if (pos().x() < otherPos.x()) {
244 int npos = (static_cast < AbstractClipItem* >(items.at(i))->startPos() - m_cropDuration).frames(m_fps);
247 // get pos just after colliding clip
248 int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
252 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
253 m_startPos = GenTime((int) newPos.x(), m_fps);
259 m_startPos = GenTime((int) newPos.x(), m_fps);
260 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
263 return QGraphicsItem::itemChange(change, value);
267 OPERATIONTYPE Transition::operationMode(QPointF pos)
269 if (isItemLocked()) return NONE;
271 const double scale = projectScene()->scale();
272 double maximumOffset = 6 / scale;
274 QRectF rect = sceneBoundingRect();
275 if (qAbs((int)(pos.x() - rect.x())) < maximumOffset) return RESIZESTART;
276 else if (qAbs((int)(pos.x() - (rect.right()))) < maximumOffset) return RESIZEEND;
280 bool Transition::hasClip(const ClipItem * clip) const
282 if (clip == m_secondClip) return true;
286 bool Transition::belongsToClip(const ClipItem * clip) const
288 if (clip == m_referenceClip) return true;
293 Transition *Transition::clone() {
294 return new Transition::Transition(rect(), m_referenceClip, toXML() , m_fps);
298 Transition *Transition::reparent(ClipItem * clip) {
299 return new Transition::Transition(rect(), clip, toXML(), m_fps, m_referenceClip->startPos());
302 bool Transition::isValid() const
304 return true; //(m_transitionDuration != GenTime());
307 const ClipItem *Transition::referencedClip() const
309 return m_referenceClip;
312 QDomElement Transition::toXML()
314 m_parameters.setAttribute("type", transitionTag());
315 //m_transitionParameters.setAttribute("inverted", invertTransition());
316 m_parameters.setAttribute("transition_atrack", track());
317 m_parameters.setAttribute("transition_btrack", m_transitionTrack);
318 m_parameters.setAttribute("start", startPos().frames(m_fps));
319 m_parameters.setAttribute("end", endPos().frames(m_fps));
320 m_parameters.setAttribute("force_track", m_forceTransitionTrack);
323 m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
324 m_parameters.setAttribute("clipb_track", transitionEndTrack());
330 bool Transition::hasGeometry()
332 QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
333 for (int i = 0;i < namenode.count() ;i++) {
334 QDomElement pa = namenode.item(i).toElement();
335 if (pa.attribute("type") == "geometry") return true;