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