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