]> git.sesse.net Git - kdenlive/blob - src/cornerswidget.cpp
Some smaller UI improvements on the settings for on-monitor widgets
[kdenlive] / src / cornerswidget.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 #include "cornerswidget.h"
21 #include "monitor.h"
22 #include "monitorscene.h"
23 #include "monitorscenecontrolwidget.h"
24 #include "onmonitoritems/onmonitorcornersitem.h"
25 #include "renderer.h"
26
27 #include <QGraphicsView>
28 #include <QHBoxLayout>
29
30 #include <KIcon>
31
32 CornersWidget::CornersWidget(Monitor* monitor, int clipPos, bool isEffect, int factor, QWidget* parent) :
33         QWidget(parent),
34         m_monitor(monitor),
35         m_clipPos(clipPos),
36         m_inPoint(0),
37         m_outPoint(1),
38         m_isEffect(isEffect),
39         m_showScene(true),
40         m_factor(factor)
41 {
42     m_ui.setupUi(this);
43
44     m_scene = monitor->getEffectScene();
45
46     m_item = new OnMonitorCornersItem(m_scene);
47     m_scene->addItem(m_item);
48
49     m_config = new MonitorSceneControlWidget(m_scene, m_ui.frameConfig);
50     QHBoxLayout *layout = new QHBoxLayout(m_ui.frameConfig);
51     layout->setContentsMargins(0, 0, 0, 0);
52     layout->addWidget(m_config);
53     QHBoxLayout *layout2 = new QHBoxLayout(m_ui.widgetConfigButton);
54     layout2->setContentsMargins(0, 0, 0, 0);
55     layout2->addWidget(m_config->getShowHideButton());
56
57     int width = m_monitor->render->frameRenderWidth();
58     int height = m_monitor->render->renderHeight();
59
60     m_ui.spinX1->setRange(-width, width * 2);
61     m_ui.spinX2->setRange(-width, width * 2);
62     m_ui.spinX3->setRange(-width, width * 2);
63     m_ui.spinX4->setRange(-width, width * 2);
64     m_ui.spinY1->setRange(-height, height * 2);
65     m_ui.spinY2->setRange(-height, height * 2);
66     m_ui.spinY3->setRange(-height, height * 2);
67     m_ui.spinY4->setRange(-height, height * 2);
68
69     m_ui.toolReset1->setIcon(KIcon("edit-undo"));
70     m_ui.toolReset1->setToolTip(i18n("Reset Corner 1"));
71     m_ui.toolReset2->setIcon(KIcon("edit-undo"));
72     m_ui.toolReset2->setToolTip(i18n("Reset Corner 2"));
73     m_ui.toolReset3->setIcon(KIcon("edit-undo"));
74     m_ui.toolReset3->setToolTip(i18n("Reset Corner 3"));
75     m_ui.toolReset4->setIcon(KIcon("edit-undo"));
76     m_ui.toolReset4->setToolTip(i18n("Reset Corner 4"));
77
78     connect(m_ui.spinX1, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateItem()));
79     connect(m_ui.spinX2, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateItem()));
80     connect(m_ui.spinX3, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateItem()));
81     connect(m_ui.spinX4, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateItem()));
82     connect(m_ui.spinY1, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateItem()));
83     connect(m_ui.spinY2, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateItem()));
84     connect(m_ui.spinY3, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateItem()));
85     connect(m_ui.spinY4, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateItem()));
86
87     connect(m_ui.toolReset1, SIGNAL(clicked()), this, SLOT(slotResetCorner1()));
88     connect(m_ui.toolReset2, SIGNAL(clicked()), this, SLOT(slotResetCorner2()));
89     connect(m_ui.toolReset3, SIGNAL(clicked()), this, SLOT(slotResetCorner3()));
90     connect(m_ui.toolReset4, SIGNAL(clicked()), this, SLOT(slotResetCorner4()));
91
92     connect(m_config, SIGNAL(showScene(bool)), this, SLOT(slotShowScene(bool)));
93     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
94     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateProperties()));
95 }
96
97 CornersWidget::~CornersWidget()
98 {
99     delete m_config;
100     m_scene->removeItem(m_item);
101     delete m_item;
102     if (m_monitor)
103         m_monitor->slotEffectScene(false);
104 }
105
106 void CornersWidget::setRange(int minframe, int maxframe)
107 {
108     m_inPoint = minframe;
109     m_outPoint = maxframe;
110     slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
111 }
112
113 void CornersWidget::slotUpdateItem()
114 {
115     QPointF c1(m_ui.spinX1->value(), m_ui.spinY1->value());
116     QPointF c2(m_ui.spinX2->value(), m_ui.spinY2->value());
117     QPointF c3(m_ui.spinX3->value(), m_ui.spinY3->value());
118     QPointF c4(m_ui.spinX4->value(), m_ui.spinY4->value());
119
120     m_item->setPolygon(QPolygonF() << c1 << c2 << c3 << c4);
121
122     emit parameterChanged();
123 }
124
125 void CornersWidget::slotUpdateProperties(bool changed)
126 {
127     QPolygon pol = m_item->polygon().toPolygon();
128     blockSignals(true);
129     m_ui.spinX1->setValue(pol.at(0).x());
130     m_ui.spinX2->setValue(pol.at(1).x());
131     m_ui.spinX3->setValue(pol.at(2).x());
132     m_ui.spinX4->setValue(pol.at(3).x());
133     m_ui.spinY1->setValue(pol.at(0).y());
134     m_ui.spinY2->setValue(pol.at(1).y());
135     m_ui.spinY3->setValue(pol.at(2).y());
136     m_ui.spinY4->setValue(pol.at(3).y());
137     blockSignals(false);
138
139     if (changed)
140         emit parameterChanged();
141 }
142
143
144 QPolygon CornersWidget::getValue()
145 {
146     qreal width = m_monitor->render->frameRenderWidth();
147     qreal height = m_monitor->render->renderHeight();
148     QPolygon corners = m_item->polygon().toPolygon();
149     QPolygon points;
150     QPoint p;
151     for (int i = 0; i < 4; ++i) {
152         p = corners.at(i);
153         p.setX((p.x() / width + 1) / 3.0 * m_factor);
154         p.setY((p.y() / height + 1) / 3.0 * m_factor);
155         points << p;
156     }
157     return points;
158 }
159
160 void CornersWidget::setValue(const QPolygon& points)
161 {
162     int width = m_monitor->render->frameRenderWidth();
163     int height = m_monitor->render->renderHeight();
164     QPolygonF corners;
165     QPoint p;
166     for (int i = 0; i < 4; ++i) {
167         p = points.at(i);
168         p.setX((p.x() / (qreal)m_factor * 3 - 1) * width);
169         p.setY((p.y() / (qreal)m_factor * 3 - 1) * height);
170         corners << p;
171     }
172     m_item->setPolygon(corners);
173
174     slotUpdateProperties(false);
175 }
176
177 void CornersWidget::slotCheckMonitorPosition(int renderPos)
178 {
179     if (m_showScene) {
180         /*
181            We do only get the position in timeline if this geometry belongs to a transition,
182            therefore we need two ways here.
183          */
184         if (m_isEffect) {
185             emit checkMonitorPosition(renderPos);
186         } else {
187             if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
188                 if (!m_scene->views().at(0)->isVisible())
189                     m_monitor->slotEffectScene(true);
190             } else {
191                 m_monitor->slotEffectScene(false);
192             }
193         }
194     }
195 }
196
197 void CornersWidget::slotShowScene(bool show)
198 {
199     m_showScene = show;
200     if (!m_showScene)
201         m_monitor->slotEffectScene(false);
202     else
203         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
204 }
205
206 void CornersWidget::slotResetCorner1()
207 {
208     blockSignals(true);
209     m_ui.spinX1->setValue(0);
210     m_ui.spinY1->setValue(0);
211     blockSignals(false);
212     slotUpdateItem();
213 }
214
215 void CornersWidget::slotResetCorner2()
216 {
217     blockSignals(true);
218     m_ui.spinX2->setValue(m_monitor->render->frameRenderWidth());
219     m_ui.spinY2->setValue(0);
220     blockSignals(false);
221     slotUpdateItem();
222 }
223
224 void CornersWidget::slotResetCorner3()
225 {
226     blockSignals(true);
227     m_ui.spinX3->setValue(m_monitor->render->frameRenderWidth());
228     m_ui.spinY3->setValue(m_monitor->render->renderHeight());
229     blockSignals(false);
230     slotUpdateItem();
231 }
232
233 void CornersWidget::slotResetCorner4()
234 {
235     blockSignals(true);
236     m_ui.spinX4->setValue(0);
237     m_ui.spinY4->setValue(m_monitor->render->renderHeight());
238     blockSignals(false);
239     slotUpdateItem();
240 }
241
242 #include "cornerswidget.moc"