]> git.sesse.net Git - kdenlive/blob - src/rotoscoping/rotowidget.cpp
d1c2133d73d8814932a0b7ecf1df020fd13d5b09
[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, m_out - m_in - 1, 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() - m_in);
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) + m_in))) {
74                 QVariant value = map.take(QString::number(keyframes.at(j) + m_in));
75                 map[QString::number(keyframes.at(j) + m_in).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_keyframeWidget, SIGNAL(keyframeMoved(int,int)), this, SLOT(slotMoveKeyframe(int,int)));
92     connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
93
94     slotPositionChanged(0, false);
95 }
96
97 RotoWidget::~RotoWidget()
98 {
99     delete m_keyframeWidget;
100
101     m_scene->removeItem(m_item);
102     delete m_item;
103
104     if (m_monitor) {
105         MonitorEditWidget *edit = m_monitor->getEffectEdit();
106         edit->showVisibilityButton(false);
107         edit->removeCustomControls();
108         m_monitor->slotEffectScene(false);
109     }
110 }
111
112 void RotoWidget::slotCheckMonitorPosition(int renderPos)
113 {
114     if (m_showScene)
115         emit checkMonitorPosition(renderPos);
116 }
117
118 void RotoWidget::slotSyncPosition(int relTimelinePos)
119 {
120     relTimelinePos = qBound(0, relTimelinePos, m_out);
121     m_keyframeWidget->slotSetPosition(relTimelinePos, false);
122     slotPositionChanged(relTimelinePos, false);
123 }
124
125 void RotoWidget::slotShowScene(bool show)
126 {
127     m_showScene = show;
128     if (!m_showScene)
129         m_monitor->slotEffectScene(false);
130     else
131         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
132 }
133
134 void RotoWidget::slotUpdateData(int pos, bool editing)
135 {
136     Q_UNUSED(editing)
137
138     int width = m_monitor->render->frameRenderWidth();
139     int height = m_monitor->render->renderHeight();
140
141     QList <BPoint> spline = m_item->getPoints();
142     QList <QVariant> vlist;
143     foreach (const BPoint &point, spline) {
144         QList <QVariant> pl;
145         for (int i = 0; i < 3; ++i)
146             pl << QVariant(QList <QVariant>() << QVariant(point[i].x() / width) << QVariant(point[i].y() / height));
147         vlist << QVariant(pl);
148     }
149
150     if (m_data.canConvert(QVariant::Map)) {
151         QMap <QString, QVariant> map = m_data.toMap();
152         map[QString::number((pos < 0 ? m_keyframeWidget->getPosition() : pos) + m_in).rightJustified(qRound(log10((double)m_out)), '0')] = QVariant(vlist);
153         m_data = QVariant(map);
154     } else {
155         m_data = QVariant(vlist);
156     }
157
158     emit valueChanged();
159 }
160
161 void RotoWidget::slotUpdateData(bool editing)
162 {
163     slotUpdateData(-1, editing);
164 }
165
166 QString RotoWidget::getSpline()
167 {
168     QJson::Serializer serializer;
169     return QString(serializer.serialize(m_data));
170 }
171
172 void RotoWidget::slotPositionChanged(int pos, bool seek)
173 {
174     if (m_item->editing())
175         return;
176
177     m_keyframeWidget->slotSetPosition(pos, false);
178
179     pos += m_in;
180
181     if (m_data.canConvert(QVariant::Map)) {
182         QMap <QString, QVariant> map = m_data.toMap();
183         QMap <QString, QVariant>::const_iterator i = map.constBegin();
184         int keyframe1, keyframe2;
185         keyframe1 = keyframe2 = i.key().toInt();
186         while (i.key().toInt() < pos && ++i != map.constEnd()) {
187             keyframe1 = keyframe2;
188             keyframe2 = i.key().toInt();
189         }
190
191         if (keyframe1 != keyframe2 && pos < keyframe2) {
192             QList <BPoint> p1 = getPoints(keyframe1);
193             QList <BPoint> p2 = getPoints(keyframe2);
194             QList <BPoint> p;
195             qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
196
197             for (int i = 0; i < p1.count(); ++i) {
198                 BPoint bp;
199                 for (int j = 0; j < 3; ++j) {
200                     if (p1.at(i)[j] != p2.at(i)[j])
201                         bp[j] = QLineF(p1.at(i)[j], p2.at(i)[j]).pointAt(relPos);
202                     else
203                         bp[j] = p1.at(i)[j];
204                 }
205                 p.append(bp);
206             }
207
208             m_item->setPoints(p);
209             m_item->setEnabled(false);
210             m_scene->setEnabled(false);
211         } else {
212             m_item->setPoints(getPoints(keyframe2));
213             m_item->setEnabled(pos == keyframe2);
214             m_scene->setEnabled(pos == keyframe2);
215         }
216     } else {
217         m_item->setPoints(getPoints(-1));
218         m_item->setEnabled(true);
219         m_scene->setEnabled(true);
220     }
221
222     if (seek)
223         emit seekToPos(pos - m_in);
224 }
225
226 QList <BPoint> RotoWidget::getPoints(int keyframe)
227 {
228     int width = m_monitor->render->frameRenderWidth();
229     int height = m_monitor->render->renderHeight();
230     QList <BPoint> points;
231     QList <QVariant> data;
232     if (keyframe >= 0)
233         data = m_data.toMap()[QString::number(keyframe).rightJustified(qRound(log10((double)m_out)), '0')].toList();
234     else
235         data = m_data.toList();
236     foreach (const QVariant &bpoint, data) {
237         QList <QVariant> l = bpoint.toList();
238         BPoint p;
239         p.h1 = QPointF(l.at(0).toList().at(0).toDouble() * width, l.at(0).toList().at(1).toDouble() * height);
240         p.p = QPointF(l.at(1).toList().at(0).toDouble() * width, l.at(1).toList().at(1).toDouble() * height);
241         p.h2 = QPointF(l.at(2).toList().at(0).toDouble() * width, l.at(2).toList().at(1).toDouble() * height);
242         points << p;
243     }
244     return points;
245 }
246
247 void RotoWidget::slotAddKeyframe(int pos)
248 {
249     if (!m_data.canConvert(QVariant::Map)) {
250         QVariant data = m_data;
251         QMap<QString, QVariant> map;
252         map[QString::number(m_in).rightJustified(qRound(log10((double)m_out)), '0')] = data;
253         m_data = QVariant(map);
254     }
255
256     if (pos < 0)
257         m_keyframeWidget->addKeyframe();
258
259     slotUpdateData(pos);
260     m_item->setEnabled(true);
261     m_scene->setEnabled(true);
262 }
263
264 void RotoWidget::slotRemoveKeyframe(int pos)
265 {
266     if (pos < 0)
267         pos = m_keyframeWidget->getPosition();
268
269     if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
270         return;
271
272     m_data.toMap().remove(QString::number(pos - m_in).rightJustified(qRound(log10((double)m_out)), '0'));
273
274     if (m_data.toMap().count() == 1)
275         m_data = m_data.toMap().begin().value();
276
277     slotPositionChanged(m_keyframeWidget->getPosition(), false);
278 }
279
280 void RotoWidget::slotMoveKeyframe(int oldPos, int newPos)
281 {
282     if (m_data.canConvert(QVariant::Map)) {
283         QMap<QString, QVariant> map = m_data.toMap();
284         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'));
285         m_data = QVariant(map);
286     }
287
288     slotPositionChanged(m_keyframeWidget->getPosition(), false);
289     emit valueChanged();
290 }
291
292 #include "rotowidget.moc"