]> git.sesse.net Git - kdenlive/blob - src/transition.cpp
* Fix crash when using razor tool while playing
[kdenlive] / src / transition.cpp
1 /***************************************************************************
2                           transition.cpp  -  description
3                              -------------------
4     begin                : Tue Jan 24 2006
5     copyright            : (C) 2006 by Jean-Baptiste Mardelle
6     email                : jb@ader.ch
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
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.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include <QBrush>
19 #include <QDomElement>
20 #include <QPainter>
21 #include <QGraphicsScene>
22 #include <QGraphicsView>
23 #include <QScrollBar>
24 #include <QStyleOptionGraphicsItem>
25
26 #include <kdebug.h>
27 #include <KIcon>
28 #include <klocale.h>
29
30 #include "transition.h"
31 #include "clipitem.h"
32 #include "kdenlivesettings.h"
33 #include "customtrackscene.h"
34 #include "mainwindow.h"
35
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));
39
40     m_singleClip = true;
41     m_transitionTrack = transitiontrack;
42     m_secondClip = NULL;
43     m_cropStart = GenTime();
44     m_maxDuration = GenTime(10000, fps);
45
46     m_gradient.setColorAt(0, QColor(200, 200, 0, 150));
47     m_gradient.setColorAt(1, QColor(200, 200, 200, 120));
48
49     //m_referenceClip = clipa;
50     if (params.isNull()) {
51         m_parameters = MainWindow::transitions.getEffectByName("Luma");
52     } else {
53         m_parameters = params;
54     }
55
56     m_name = m_parameters.elementsByTagName("name").item(0).toElement().text();
57     m_secondClip = 0;
58     setFlags(QGraphicsItem::ItemClipsToShape | QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
59     setZValue(2);
60
61     //m_referenceClip->addTransition(this);
62 }
63
64 Transition::~Transition() {
65 }
66
67 Transition *Transition::clone() {
68     QDomElement xml = toXML().cloneNode().toElement();
69     Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
70     return tr;
71 }
72
73 QString Transition::transitionName() const {
74     return m_name;
75 }
76
77 QString Transition::transitionTag() const {
78     return m_parameters.attribute("tag");
79 }
80
81 void Transition::setTransitionParameters(const QDomElement params) {
82     m_parameters = params;
83     m_name = m_parameters.elementsByTagName("name").item(0).toElement().text();
84     update();
85 }
86
87
88 bool Transition::invertedTransition() const {
89     return false; //m_parameters.attribute("reverse").toInt();
90 }
91
92 QPixmap Transition::transitionPixmap() const {
93     KIcon icon;
94     QString tag = transitionTag();
95     if (tag == "luma") {
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));
104 }
105
106
107 void Transition::setTransitionDirection(bool inv) {
108     //m_parameters.setAttribute("reverse", inv);
109 }
110
111 int Transition::transitionEndTrack() const {
112     return m_transitionTrack;
113 }
114
115 void Transition::updateTransitionEndTrack(int newtrack) {
116     m_transitionTrack = newtrack;
117 }
118
119 void Transition::paint(QPainter *painter,
120                        const QStyleOptionGraphicsItem *option,
121                        QWidget *widget) {
122     QRectF exposed = option->exposedRect;
123     exposed.setRight(exposed.right() + 1);
124     exposed.setBottom(exposed.bottom() + 1);
125     painter->setClipRect(exposed);
126     QRectF br = rect();
127     m_gradient.setStart(0, br.y());
128     m_gradient.setFinalStop(0, br.bottom());
129     painter->fillRect(br, m_gradient);
130
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();
144     if (isSelected()) {
145         pen.setColor(Qt::red);
146         //pen.setWidth(2);
147     } else {
148         pen.setColor(Qt::black);
149         //pen.setWidth(1);
150     }
151     painter->setPen(pen);
152     painter->drawRect(br);
153 }
154
155 int Transition::type() const {
156     return TRANSITIONWIDGET;
157 }
158
159 //virtual
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);
166         newPos.setX(xpos);
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);
176
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);
187                         newPos.setX(npos);
188                     } else {
189                         // get pos just after colliding clip
190                         int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
191                         newPos.setX(npos);
192                     }
193                     m_track = newTrack;
194                     //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
195                     m_startPos = GenTime((int) newPos.x(), m_fps);
196                     return newPos;
197                 }
198             }
199         }
200         m_track = newTrack;
201         m_startPos = GenTime((int) newPos.x(), m_fps);
202         //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
203         return newPos;
204     }
205     return QGraphicsItem::itemChange(change, value);
206 }
207
208
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;
213     return MOVE;
214 }
215
216 bool Transition::hasClip(const ClipItem * clip) const {
217     if (clip == m_secondClip) return true;
218     return false;
219 }
220
221 bool Transition::belongsToClip(const ClipItem * clip) const {
222     if (clip == m_referenceClip) return true;
223     return false;
224 }
225
226 /*
227 Transition *Transition::clone() {
228     return new Transition::Transition(rect(), m_referenceClip, this->toXML() , m_fps);
229 }*/
230
231 /*
232 Transition *Transition::reparent(ClipItem * clip) {
233     return new Transition::Transition(rect(), clip, this->toXML(), m_fps, m_referenceClip->startPos());
234 }*/
235
236 bool Transition::isValid() const {
237     return true; //(m_transitionDuration != GenTime());
238 }
239
240 const ClipItem *Transition::referencedClip() const {
241     return m_referenceClip;
242 }
243
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));
251
252     if (m_secondClip) {
253         m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
254         m_parameters.setAttribute("clipb_track", transitionEndTrack());
255     }
256
257     return m_parameters;
258 }
259