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) :
45 QVBoxLayout *l = new QVBoxLayout(this);
46 m_keyframeWidget = new SimpleKeyframeWidget(t, in, out, this);
47 l->addWidget(m_keyframeWidget);
49 MonitorEditWidget *edit = monitor->getEffectEdit();
50 edit->showVisibilityButton(true);
51 m_scene = edit->getScene();
55 m_data = parser.parse(data.toUtf8(), &ok);
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());
69 m_keyframeWidget->setKeyframes(keyframes);
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;
78 m_data = QVariant(map);
80 m_keyframeWidget->setKeyframes(QList <int>() << 0);
83 m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
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()));
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).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);
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();
188 if (keyframe1 != keyframe2 && pos < keyframe2) {
189 QList <BPoint> p1 = getPoints(keyframe1);
190 QList <BPoint> p2 = getPoints(keyframe2);
192 qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
194 for (int i = 0; i < p1.count(); ++i) {
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);
205 m_item->setPoints(p);
206 m_item->setEnabled(false);
207 m_scene->setEnabled(false);
209 m_item->setPoints(getPoints(keyframe2));
210 m_item->setEnabled(pos == keyframe2);
211 m_scene->setEnabled(pos == keyframe2);
214 m_item->setPoints(getPoints(-1));
215 m_item->setEnabled(true);
216 m_scene->setEnabled(true);
223 QList <BPoint> RotoWidget::getPoints(int keyframe)
225 int width = m_monitor->render->frameRenderWidth();
226 int height = m_monitor->render->renderHeight();
227 QList <BPoint> points;
228 QList <QVariant> data;
230 data = m_data.toMap()[QString::number(keyframe).rightJustified(qRound(log10((double)m_out)), '0')].toList();
232 data = m_data.toList();
233 foreach (const QVariant &bpoint, data) {
234 QList <QVariant> l = bpoint.toList();
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);
244 void RotoWidget::slotAddKeyframe(int pos)
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);
254 m_keyframeWidget->addKeyframe();
257 m_item->setEnabled(true);
258 m_scene->setEnabled(true);
261 void RotoWidget::slotRemoveKeyframe(int pos)
264 pos = m_keyframeWidget->getPosition();
266 if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
269 m_data.toMap().remove(QString::number(pos).rightJustified(qRound(log10((double)m_out)), '0'));
271 if (m_data.toMap().count() == 1)
272 m_data = m_data.toMap().begin().value();
274 slotPositionChanged(m_keyframeWidget->getPosition(), false);
277 #include "rotowidget.moc"