]> git.sesse.net Git - kdenlive/blob - src/rotoscoping/rotowidget.cpp
rotoscoping: fix keyframe insertion between two existing keyframes
[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         m_pos(0)
44 {
45     QVBoxLayout *l = new QVBoxLayout(this);
46     m_keyframeWidget = new SimpleKeyframeWidget(t, in, out, this);
47     l->addWidget(m_keyframeWidget);
48
49     MonitorEditWidget *edit = monitor->getEffectEdit();
50     edit->showVisibilityButton(true);
51     m_scene = edit->getScene();
52
53     QJson::Parser parser;
54     bool ok;
55     m_data = parser.parse(data.toUtf8(), &ok);
56     if (!ok) {
57         // :(
58     }
59
60     
61     if (m_data.canConvert(QVariant::Map)) {
62         QList <int> keyframes;
63         QMap <QString, QVariant> map = m_data.toMap();
64         QMap <QString, QVariant>::const_iterator i = map.constBegin();
65         while (i != map.constEnd()) {
66             keyframes.append(i.key().toInt());
67             ++i;
68         }
69         m_keyframeWidget->setKeyframes(keyframes);
70
71         for (int j = 0; j < keyframes.count(); ++j) {
72             // key might already be justified
73             if (map.contains(QString::number(keyframes.at(j)))) {
74                 QVariant value = map.take(QString::number(keyframes.at(j)));
75                 map[QString::number(keyframes.at(j)).rightJustified(qRound(log10((double)m_out)), '0')] = value;
76             }
77         }
78         m_data = QVariant(map);
79     } else {
80         m_keyframeWidget->setKeyframes(QList <int>() << 0);
81     }
82
83     m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
84
85     connect(m_item, SIGNAL(changed(bool)), this, SLOT(slotUpdateData(bool)));
86     connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
87     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
88     connect(m_keyframeWidget, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
89     connect(m_keyframeWidget, SIGNAL(keyframeAdded(int)), this, SLOT(slotAddKeyframe(int)));
90     connect(m_keyframeWidget, SIGNAL(keyframeRemoved(int)), this, SLOT(slotRemoveKeyframe(int)));
91     connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
92
93     slotPositionChanged(0, false);
94 }
95
96 RotoWidget::~RotoWidget()
97 {
98     delete m_keyframeWidget;
99
100     m_scene->removeItem(m_item);
101     delete m_item;
102
103     if (m_monitor) {
104         MonitorEditWidget *edit = m_monitor->getEffectEdit();
105         edit->showVisibilityButton(false);
106         edit->removeCustomControls();
107         m_monitor->slotEffectScene(false);
108     }
109 }
110
111 void RotoWidget::slotCheckMonitorPosition(int renderPos)
112 {
113     if (m_showScene)
114         emit checkMonitorPosition(renderPos);
115 }
116
117 void RotoWidget::slotSyncPosition(int relTimelinePos)
118 {
119     relTimelinePos = qBound(0, relTimelinePos, m_out);
120     m_keyframeWidget->slotSetPosition(relTimelinePos, false);
121     slotPositionChanged(relTimelinePos, false);
122 }
123
124 void RotoWidget::slotShowScene(bool show)
125 {
126     m_showScene = show;
127     if (!m_showScene)
128         m_monitor->slotEffectScene(false);
129     else
130         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
131 }
132
133 void RotoWidget::slotUpdateData(int pos, bool editing)
134 {
135     Q_UNUSED(editing)
136
137     int width = m_monitor->render->frameRenderWidth();
138     int height = m_monitor->render->renderHeight();
139
140     QList <BPoint> spline = m_item->getPoints();
141     QList <QVariant> vlist;
142     foreach (const BPoint &point, spline) {
143         QList <QVariant> pl;
144         for (int i = 0; i < 3; ++i)
145             pl << QVariant(QList <QVariant>() << QVariant(point[i].x() / width) << QVariant(point[i].y() / height));
146         vlist << QVariant(pl);
147     }
148
149     if (m_data.canConvert(QVariant::Map)) {
150         QMap <QString, QVariant> map = m_data.toMap();
151         map[QString::number(pos < 0 ? m_keyframeWidget->getPosition() : pos).rightJustified(qRound(log10((double)m_out)), '0')] = QVariant(vlist);
152         m_data = QVariant(map);
153     } else {
154         m_data = QVariant(vlist);
155     }
156
157     emit valueChanged();
158 }
159
160 void RotoWidget::slotUpdateData(bool editing)
161 {
162     slotUpdateData(-1, editing);
163 }
164
165 QString RotoWidget::getSpline()
166 {
167     QJson::Serializer serializer;
168     return QString(serializer.serialize(m_data));
169 }
170
171 void RotoWidget::slotPositionChanged(int pos, bool seek)
172 {
173     if (m_item->editing())
174         return;
175
176     m_keyframeWidget->slotSetPosition(pos, false);
177
178     if (m_data.canConvert(QVariant::Map)) {
179         QMap <QString, QVariant> map = m_data.toMap();
180         QMap <QString, QVariant>::const_iterator i = map.constBegin();
181         int keyframe1, keyframe2;
182         keyframe1 = keyframe2 = i.key().toInt();
183         while (i.key().toInt() < pos && ++i != map.constEnd()) {
184             keyframe1 = keyframe2;
185             keyframe2 = i.key().toInt();
186         }
187
188         if (keyframe1 != keyframe2 && pos < keyframe2) {
189             QList <BPoint> p1 = getPoints(keyframe1);
190             QList <BPoint> p2 = getPoints(keyframe2);
191             QList <BPoint> p;
192             qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
193
194             for (int i = 0; i < p1.count(); ++i) {
195                 BPoint bp;
196                 for (int j = 0; j < 3; ++j) {
197                     if (p1.at(i)[j] != p2.at(i)[j])
198                         bp[j] = QLineF(p1.at(i)[j], p2.at(i)[j]).pointAt(relPos);
199                     else
200                         bp[j] = p1.at(i)[j];
201                 }
202                 p.append(bp);
203             }
204
205             m_item->setPoints(p);
206             m_item->setEnabled(false);
207             m_scene->setEnabled(false);
208         } else {
209             m_item->setPoints(getPoints(keyframe2));
210             m_item->setEnabled(pos == keyframe2);
211             m_scene->setEnabled(pos == keyframe2);
212         }
213     } else {
214         m_item->setPoints(getPoints(-1));
215         m_item->setEnabled(true);
216         m_scene->setEnabled(true);
217     }
218
219     if (seek)
220         emit seekToPos(pos);
221 }
222
223 QList <BPoint> RotoWidget::getPoints(int keyframe)
224 {
225     int width = m_monitor->render->frameRenderWidth();
226     int height = m_monitor->render->renderHeight();
227     QList <BPoint> points;
228     QList <QVariant> data;
229     if (keyframe >= 0)
230         data = m_data.toMap()[QString::number(keyframe).rightJustified(qRound(log10((double)m_out)), '0')].toList();
231     else
232         data = m_data.toList();
233     foreach (const QVariant &bpoint, data) {
234         QList <QVariant> l = bpoint.toList();
235         BPoint p;
236         p.h1 = QPointF(l.at(0).toList().at(0).toDouble() * width, l.at(0).toList().at(1).toDouble() * height);
237         p.p = QPointF(l.at(1).toList().at(0).toDouble() * width, l.at(1).toList().at(1).toDouble() * height);
238         p.h2 = QPointF(l.at(2).toList().at(0).toDouble() * width, l.at(2).toList().at(1).toDouble() * height);
239         points << p;
240     }
241     return points;
242 }
243
244 void RotoWidget::slotAddKeyframe(int pos)
245 {
246     if (!m_data.canConvert(QVariant::Map)) {
247         QVariant data = m_data;
248         QMap<QString, QVariant> map;
249         map[QString::number(m_in).rightJustified(qRound(log10((double)m_out)), '0')] = data;
250         m_data = QVariant(map);
251     }
252
253     if (pos < 0)
254         m_keyframeWidget->addKeyframe();
255
256     slotUpdateData(pos);
257     m_item->setEnabled(true);
258     m_scene->setEnabled(true);
259 }
260
261 void RotoWidget::slotRemoveKeyframe(int pos)
262 {
263     if (pos < 0)
264         pos = m_keyframeWidget->getPosition();
265
266     if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
267         return;
268
269     m_data.toMap().remove(QString::number(pos).rightJustified(qRound(log10((double)m_out)), '0'));
270
271     if (m_data.toMap().count() == 1)
272         m_data = m_data.toMap().begin().value();
273
274     slotPositionChanged(m_keyframeWidget->getPosition(), false);
275 }
276
277 #include "rotowidget.moc"