]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
On-Monitor effects:
[kdenlive] / src / geometrywidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "geometrywidget.h"
22 #include "monitor.h"
23 #include "renderer.h"
24 #include "monitorscene.h"
25
26 #include <QGraphicsView>
27 #include <QGraphicsRectItem>
28
29 GeometryWidget::GeometryWidget(Monitor* monitor, int clipPos, QWidget* parent ):
30         QWidget(parent),
31         m_monitor(monitor),
32         m_clipPos(clipPos),
33         m_inPoint(0),
34         m_outPoint(1),
35         m_rect(NULL),
36         m_geometry(NULL)
37 {
38     m_ui.setupUi(this);
39     m_scene = monitor->getEffectScene();
40     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
41     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateProperties()));
42
43     connect(m_monitor->render, SIGNAL(rendererPosition(int)), this, SLOT(slotCheckPosition(int)));
44
45     connect(m_ui.spinX,      SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
46     connect(m_ui.spinY,      SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
47     connect(m_ui.spinWidth,  SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
48     connect(m_ui.spinHeight, SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
49 }
50
51 GeometryWidget::~GeometryWidget()
52 {
53     m_monitor->slotEffectScene(false);
54     m_scene->disconnect(this);
55     delete m_rect;
56     delete m_geometry;
57 }
58
59 QString GeometryWidget::getValue() const
60 {
61     return m_geometry->serialise();
62 }
63
64 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
65 {
66     m_inPoint = minframe;
67     m_outPoint = maxframe;
68     QString value = elem.attribute("value");
69
70     char *tmp = (char *) qstrdup(value.toUtf8().data());
71     if (m_geometry)
72         m_geometry->parse(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
73     else
74         m_geometry = new Mlt::Geometry(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
75     delete[] tmp;
76
77     Mlt::GeometryItem item;
78
79     m_geometry->fetch(&item, 0);
80     delete m_rect;
81     m_rect = new QGraphicsRectItem(QRectF(0, 0, item.w(), item.h()));
82     m_rect->setPos(item.x(), item.y());
83     m_rect->setZValue(0);
84     m_rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
85
86     QPen framepen(Qt::DotLine);
87     framepen.setColor(Qt::yellow);
88     m_rect->setPen(framepen);
89     m_rect->setBrush(Qt::transparent);
90     m_scene->addItem(m_rect);
91
92     slotUpdateProperties();
93     slotCheckPosition(m_monitor->render->seekFramePosition());
94 }
95
96 void GeometryWidget::slotCheckPosition(int renderPos)
97 {
98     if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
99         if (!m_scene->views().at(0)->isVisible())
100             m_monitor->slotEffectScene(true);
101     } else
102         m_monitor->slotEffectScene(false);
103 }
104
105 void GeometryWidget::slotUpdateGeometry()
106 {
107     Mlt::GeometryItem item;
108     m_geometry->next_key(&item, 0);
109
110     QRectF rectSize = m_rect->rect().normalized();
111     QPointF rectPos = m_rect->pos();
112     item.x(rectPos.x());
113     item.y(rectPos.y());
114     item.w(rectSize.width());
115     item.h(rectSize.height());
116     m_geometry->insert(item);
117     emit parameterChanged();
118 }
119
120 void GeometryWidget::slotUpdateProperties()
121 {
122     QRectF rectSize = m_rect->rect().normalized();
123     QPointF rectPos = m_rect->pos();
124
125     m_ui.spinX->blockSignals(true);
126     m_ui.spinY->blockSignals(true);
127     m_ui.spinWidth->blockSignals(true);
128     m_ui.spinHeight->blockSignals(true);
129
130     m_ui.spinX->setValue(rectPos.x());
131     m_ui.spinY->setValue(rectPos.y());
132     m_ui.spinWidth->setValue(rectSize.width());
133     m_ui.spinHeight->setValue(rectSize.height());
134
135     m_ui.spinX->blockSignals(false);
136     m_ui.spinY->blockSignals(false);
137     m_ui.spinWidth->blockSignals(false);
138     m_ui.spinHeight->blockSignals(false);
139 }
140
141 void GeometryWidget::slotSetX(int value)
142 {
143     m_rect->setPos(value, m_ui.spinY->value());
144     slotUpdateGeometry();
145 }
146
147 void GeometryWidget::slotSetY(int value)
148 {
149     m_rect->setPos(m_ui.spinX->value(), value);
150     slotUpdateGeometry();
151 }
152
153 void GeometryWidget::slotSetWidth(int value)
154 {
155     m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
156     slotUpdateGeometry();
157 }
158
159 void GeometryWidget::slotSetHeight(int value)
160 {
161     m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
162     slotUpdateGeometry();
163 }
164
165 #include "geometrywidget.moc"