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