1 /***************************************************************************
2 * Copyright (C) 2011 by Till Theato (root@ttill.de) *
3 * This file is part of Kdenlive (www.kdenlive.org). *
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. *
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. *
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 ***************************************************************************/
19 #include "rotowidget.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"
31 #include <qjson/parser.h>
32 #include <qjson/serializer.h>
34 #include <QVBoxLayout>
37 RotoWidget::RotoWidget(QString data, Monitor *monitor, int in, int out, Timecode t, QWidget* parent) :
44 QVBoxLayout *l = new QVBoxLayout(this);
45 m_keyframeWidget = new SimpleKeyframeWidget(t, m_out - m_in, this);
46 l->addWidget(m_keyframeWidget);
48 MonitorEditWidget *edit = monitor->getEffectEdit();
49 edit->showVisibilityButton(true);
50 m_scene = edit->getScene();
54 m_data = parser.parse(data.toUtf8(), &ok);
60 if (m_data.canConvert(QVariant::Map)) {
62 * pass keyframe data to keyframe timeline
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);
71 m_keyframeWidget->setKeyframes(keyframes);
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(log10((double)m_out) + 1, '0')] = value;
80 m_data = QVariant(map);
82 // static (only one keyframe)
83 m_keyframeWidget->setKeyframes(QList <int>() << 0);
86 m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
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()));
97 slotPositionChanged(0, false);
100 RotoWidget::~RotoWidget()
102 delete m_keyframeWidget;
104 m_scene->removeItem(m_item);
108 MonitorEditWidget *edit = m_monitor->getEffectEdit();
109 edit->showVisibilityButton(false);
110 edit->removeCustomControls();
111 m_monitor->slotEffectScene(false);
115 void RotoWidget::slotCheckMonitorPosition(int renderPos)
118 emit checkMonitorPosition(renderPos);
121 void RotoWidget::slotSyncPosition(int relTimelinePos)
123 relTimelinePos = qBound(0, relTimelinePos, m_out);
124 m_keyframeWidget->slotSetPosition(relTimelinePos, false);
125 slotPositionChanged(relTimelinePos, false);
128 void RotoWidget::slotShowScene(bool show)
132 m_monitor->slotEffectScene(false);
134 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
137 void RotoWidget::slotUpdateData(int pos, bool editing)
141 int width = m_monitor->render->frameRenderWidth();
142 int height = m_monitor->render->renderHeight();
145 * use the position of the on-monitor points to create a storable list
147 QList <BPoint> spline = m_item->getPoints();
148 QList <QVariant> vlist;
149 foreach (const BPoint &point, spline) {
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);
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(log10((double)m_out) + 1, '0')] = QVariant(vlist);
161 m_data = QVariant(map);
163 m_data = QVariant(vlist);
169 void RotoWidget::slotUpdateData(bool editing)
171 slotUpdateData(-1, editing);
174 QString RotoWidget::getSpline()
176 QJson::Serializer serializer;
177 return QString(serializer.serialize(m_data));
180 void RotoWidget::slotPositionChanged(int pos, bool seek)
182 // do not update while the spline is being edited (points are being dragged)
183 if (m_item->editing())
186 m_keyframeWidget->slotSetPosition(pos, false);
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();
203 if (keyframe1 != keyframe2 && pos < keyframe2) {
205 * in between two keyframes
208 QList <BPoint> p1 = getPoints(keyframe1);
209 QList <BPoint> p2 = getPoints(keyframe2);
210 qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
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) {
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);
225 m_item->setPoints(p);
226 m_item->setEnabled(false);
227 m_scene->setEnabled(false);
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);
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);
246 emit seekToPos(pos - m_in);
249 QList <BPoint> RotoWidget::getPoints(int keyframe)
251 int width = m_monitor->render->frameRenderWidth();
252 int height = m_monitor->render->renderHeight();
253 QList <BPoint> points;
254 QList <QVariant> data;
256 data = m_data.toMap()[QString::number(keyframe).rightJustified(log10((double)m_out) + 1, '0')].toList();
258 data = m_data.toList();
259 foreach (const QVariant &bpoint, data) {
260 QList <QVariant> l = bpoint.toList();
262 for (int i = 0; i < 3; ++i)
263 p[i] = QPointF(l.at(i).toList().at(0).toDouble() * width, l.at(i).toList().at(1).toDouble() * height);
269 void RotoWidget::slotAddKeyframe(int pos)
271 if (!m_data.canConvert(QVariant::Map)) {
272 QVariant data = m_data;
273 QMap<QString, QVariant> map;
274 map[QString::number(m_in).rightJustified(log10((double)m_out) + 1, '0')] = data;
275 m_data = QVariant(map);
279 m_keyframeWidget->addKeyframe();
282 m_item->setEnabled(true);
283 m_scene->setEnabled(true);
286 void RotoWidget::slotRemoveKeyframe(int pos)
289 pos = m_keyframeWidget->getPosition();
291 if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
294 QMap<QString, QVariant> map = m_data.toMap();
295 map.remove(QString::number(pos + m_in).rightJustified(log10((double)m_out) + 1, '0'));
296 m_data = QVariant(map);
298 if (m_data.toMap().count() == 1) {
299 // only one keyframe -> switch from map to list again
300 m_data = m_data.toMap().begin().value();
303 slotPositionChanged(m_keyframeWidget->getPosition(), false);
307 void RotoWidget::slotMoveKeyframe(int oldPos, int newPos)
309 if (m_data.canConvert(QVariant::Map)) {
310 QMap<QString, QVariant> map = m_data.toMap();
311 map[QString::number(newPos + m_in).rightJustified(log10((double)m_out) + 1, '0')] = map.take(QString::number(oldPos + m_in).rightJustified(log10((double)m_out) + 1, '0'));
312 m_data = QVariant(map);
315 slotPositionChanged(m_keyframeWidget->getPosition(), false);
319 void RotoWidget::updateTimecodeFormat()
321 m_keyframeWidget->updateTimecodeFormat();
326 static QVariant interpolate(int position, int in, int out, QVariant *splineIn, QVariant *splineOut)
328 qreal relPos = (position - in) / (qreal)(out - in + 1);
329 QList<QVariant> keyframe1 = splineIn->toList();
330 QList<QVariant> keyframe2 = splineOut->toList();
331 QList<QVariant> keyframe;
332 int max = qMin(keyframe1.count(), keyframe2.count());
333 for (int i = 0; i < max; ++i) {
334 QList<QVariant> p1 = keyframe1.at(i).toList();
335 QList<QVariant> p2 = keyframe2.at(i).toList();
337 for (int j = 0; j < 3; ++j) {
338 QPointF middle = QLineF(QPointF(p1.at(j).toList().at(0).toDouble(), p1.at(j).toList().at(1).toDouble()),
339 QPointF(p2.at(j).toList().at(0).toDouble(), p2.at(j).toList().at(1).toDouble())).pointAt(relPos);
340 p.append(QVariant(QList<QVariant>() << QVariant(middle.x()) << QVariant(middle.y())));
342 keyframe.append(QVariant(p));
344 return QVariant(keyframe);
347 bool adjustRotoDuration(QString* data, int in, int out, bool cut)
351 QJson::Parser parser;
353 QVariant splines = parser.parse(data->toUtf8(), &ok);
359 if (!splines.canConvert(QVariant::Map))
362 QMap<QString, QVariant> map = splines.toMap();
363 QMap<QString, QVariant>::iterator i = map.end();
368 * Take care of resize from start
370 bool startFound = false;
371 while (i-- != map.begin()) {
372 if (i.key().toInt() < in) {
376 map[QString::number(in).rightJustified(log10((double)out) + 1, '0')] = i.value();
378 map[QString::number(in).rightJustified(log10((double)out) + 1, '0')] = interpolate(in, i.key().toInt(), lastPos, &i.value(), &last);
381 lastPos = i.key().toInt();
388 * Take care of resize from end
392 bool endFound = false;
393 while (i != map.end()) {
394 if (i.key().toInt() > out) {
398 map[QString::number(out)] = i.value();
400 map[QString::number(out)] = interpolate(out, lastPos, i.key().toInt(), &last, &i.value());
403 lastPos = i.key().toInt();
411 QJson::Serializer serializer;
412 *data = QString(serializer.serialize(QVariant(map)));
414 if (startFound || endFound)
419 #include "rotowidget.moc"