]> git.sesse.net Git - kdenlive/blob - src/transition.cpp
Merge branch 'master' into next
[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 #if QT_VERSION >= 0x040600
33 #include <QPropertyAnimation>
34 #endif
35
36 Transition::Transition(const ItemInfo &info, int transitiontrack, double fps, QDomElement params, bool automaticTransition) :
37         AbstractClipItem(info, QRectF(), fps),
38         m_forceTransitionTrack(false),
39         m_automaticTransition(automaticTransition),
40         m_secondClip(NULL),
41         m_transitionTrack(transitiontrack)
42 {
43     setZValue(3);
44     m_info.cropDuration = info.endPos - info.startPos;
45     setPos(info.startPos.frames(fps), (int)(info.track * KdenliveSettings::trackheight() + itemOffset() + 1));
46
47 #if QT_VERSION >= 0x040600
48     if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
49         // animation disabled
50         setRect(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight());
51     }
52     else {
53         QPropertyAnimation *startAnimation = new QPropertyAnimation(this, "rect");
54         startAnimation->setDuration(200);
55         const QRectF r(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight() / 2);
56         const QRectF r2(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal)itemHeight());
57         startAnimation->setStartValue(r);
58         startAnimation->setEndValue(r2);
59         startAnimation->setEasingCurve(QEasingCurve::OutQuad);
60         startAnimation->start(QAbstractAnimation::DeleteWhenStopped);
61     }
62 #else
63     setRect(0, 0, m_info.cropDuration.frames(fps) - 0.02, (qreal) itemHeight());
64 #endif
65
66     m_info.cropStart = GenTime();
67     m_maxDuration = GenTime(600);
68
69     if (m_automaticTransition) setBrush(QColor(200, 200, 50, 180));
70     else setBrush(QColor(200, 100, 50, 180));
71
72     //m_referenceClip = clipa;
73     if (params.isNull()) {
74         m_parameters = MainWindow::transitions.getEffectByTag("luma", "dissolve").cloneNode().toElement();
75     } else {
76         m_parameters = params;
77     }
78     if (m_automaticTransition) m_parameters.setAttribute("automatic", 1);
79     else if (m_parameters.attribute("automatic") == "1") m_automaticTransition = true;
80     if (m_parameters.attribute("force_track") == "1") m_forceTransitionTrack = true;
81     m_name = i18n(m_parameters.firstChildElement("name").text().toUtf8().data());
82     m_secondClip = 0;
83
84     //m_referenceClip->addTransition(this);
85 }
86
87 Transition::~Transition()
88 {
89     blockSignals(true);
90     if (scene()) scene()->removeItem(this);
91 }
92
93 Transition *Transition::clone()
94 {
95     QDomElement xml = toXML().cloneNode().toElement();
96     Transition *tr = new Transition(info(), transitionEndTrack(), m_fps, xml);
97     return tr;
98 }
99
100 QString Transition::transitionTag() const
101 {
102     return m_parameters.attribute("tag");
103 }
104
105 QStringList Transition::transitionInfo() const
106 {
107     QStringList info;
108     info << m_name << m_parameters.attribute("tag") << m_parameters.attribute("id");
109     return info;
110 }
111
112 bool Transition::isAutomatic() const
113 {
114     return m_automaticTransition;
115 }
116
117 void Transition::setAutomatic(bool automatic)
118 {
119     m_automaticTransition = automatic;
120     if (automatic) {
121         m_parameters.setAttribute("automatic", 1);
122         setBrush(QColor(200, 200, 50, 180));
123     } else {
124         m_parameters.removeAttribute("automatic");
125         setBrush(QColor(200, 100, 50, 180));
126     }
127     update();
128 }
129
130 void Transition::setTransitionParameters(const QDomElement params)
131 {
132     m_parameters = params;
133     if (m_parameters.attribute("force_track") == "1") setForcedTrack(true, m_parameters.attribute("transition_btrack").toInt());
134     else if (m_parameters.attribute("force_track") == "0") setForcedTrack(false, m_parameters.attribute("transition_btrack").toInt());
135     m_name = i18n(m_parameters.firstChildElement("name").text().toUtf8().data());
136     update();
137 }
138
139
140 bool Transition::invertedTransition() const
141 {
142     return false; //m_parameters.attribute("reverse").toInt();
143 }
144
145 void Transition::setTransitionDirection(bool /*inv*/)
146 {
147     //m_parameters.setAttribute("reverse", inv);
148 }
149
150 int Transition::transitionEndTrack() const
151 {
152     return m_transitionTrack;
153 }
154
155 void Transition::updateTransitionEndTrack(int newtrack)
156 {
157     if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
158 }
159
160 void Transition::setForcedTrack(bool force, int track)
161 {
162     m_forceTransitionTrack = force;
163     m_transitionTrack = track;
164 }
165
166 bool Transition::forcedTrack() const
167 {
168     return m_forceTransitionTrack;
169 }
170
171 void Transition::paint(QPainter *painter,
172                        const QStyleOptionGraphicsItem *option,
173                        QWidget */*widget*/)
174 {
175     const QRectF exposed = option->exposedRect;
176     painter->setClipRect(exposed);
177     const QRectF br = rect();
178     QPen framePen;
179     const QRectF mapped = painter->worldTransform().mapRect(br);
180
181     painter->fillRect(exposed, brush());
182
183     QPointF p1(br.x(), br.y() + br.height() / 2 - 7);
184     painter->setWorldMatrixEnabled(false);
185     const QString text = m_name + (m_forceTransitionTrack ? "|>" : QString());
186
187     // Draw clip name
188     if (isSelected() || (parentItem() && parentItem()->isSelected())) {
189         framePen.setColor(Qt::red);
190         framePen.setWidthF(2.0);
191     }
192     else {
193         framePen.setColor(brush().color().darker());
194     }
195
196     const QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + text + ' ');
197     painter->setBrush(framePen.color());
198     painter->setPen(Qt::NoPen);
199     painter->drawRoundedRect(txtBounding, 3, 3);
200     painter->setBrush(QBrush(Qt::NoBrush));
201
202     painter->setPen(Qt::white);
203     painter->drawText(txtBounding, Qt::AlignCenter, text);
204
205     // Draw frame
206     painter->setPen(framePen);
207     painter->setClipping(false);
208     painter->drawRect(mapped.adjusted(0, 0, -0.5, -0.5));
209 }
210
211 int Transition::type() const
212 {
213     return TRANSITIONWIDGET;
214 }
215
216 //virtual
217 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
218 {
219     if (change == QGraphicsItem::ItemSelectedChange) {
220         if (value.toBool()) setZValue(10);
221         else setZValue(3);
222     }
223     if (change == ItemPositionChange && scene()) {
224         // calculate new position.
225         QPointF newPos = value.toPointF();
226         int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
227         xpos = qMax(xpos, 0);
228         newPos.setX(xpos);
229         int newTrack = newPos.y() / KdenliveSettings::trackheight();
230         newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
231         newTrack = qMax(newTrack, 0);
232         newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + itemOffset() + 1));
233         // Only one clip is moving
234         QRectF sceneShape = rect();
235         sceneShape.translate(newPos);
236         QList<QGraphicsItem*> items;
237         // TODO: manage transitions in OVERWRITE MODE
238         //if (projectScene()->editMode() == NORMALEDIT)
239         items = scene()->items(sceneShape, Qt::IntersectsItemShape);
240         items.removeAll(this);
241
242         bool forwardMove = newPos.x() > pos().x();
243         int offset = 0;
244         if (!items.isEmpty()) {
245             for (int i = 0; i < items.count(); i++) {
246                 if (!items.at(i)->isEnabled()) continue;
247                 if (items.at(i)->type() == type()) {
248                     // Collision!
249                     QPointF otherPos = items.at(i)->pos();
250                     if ((int) otherPos.y() != (int) pos().y()) {
251                         return pos();
252                     }
253                     if (forwardMove) {
254                         offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
255                     } else {
256                         offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
257                     }
258
259                     if (offset > 0) {
260                         if (forwardMove) {
261                             sceneShape.translate(QPointF(-offset, 0));
262                             newPos.setX(newPos.x() - offset);
263                         } else {
264                             sceneShape.translate(QPointF(offset, 0));
265                             newPos.setX(newPos.x() + offset);
266                         }
267                         QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
268                         subitems.removeAll(this);
269                         for (int j = 0; j < subitems.count(); j++) {
270                             if (!subitems.at(j)->isEnabled()) continue;
271                             if (subitems.at(j)->type() == type()) {
272                                 // move was not successful, revert to previous pos
273                                 m_info.startPos = GenTime((int) pos().x(), m_fps);
274                                 return pos();
275                             }
276                         }
277                     }
278
279                     m_info.track = newTrack;
280                     m_info.startPos = GenTime((int) newPos.x(), m_fps);
281
282                     return newPos;
283                 }
284             }
285         }
286        
287         m_info.track = newTrack;
288         m_info.startPos = GenTime((int) newPos.x(), m_fps);
289         //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
290         return newPos;
291     }
292     return QGraphicsItem::itemChange(change, value);
293 }
294
295
296 OPERATIONTYPE Transition::operationMode(QPointF pos)
297 {
298     if (isItemLocked()) return NONE;
299
300     const double scale = projectScene()->scale().x();
301     double maximumOffset = 6 / scale;
302
303     QRectF rect = sceneBoundingRect();
304     if (qAbs((int)(pos.x() - rect.x())) < maximumOffset) return RESIZESTART;
305     else if (qAbs((int)(pos.x() - (rect.right()))) < maximumOffset) return RESIZEEND;
306     return MOVE;
307 }
308
309 int Transition::itemHeight()
310 {
311     return (int) (KdenliveSettings::trackheight() / 3 * 2 - 1);
312 }
313
314 int Transition::itemOffset()
315 {
316     return (int) (KdenliveSettings::trackheight() / 3 * 2);
317 }
318
319 bool Transition::hasClip(const ClipItem * clip) const
320 {
321     if (clip == m_secondClip) return true;
322     return false;
323 }
324
325 bool Transition::belongsToClip(const ClipItem * clip) const
326 {
327     if (clip == m_referenceClip) return true;
328     return false;
329 }
330
331 /*
332 Transition *Transition::clone() {
333     return new Transition::Transition(rect(), m_referenceClip, toXML() , m_fps);
334 }*/
335
336 /*
337 Transition *Transition::reparent(ClipItem * clip) {
338     return new Transition::Transition(rect(), clip, toXML(), m_fps, m_referenceClip->startPos());
339 }*/
340
341 bool Transition::isValid() const
342 {
343     return true; //(m_transitionDuration != GenTime());
344 }
345
346 const ClipItem *Transition::referencedClip() const
347 {
348     return m_referenceClip;
349 }
350
351 QDomElement Transition::toXML()
352 {
353     m_parameters.setAttribute("type", transitionTag());
354     //m_transitionParameters.setAttribute("inverted", invertTransition());
355     m_parameters.setAttribute("transition_atrack", track());
356     m_parameters.setAttribute("transition_btrack", m_transitionTrack);
357     m_parameters.setAttribute("start", startPos().frames(m_fps));
358     m_parameters.setAttribute("end", endPos().frames(m_fps));
359     m_parameters.setAttribute("force_track", m_forceTransitionTrack);
360     m_parameters.setAttribute("automatic", m_automaticTransition);
361
362     if (m_secondClip) {
363         m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
364         m_parameters.setAttribute("clipb_track", transitionEndTrack());
365     }
366     return m_parameters.cloneNode().toElement();
367 }
368
369 bool Transition::hasGeometry()
370 {
371     QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
372     for (int i = 0; i < namenode.count() ; i++) {
373         QDomElement pa = namenode.item(i).toElement();
374         if (pa.attribute("type") == "geometry") return true;
375     }
376     return false;
377 }
378
379 int Transition::defaultZValue() const
380 {
381     return 3;
382 }
383
384 bool Transition::updateKeyframes()
385 {
386     QString keyframes;
387     QDomElement pa;
388     bool modified = false;
389     QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
390     for (int i = 0; i < namenode.count() ; i++) {
391         pa = namenode.item(i).toElement();
392         if (pa.attribute("type") == "geometry") {
393             keyframes = pa.attribute("value");
394             break;
395         }
396     }
397     if (keyframes.isEmpty()) return false;
398     int duration = cropDuration().frames(m_fps) - 1;
399     QStringList values = keyframes.split(";");
400     int frame;
401     int i = 0;
402     foreach(const QString &pos, values) {
403         if (!pos.contains('=')) {
404             i++;
405             continue;
406         }
407         frame = pos.section('=', 0, 0).toInt();
408         if (frame > duration) {
409             modified = true;
410             break;
411         }
412         i++;
413     }
414     if (modified) {
415         if (i > 0) {
416             // Check if there is a keyframe at transition end
417             QString prev = values.at(i-1);
418             bool done = false;
419             if (prev.contains('=')) {
420                 int previousKeyframe = prev.section('=', 0, 0).toInt();
421                 if (previousKeyframe == duration) {
422                     // Remove the last keyframes
423                     while (values.count() > i) {
424                         values.removeLast();
425                     }
426                     done = true;
427                 }
428             }
429             if (!done) {
430                 // Add new keyframe at end and remove last keyframes
431                 QString last = values.at(i);
432                 last = QString::number(duration) + '=' + last.section('=', 1);
433                 values[i] = last;
434                 while (values.count() > (i + 1)) {
435                     values.removeLast();
436                 }
437             }
438         }
439         pa.setAttribute("value", values.join(";"));
440     }
441     
442     return true;
443 }
444