]> git.sesse.net Git - kdenlive/blob - src/rotoscoping/rotowidget.cpp
rotoscoping:
[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 <qjson/parser.h>
30 #include <qjson/serializer.h>
31
32 #include <QVBoxLayout>
33
34 #include <KDebug>
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, in, out, 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());
67             ++i;
68         }
69         m_keyframeWidget->setKeyframes(keyframes);
70     } else {
71         m_keyframeWidget->setKeyframes(QList <int>() << 0);
72     }
73
74     m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
75
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()));
83
84     slotPositionChanged(0, false);
85 }
86
87 RotoWidget::~RotoWidget()
88 {
89     delete m_keyframeWidget;
90
91     m_scene->removeItem(m_item);
92     delete m_item;
93
94     if (m_monitor) {
95         MonitorEditWidget *edit = m_monitor->getEffectEdit();
96         edit->showVisibilityButton(false);
97         edit->removeCustomControls();
98         m_monitor->slotEffectScene(false);
99     }
100 }
101
102 void RotoWidget::slotCheckMonitorPosition(int renderPos)
103 {
104     if (m_showScene)
105         emit checkMonitorPosition(renderPos);
106 }
107
108 void RotoWidget::slotSyncPosition(int relTimelinePos)
109 {
110     relTimelinePos = qBound(0, relTimelinePos, m_out);
111     m_keyframeWidget->slotSetPosition(relTimelinePos, false);
112     slotPositionChanged(relTimelinePos, false);
113 }
114
115 void RotoWidget::slotShowScene(bool show)
116 {
117     m_showScene = show;
118     if (!m_showScene)
119         m_monitor->slotEffectScene(false);
120     else
121         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
122 }
123
124 void RotoWidget::slotUpdateData(int pos, bool editing)
125 {
126     Q_UNUSED(editing)
127
128     int width = m_monitor->render->frameRenderWidth();
129     int height = m_monitor->render->renderHeight();
130
131     QList <BPoint> spline = m_item->getPoints();
132     QList <QVariant> vlist;
133     foreach (const BPoint &point, spline) {
134         QList <QVariant> pl;
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);
138     }
139
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);
144     } else {
145         m_data = QVariant(vlist);
146     }
147
148     emit valueChanged();
149 }
150
151 void RotoWidget::slotUpdateData(bool editing)
152 {
153     slotUpdateData(-1, editing);
154 }
155
156 QString RotoWidget::getSpline()
157 {
158     QJson::Serializer serializer;
159     return QString(serializer.serialize(m_data));
160 }
161
162 void RotoWidget::slotPositionChanged(int pos, bool seek)
163 {
164     if (m_item->editing())
165         return;
166
167     m_keyframeWidget->slotSetPosition(pos, false);
168
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();
177         }
178
179         if (keyframe1 != keyframe2 && pos < keyframe2) {
180             QList <BPoint> p1 = getPoints(keyframe1);
181             QList <BPoint> p2 = getPoints(keyframe2);
182             QList <BPoint> p;
183             qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
184
185             for (int i = 0; i < p1.count(); ++i) {
186                 BPoint bp;
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);
190                     else
191                         bp[j] = p1.at(i)[j];
192                 }
193                 p.append(bp);
194             }
195
196             m_item->setPoints(p);
197             m_item->setEnabled(false);
198             m_scene->setEnabled(false);
199         } else {
200             m_item->setPoints(getPoints(keyframe2));
201             m_item->setEnabled(pos == keyframe2);
202             m_scene->setEnabled(pos == keyframe2);
203         }
204     } else {
205         m_item->setPoints(getPoints(-1));
206         m_item->setEnabled(true);
207         m_scene->setEnabled(true);
208     }
209
210     if (seek)
211         emit seekToPos(pos);
212 }
213
214 QList <BPoint> RotoWidget::getPoints(int keyframe)
215 {
216     int width = m_monitor->render->frameRenderWidth();
217     int height = m_monitor->render->renderHeight();
218     QList <BPoint> points;
219     QList <QVariant> data;
220     if (keyframe >= 0)
221         data = m_data.toMap()[QString::number(keyframe)].toList();
222     else
223         data = m_data.toList();
224     foreach (const QVariant &bpoint, data) {
225         QList <QVariant> l = bpoint.toList();
226         BPoint p;
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);
230         points << p;
231     }
232     return points;
233 }
234
235 void RotoWidget::slotAddKeyframe(int pos)
236 {
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);
242     }
243
244     if (pos < 0)
245         m_keyframeWidget->addKeyframe();
246
247     slotUpdateData(pos);
248     m_item->setEnabled(true);
249     m_scene->setEnabled(true);
250 }
251
252 void RotoWidget::slotRemoveKeyframe(int pos)
253 {
254     if (pos < 0)
255         pos = m_keyframeWidget->getPosition();
256
257     if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
258         return;
259
260     m_data.toMap().remove(QString::number(pos));
261
262     if (m_data.toMap().count() == 1)
263         m_data = m_data.toMap().begin().value();
264
265     slotPositionChanged(m_keyframeWidget->getPosition(), false);
266 }
267
268 #include "rotowidget.moc"