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 ***************************************************************************/
19 #include <QDomElement>
21 #include <QGraphicsScene>
22 #include <QGraphicsView>
24 #include <QStyleOptionGraphicsItem>
30 #include "transition.h"
32 #include "kdenlivesettings.h"
33 #include "customtrackscene.h"
34 #include "mainwindow.h"
36 Transition::Transition(const ItemInfo info, int transitiontrack, double fps, QDomElement params) : AbstractClipItem(info, QRectF(), fps), m_gradient(QLinearGradient(0, 0, 0, 0)) {
37 setRect(0, 0, (qreal)(info.endPos - info.startPos).frames(fps) - 0.02, (qreal)(KdenliveSettings::trackheight() / 3 * 2 - 1));
38 setPos((qreal) info.startPos.frames(fps), (qreal)(info.track * KdenliveSettings::trackheight() + KdenliveSettings::trackheight() / 3 * 2));
41 m_transitionTrack = transitiontrack;
43 m_cropStart = GenTime();
44 m_maxDuration = GenTime(10000, fps);
46 m_gradient.setColorAt(0, QColor(200, 200, 0, 150));
47 m_gradient.setColorAt(1, QColor(200, 200, 200, 120));
49 //m_referenceClip = clipa;
50 if (params.isNull()) {
51 m_parameters = MainWindow::transitions.getEffectByName("Luma");
53 m_parameters = params;
56 m_name = m_parameters.elementsByTagName("name").item(0).toElement().text();
58 setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
61 //m_referenceClip->addTransition(this);
64 Transition::~Transition() {
67 Transition *Transition::clone() {
68 QDomElement xml = toXML().cloneNode().toElement();
69 Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
73 QString Transition::transitionName() const {
77 QString Transition::transitionTag() const {
78 return m_parameters.attribute("tag");
81 void Transition::setTransitionParameters(const QDomElement params) {
82 m_parameters = params;
83 m_name = m_parameters.elementsByTagName("name").item(0).toElement().text();
88 bool Transition::invertedTransition() const {
89 return false; //m_parameters.attribute("reverse").toInt();
92 QPixmap Transition::transitionPixmap() const {
94 QString tag = transitionTag();
96 if (invertedTransition()) icon = KIcon("kdenlive_trans_up");
97 else icon = KIcon("kdenlive_trans_down");
98 } else if (tag == "composite") {
99 icon = KIcon("kdenlive_trans_wiper");
100 } else if (tag == "lumafile") {
101 icon = KIcon("kdenlive_trans_luma");
102 } else icon = KIcon("kdenlive_trans_pip");
103 return icon.pixmap(QSize(15, 15));
107 void Transition::setTransitionDirection(bool inv) {
108 //m_parameters.setAttribute("reverse", inv);
111 int Transition::transitionEndTrack() const {
112 return m_transitionTrack;
115 void Transition::updateTransitionEndTrack(int newtrack) {
116 m_transitionTrack = newtrack;
119 void Transition::paint(QPainter *painter,
120 const QStyleOptionGraphicsItem *option,
122 QRectF exposed = option->exposedRect;
123 exposed.setRight(exposed.right() + 1);
124 exposed.setBottom(exposed.bottom() + 1);
125 painter->setClipRect(exposed);
127 m_gradient.setStart(0, br.y());
128 m_gradient.setFinalStop(0, br.bottom());
129 painter->fillRect(br, m_gradient);
131 int top = (int)(br.y() + br.height() / 2 - 7);
132 QPointF p1(br.x(), br.y() + br.height() / 2 - 7);
133 painter->setMatrixEnabled(false);
134 painter->drawPixmap(painter->matrix().map(p1) + QPointF(5, 0), transitionPixmap());
135 painter->setPen(QColor(0, 0, 0, 180));
136 top += painter->fontInfo().pixelSize();
137 QPointF p2(br.x(), top);
138 painter->drawText(painter->matrix().map(p2) + QPointF(26, 1), transitionName());
139 painter->setPen(QColor(255, 255, 255, 180));
140 QPointF p3(br.x(), top);
141 painter->drawText(painter->matrix().map(p3) + QPointF(25, 0), transitionName());
142 painter->setMatrixEnabled(true);
143 QPen pen = painter->pen();
145 pen.setColor(Qt::red);
148 pen.setColor(Qt::black);
151 painter->setPen(pen);
152 painter->drawRect(br);
155 int Transition::type() const {
156 return TRANSITIONWIDGET;
160 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value) {
161 if (change == ItemPositionChange && scene()) {
162 // calculate new position.
163 QPointF newPos = value.toPointF();
164 int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
165 xpos = qMax(xpos, 0);
167 int newTrack = newPos.y() / KdenliveSettings::trackheight();
168 newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
169 newTrack = qMax(newTrack, 0);
170 newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + KdenliveSettings::trackheight() / 3 * 2));
171 // Only one clip is moving
172 QRectF sceneShape = rect();
173 sceneShape.translate(newPos);
174 QList<QGraphicsItem*> items = scene()->items(sceneShape, Qt::IntersectsItemShape);
175 items.removeAll(this);
177 if (!items.isEmpty()) {
178 for (int i = 0; i < items.count(); i++) {
179 if (items.at(i)->type() == type()) {
180 // Collision! Don't move.
181 //kDebug()<<"/// COLLISION WITH ITEM: "<<items.at(i)->boundingRect()<<", POS: "<<items.at(i)->pos()<<", ME: "<<newPos;
182 QPointF otherPos = items.at(i)->pos();
183 if ((int) otherPos.y() != (int) pos().y()) return pos();
184 //kDebug()<<"//// CURRENT Y: "<<pos().y()<<", COLLIDING Y: "<<otherPos.y();
185 if (pos().x() < otherPos.x()) {
186 int npos = (static_cast < AbstractClipItem* >(items.at(i))->startPos() - m_cropDuration).frames(m_fps);
189 // get pos just after colliding clip
190 int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
194 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
195 m_startPos = GenTime((int) newPos.x(), m_fps);
201 m_startPos = GenTime((int) newPos.x(), m_fps);
202 //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
205 return QGraphicsItem::itemChange(change, value);
209 OPERATIONTYPE Transition::operationMode(QPointF pos) {
210 QRectF rect = sceneBoundingRect();
211 if (qAbs((int)(pos.x() - rect.x())) < 6) return RESIZESTART;
212 else if (qAbs((int)(pos.x() - (rect.right()))) < 6) return RESIZEEND;
216 bool Transition::hasClip(const ClipItem * clip) const {
217 if (clip == m_secondClip) return true;
221 bool Transition::belongsToClip(const ClipItem * clip) const {
222 if (clip == m_referenceClip) return true;
227 Transition *Transition::clone() {
228 return new Transition::Transition(rect(), m_referenceClip, this->toXML() , m_fps);
232 Transition *Transition::reparent(ClipItem * clip) {
233 return new Transition::Transition(rect(), clip, this->toXML(), m_fps, m_referenceClip->startPos());
236 bool Transition::isValid() const {
237 return true; //(m_transitionDuration != GenTime());
240 const ClipItem *Transition::referencedClip() const {
241 return m_referenceClip;
244 QDomElement Transition::toXML() {
245 m_parameters.setAttribute("type", transitionTag());
246 //m_transitionParameters.setAttribute("inverted", invertTransition());
247 m_parameters.setAttribute("transition_atrack", track());
248 m_parameters.setAttribute("transition_btrack", m_transitionTrack);
249 m_parameters.setAttribute("start", startPos().frames(m_fps));
250 m_parameters.setAttribute("end", endPos().frames(m_fps));
253 m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
254 m_parameters.setAttribute("clipb_track", transitionEndTrack());