]> git.sesse.net Git - kdenlive/blob - src/transition.cpp
translate transition name in timeline
[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 "transition.h"
19 #include "clipitem.h"
20 #include "kdenlivesettings.h"
21 #include "customtrackscene.h"
22 #include "mainwindow.h"
23
24 #include <kdebug.h>
25 #include <KIcon>
26 #include <klocale.h>
27
28 #include <QBrush>
29 #include <QDomElement>
30 #include <QPainter>
31 #include <QStyleOptionGraphicsItem>
32
33
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),
38         m_secondClip(NULL),
39         m_transitionTrack(transitiontrack)
40 {
41     setZValue(3);
42     m_info.cropDuration = info.endPos - info.startPos;
43     setPos(info.startPos.frames(fps), (qreal)(info.track * KdenliveSettings::trackheight() + KdenliveSettings::trackheight() / 3 * 2));
44
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();
54 #else
55     setRect(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal)(KdenliveSettings::trackheight() / 3 * 2 - 1));
56 #endif
57
58     m_info.cropStart = GenTime();
59     m_maxDuration = GenTime(600);
60
61     if (m_automaticTransition) setBrush(QColor(200, 200, 50, 100));
62     else setBrush(QColor(200, 100, 50, 100));
63
64     //m_referenceClip = clipa;
65     if (params.isNull()) {
66         m_parameters = MainWindow::transitions.getEffectByTag("luma", "dissolve").cloneNode().toElement();
67     } else {
68         m_parameters = params;
69     }
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 = i18n(m_parameters.elementsByTagName("name").item(0).toElement().text().toUtf8().data());
74     m_secondClip = 0;
75
76     //m_referenceClip->addTransition(this);
77 }
78
79 Transition::~Transition()
80 {
81     blockSignals(true);
82 #if QT_VERSION >= 0x040600
83     delete m_startAnimation;
84 #endif
85     if (scene()) scene()->removeItem(this);
86 }
87
88 Transition *Transition::clone()
89 {
90     QDomElement xml = toXML().cloneNode().toElement();
91     Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
92     return tr;
93 }
94
95 QString Transition::transitionName() const
96 {
97     return m_name;
98 }
99
100 QString Transition::transitionTag() const
101 {
102     return m_parameters.attribute("tag");
103 }
104
105 bool Transition::isAutomatic() const
106 {
107     return m_automaticTransition;
108 }
109
110 void Transition::setAutomatic(bool automatic)
111 {
112     m_automaticTransition = automatic;
113     if (automatic) {
114         m_parameters.setAttribute("automatic", 1);
115         setBrush(QColor(200, 200, 50, 150));
116     } else {
117         m_parameters.removeAttribute("automatic");
118         setBrush(QColor(200, 50, 50, 150));
119     }
120     update();
121 }
122
123 void Transition::setTransitionParameters(const QDomElement params)
124 {
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 = i18n(m_parameters.elementsByTagName("name").item(0).toElement().text().toUtf8().data());
129     update();
130 }
131
132
133 bool Transition::invertedTransition() const
134 {
135     return false; //m_parameters.attribute("reverse").toInt();
136 }
137
138 void Transition::setTransitionDirection(bool /*inv*/)
139 {
140     //m_parameters.setAttribute("reverse", inv);
141 }
142
143 int Transition::transitionEndTrack() const
144 {
145     return m_transitionTrack;
146 }
147
148 void Transition::updateTransitionEndTrack(int newtrack)
149 {
150     if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
151 }
152
153 void Transition::setForcedTrack(bool force, int track)
154 {
155     m_forceTransitionTrack = force;
156     m_transitionTrack = track;
157 }
158
159 bool Transition::forcedTrack() const
160 {
161     return m_forceTransitionTrack;
162 }
163
164 void Transition::paint(QPainter *painter,
165                        const QStyleOptionGraphicsItem *option,
166                        QWidget */*widget*/)
167 {
168     const QRectF exposed = option->exposedRect;
169     painter->setClipRect(exposed);
170     const QRectF br = rect();
171     const QRectF mapped = painter->matrix().mapRect(br);
172
173     painter->fillRect(exposed, brush());
174
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());
180
181     // Draw clip name
182     QColor frameColor(brush().color().darker());
183     if (isSelected() || (parentItem() && parentItem()->isSelected())) {
184         frameColor = QColor(Qt::red);
185     }
186     frameColor.setAlpha(160);
187
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));
194
195     painter->setPen(Qt::white);
196     painter->drawText(txtBounding, Qt::AlignCenter, text);
197
198     // Draw frame
199     QPen pen = painter->pen();
200     pen.setColor(frameColor);
201     painter->setPen(pen);
202     painter->setClipping(false);
203     painter->drawRect(painter->matrix().mapRect(rect()));
204 }
205
206 int Transition::type() const
207 {
208     return TRANSITIONWIDGET;
209 }
210
211 //virtual
212 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
213 {
214     if (change == QGraphicsItem::ItemSelectedChange) {
215         if (value.toBool()) setZValue(10);
216         else setZValue(3);
217     }
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);
223         newPos.setX(xpos);
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);
235
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);
247                         newPos.setX(npos);
248                     } else {
249                         // get pos just after colliding clip
250                         int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
251                         newPos.setX(npos);
252                     }
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);
256                     return newPos;
257                 }
258             }
259         }
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();
263         return newPos;
264     }
265     return QGraphicsItem::itemChange(change, value);
266 }
267
268
269 OPERATIONTYPE Transition::operationMode(QPointF pos)
270 {
271     if (isItemLocked()) return NONE;
272
273     const double scale = projectScene()->scale().x();
274     double maximumOffset = 6 / scale;
275
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;
279     return MOVE;
280 }
281
282 bool Transition::hasClip(const ClipItem * clip) const
283 {
284     if (clip == m_secondClip) return true;
285     return false;
286 }
287
288 bool Transition::belongsToClip(const ClipItem * clip) const
289 {
290     if (clip == m_referenceClip) return true;
291     return false;
292 }
293
294 /*
295 Transition *Transition::clone() {
296     return new Transition::Transition(rect(), m_referenceClip, toXML() , m_fps);
297 }*/
298
299 /*
300 Transition *Transition::reparent(ClipItem * clip) {
301     return new Transition::Transition(rect(), clip, toXML(), m_fps, m_referenceClip->startPos());
302 }*/
303
304 bool Transition::isValid() const
305 {
306     return true; //(m_transitionDuration != GenTime());
307 }
308
309 const ClipItem *Transition::referencedClip() const
310 {
311     return m_referenceClip;
312 }
313
314 QDomElement Transition::toXML()
315 {
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);
323
324     if (m_secondClip) {
325         m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
326         m_parameters.setAttribute("clipb_track", transitionEndTrack());
327     }
328     return m_parameters.cloneNode().toElement();
329 }
330
331 bool Transition::hasGeometry()
332 {
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;
337     }
338     return false;
339 }
340
341 int Transition::defaultZValue() const
342 {
343     return 3;
344 }
345