]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
- Use a frame image from the consumer-frame-show event handler as monitor scene backg...
[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
42     m_ui.buttonMoveLeft->setIcon(KIcon("kdenlive-align-left"));
43     m_ui.buttonMoveLeft->setToolTip(i18n("Move to left"));
44     m_ui.buttonCenterH->setIcon(KIcon("kdenlive-align-hor"));
45     m_ui.buttonCenterH->setToolTip(i18n("Center horizontally"));
46     m_ui.buttonMoveRight->setIcon(KIcon("kdenlive-align-right"));
47     m_ui.buttonMoveRight->setToolTip(i18n("Move to right"));
48     m_ui.buttonMoveTop->setIcon(KIcon("kdenlive-align-top"));
49     m_ui.buttonMoveTop->setToolTip(i18n("Move to top"));
50     m_ui.buttonCenterV->setIcon(KIcon("kdenlive-align-vert"));
51     m_ui.buttonCenterV->setToolTip(i18n("Center vertically"));
52     m_ui.buttonMoveBottom->setIcon(KIcon("kdenlive-align-bottom"));
53     m_ui.buttonMoveBottom->setToolTip(i18n("Move to bottom"));
54
55
56     connect(m_ui.spinX,            SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
57     connect(m_ui.spinY,            SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
58     connect(m_ui.spinWidth,        SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
59     connect(m_ui.spinHeight,       SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
60
61     connect(m_ui.spinSize,         SIGNAL(valueChanged(int)), this, SLOT(slotResize(int)));
62
63     connect(m_ui.buttonMoveLeft,   SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
64     connect(m_ui.buttonCenterH,    SIGNAL(clicked()), this, SLOT(slotCenterH()));
65     connect(m_ui.buttonMoveRight,  SIGNAL(clicked()), this, SLOT(slotMoveRight()));
66     connect(m_ui.buttonMoveTop,    SIGNAL(clicked()), this, SLOT(slotMoveTop()));
67     connect(m_ui.buttonCenterV,    SIGNAL(clicked()), this, SLOT(slotCenterV()));
68     connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));
69
70     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
71     connect(m_monitor->render, SIGNAL(rendererPosition(int)), this, SLOT(slotCheckPosition(int)));
72     connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
73 }
74
75 GeometryWidget::~GeometryWidget()
76 {
77     m_scene->removeItem(m_rect);
78     delete m_geometry;
79     m_monitor->slotEffectScene(false);
80 }
81
82 QString GeometryWidget::getValue() const
83 {
84     return m_geometry->serialise();
85 }
86
87 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
88 {
89     m_inPoint = minframe;
90     m_outPoint = maxframe;
91     QString value = elem.attribute("value");
92
93     char *tmp = (char *) qstrdup(value.toUtf8().data());
94     if (m_geometry)
95         m_geometry->parse(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
96     else
97         m_geometry = new Mlt::Geometry(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
98     delete[] tmp;
99
100     Mlt::GeometryItem item;
101
102     m_geometry->fetch(&item, 0);
103     delete m_rect;
104     m_rect = new QGraphicsRectItem(QRectF(0, 0, item.w(), item.h()));
105     m_rect->setPos(item.x(), item.y());
106     m_rect->setZValue(0);
107     m_rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
108
109     QPen framepen(Qt::DotLine);
110     framepen.setColor(Qt::yellow);
111     m_rect->setPen(framepen);
112     m_rect->setBrush(Qt::transparent);
113     m_scene->addItem(m_rect);
114
115     slotUpdateProperties();
116     slotCheckPosition(m_monitor->render->seekFramePosition());
117 }
118
119 void GeometryWidget::slotCheckPosition(int renderPos)
120 {
121     qDebug() << m_clipPos << m_inPoint << m_outPoint;
122     if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
123         if (!m_scene->views().at(0)->isVisible())
124             m_monitor->slotEffectScene(true);
125     } else {
126         m_monitor->slotEffectScene(false);
127     }
128 }
129
130 void GeometryWidget::slotUpdateGeometry()
131 {
132     Mlt::GeometryItem item;
133     m_geometry->next_key(&item, 0);
134
135     QRectF rectSize = m_rect->rect().normalized();
136     QPointF rectPos = m_rect->pos();
137     item.x(rectPos.x());
138     item.y(rectPos.y());
139     item.w(rectSize.width());
140     item.h(rectSize.height());
141     m_geometry->insert(item);
142     emit parameterChanged();
143 }
144
145 void GeometryWidget::slotUpdateProperties()
146 {
147     QRectF rectSize = m_rect->rect().normalized();
148     QPointF rectPos = m_rect->pos();
149     int size;
150     if (rectSize.width() / m_monitor->render->dar() < rectSize.height())
151         size = (int)(rectSize.width() * 100 / m_monitor->render->renderWidth());
152     else
153         size = (int)(rectSize.height() * 100 / m_monitor->render->renderHeight());
154
155     m_ui.spinX->blockSignals(true);
156     m_ui.spinY->blockSignals(true);
157     m_ui.spinWidth->blockSignals(true);
158     m_ui.spinHeight->blockSignals(true);
159     m_ui.spinSize->blockSignals(true);
160
161     m_ui.spinX->setValue(rectPos.x());
162     m_ui.spinY->setValue(rectPos.y());
163     m_ui.spinWidth->setValue(rectSize.width());
164     m_ui.spinHeight->setValue(rectSize.height());
165     m_ui.spinSize->setValue(size);
166
167     m_ui.spinX->blockSignals(false);
168     m_ui.spinY->blockSignals(false);
169     m_ui.spinWidth->blockSignals(false);
170     m_ui.spinHeight->blockSignals(false);
171     m_ui.spinSize->blockSignals(false);
172 }
173
174
175 void GeometryWidget::slotSetX(int value)
176 {
177     m_rect->setPos(value, m_ui.spinY->value());
178     slotUpdateGeometry();
179 }
180
181 void GeometryWidget::slotSetY(int value)
182 {
183     m_rect->setPos(m_ui.spinX->value(), value);
184     slotUpdateGeometry();
185 }
186
187 void GeometryWidget::slotSetWidth(int value)
188 {
189     m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
190     slotUpdateGeometry();
191 }
192
193 void GeometryWidget::slotSetHeight(int value)
194 {
195     m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
196     slotUpdateGeometry();
197 }
198
199
200 void GeometryWidget::slotResize(int value)
201 {
202     m_rect->setRect(0, 0, m_monitor->render->renderWidth() * value / 100, m_monitor->render->renderHeight() * value / 100);
203     slotUpdateGeometry();
204 }
205
206
207 void GeometryWidget::slotMoveLeft()
208 {
209     m_rect->setPos(0, m_rect->pos().y());
210     slotUpdateGeometry();
211 }
212
213 void GeometryWidget::slotCenterH()
214 {
215     m_rect->setPos((m_monitor->render->renderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
216     slotUpdateGeometry();
217 }
218
219 void GeometryWidget::slotMoveRight()
220 {
221     m_rect->setPos(m_monitor->render->renderWidth() - m_rect->rect().width(), m_rect->pos().y());
222     slotUpdateGeometry();
223 }
224
225 void GeometryWidget::slotMoveTop()
226 {
227     m_rect->setPos(m_rect->pos().x(), 0);
228     slotUpdateGeometry();
229 }
230
231 void GeometryWidget::slotCenterV()
232 {
233     m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
234     slotUpdateGeometry();
235 }
236
237 void GeometryWidget::slotMoveBottom()
238 {
239     m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
240     slotUpdateGeometry();
241 }
242
243 #include "geometrywidget.moc"