]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
* Fix wrong resizing in composite transition when using "Fit to width / height"
[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, 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, 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(), 0, -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(), 0, -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, 2, -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, 0, -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(double)), this, SLOT(slotResize(double)));
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 = qBound(0, relTimelinePos, m_timePos->maximum());
252         if (relTimelinePos != m_timePos->getValue())
253             slotPositionChanged(relTimelinePos, false);
254     }
255 }
256
257
258 void GeometryWidget::slotPositionChanged(int pos, bool seek)
259 {
260     if (pos == -1)
261         pos = m_timePos->getValue();
262     else
263         m_timePos->setValue(pos);
264
265     m_timeline->blockSignals(true);
266     m_timeline->setValue(pos);
267     m_timeline->blockSignals(false);
268
269     Mlt::GeometryItem item;
270     if (m_geometry->fetch(&item, pos) || item.key() == false) {
271         // no keyframe
272         m_rect->setEnabled(false);
273         m_scene->setEnabled(false);
274         m_ui.widgetGeometry->setEnabled(false);
275         m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
276         m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
277     } else {
278         // keyframe
279         m_rect->setEnabled(true);
280         m_scene->setEnabled(true);
281         m_ui.widgetGeometry->setEnabled(true);
282         m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
283         m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
284     }
285
286     m_rect->setPos(item.x(), item.y());
287     m_rect->setRect(0, 0, item.w(), item.h());
288
289     m_opacity->blockSignals(true);
290     m_opacity->setValue(item.mix());
291     m_opacity->blockSignals(false);
292
293     slotUpdateProperties();
294
295     if (seek && KdenliveSettings::transitionfollowcursor())
296         emit seekToPos(m_clipPos + pos);
297 }
298
299 void GeometryWidget::slotKeyframeMoved(int pos)
300 {
301     slotPositionChanged(pos);
302     slotUpdateGeometry();
303     QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
304 }
305
306 void GeometryWidget::slotAddKeyframe(int pos)
307 {
308     Mlt::GeometryItem item;
309     if (pos == -1)
310         pos = m_timePos->getValue();
311     item.frame(pos);
312     QRectF r = m_rect->rect().normalized();
313     QPointF rectpos = m_rect->pos();
314     item.x(rectpos.x());
315     item.y(rectpos.y());
316     item.w(r.width());
317     item.h(r.height());
318     item.mix(m_opacity->value());
319     m_geometry->insert(item);
320
321     m_timeline->update();
322     slotPositionChanged(pos, false);
323     emit parameterChanged();
324 }
325
326 void GeometryWidget::slotDeleteKeyframe(int pos)
327 {
328     Mlt::GeometryItem item;
329     if (pos == -1)
330         pos = m_timePos->getValue();
331     // check there is more than one keyframe, do not allow to delete last one
332     if (m_geometry->next_key(&item, pos + 1)) {
333         if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
334             return;
335     }
336     m_geometry->remove(pos);
337
338     m_timeline->update();
339     slotPositionChanged(pos, false);
340     emit parameterChanged();
341 }
342
343 void GeometryWidget::slotPreviousKeyframe()
344 {
345     Mlt::GeometryItem item;
346     // Go to start if no keyframe is found
347     int currentPos = m_timePos->getValue();
348     int pos = 0;
349     if (!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
350         pos = item.frame();
351
352     slotPositionChanged(pos);
353 }
354
355 void GeometryWidget::slotNextKeyframe()
356 {
357     Mlt::GeometryItem item;
358     // Go to end if no keyframe is found
359     int pos = m_timeline->frameLength;
360     if (!m_geometry->next_key(&item, m_timeline->value() + 1))
361         pos = item.frame();
362
363     slotPositionChanged(pos);
364 }
365
366 void GeometryWidget::slotAddDeleteKeyframe()
367 {
368     Mlt::GeometryItem item;
369     if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
370         slotAddKeyframe();
371     else
372         slotDeleteKeyframe();
373 }
374
375
376 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
377 {
378     if (m_showScene) {
379         /*
380             We do only get the position in timeline if this geometry belongs to a transition,
381             therefore we need two ways here.
382         */
383         if (m_isEffect) {
384             emit checkMonitorPosition(renderPos);
385         } else {
386             if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
387                 if (!m_scene->views().at(0)->isVisible())
388                     m_monitor->slotEffectScene(true);
389             } else {
390                 m_monitor->slotEffectScene(false);
391             }
392         }
393     }
394 }
395
396
397 void GeometryWidget::slotUpdateGeometry()
398 {
399     Mlt::GeometryItem item;
400     int pos = m_timePos->getValue();
401
402     // get keyframe and make sure it is the correct one
403     if (m_geometry->next_key(&item, pos) || item.frame() != pos)
404         return;
405
406     QRectF rectSize = m_rect->rect().normalized();
407     QPointF rectPos = m_rect->pos();
408     item.x(rectPos.x());
409     item.y(rectPos.y());
410     item.w(rectSize.width());
411     item.h(rectSize.height());
412     m_geometry->insert(item);
413     emit parameterChanged();
414 }
415
416 void GeometryWidget::slotUpdateProperties()
417 {
418     QRectF rectSize = m_rect->rect().normalized();
419     QPointF rectPos = m_rect->pos();
420     double size;
421     if (rectSize.width() / m_monitor->render->dar() > rectSize.height())
422         size = rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth();
423     else
424         size = rectSize.height() * 100.0 / m_monitor->render->renderHeight();
425
426     m_spinX->blockSignals(true);
427     m_spinY->blockSignals(true);
428     m_spinWidth->blockSignals(true);
429     m_spinHeight->blockSignals(true);
430     m_spinSize->blockSignals(true);
431
432     m_spinX->setValue(rectPos.x());
433     m_spinY->setValue(rectPos.y());
434     m_spinWidth->setValue(rectSize.width());
435     m_spinHeight->setValue(rectSize.height());
436     m_spinSize->setValue(size);
437
438     m_spinX->blockSignals(false);
439     m_spinY->blockSignals(false);
440     m_spinWidth->blockSignals(false);
441     m_spinHeight->blockSignals(false);
442     m_spinSize->blockSignals(false);
443 }
444
445
446 void GeometryWidget::slotSetX(int value)
447 {
448     m_rect->setPos(value, m_spinY->value());
449     slotUpdateGeometry();
450 }
451
452 void GeometryWidget::slotSetY(int value)
453 {
454     m_rect->setPos(m_spinX->value(), value);
455     slotUpdateGeometry();
456 }
457
458 void GeometryWidget::slotSetWidth(int value)
459 {
460     m_rect->setRect(0, 0, value, m_spinHeight->value());
461     slotUpdateGeometry();
462 }
463
464 void GeometryWidget::slotSetHeight(int value)
465 {
466     m_rect->setRect(0, 0, m_spinWidth->value(), value);
467     slotUpdateGeometry();
468 }
469
470
471 void GeometryWidget::slotResize(double value)
472 {
473     m_rect->setRect(0, 0,
474                     (int)((m_monitor->render->frameRenderWidth() * value / 100.0) + 0.5),
475                     (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
476     slotUpdateGeometry();
477 }
478
479
480 void GeometryWidget::slotSetOpacity(int value)
481 {
482     int pos = m_timePos->getValue();
483     Mlt::GeometryItem item;
484     if (m_geometry->fetch(&item, pos) || item.key() == false)
485         return;
486     item.mix(value);
487     m_geometry->insert(item);
488     emit parameterChanged();
489 }
490
491
492 void GeometryWidget::slotMoveLeft()
493 {
494     m_rect->setPos(0, m_rect->pos().y());
495     slotUpdateGeometry();
496 }
497
498 void GeometryWidget::slotCenterH()
499 {
500     m_rect->setPos((m_monitor->render->frameRenderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
501     slotUpdateGeometry();
502 }
503
504 void GeometryWidget::slotMoveRight()
505 {
506     m_rect->setPos(m_monitor->render->frameRenderWidth() - m_rect->rect().width(), m_rect->pos().y());
507     slotUpdateGeometry();
508 }
509
510 void GeometryWidget::slotMoveTop()
511 {
512     m_rect->setPos(m_rect->pos().x(), 0);
513     slotUpdateGeometry();
514 }
515
516 void GeometryWidget::slotCenterV()
517 {
518     m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
519     slotUpdateGeometry();
520 }
521
522 void GeometryWidget::slotMoveBottom()
523 {
524     m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
525     slotUpdateGeometry();
526 }
527
528
529 void GeometryWidget::slotSetSynchronize(bool sync)
530 {
531     KdenliveSettings::setTransitionfollowcursor(sync);
532     if (sync)
533         emit seekToPos(m_clipPos + m_timePos->getValue());
534 }
535
536 void GeometryWidget::slotShowScene(bool show)
537 {
538     m_showScene = show;
539     if (!m_showScene)
540         m_monitor->slotEffectScene(false);
541     else
542         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
543 }
544
545 void GeometryWidget::setFrameSize(QPoint size)
546 {
547     m_frameSize = size;
548 }
549
550 void GeometryWidget::slotAdjustToFrameSize()
551 {
552     if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
553     m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() + 0.5));
554     m_spinHeight->setValue(m_frameSize.y());
555 }
556
557 void GeometryWidget::slotFitToWidth()
558 {
559     if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
560     double factor = (double) m_monitor->render->frameRenderWidth() / m_frameSize.x() * m_monitor->render->sar();
561     m_spinHeight->setValue((int) (m_frameSize.y() * factor + 0.5));
562     m_spinWidth->setValue(m_monitor->render->frameRenderWidth());
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 = (double) m_monitor->render->renderHeight() / m_frameSize.y();
569     m_spinHeight->setValue(m_monitor->render->renderHeight());
570     m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() * factor + 0.5));
571 }
572
573 #include "geometrywidget.moc"