]> git.sesse.net Git - kdenlive/blob - src/rotoscoping/rotowidget.cpp
First work on rotoscoping GUI
[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
27 #include <qjson/parser.h>
28 #include <qjson/serializer.h>
29
30
31 RotoWidget::RotoWidget(QString data, Monitor *monitor, int in, int out, QWidget* parent) :
32         QWidget(parent),
33         m_monitor(monitor),
34         m_showScene(true),
35         m_in(in),
36         m_out(out),
37         m_pos(0)
38 {
39     MonitorEditWidget *edit = monitor->getEffectEdit();
40     edit->showVisibilityButton(true);
41     m_scene = edit->getScene();
42
43     QJson::Parser parser;
44     bool ok;
45     m_data = parser.parse(data.toUtf8(), &ok);
46     if (!ok) {
47         // :(
48     }
49
50     int width = m_monitor->render->frameRenderWidth();
51     int height = m_monitor->render->renderHeight();
52     QList <BPoint> points;
53     foreach (const QVariant &bpoint, m_data.toList()) {
54         QList <QVariant> l = bpoint.toList();
55         BPoint p;
56         p.h1 = QPointF(l.at(0).toList().at(0).toDouble() * width, l.at(0).toList().at(1).toDouble() * height);
57         p.p = QPointF(l.at(1).toList().at(0).toDouble() * width, l.at(2).toList().at(1).toDouble() * height);
58         p.h2 = QPointF(l.at(2).toList().at(0).toDouble() * width, l.at(2).toList().at(1).toDouble() * height);
59         points << p;
60     }
61
62     m_item = new SplineItem(points, NULL, m_scene);
63
64     connect(m_item, SIGNAL(changed()), this, SLOT(slotUpdateData()));
65     connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
66     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
67 }
68
69 RotoWidget::~RotoWidget()
70 {
71     m_scene->removeItem(m_item);
72     delete m_item;
73
74     if (m_monitor) {
75         MonitorEditWidget *edit = m_monitor->getEffectEdit();
76         edit->showVisibilityButton(false);
77         edit->removeCustomControls();
78         m_monitor->slotEffectScene(false);
79     }
80 }
81
82 void RotoWidget::slotCheckMonitorPosition(int renderPos)
83 {
84     if (m_showScene)
85         emit checkMonitorPosition(renderPos);
86 }
87
88 void RotoWidget::slotSyncPosition(int relTimelinePos)
89 {
90     Q_UNUSED(relTimelinePos);
91 }
92
93 void RotoWidget::slotShowScene(bool show)
94 {
95     m_showScene = show;
96     if (!m_showScene)
97         m_monitor->slotEffectScene(false);
98     else
99         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
100 }
101
102 void RotoWidget::slotUpdateData()
103 {
104     int width = m_monitor->render->frameRenderWidth();
105     int height = m_monitor->render->renderHeight();
106
107     QList <BPoint> spline = m_item->getPoints();
108     QList <QVariant> vlist;
109     foreach (const BPoint &point, spline) {
110         QList <QVariant> pl;
111         for (int i = 0; i < 3; ++i)
112             pl << QVariant(QList <QVariant>() << QVariant(point[i].x() / width) << QVariant(point[i].y() / height));
113         vlist << QVariant(pl);
114     }
115     m_data = QVariant(vlist);
116
117     emit valueChanged();
118 }
119
120 QString RotoWidget::getSpline()
121 {
122     QJson::Serializer serializer;
123     return QString(serializer.serialize(m_data));
124 }
125
126 #include "rotowidget.moc"