]> 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 <QtCore>
27 #include <QGraphicsView>
28 #include <QGraphicsRectItem>
29
30 GeometryWidget::GeometryWidget(Monitor* monitor, int clipPos, QWidget* parent ):
31         QWidget(parent),
32         m_monitor(monitor),
33         m_clipPos(clipPos),
34         m_inPoint(0),
35         m_outPoint(1),
36         m_rect(NULL),
37         m_geometry(NULL)
38 {
39     m_ui.setupUi(this);
40     m_scene = monitor->getEffectScene();
41     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
42     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateProperties()));
43
44     connect(m_monitor->render, SIGNAL(rendererPosition(int)), this, SLOT(slotCheckPosition(int)));
45
46     connect(m_ui.spinX,      SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
47     connect(m_ui.spinY,      SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
48     connect(m_ui.spinWidth,  SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
49     connect(m_ui.spinHeight, SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
50 }
51
52 GeometryWidget::~GeometryWidget()
53 {
54     if (m_monitor)
55         m_monitor->slotEffectScene(false);
56     delete m_rect;
57     delete m_geometry;
58 }
59
60 QString GeometryWidget::getValue() const
61 {
62     return m_geometry->serialise();
63 }
64
65 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
66 {
67     m_inPoint = minframe;
68     m_outPoint = maxframe;
69     QString value = elem.attribute("value");
70
71     char *tmp = (char *) qstrdup(value.toUtf8().data());
72     if (m_geometry)
73         m_geometry->parse(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
74     else
75         m_geometry = new Mlt::Geometry(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
76     delete[] tmp;
77
78     Mlt::GeometryItem item;
79
80     m_geometry->fetch(&item, 0);
81     delete m_rect;
82     m_rect = new QGraphicsRectItem(QRectF(0, 0, item.w(), item.h()));
83     m_rect->setPos(item.x(), item.y());
84     m_rect->setZValue(0);
85     m_rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
86
87     QPen framepen(Qt::DotLine);
88     framepen.setColor(Qt::yellow);
89     m_rect->setPen(framepen);
90     m_rect->setBrush(Qt::transparent);
91     m_scene->addItem(m_rect);
92
93     slotUpdateProperties();
94     slotCheckPosition(m_monitor->render->seekFramePosition());
95 }
96
97 void GeometryWidget::slotCheckPosition(int renderPos)
98 {
99     if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
100         if (!m_scene->views().at(0)->isVisible())
101             m_monitor->slotEffectScene(true);
102     } else
103         m_monitor->slotEffectScene(false);
104 }
105
106 void GeometryWidget::slotUpdateGeometry()
107 {
108     Mlt::GeometryItem item;
109     m_geometry->next_key(&item, 0);
110
111     QRectF rectSize = m_rect->rect().normalized();
112     QPointF rectPos = m_rect->pos();
113     item.x(rectPos.x());
114     item.y(rectPos.y());
115     item.w(rectSize.width());
116     item.h(rectSize.height());
117     m_geometry->insert(item);
118     emit parameterChanged();
119 }
120
121 void GeometryWidget::slotUpdateProperties()
122 {
123     QRectF rectSize = m_rect->rect().normalized();
124     QPointF rectPos = m_rect->pos();
125
126     m_ui.spinX->blockSignals(true);
127     m_ui.spinY->blockSignals(true);
128     m_ui.spinWidth->blockSignals(true);
129     m_ui.spinHeight->blockSignals(true);
130
131     m_ui.spinX->setValue(rectPos.x());
132     m_ui.spinY->setValue(rectPos.y());
133     m_ui.spinWidth->setValue(rectSize.width());
134     m_ui.spinHeight->setValue(rectSize.height());
135
136     m_ui.spinX->blockSignals(false);
137     m_ui.spinY->blockSignals(false);
138     m_ui.spinWidth->blockSignals(false);
139     m_ui.spinHeight->blockSignals(false);
140 }
141
142 void GeometryWidget::slotSetX(int value)
143 {
144     m_rect->setPos(value, m_ui.spinY->value());
145     slotUpdateGeometry();
146 }
147
148 void GeometryWidget::slotSetY(int value)
149 {
150     m_rect->setPos(m_ui.spinX->value(), value);
151     slotUpdateGeometry();
152 }
153
154 void GeometryWidget::slotSetWidth(int value)
155 {
156     m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
157     slotUpdateGeometry();
158 }
159
160 void GeometryWidget::slotSetHeight(int value)
161 {
162     m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
163     slotUpdateGeometry();
164 }
165
166 #include "geometrywidget.moc"