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"
29 #include <qjson/parser.h>
30 #include <qjson/serializer.h>
32 #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 m_keyframeWidget->setKeyframes(QList <int>() << 0);
74 m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
76 connect(m_item, SIGNAL(changed(bool)), this, SLOT(slotUpdateData(bool)));
77 connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
78 connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
79 connect(m_keyframeWidget, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
80 connect(m_keyframeWidget, SIGNAL(keyframeAdded(int)), this, SLOT(slotAddKeyframe(int)));
81 connect(m_keyframeWidget, SIGNAL(keyframeRemoved(int)), this, SLOT(slotRemoveKeyframe(int)));
82 connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
84 slotPositionChanged(0, false);
87 RotoWidget::~RotoWidget()
89 delete m_keyframeWidget;
91 m_scene->removeItem(m_item);
95 MonitorEditWidget *edit = m_monitor->getEffectEdit();
96 edit->showVisibilityButton(false);
97 edit->removeCustomControls();
98 m_monitor->slotEffectScene(false);
102 void RotoWidget::slotCheckMonitorPosition(int renderPos)
105 emit checkMonitorPosition(renderPos);
108 void RotoWidget::slotSyncPosition(int relTimelinePos)
110 relTimelinePos = qBound(0, relTimelinePos, m_out);
111 m_keyframeWidget->slotSetPosition(relTimelinePos, false);
112 slotPositionChanged(relTimelinePos, false);
115 void RotoWidget::slotShowScene(bool show)
119 m_monitor->slotEffectScene(false);
121 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
124 void RotoWidget::slotUpdateData(int pos, bool editing)
128 int width = m_monitor->render->frameRenderWidth();
129 int height = m_monitor->render->renderHeight();
131 QList <BPoint> spline = m_item->getPoints();
132 QList <QVariant> vlist;
133 foreach (const BPoint &point, spline) {
135 for (int i = 0; i < 3; ++i)
136 pl << QVariant(QList <QVariant>() << QVariant(point[i].x() / width) << QVariant(point[i].y() / height));
137 vlist << QVariant(pl);
140 if (m_data.canConvert(QVariant::Map)) {
141 QMap <QString, QVariant> map = m_data.toMap();
142 map[QString::number(pos < 0 ? m_keyframeWidget->getPosition() : pos)] = QVariant(vlist);
143 m_data = QVariant(map);
145 m_data = QVariant(vlist);
151 void RotoWidget::slotUpdateData(bool editing)
153 slotUpdateData(-1, editing);
156 QString RotoWidget::getSpline()
158 QJson::Serializer serializer;
159 return QString(serializer.serialize(m_data));
162 void RotoWidget::slotPositionChanged(int pos, bool seek)
164 if (m_item->editing())
167 m_keyframeWidget->slotSetPosition(pos, false);
169 if (m_data.canConvert(QVariant::Map)) {
170 QMap <QString, QVariant> map = m_data.toMap();
171 QMap <QString, QVariant>::const_iterator i = map.constBegin();
172 int keyframe1, keyframe2;
173 keyframe1 = keyframe2 = i.key().toInt();
174 while (i.key().toInt() < pos && ++i != map.constEnd()) {
175 keyframe1 = keyframe2;
176 keyframe2 = i.key().toInt();
179 if (keyframe1 != keyframe2 && pos < keyframe2) {
180 QList <BPoint> p1 = getPoints(keyframe1);
181 QList <BPoint> p2 = getPoints(keyframe2);
183 qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
185 for (int i = 0; i < p1.count(); ++i) {
187 for (int j = 0; j < 3; ++j) {
188 if (p1.at(i)[j] != p2.at(i)[j])
189 bp[j] = QLineF(p1.at(i)[j], p2.at(i)[j]).pointAt(relPos);
196 m_item->setPoints(p);
197 m_item->setEnabled(false);
198 m_scene->setEnabled(false);
200 m_item->setPoints(getPoints(keyframe2));
201 m_item->setEnabled(pos == keyframe2);
202 m_scene->setEnabled(pos == keyframe2);
205 m_item->setPoints(getPoints(-1));
206 m_item->setEnabled(true);
207 m_scene->setEnabled(true);
214 QList <BPoint> RotoWidget::getPoints(int keyframe)
216 int width = m_monitor->render->frameRenderWidth();
217 int height = m_monitor->render->renderHeight();
218 QList <BPoint> points;
219 QList <QVariant> data;
221 data = m_data.toMap()[QString::number(keyframe)].toList();
223 data = m_data.toList();
224 foreach (const QVariant &bpoint, data) {
225 QList <QVariant> l = bpoint.toList();
227 p.h1 = QPointF(l.at(0).toList().at(0).toDouble() * width, l.at(0).toList().at(1).toDouble() * height);
228 p.p = QPointF(l.at(1).toList().at(0).toDouble() * width, l.at(1).toList().at(1).toDouble() * height);
229 p.h2 = QPointF(l.at(2).toList().at(0).toDouble() * width, l.at(2).toList().at(1).toDouble() * height);
235 void RotoWidget::slotAddKeyframe(int pos)
237 if (!m_data.canConvert(QVariant::Map)) {
238 QVariant data = m_data;
239 QMap<QString, QVariant> map;
240 map[QString::number(m_in)] = data;
241 m_data = QVariant(map);
245 m_keyframeWidget->addKeyframe();
248 m_item->setEnabled(true);
249 m_scene->setEnabled(true);
252 void RotoWidget::slotRemoveKeyframe(int pos)
255 pos = m_keyframeWidget->getPosition();
257 if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
260 m_data.toMap().remove(QString::number(pos));
262 if (m_data.toMap().count() == 1)
263 m_data = m_data.toMap().begin().value();
265 slotPositionChanged(m_keyframeWidget->getPosition(), false);
268 #include "rotowidget.moc"