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