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