]> git.sesse.net Git - kdenlive/blob - src/transition.cpp
some more animations (Qt 4.6)
[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 = m_parameters.elementsByTagName("name").item(0).toElement().text();
74     m_secondClip = 0;
75
76     //m_referenceClip->addTransition(this);
77 }
78
79 Transition::~Transition()
80 {
81     blockSignals(true);
82     delete m_startAnimation;
83     if (scene()) scene()->removeItem(this);
84 }
85
86 Transition *Transition::clone()
87 {
88     QDomElement xml = toXML().cloneNode().toElement();
89     Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
90     return tr;
91 }
92
93 QString Transition::transitionName() const
94 {
95     return m_name;
96 }
97
98 QString Transition::transitionTag() const
99 {
100     return m_parameters.attribute("tag");
101 }
102
103 bool Transition::isAutomatic() const
104 {
105     return m_automaticTransition;
106 }
107
108 void Transition::setAutomatic(bool automatic)
109 {
110     m_automaticTransition = automatic;
111     if (automatic) {
112         m_parameters.setAttribute("automatic", 1);
113         setBrush(QColor(200, 200, 50, 150));
114     } else {
115         m_parameters.removeAttribute("automatic");
116         setBrush(QColor(200, 50, 50, 150));
117     }
118     update();
119 }
120
121 void Transition::setTransitionParameters(const QDomElement params)
122 {
123     m_parameters = params;
124     if (m_parameters.attribute("force_track") == "1") setForcedTrack(true, m_parameters.attribute("transition_btrack").toInt());
125     else if (m_parameters.attribute("force_track") == "0") setForcedTrack(false, m_parameters.attribute("transition_btrack").toInt());
126     m_name = m_parameters.elementsByTagName("name").item(0).toElement().text();
127     update();
128 }
129
130
131 bool Transition::invertedTransition() const
132 {
133     return false; //m_parameters.attribute("reverse").toInt();
134 }
135
136 void Transition::setTransitionDirection(bool /*inv*/)
137 {
138     //m_parameters.setAttribute("reverse", inv);
139 }
140
141 int Transition::transitionEndTrack() const
142 {
143     return m_transitionTrack;
144 }
145
146 void Transition::updateTransitionEndTrack(int newtrack)
147 {
148     if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
149 }
150
151 void Transition::setForcedTrack(bool force, int track)
152 {
153     m_forceTransitionTrack = force;
154     m_transitionTrack = track;
155 }
156
157 bool Transition::forcedTrack() const
158 {
159     return m_forceTransitionTrack;
160 }
161
162 void Transition::paint(QPainter *painter,
163                        const QStyleOptionGraphicsItem *option,
164                        QWidget */*widget*/)
165 {
166     const QRectF exposed = option->exposedRect;
167     painter->setClipRect(exposed);
168     const QRectF br = rect();
169     const QRectF mapped = painter->matrix().mapRect(br);
170
171     painter->fillRect(exposed, brush());
172
173     //int top = (int)(br.y() + br.height() / 2 - 7);
174     QPointF p1(br.x(), br.y() + br.height() / 2 - 7);
175     painter->setMatrixEnabled(false);
176     //painter->drawPixmap(painter->matrix().map(p1) + QPointF(5, 0), transitionPixmap());
177     const QString text = m_name + (m_forceTransitionTrack ? "|>" : QString());
178
179     // Draw clip name
180     QColor frameColor(brush().color().darker());
181     if (isSelected() || (parentItem() && parentItem()->isSelected())) {
182         frameColor = QColor(Qt::red);
183     }
184     frameColor.setAlpha(160);
185
186     const QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + text + ' ');
187     //painter->fillRect(txtBounding2, frameColor);
188     painter->setBrush(frameColor);
189     painter->setPen(Qt::NoPen);
190     painter->drawRoundedRect(txtBounding, 3, 3);
191     painter->setBrush(QBrush(Qt::NoBrush));
192
193     painter->setPen(Qt::white);
194     painter->drawText(txtBounding, Qt::AlignCenter, text);
195
196     // Draw frame
197     QPen pen = painter->pen();
198     pen.setColor(frameColor);
199     painter->setPen(pen);
200     painter->setClipping(false);
201     painter->drawRect(painter->matrix().mapRect(rect()));
202 }
203
204 int Transition::type() const
205 {
206     return TRANSITIONWIDGET;
207 }
208
209 //virtual
210 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
211 {
212     if (change == ItemPositionChange && scene()) {
213         // calculate new position.
214         QPointF newPos = value.toPointF();
215         int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
216         xpos = qMax(xpos, 0);
217         newPos.setX(xpos);
218         int newTrack = newPos.y() / KdenliveSettings::trackheight();
219         newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
220         newTrack = qMax(newTrack, 0);
221         newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + KdenliveSettings::trackheight() / 3 * 2));
222         // Only one clip is moving
223         QRectF sceneShape = rect();
224         sceneShape.translate(newPos);
225         QList<QGraphicsItem*> items;
226         if (projectScene()->editMode() == NORMALEDIT)
227             items = scene()->items(sceneShape, Qt::IntersectsItemShape);
228         items.removeAll(this);
229
230         if (!items.isEmpty()) {
231             for (int i = 0; i < items.count(); i++) {
232                 if (items.at(i)->type() == type()) {
233                     // Collision! Don't move.
234                     //kDebug()<<"/// COLLISION WITH ITEM: "<<items.at(i)->boundingRect()<<", POS: "<<items.at(i)->pos()<<", ME: "<<newPos;
235                     QPointF otherPos = items.at(i)->pos();
236                     if ((int) otherPos.y() != (int) pos().y()) return pos();
237                     //kDebug()<<"////  CURRENT Y: "<<pos().y()<<", COLLIDING Y: "<<otherPos.y();
238                     if (pos().x() < otherPos.x()) {
239                         int npos = (static_cast < AbstractClipItem* >(items.at(i))->startPos() - m_info.cropDuration).frames(m_fps);
240                         newPos.setX(npos);
241                     } else {
242                         // get pos just after colliding clip
243                         int npos = static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps);
244                         newPos.setX(npos);
245                     }
246                     m_info.track = newTrack;
247                     //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
248                     m_info.startPos = GenTime((int) newPos.x(), m_fps);
249                     return newPos;
250                 }
251             }
252         }
253         m_info.track = newTrack;
254         m_info.startPos = GenTime((int) newPos.x(), m_fps);
255         //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
256         return newPos;
257     }
258     return QGraphicsItem::itemChange(change, value);
259 }
260
261
262 OPERATIONTYPE Transition::operationMode(QPointF pos)
263 {
264     if (isItemLocked()) return NONE;
265
266     const double scale = projectScene()->scale().x();
267     double maximumOffset = 6 / scale;
268
269     QRectF rect = sceneBoundingRect();
270     if (qAbs((int)(pos.x() - rect.x())) < maximumOffset) return RESIZESTART;
271     else if (qAbs((int)(pos.x() - (rect.right()))) < maximumOffset) return RESIZEEND;
272     return MOVE;
273 }
274
275 bool Transition::hasClip(const ClipItem * clip) const
276 {
277     if (clip == m_secondClip) return true;
278     return false;
279 }
280
281 bool Transition::belongsToClip(const ClipItem * clip) const
282 {
283     if (clip == m_referenceClip) return true;
284     return false;
285 }
286
287 /*
288 Transition *Transition::clone() {
289     return new Transition::Transition(rect(), m_referenceClip, toXML() , m_fps);
290 }*/
291
292 /*
293 Transition *Transition::reparent(ClipItem * clip) {
294     return new Transition::Transition(rect(), clip, toXML(), m_fps, m_referenceClip->startPos());
295 }*/
296
297 bool Transition::isValid() const
298 {
299     return true; //(m_transitionDuration != GenTime());
300 }
301
302 const ClipItem *Transition::referencedClip() const
303 {
304     return m_referenceClip;
305 }
306
307 QDomElement Transition::toXML()
308 {
309     m_parameters.setAttribute("type", transitionTag());
310     //m_transitionParameters.setAttribute("inverted", invertTransition());
311     m_parameters.setAttribute("transition_atrack", track());
312     m_parameters.setAttribute("transition_btrack", m_transitionTrack);
313     m_parameters.setAttribute("start", startPos().frames(m_fps));
314     m_parameters.setAttribute("end", endPos().frames(m_fps));
315     m_parameters.setAttribute("force_track", m_forceTransitionTrack);
316
317     if (m_secondClip) {
318         m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
319         m_parameters.setAttribute("clipb_track", transitionEndTrack());
320     }
321     return m_parameters.cloneNode().toElement();
322 }
323
324 bool Transition::hasGeometry()
325 {
326     QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
327     for (int i = 0; i < namenode.count() ; i++) {
328         QDomElement pa = namenode.item(i).toElement();
329         if (pa.attribute("type") == "geometry") return true;
330     }
331     return false;
332 }
333
334 int Transition::defaultZValue() const
335 {
336     return 3;
337 }
338