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