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