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