]> git.sesse.net Git - kdenlive/blob - src/rotoscoping/rotowidget.cpp
start documenting the rotoscoping code
[kdenlive] / src / rotoscoping / rotowidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Till Theato (root@ttill.de)                     *
3  *   This file is part of Kdenlive (www.kdenlive.org).                     *
4  *                                                                         *
5  *   Kdenlive is free software: you can redistribute it and/or modify      *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation, either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   Kdenlive is distributed in the hope that it will be useful,           *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with Kdenlive.  If not, see <http://www.gnu.org/licenses/>.     *
17  ***************************************************************************/
18
19 #include "rotowidget.h"
20 #include "monitor.h"
21 #include "renderer.h"
22 #include "monitorscene.h"
23 #include "monitoreditwidget.h"
24 #include "onmonitoritems/rotoscoping/bpointitem.h"
25 #include "onmonitoritems/rotoscoping/splineitem.h"
26 #include "simplekeyframes/simplekeyframewidget.h"
27 #include "kdenlivesettings.h"
28
29 #include <math.h>
30
31 #include <qjson/parser.h>
32 #include <qjson/serializer.h>
33
34 #include <QVBoxLayout>
35
36
37 RotoWidget::RotoWidget(QString data, Monitor *monitor, int in, int out, Timecode t, QWidget* parent) :
38         QWidget(parent),
39         m_monitor(monitor),
40         m_showScene(true),
41         m_in(in),
42         m_out(out)
43 {
44     QVBoxLayout *l = new QVBoxLayout(this);
45     m_keyframeWidget = new SimpleKeyframeWidget(t, m_out - m_in - 1, this);
46     l->addWidget(m_keyframeWidget);
47
48     MonitorEditWidget *edit = monitor->getEffectEdit();
49     edit->showVisibilityButton(true);
50     m_scene = edit->getScene();
51
52     QJson::Parser parser;
53     bool ok;
54     m_data = parser.parse(data.toUtf8(), &ok);
55     if (!ok) {
56         // :(
57     }
58
59     
60     if (m_data.canConvert(QVariant::Map)) {
61         /*
62          * pass keyframe data to keyframe timeline
63          */
64         QList <int> keyframes;
65         QMap <QString, QVariant> map = m_data.toMap();
66         QMap <QString, QVariant>::const_iterator i = map.constBegin();
67         while (i != map.constEnd()) {
68             keyframes.append(i.key().toInt() - m_in);
69             ++i;
70         }
71         m_keyframeWidget->setKeyframes(keyframes);
72
73         for (int j = 0; j < keyframes.count(); ++j) {
74             // key might already be justified
75             if (map.contains(QString::number(keyframes.at(j) + m_in))) {
76                 QVariant value = map.take(QString::number(keyframes.at(j) + m_in));
77                 map[QString::number(keyframes.at(j) + m_in).rightJustified(qRound(log10((double)m_out)), '0')] = value;
78             }
79         }
80         m_data = QVariant(map);
81     } else {
82         // static (only one keyframe)
83         m_keyframeWidget->setKeyframes(QList <int>() << 0);
84     }
85
86     m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
87
88     connect(m_item, SIGNAL(changed(bool)), this, SLOT(slotUpdateData(bool)));
89     connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
90     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
91     connect(m_keyframeWidget, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
92     connect(m_keyframeWidget, SIGNAL(keyframeAdded(int)), this, SLOT(slotAddKeyframe(int)));
93     connect(m_keyframeWidget, SIGNAL(keyframeRemoved(int)), this, SLOT(slotRemoveKeyframe(int)));
94     connect(m_keyframeWidget, SIGNAL(keyframeMoved(int,int)), this, SLOT(slotMoveKeyframe(int,int)));
95     connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
96
97     slotPositionChanged(0, false);
98 }
99
100 RotoWidget::~RotoWidget()
101 {
102     delete m_keyframeWidget;
103
104     m_scene->removeItem(m_item);
105     delete m_item;
106
107     if (m_monitor) {
108         MonitorEditWidget *edit = m_monitor->getEffectEdit();
109         edit->showVisibilityButton(false);
110         edit->removeCustomControls();
111         m_monitor->slotEffectScene(false);
112     }
113 }
114
115 void RotoWidget::slotCheckMonitorPosition(int renderPos)
116 {
117     if (m_showScene)
118         emit checkMonitorPosition(renderPos);
119 }
120
121 void RotoWidget::slotSyncPosition(int relTimelinePos)
122 {
123     relTimelinePos = qBound(0, relTimelinePos, m_out);
124     m_keyframeWidget->slotSetPosition(relTimelinePos, false);
125     slotPositionChanged(relTimelinePos, false);
126 }
127
128 void RotoWidget::slotShowScene(bool show)
129 {
130     m_showScene = show;
131     if (!m_showScene)
132         m_monitor->slotEffectScene(false);
133     else
134         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
135 }
136
137 void RotoWidget::slotUpdateData(int pos, bool editing)
138 {
139     Q_UNUSED(editing)
140
141     int width = m_monitor->render->frameRenderWidth();
142     int height = m_monitor->render->renderHeight();
143
144     /*
145      * use the position of the on-monitor points to create a storable list
146      */
147     QList <BPoint> spline = m_item->getPoints();
148     QList <QVariant> vlist;
149     foreach (const BPoint &point, spline) {
150         QList <QVariant> pl;
151         for (int i = 0; i < 3; ++i)
152             pl << QVariant(QList <QVariant>() << QVariant(point[i].x() / width) << QVariant(point[i].y() / height));
153         vlist << QVariant(pl);
154     }
155
156     if (m_data.canConvert(QVariant::Map)) {
157         QMap <QString, QVariant> map = m_data.toMap();
158         // replace or insert at position
159         // we have to fill with 0s to maintain the correct order
160         map[QString::number((pos < 0 ? m_keyframeWidget->getPosition() : pos) + m_in).rightJustified(qRound(log10((double)m_out)), '0')] = QVariant(vlist);
161         m_data = QVariant(map);
162     } else {
163         m_data = QVariant(vlist);
164     }
165
166     emit valueChanged();
167 }
168
169 void RotoWidget::slotUpdateData(bool editing)
170 {
171     slotUpdateData(-1, editing);
172 }
173
174 QString RotoWidget::getSpline()
175 {
176     QJson::Serializer serializer;
177     return QString(serializer.serialize(m_data));
178 }
179
180 void RotoWidget::slotPositionChanged(int pos, bool seek)
181 {
182     // do not update while the spline is being edited (points are being dragged)
183     if (m_item->editing())
184         return;
185
186     m_keyframeWidget->slotSetPosition(pos, false);
187
188     pos += m_in;
189
190     QList <BPoint> p;
191
192     if (m_data.canConvert(QVariant::Map)) {
193         QMap <QString, QVariant> map = m_data.toMap();
194         QMap <QString, QVariant>::const_iterator i = map.constBegin();
195         int keyframe1, keyframe2;
196         keyframe1 = keyframe2 = i.key().toInt();
197         // find keyframes next to pos
198         while (i.key().toInt() < pos && ++i != map.constEnd()) {
199             keyframe1 = keyframe2;
200             keyframe2 = i.key().toInt();
201         }
202
203         if (keyframe1 != keyframe2 && pos < keyframe2) {
204             /*
205              * in between two keyframes
206              * -> interpolate
207              */
208             QList <BPoint> p1 = getPoints(keyframe1);
209             QList <BPoint> p2 = getPoints(keyframe2);
210             qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
211
212             // additionaly points are ignored (same behavior as MLT filter)
213             int count = qMin(p1.count(), p2.count());
214             for (int i = 0; i < count; ++i) {
215                 BPoint bp;
216                 for (int j = 0; j < 3; ++j) {
217                     if (p1.at(i)[j] != p2.at(i)[j])
218                         bp[j] = QLineF(p1.at(i)[j], p2.at(i)[j]).pointAt(relPos);
219                     else
220                         bp[j] = p1.at(i)[j];
221                 }
222                 p.append(bp);
223             }
224
225             m_item->setPoints(p);
226             m_item->setEnabled(false);
227             m_scene->setEnabled(false);
228         } else {
229             p = getPoints(keyframe2);
230             // only update if necessary to preserve the current point selection
231             if (p != m_item->getPoints())
232                 m_item->setPoints(p);
233             m_item->setEnabled(pos == keyframe2);
234             m_scene->setEnabled(pos == keyframe2);
235         }
236     } else {
237         p = getPoints(-1);
238         // only update if necessary to preserve the current point selection
239         if (p != m_item->getPoints())
240             m_item->setPoints(p);
241         m_item->setEnabled(true);
242         m_scene->setEnabled(true);
243     }
244
245     if (seek)
246         emit seekToPos(pos - m_in);
247 }
248
249 QList <BPoint> RotoWidget::getPoints(int keyframe)
250 {
251     int width = m_monitor->render->frameRenderWidth();
252     int height = m_monitor->render->renderHeight();
253     QList <BPoint> points;
254     QList <QVariant> data;
255     if (keyframe >= 0)
256         data = m_data.toMap()[QString::number(keyframe).rightJustified(qRound(log10((double)m_out)), '0')].toList();
257     else
258         data = m_data.toList();
259     foreach (const QVariant &bpoint, data) {
260         QList <QVariant> l = bpoint.toList();
261         BPoint p;
262         p.h1 = QPointF(l.at(0).toList().at(0).toDouble() * width, l.at(0).toList().at(1).toDouble() * height);
263         p.p = QPointF(l.at(1).toList().at(0).toDouble() * width, l.at(1).toList().at(1).toDouble() * height);
264         p.h2 = QPointF(l.at(2).toList().at(0).toDouble() * width, l.at(2).toList().at(1).toDouble() * height);
265         points << p;
266     }
267     return points;
268 }
269
270 void RotoWidget::slotAddKeyframe(int pos)
271 {
272     if (!m_data.canConvert(QVariant::Map)) {
273         QVariant data = m_data;
274         QMap<QString, QVariant> map;
275         map[QString::number(m_in).rightJustified(qRound(log10((double)m_out)), '0')] = data;
276         m_data = QVariant(map);
277     }
278
279     if (pos < 0)
280         m_keyframeWidget->addKeyframe();
281
282     slotUpdateData(pos);
283     m_item->setEnabled(true);
284     m_scene->setEnabled(true);
285 }
286
287 void RotoWidget::slotRemoveKeyframe(int pos)
288 {
289     if (pos < 0)
290         pos = m_keyframeWidget->getPosition();
291
292     if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
293         return;
294
295     QMap<QString, QVariant> map = m_data.toMap();
296     map.remove(QString::number(pos + m_in).rightJustified(qRound(log10((double)m_out)), '0'));
297     m_data = QVariant(map);
298
299     if (m_data.toMap().count() == 1) {
300         // only one keyframe -> switch from map to list again
301         m_data = m_data.toMap().begin().value();
302     }
303
304     slotPositionChanged(m_keyframeWidget->getPosition(), false);
305     emit valueChanged();
306 }
307
308 void RotoWidget::slotMoveKeyframe(int oldPos, int newPos)
309 {
310     if (m_data.canConvert(QVariant::Map)) {
311         QMap<QString, QVariant> map = m_data.toMap();
312         map[QString::number(newPos + m_in).rightJustified(qRound(log10((double)m_out)), '0')] = map.take(QString::number(oldPos + m_in).rightJustified(qRound(log10((double)m_out)), '0'));
313         m_data = QVariant(map);
314     }
315
316     slotPositionChanged(m_keyframeWidget->getPosition(), false);
317     emit valueChanged();
318 }
319
320 void RotoWidget::updateTimecodeFormat()
321 {
322     m_keyframeWidget->updateTimecodeFormat();
323 }
324
325 #include "rotowidget.moc"