]> git.sesse.net Git - kdenlive/blob - src/rotoscoping/rotowidget.cpp
rotoscoping: preserve point selection after mouse release
[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 {
44     QVBoxLayout *l = new QVBoxLayout(this);
45     m_keyframeWidget = new SimpleKeyframeWidget(t, m_out - m_in - 1, this);
46     l->addWidget(m_keyframeWidget);
47
48     MonitorEditWidget *edit = monitor->getEffectEdit();
49     edit->showVisibilityButton(true);
50     m_scene = edit->getScene();
51
52     QJson::Parser parser;
53     bool ok;
54     m_data = parser.parse(data.toUtf8(), &ok);
55     if (!ok) {
56         // :(
57     }
58
59     
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);
66             ++i;
67         }
68         m_keyframeWidget->setKeyframes(keyframes);
69
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;
75             }
76         }
77         m_data = QVariant(map);
78     } else {
79         m_keyframeWidget->setKeyframes(QList <int>() << 0);
80     }
81
82     m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
83
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()));
92
93     slotPositionChanged(0, false);
94 }
95
96 RotoWidget::~RotoWidget()
97 {
98     delete m_keyframeWidget;
99
100     m_scene->removeItem(m_item);
101     delete m_item;
102
103     if (m_monitor) {
104         MonitorEditWidget *edit = m_monitor->getEffectEdit();
105         edit->showVisibilityButton(false);
106         edit->removeCustomControls();
107         m_monitor->slotEffectScene(false);
108     }
109 }
110
111 void RotoWidget::slotCheckMonitorPosition(int renderPos)
112 {
113     if (m_showScene)
114         emit checkMonitorPosition(renderPos);
115 }
116
117 void RotoWidget::slotSyncPosition(int relTimelinePos)
118 {
119     relTimelinePos = qBound(0, relTimelinePos, m_out);
120     m_keyframeWidget->slotSetPosition(relTimelinePos, false);
121     slotPositionChanged(relTimelinePos, false);
122 }
123
124 void RotoWidget::slotShowScene(bool show)
125 {
126     m_showScene = show;
127     if (!m_showScene)
128         m_monitor->slotEffectScene(false);
129     else
130         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
131 }
132
133 void RotoWidget::slotUpdateData(int pos, bool editing)
134 {
135     Q_UNUSED(editing)
136
137     int width = m_monitor->render->frameRenderWidth();
138     int height = m_monitor->render->renderHeight();
139
140     QList <BPoint> spline = m_item->getPoints();
141     QList <QVariant> vlist;
142     foreach (const BPoint &point, spline) {
143         QList <QVariant> pl;
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);
147     }
148
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);
153     } else {
154         m_data = QVariant(vlist);
155     }
156
157     emit valueChanged();
158 }
159
160 void RotoWidget::slotUpdateData(bool editing)
161 {
162     slotUpdateData(-1, editing);
163 }
164
165 QString RotoWidget::getSpline()
166 {
167     QJson::Serializer serializer;
168     return QString(serializer.serialize(m_data));
169 }
170
171 void RotoWidget::slotPositionChanged(int pos, bool seek)
172 {
173     if (m_item->editing())
174         return;
175
176     m_keyframeWidget->slotSetPosition(pos, false);
177
178     pos += m_in;
179
180     QList <BPoint> p;
181
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();
190         }
191
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);
196
197             int count = qMin(p1.count(), p2.count());
198             for (int i = 0; i < count; ++i) {
199                 BPoint bp;
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);
203                     else
204                         bp[j] = p1.at(i)[j];
205                 }
206                 p.append(bp);
207             }
208
209             m_item->setPoints(p);
210             m_item->setEnabled(false);
211             m_scene->setEnabled(false);
212         } else {
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);
219         }
220     } else {
221         p = getPoints(-1);
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);
227     }
228
229     if (seek)
230         emit seekToPos(pos - m_in);
231 }
232
233 QList <BPoint> RotoWidget::getPoints(int keyframe)
234 {
235     int width = m_monitor->render->frameRenderWidth();
236     int height = m_monitor->render->renderHeight();
237     QList <BPoint> points;
238     QList <QVariant> data;
239     if (keyframe >= 0)
240         data = m_data.toMap()[QString::number(keyframe).rightJustified(qRound(log10((double)m_out)), '0')].toList();
241     else
242         data = m_data.toList();
243     foreach (const QVariant &bpoint, data) {
244         QList <QVariant> l = bpoint.toList();
245         BPoint p;
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);
249         points << p;
250     }
251     return points;
252 }
253
254 void RotoWidget::slotAddKeyframe(int pos)
255 {
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);
261     }
262
263     if (pos < 0)
264         m_keyframeWidget->addKeyframe();
265
266     slotUpdateData(pos);
267     m_item->setEnabled(true);
268     m_scene->setEnabled(true);
269 }
270
271 void RotoWidget::slotRemoveKeyframe(int pos)
272 {
273     if (pos < 0)
274         pos = m_keyframeWidget->getPosition();
275
276     if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
277         return;
278
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);
282
283     if (m_data.toMap().count() == 1)
284         m_data = m_data.toMap().begin().value();
285
286     slotPositionChanged(m_keyframeWidget->getPosition(), false);
287     emit valueChanged();
288 }
289
290 void RotoWidget::slotMoveKeyframe(int oldPos, int newPos)
291 {
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);
296     }
297
298     slotPositionChanged(m_keyframeWidget->getPosition(), false);
299     emit valueChanged();
300 }
301
302 #include "rotowidget.moc"