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 - 1, 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)) {
61 QList <int> keyframes;
62 QMap <QString, QVariant> map = m_data.toMap();
63 QMap <QString, QVariant>::const_iterator i = map.constBegin();
64 while (i != map.constEnd()) {
65 keyframes.append(i.key().toInt() - m_in);
68 m_keyframeWidget->setKeyframes(keyframes);
70 for (int j = 0; j < keyframes.count(); ++j) {
71 // key might already be justified
72 if (map.contains(QString::number(keyframes.at(j) + m_in))) {
73 QVariant value = map.take(QString::number(keyframes.at(j) + m_in));
74 map[QString::number(keyframes.at(j) + m_in).rightJustified(qRound(log10((double)m_out)), '0')] = value;
77 m_data = QVariant(map);
79 m_keyframeWidget->setKeyframes(QList <int>() << 0);
82 m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
84 connect(m_item, SIGNAL(changed(bool)), this, SLOT(slotUpdateData(bool)));
85 connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
86 connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
87 connect(m_keyframeWidget, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
88 connect(m_keyframeWidget, SIGNAL(keyframeAdded(int)), this, SLOT(slotAddKeyframe(int)));
89 connect(m_keyframeWidget, SIGNAL(keyframeRemoved(int)), this, SLOT(slotRemoveKeyframe(int)));
90 connect(m_keyframeWidget, SIGNAL(keyframeMoved(int,int)), this, SLOT(slotMoveKeyframe(int,int)));
91 connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
93 slotPositionChanged(0, false);
96 RotoWidget::~RotoWidget()
98 delete m_keyframeWidget;
100 m_scene->removeItem(m_item);
104 MonitorEditWidget *edit = m_monitor->getEffectEdit();
105 edit->showVisibilityButton(false);
106 edit->removeCustomControls();
107 m_monitor->slotEffectScene(false);
111 void RotoWidget::slotCheckMonitorPosition(int renderPos)
114 emit checkMonitorPosition(renderPos);
117 void RotoWidget::slotSyncPosition(int relTimelinePos)
119 relTimelinePos = qBound(0, relTimelinePos, m_out);
120 m_keyframeWidget->slotSetPosition(relTimelinePos, false);
121 slotPositionChanged(relTimelinePos, false);
124 void RotoWidget::slotShowScene(bool show)
128 m_monitor->slotEffectScene(false);
130 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
133 void RotoWidget::slotUpdateData(int pos, bool editing)
137 int width = m_monitor->render->frameRenderWidth();
138 int height = m_monitor->render->renderHeight();
140 QList <BPoint> spline = m_item->getPoints();
141 QList <QVariant> vlist;
142 foreach (const BPoint &point, spline) {
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);
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) + m_in).rightJustified(qRound(log10((double)m_out)), '0')] = QVariant(vlist);
152 m_data = QVariant(map);
154 m_data = QVariant(vlist);
160 void RotoWidget::slotUpdateData(bool editing)
162 slotUpdateData(-1, editing);
165 QString RotoWidget::getSpline()
167 QJson::Serializer serializer;
168 return QString(serializer.serialize(m_data));
171 void RotoWidget::slotPositionChanged(int pos, bool seek)
173 if (m_item->editing())
176 m_keyframeWidget->slotSetPosition(pos, false);
182 if (m_data.canConvert(QVariant::Map)) {
183 QMap <QString, QVariant> map = m_data.toMap();
184 QMap <QString, QVariant>::const_iterator i = map.constBegin();
185 int keyframe1, keyframe2;
186 keyframe1 = keyframe2 = i.key().toInt();
187 while (i.key().toInt() < pos && ++i != map.constEnd()) {
188 keyframe1 = keyframe2;
189 keyframe2 = i.key().toInt();
192 if (keyframe1 != keyframe2 && pos < keyframe2) {
193 QList <BPoint> p1 = getPoints(keyframe1);
194 QList <BPoint> p2 = getPoints(keyframe2);
195 qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
197 int count = qMin(p1.count(), p2.count());
198 for (int i = 0; i < count; ++i) {
200 for (int j = 0; j < 3; ++j) {
201 if (p1.at(i)[j] != p2.at(i)[j])
202 bp[j] = QLineF(p1.at(i)[j], p2.at(i)[j]).pointAt(relPos);
209 m_item->setPoints(p);
210 m_item->setEnabled(false);
211 m_scene->setEnabled(false);
213 p = getPoints(keyframe2);
214 // only update if necessary to preserve the current point selection
215 if (p != m_item->getPoints())
216 m_item->setPoints(p);
217 m_item->setEnabled(pos == keyframe2);
218 m_scene->setEnabled(pos == keyframe2);
222 // only update if necessary to preserve the current point selection
223 if (p != m_item->getPoints())
224 m_item->setPoints(p);
225 m_item->setEnabled(true);
226 m_scene->setEnabled(true);
230 emit seekToPos(pos - m_in);
233 QList <BPoint> RotoWidget::getPoints(int keyframe)
235 int width = m_monitor->render->frameRenderWidth();
236 int height = m_monitor->render->renderHeight();
237 QList <BPoint> points;
238 QList <QVariant> data;
240 data = m_data.toMap()[QString::number(keyframe).rightJustified(qRound(log10((double)m_out)), '0')].toList();
242 data = m_data.toList();
243 foreach (const QVariant &bpoint, data) {
244 QList <QVariant> l = bpoint.toList();
246 p.h1 = QPointF(l.at(0).toList().at(0).toDouble() * width, l.at(0).toList().at(1).toDouble() * height);
247 p.p = QPointF(l.at(1).toList().at(0).toDouble() * width, l.at(1).toList().at(1).toDouble() * height);
248 p.h2 = QPointF(l.at(2).toList().at(0).toDouble() * width, l.at(2).toList().at(1).toDouble() * height);
254 void RotoWidget::slotAddKeyframe(int pos)
256 if (!m_data.canConvert(QVariant::Map)) {
257 QVariant data = m_data;
258 QMap<QString, QVariant> map;
259 map[QString::number(m_in).rightJustified(qRound(log10((double)m_out)), '0')] = data;
260 m_data = QVariant(map);
264 m_keyframeWidget->addKeyframe();
267 m_item->setEnabled(true);
268 m_scene->setEnabled(true);
271 void RotoWidget::slotRemoveKeyframe(int pos)
274 pos = m_keyframeWidget->getPosition();
276 if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
279 QMap<QString, QVariant> map = m_data.toMap();
280 map.remove(QString::number(pos + m_in).rightJustified(qRound(log10((double)m_out)), '0'));
281 m_data = QVariant(map);
283 if (m_data.toMap().count() == 1)
284 m_data = m_data.toMap().begin().value();
286 slotPositionChanged(m_keyframeWidget->getPosition(), false);
290 void RotoWidget::slotMoveKeyframe(int oldPos, int newPos)
292 if (m_data.canConvert(QVariant::Map)) {
293 QMap<QString, QVariant> map = m_data.toMap();
294 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'));
295 m_data = QVariant(map);
298 slotPositionChanged(m_keyframeWidget->getPosition(), false);
302 void RotoWidget::updateTimecodeFormat()
304 m_keyframeWidget->updateTimecodeFormat();
307 #include "rotowidget.moc"