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