]> git.sesse.net Git - kdenlive/blob - src/transition.cpp
Fix clip effect stack not updated when dropping effect in a group, nicer 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 #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 int Transition::transitionEndTrack() const
140 {
141     return m_transitionTrack;
142 }
143
144 void Transition::updateTransitionEndTrack(int newtrack)
145 {
146     if (!m_forceTransitionTrack) m_transitionTrack = newtrack;
147 }
148
149 void Transition::setForcedTrack(bool force, int track)
150 {
151     m_forceTransitionTrack = force;
152     m_transitionTrack = track;
153 }
154
155 bool Transition::forcedTrack() const
156 {
157     return m_forceTransitionTrack;
158 }
159
160 void Transition::paint(QPainter *painter,
161                        const QStyleOptionGraphicsItem *option,
162                        QWidget */*widget*/)
163 {
164     const QRectF exposed = painter->worldTransform().mapRect(option->exposedRect);
165     const QRectF br = rect();
166     QPen framePen;
167     framePen.setWidthF(1.2);
168     const QRectF mapped = painter->worldTransform().mapRect(br);
169
170     QPointF p1(br.x(), br.y() + br.height() / 2 - 7);
171     painter->setWorldMatrixEnabled(false);
172     QPainterPath p;
173     p.addRect(exposed);
174     QPainterPath q;
175     q.addRoundedRect(mapped.adjusted(0, 0, -1, -1), 3, 3);
176     painter->setClipPath(p.intersected(q));
177     painter->fillRect(exposed, brush());
178     const QString text = m_name + (m_forceTransitionTrack ? "|>" : QString());
179
180     // Draw clip name
181     if (isSelected() || (parentItem() && parentItem()->isSelected())) {
182         framePen.setColor(scene()->palette().highlight().color());
183         framePen.setColor(Qt::red);
184     }
185     else {
186         framePen.setColor(brush().color().darker());
187     }
188
189     const QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + text + ' ');
190     painter->setBrush(framePen.color());
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     painter->setPen(framePen);
200     painter->setClipping(false);
201     painter->setRenderHint(QPainter::Antialiasing, true);
202     painter->drawRoundedRect(mapped.adjusted(0, 0, -0.5, -0.5), 3, 3);
203 }
204
205 int Transition::type() const
206 {
207     return TRANSITIONWIDGET;
208 }
209
210 //virtual
211 QVariant Transition::itemChange(GraphicsItemChange change, const QVariant &value)
212 {
213     if (change == QGraphicsItem::ItemSelectedChange) {
214         if (value.toBool()) setZValue(10);
215         else setZValue(3);
216     }
217     if (change == ItemPositionChange && scene()) {
218         // calculate new position.
219         QPointF newPos = value.toPointF();
220         int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
221         xpos = qMax(xpos, 0);
222         newPos.setX(xpos);
223         int newTrack = newPos.y() / KdenliveSettings::trackheight();
224         newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
225         newTrack = qMax(newTrack, 0);
226         newPos.setY((int)(newTrack * KdenliveSettings::trackheight() + itemOffset() + 1));
227         // Only one clip is moving
228         QRectF sceneShape = rect();
229         sceneShape.translate(newPos);
230         QList<QGraphicsItem*> items;
231         // TODO: manage transitions in OVERWRITE MODE
232         //if (projectScene()->editMode() == NORMALEDIT)
233         items = scene()->items(sceneShape, Qt::IntersectsItemShape);
234         items.removeAll(this);
235
236         bool forwardMove = newPos.x() > pos().x();
237         int offset = 0;
238         if (!items.isEmpty()) {
239             for (int i = 0; i < items.count(); i++) {
240                 if (!items.at(i)->isEnabled()) continue;
241                 if (items.at(i)->type() == type()) {
242                     // Collision!
243                     QPointF otherPos = items.at(i)->pos();
244                     if ((int) otherPos.y() != (int) pos().y()) {
245                         return pos();
246                     }
247                     if (forwardMove) {
248                         offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
249                     } else {
250                         offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
251                     }
252
253                     if (offset > 0) {
254                         if (forwardMove) {
255                             sceneShape.translate(QPointF(-offset, 0));
256                             newPos.setX(newPos.x() - offset);
257                         } else {
258                             sceneShape.translate(QPointF(offset, 0));
259                             newPos.setX(newPos.x() + offset);
260                         }
261                         QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
262                         subitems.removeAll(this);
263                         for (int j = 0; j < subitems.count(); j++) {
264                             if (!subitems.at(j)->isEnabled()) continue;
265                             if (subitems.at(j)->type() == type()) {
266                                 // move was not successful, revert to previous pos
267                                 m_info.startPos = GenTime((int) pos().x(), m_fps);
268                                 return pos();
269                             }
270                         }
271                     }
272
273                     m_info.track = newTrack;
274                     m_info.startPos = GenTime((int) newPos.x(), m_fps);
275
276                     return newPos;
277                 }
278             }
279         }
280        
281         m_info.track = newTrack;
282         m_info.startPos = GenTime((int) newPos.x(), m_fps);
283         //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
284         return newPos;
285     }
286     return QGraphicsItem::itemChange(change, value);
287 }
288
289
290 OPERATIONTYPE Transition::operationMode(QPointF pos)
291 {
292     if (isItemLocked()) return NONE;
293
294     const double scale = projectScene()->scale().x();
295     double maximumOffset = 6 / scale;
296
297     QRectF rect = sceneBoundingRect();
298     if (qAbs((int)(pos.x() - rect.x())) < maximumOffset) return RESIZESTART;
299     else if (qAbs((int)(pos.x() - (rect.right()))) < maximumOffset) return RESIZEEND;
300     return MOVE;
301 }
302
303 //static
304 int Transition::itemHeight()
305 {
306     return (int) (KdenliveSettings::trackheight() / 3 * 2 - 1);
307 }
308
309 //static
310 int Transition::itemOffset()
311 {
312     return (int) (KdenliveSettings::trackheight() / 3 * 2);
313 }
314
315 bool Transition::hasClip(const ClipItem * clip) const
316 {
317     if (clip == m_secondClip) return true;
318     return false;
319 }
320
321 bool Transition::belongsToClip(const ClipItem * clip) const
322 {
323     if (clip == m_referenceClip) return true;
324     return false;
325 }
326
327 /*
328 Transition *Transition::clone() {
329     return new Transition::Transition(rect(), m_referenceClip, toXML() , m_fps);
330 }*/
331
332 const ClipItem *Transition::referencedClip() const
333 {
334     return m_referenceClip;
335 }
336
337 QDomElement Transition::toXML()
338 {
339     m_parameters.setAttribute("type", transitionTag());
340     //m_transitionParameters.setAttribute("inverted", invertTransition());
341     m_parameters.setAttribute("transition_atrack", track());
342     m_parameters.setAttribute("transition_btrack", m_transitionTrack);
343     m_parameters.setAttribute("start", startPos().frames(m_fps));
344     m_parameters.setAttribute("end", endPos().frames(m_fps));
345     m_parameters.setAttribute("force_track", m_forceTransitionTrack);
346     m_parameters.setAttribute("automatic", m_automaticTransition);
347
348     if (m_secondClip) {
349         m_parameters.setAttribute("clipb_starttime", m_secondClip->startPos().frames(m_referenceClip->fps()));
350         m_parameters.setAttribute("clipb_track", transitionEndTrack());
351     }
352     return m_parameters.cloneNode().toElement();
353 }
354
355 bool Transition::hasGeometry()
356 {
357     QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
358     for (int i = 0; i < namenode.count() ; i++) {
359         QDomElement pa = namenode.item(i).toElement();
360         if (pa.attribute("type") == "geometry") return true;
361     }
362     return false;
363 }
364
365 int Transition::defaultZValue() const
366 {
367     return 3;
368 }
369
370 bool Transition::updateKeyframes()
371 {
372     QString keyframes;
373     QDomElement pa;
374     bool modified = false;
375     QDomNodeList namenode = m_parameters.elementsByTagName("parameter");
376     for (int i = 0; i < namenode.count() ; i++) {
377         pa = namenode.item(i).toElement();
378         if (pa.attribute("type") == "geometry") {
379             keyframes = pa.attribute("value");
380             break;
381         }
382     }
383     if (keyframes.isEmpty()) return false;
384     int duration = cropDuration().frames(m_fps) - 1;
385     QStringList values = keyframes.split(';');
386     int frame;
387     int i = 0;
388     foreach(const QString &pos, values) {
389         if (!pos.contains('=')) {
390             i++;
391             continue;
392         }
393         frame = pos.section('=', 0, 0).toInt();
394         if (frame > duration) {
395             modified = true;
396             break;
397         }
398         i++;
399     }
400     if (modified) {
401         if (i > 0) {
402             // Check if there is a keyframe at transition end
403             QString prev = values.at(i-1);
404             bool done = false;
405             if (prev.contains('=')) {
406                 int previousKeyframe = prev.section('=', 0, 0).toInt();
407                 if (previousKeyframe == duration) {
408                     // Remove the last keyframes
409                     while (values.count() > i) {
410                         values.removeLast();
411                     }
412                     done = true;
413                 }
414             }
415             if (!done) {
416                 // Add new keyframe at end and remove last keyframes
417                 QString last = values.at(i);
418                 last = QString::number(duration) + '=' + last.section('=', 1);
419                 values[i] = last;
420                 while (values.count() > (i + 1)) {
421                     values.removeLast();
422                 }
423             }
424         }
425         pa.setAttribute("value", values.join(";"));
426     }
427     
428     return true;
429 }
430