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