]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
Add on-monitor GUI for c0rners effect. (Work in progress)
[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 "keyframehelper.h"
25 #include "timecodedisplay.h"
26 #include "monitorscene.h"
27 #include "monitorscenecontrolwidget.h"
28 #include "onmonitoritems/onmonitorrectitem.h"
29 #include "kdenlivesettings.h"
30
31 #include <QtCore>
32 #include <QGraphicsView>
33 #include <QVBoxLayout>
34 #include <QGridLayout>
35
36
37
38 GeometryWidget::GeometryWidget(Monitor* monitor, Timecode timecode, int clipPos, bool isEffect, QWidget* parent):
39         QWidget(parent),
40         m_monitor(monitor),
41         m_timePos(new TimecodeDisplay(timecode)),
42         m_clipPos(clipPos),
43         m_inPoint(0),
44         m_outPoint(1),
45         m_isEffect(isEffect),
46         m_rect(NULL),
47         m_geometry(NULL),
48         m_showScene(true)
49 {
50     m_ui.setupUi(this);
51     m_scene = monitor->getEffectScene();
52
53
54     /*
55         Setup of timeline and keyframe controls
56     */
57
58     ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 6);
59
60     QVBoxLayout *layout = new QVBoxLayout(m_ui.frameTimeline);
61     m_timeline = new KeyframeHelper(m_ui.frameTimeline);
62     layout->addWidget(m_timeline);
63     layout->setContentsMargins(0, 0, 0, 0);
64
65     m_ui.buttonPrevious->setIcon(KIcon("media-skip-backward"));
66     m_ui.buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
67     m_ui.buttonNext->setIcon(KIcon("media-skip-forward"));
68     m_ui.buttonNext->setToolTip(i18n("Go to next keyframe"));
69     m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
70     m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
71
72     m_ui.buttonSync->setIcon(KIcon("insert-link"));
73     m_ui.buttonSync->setToolTip(i18n("Synchronize with timeline cursor"));
74     m_ui.buttonSync->setChecked(KdenliveSettings::transitionfollowcursor());
75
76     connect(m_timeline, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
77     connect(m_timeline, SIGNAL(keyframeMoved(int)),   this, SLOT(slotKeyframeMoved(int)));
78     connect(m_timeline, SIGNAL(addKeyframe(int)),     this, SLOT(slotAddKeyframe(int)));
79     connect(m_timeline, SIGNAL(removeKeyframe(int)),  this, SLOT(slotDeleteKeyframe(int)));
80     connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotPositionChanged()));
81     connect(m_ui.buttonPrevious,  SIGNAL(clicked()), this, SLOT(slotPreviousKeyframe()));
82     connect(m_ui.buttonNext,      SIGNAL(clicked()), this, SLOT(slotNextKeyframe()));
83     connect(m_ui.buttonAddDelete, SIGNAL(clicked()), this, SLOT(slotAddDeleteKeyframe()));
84     connect(m_ui.buttonSync,      SIGNAL(toggled(bool)), this, SLOT(slotSetSynchronize(bool)));
85
86
87     /*
88         Setup of geometry controls
89     */
90
91     m_ui.buttonMoveLeft->setIcon(KIcon("kdenlive-align-left"));
92     m_ui.buttonMoveLeft->setToolTip(i18n("Move to left"));
93     m_ui.buttonCenterH->setIcon(KIcon("kdenlive-align-hor"));
94     m_ui.buttonCenterH->setToolTip(i18n("Center horizontally"));
95     m_ui.buttonMoveRight->setIcon(KIcon("kdenlive-align-right"));
96     m_ui.buttonMoveRight->setToolTip(i18n("Move to right"));
97     m_ui.buttonMoveTop->setIcon(KIcon("kdenlive-align-top"));
98     m_ui.buttonMoveTop->setToolTip(i18n("Move to top"));
99     m_ui.buttonCenterV->setIcon(KIcon("kdenlive-align-vert"));
100     m_ui.buttonCenterV->setToolTip(i18n("Center vertically"));
101     m_ui.buttonMoveBottom->setIcon(KIcon("kdenlive-align-bottom"));
102     m_ui.buttonMoveBottom->setToolTip(i18n("Move to bottom"));
103
104     connect(m_ui.spinX,            SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
105     connect(m_ui.spinY,            SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
106     connect(m_ui.spinWidth,        SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
107     connect(m_ui.spinHeight,       SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
108
109     connect(m_ui.spinSize,         SIGNAL(valueChanged(int)), this, SLOT(slotResize(int)));
110
111     connect(m_ui.spinOpacity,      SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
112     connect(m_ui.sliderOpacity,    SIGNAL(valueChanged(int)), m_ui.spinOpacity, SLOT(setValue(int)));
113
114     connect(m_ui.buttonMoveLeft,   SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
115     connect(m_ui.buttonCenterH,    SIGNAL(clicked()), this, SLOT(slotCenterH()));
116     connect(m_ui.buttonMoveRight,  SIGNAL(clicked()), this, SLOT(slotMoveRight()));
117     connect(m_ui.buttonMoveTop,    SIGNAL(clicked()), this, SLOT(slotMoveTop()));
118     connect(m_ui.buttonCenterV,    SIGNAL(clicked()), this, SLOT(slotCenterV()));
119     connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));
120
121
122     /*
123         Setup of configuration controls
124     */
125
126     m_config = new MonitorSceneControlWidget(m_scene, m_ui.frameSettings);
127     QHBoxLayout *settingsLayout = new QHBoxLayout(m_ui.frameSettings);
128     settingsLayout->addWidget(m_config);
129     settingsLayout->setContentsMargins(0, 0, 0, 0);
130     ((QGridLayout *)m_ui.widgetFirstColumn->layout())->addWidget(m_config->getShowHideButton(), 1, 2);
131     connect(m_config, SIGNAL(showScene(bool)), this, SLOT(slotShowScene(bool)));
132
133
134
135     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
136     connect(m_scene, SIGNAL(addKeyframe()),    this, SLOT(slotAddKeyframe()));
137     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
138     connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
139 }
140
141 GeometryWidget::~GeometryWidget()
142 {
143     m_scene->setEnabled(true);
144     delete m_timePos;
145     delete m_timeline;
146     m_scene->removeItem(m_rect);
147     delete m_geometry;
148     delete m_config;
149     if (m_monitor)
150         m_monitor->slotEffectScene(false);
151 }
152
153 void GeometryWidget::updateTimecodeFormat()
154 {
155     m_timePos->slotUpdateTimeCodeFormat();
156 }
157
158 QString GeometryWidget::getValue() const
159 {
160     return m_geometry->serialise();
161 }
162
163 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
164 {
165     m_inPoint = minframe;
166     m_outPoint = maxframe;
167
168     if (m_geometry)
169         m_geometry->parse(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
170     else
171         m_geometry = new Mlt::Geometry(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
172
173     if (elem.attribute("fixed") == "1" || maxframe < minframe) {
174         // Keyframes are disabled
175         m_ui.widgetTimeWrapper->setHidden(true);
176     } else {
177         m_ui.widgetTimeWrapper->setHidden(false);
178         m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint - 1);
179         m_timeline->update();
180         m_timePos->setRange(0, m_outPoint - m_inPoint - 1);
181     }
182
183     // no opacity
184     if (elem.attribute("opacity") == "false")
185         m_ui.widgetOpacity->setHidden(true);
186
187     Mlt::GeometryItem item;
188
189     m_geometry->fetch(&item, 0);
190     delete m_rect;
191     m_rect = new OnMonitorRectItem(QRectF(0, 0, item.w(), item.h()), m_scene);
192     m_rect->setPos(item.x(), item.y());
193     m_rect->setZValue(0);
194     m_scene->addItem(m_rect);
195
196     slotPositionChanged(0, false);
197     slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
198 }
199
200 void GeometryWidget::slotSyncPosition(int relTimelinePos)
201 {
202     // do only sync if this effect is keyframable
203     if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
204         relTimelinePos = qMax(0, relTimelinePos);
205         relTimelinePos = qMin(relTimelinePos, m_timePos->maximum());
206         if (relTimelinePos != m_timePos->getValue())
207             slotPositionChanged(relTimelinePos, false);
208     }
209 }
210
211
212 void GeometryWidget::slotPositionChanged(int pos, bool seek)
213 {
214     if (pos == -1)
215         pos = m_timePos->getValue();
216     else
217         m_timePos->setValue(pos);
218
219     m_timeline->blockSignals(true);
220     m_timeline->setValue(pos);
221     m_timeline->blockSignals(false);
222
223     Mlt::GeometryItem item;
224     if (m_geometry->fetch(&item, pos) || item.key() == false) {
225         // no keyframe
226         m_rect->setEnabled(false);
227         m_scene->setEnabled(false);
228         m_ui.widgetGeometry->setEnabled(false);
229         m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
230         m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
231     } else {
232         // keyframe
233         m_rect->setEnabled(true);
234         m_scene->setEnabled(true);
235         m_ui.widgetGeometry->setEnabled(true);
236         m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
237         m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
238     }
239
240     m_rect->setPos(item.x(), item.y());
241     m_rect->setRect(0, 0, item.w(), item.h());
242
243     m_ui.spinOpacity->blockSignals(true);
244     m_ui.sliderOpacity->blockSignals(true);
245     m_ui.spinOpacity->setValue(item.mix());
246     m_ui.sliderOpacity->setValue(item.mix());
247     m_ui.spinOpacity->blockSignals(false);
248     m_ui.sliderOpacity->blockSignals(false);
249
250     slotUpdateProperties();
251
252     if (seek && KdenliveSettings::transitionfollowcursor())
253         emit seekToPos(m_clipPos + pos);
254 }
255
256 void GeometryWidget::slotKeyframeMoved(int pos)
257 {
258     slotPositionChanged(pos);
259     slotUpdateGeometry();
260     QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
261 }
262
263 void GeometryWidget::slotAddKeyframe(int pos)
264 {
265     Mlt::GeometryItem item;
266     if (pos == -1)
267         pos = m_timePos->getValue();
268     item.frame(pos);
269     QRectF r = m_rect->rect().normalized();
270     QPointF rectpos = m_rect->pos();
271     item.x(rectpos.x());
272     item.y(rectpos.y());
273     item.w(r.width());
274     item.h(r.height());
275     item.mix(m_ui.spinOpacity->value());
276     m_geometry->insert(item);
277
278     m_timeline->update();
279     slotPositionChanged(pos, false);
280     emit parameterChanged();
281 }
282
283 void GeometryWidget::slotDeleteKeyframe(int pos)
284 {
285     Mlt::GeometryItem item;
286     if (pos == -1)
287         pos = m_timePos->getValue();
288     // check there is more than one keyframe, do not allow to delete last one
289     if (m_geometry->next_key(&item, pos + 1)) {
290         if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
291             return;
292     }
293     m_geometry->remove(pos);
294
295     m_timeline->update();
296     slotPositionChanged(pos, false);
297     emit parameterChanged();
298 }
299
300 void GeometryWidget::slotPreviousKeyframe()
301 {
302     Mlt::GeometryItem item;
303     // Go to start if no keyframe is found
304     int currentPos = m_timePos->getValue();
305     int pos = 0;
306     if (!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
307         pos = item.frame();
308
309     slotPositionChanged(pos);
310 }
311
312 void GeometryWidget::slotNextKeyframe()
313 {
314     Mlt::GeometryItem item;
315     // Go to end if no keyframe is found
316     int pos = m_timeline->frameLength;
317     if (!m_geometry->next_key(&item, m_timeline->value() + 1))
318         pos = item.frame();
319
320     slotPositionChanged(pos);
321 }
322
323 void GeometryWidget::slotAddDeleteKeyframe()
324 {
325     Mlt::GeometryItem item;
326     if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
327         slotAddKeyframe();
328     else
329         slotDeleteKeyframe();
330 }
331
332
333 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
334 {
335     if (m_showScene) {
336         /*
337             We do only get the position in timeline if this geometry belongs to a transition,
338             therefore we need two ways here.
339         */
340         if (m_isEffect) {
341             emit checkMonitorPosition(renderPos);
342         } else {
343             if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
344                 if (!m_scene->views().at(0)->isVisible())
345                     m_monitor->slotEffectScene(true);
346             } else {
347                 m_monitor->slotEffectScene(false);
348             }
349         }
350     }
351 }
352
353
354 void GeometryWidget::slotUpdateGeometry()
355 {
356     Mlt::GeometryItem item;
357     int pos = m_timePos->getValue();
358
359     // get keyframe and make sure it is the correct one
360     if (m_geometry->next_key(&item, pos) || item.frame() != pos)
361         return;
362
363     QRectF rectSize = m_rect->rect().normalized();
364     QPointF rectPos = m_rect->pos();
365     item.x(rectPos.x());
366     item.y(rectPos.y());
367     item.w(rectSize.width());
368     item.h(rectSize.height());
369     m_geometry->insert(item);
370     emit parameterChanged();
371 }
372
373 void GeometryWidget::slotUpdateProperties()
374 {
375     QRectF rectSize = m_rect->rect().normalized();
376     QPointF rectPos = m_rect->pos();
377     int size;
378     if (rectSize.width() / m_monitor->render->dar() < rectSize.height())
379         size = (int)((rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth()) + 0.5);
380     else
381         size = (int)((rectSize.height() * 100.0 / m_monitor->render->renderHeight()) + 0.5);
382
383     m_ui.spinX->blockSignals(true);
384     m_ui.spinY->blockSignals(true);
385     m_ui.spinWidth->blockSignals(true);
386     m_ui.spinHeight->blockSignals(true);
387     m_ui.spinSize->blockSignals(true);
388
389     m_ui.spinX->setValue(rectPos.x());
390     m_ui.spinY->setValue(rectPos.y());
391     m_ui.spinWidth->setValue(rectSize.width());
392     m_ui.spinHeight->setValue(rectSize.height());
393     m_ui.spinSize->setValue(size);
394
395     m_ui.spinX->blockSignals(false);
396     m_ui.spinY->blockSignals(false);
397     m_ui.spinWidth->blockSignals(false);
398     m_ui.spinHeight->blockSignals(false);
399     m_ui.spinSize->blockSignals(false);
400 }
401
402
403 void GeometryWidget::slotSetX(int value)
404 {
405     m_rect->setPos(value, m_ui.spinY->value());
406     slotUpdateGeometry();
407 }
408
409 void GeometryWidget::slotSetY(int value)
410 {
411     m_rect->setPos(m_ui.spinX->value(), value);
412     slotUpdateGeometry();
413 }
414
415 void GeometryWidget::slotSetWidth(int value)
416 {
417     m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
418     slotUpdateGeometry();
419 }
420
421 void GeometryWidget::slotSetHeight(int value)
422 {
423     m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
424     slotUpdateGeometry();
425 }
426
427
428 void GeometryWidget::slotResize(int value)
429 {
430     m_rect->setRect(0, 0,
431                     (int)((m_monitor->render->frameRenderWidth() * value / 100.0) + 0.5),
432                     (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
433     slotUpdateGeometry();
434 }
435
436
437 void GeometryWidget::slotSetOpacity(int value)
438 {
439     m_ui.sliderOpacity->blockSignals(true);
440     m_ui.sliderOpacity->setValue(value);
441     m_ui.sliderOpacity->blockSignals(false);
442
443     int pos = m_timePos->getValue();
444     Mlt::GeometryItem item;
445     if (m_geometry->fetch(&item, pos) || item.key() == false)
446         return;
447     item.mix(value);
448     m_geometry->insert(item);
449
450     emit parameterChanged();
451 }
452
453
454 void GeometryWidget::slotMoveLeft()
455 {
456     m_rect->setPos(0, m_rect->pos().y());
457     slotUpdateGeometry();
458 }
459
460 void GeometryWidget::slotCenterH()
461 {
462     m_rect->setPos((m_monitor->render->frameRenderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
463     slotUpdateGeometry();
464 }
465
466 void GeometryWidget::slotMoveRight()
467 {
468     m_rect->setPos(m_monitor->render->frameRenderWidth() - m_rect->rect().width(), m_rect->pos().y());
469     slotUpdateGeometry();
470 }
471
472 void GeometryWidget::slotMoveTop()
473 {
474     m_rect->setPos(m_rect->pos().x(), 0);
475     slotUpdateGeometry();
476 }
477
478 void GeometryWidget::slotCenterV()
479 {
480     m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
481     slotUpdateGeometry();
482 }
483
484 void GeometryWidget::slotMoveBottom()
485 {
486     m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
487     slotUpdateGeometry();
488 }
489
490
491 void GeometryWidget::slotSetSynchronize(bool sync)
492 {
493     KdenliveSettings::setTransitionfollowcursor(sync);
494     if (sync)
495         emit seekToPos(m_clipPos + m_timePos->getValue());
496 }
497
498 void GeometryWidget::slotShowScene(bool show)
499 {
500     m_showScene = show;
501     if (!m_showScene)
502         m_monitor->slotEffectScene(false);
503     else
504         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
505 }
506
507 #include "geometrywidget.moc"