]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
Cleanup effect stack (part 2) - show monitor scene when required
[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, bool showRotation, 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_previous(NULL),
50     m_geometry(NULL),
51     m_showScene(true),
52     m_showRotation(showRotation)
53 {
54     m_ui.setupUi(this);
55     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
56     MonitorEditWidget *edit = monitor->getEffectEdit();
57     edit->removeCustomControls();
58     edit->addCustomButton(KIcon("transform-crop"), i18n("Show previous keyframe"), this, SLOT(slotShowPreviousKeyFrame(bool)), true, KdenliveSettings::onmonitoreffects_geometryshowprevious());
59     edit->showVisibilityButton(true);
60     m_scene = edit->getScene();
61
62
63     /*
64         Setup of timeline and keyframe controls
65     */
66
67     ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 6);
68
69     QVBoxLayout *layout = new QVBoxLayout(m_ui.frameTimeline);
70     m_timeline = new KeyframeHelper(m_ui.frameTimeline);
71     layout->addWidget(m_timeline);
72     layout->setContentsMargins(0, 0, 0, 0);
73     
74     int size = style()->pixelMetric(QStyle::PM_SmallIconSize);
75     QSize iconSize(size, size);
76
77     m_ui.buttonPrevious->setIcon(KIcon("media-skip-backward"));
78     m_ui.buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
79     m_ui.buttonPrevious->setIconSize(iconSize);
80     m_ui.buttonNext->setIcon(KIcon("media-skip-forward"));
81     m_ui.buttonNext->setToolTip(i18n("Go to next keyframe"));
82     m_ui.buttonNext->setIconSize(iconSize);
83     m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
84     m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
85     m_ui.buttonAddDelete->setIconSize(iconSize);
86
87     m_ui.buttonSync->setIcon(KIcon("insert-link"));
88     m_ui.buttonSync->setToolTip(i18n("Synchronize with timeline cursor"));
89     m_ui.buttonSync->setChecked(KdenliveSettings::transitionfollowcursor());
90     m_ui.buttonSync->setIconSize(iconSize);
91
92     connect(m_timeline, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
93     connect(m_timeline, SIGNAL(keyframeMoved(int)),   this, SLOT(slotKeyframeMoved(int)));
94     connect(m_timeline, SIGNAL(addKeyframe(int)),     this, SLOT(slotAddKeyframe(int)));
95     connect(m_timeline, SIGNAL(removeKeyframe(int)),  this, SLOT(slotDeleteKeyframe(int)));
96     connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotPositionChanged()));
97     connect(m_ui.buttonPrevious,  SIGNAL(clicked()), this, SLOT(slotPreviousKeyframe()));
98     connect(m_ui.buttonNext,      SIGNAL(clicked()), this, SLOT(slotNextKeyframe()));
99     connect(m_ui.buttonAddDelete, SIGNAL(clicked()), this, SLOT(slotAddDeleteKeyframe()));
100     connect(m_ui.buttonSync,      SIGNAL(toggled(bool)), this, SLOT(slotSetSynchronize(bool)));
101
102     m_spinX = new DragValue(i18nc("x axis position", "X"), 0, 0, -99000, 99000, -1, QString(), false, this);
103     m_ui.horizontalLayout->addWidget(m_spinX, 0, 0);
104     
105     m_spinY = new DragValue(i18nc("y axis position", "Y"), 0, 0, -99000, 99000, -1, QString(), false, this);
106     m_ui.horizontalLayout->addWidget(m_spinY, 0, 1);
107     
108     m_spinWidth = new DragValue(i18nc("Frame width", "W"), m_monitor->render->frameRenderWidth(), 0, 1, 99000, -1, QString(), false, this);
109     m_ui.horizontalLayout->addWidget(m_spinWidth, 0, 2);
110     
111     m_spinHeight = new DragValue(i18nc("Frame height", "H"), m_monitor->render->renderHeight(), 0, 1, 99000, -1, QString(), false, this);
112     m_ui.horizontalLayout->addWidget(m_spinHeight, 0, 3);
113
114     QMenu *menu = new QMenu(this);
115     QAction *adjustSize = new QAction(i18n("Adjust to original size"), this);
116     connect(adjustSize, SIGNAL(triggered()), this, SLOT(slotAdjustToFrameSize()));
117     menu->addAction(adjustSize);
118     QAction *fitToWidth = new QAction(i18n("Fit to width"), this);
119     connect(fitToWidth, SIGNAL(triggered()), this, SLOT(slotFitToWidth()));
120     menu->addAction(fitToWidth);
121     QAction *fitToHeight = new QAction(i18n("Fit to height"), this);
122     connect(fitToHeight, SIGNAL(triggered()), this, SLOT(slotFitToHeight()));
123     menu->addAction(fitToHeight);
124     menu->addSeparator();
125
126     QAction *alignleft = new QAction(KIcon("kdenlive-align-left"), i18n("Align left"), this);
127     connect(alignleft, SIGNAL(triggered()), this, SLOT(slotMoveLeft()));
128     menu->addAction(alignleft);
129     QAction *alignhcenter = new QAction(KIcon("kdenlive-align-hor"), i18n("Center horizontally"), this);
130     connect(alignhcenter, SIGNAL(triggered()), this, SLOT(slotCenterH()));
131     menu->addAction(alignhcenter);
132     QAction *alignright = new QAction(KIcon("kdenlive-align-right"), i18n("Align right"), this);
133     connect(alignright, SIGNAL(triggered()), this, SLOT(slotMoveRight()));
134     menu->addAction(alignright);
135     QAction *aligntop = new QAction(KIcon("kdenlive-align-top"), i18n("Align top"), this);
136     connect(aligntop, SIGNAL(triggered()), this, SLOT(slotMoveTop()));
137     menu->addAction(aligntop);
138     QAction *alignvcenter = new QAction(KIcon("kdenlive-align-vert"), i18n("Center vertically"), this);
139     connect(alignvcenter, SIGNAL(triggered()), this, SLOT(slotCenterV()));
140     menu->addAction(alignvcenter);
141     QAction *alignbottom = new QAction(KIcon("kdenlive-align-bottom"), i18n("Align bottom"), this);
142     connect(alignbottom, SIGNAL(triggered()), this, SLOT(slotMoveBottom()));
143     menu->addAction(alignbottom);
144     m_ui.buttonOptions->setMenu(menu);
145
146     QHBoxLayout *alignLayout = new QHBoxLayout;
147     alignLayout->setSpacing(0);
148     QToolButton *alignButton = new QToolButton;
149     alignButton->setDefaultAction(alignleft);
150     alignButton->setAutoRaise(true);
151     alignLayout->addWidget(alignButton);
152
153     alignButton = new QToolButton;
154     alignButton->setDefaultAction(alignhcenter);
155     alignButton->setAutoRaise(true);
156     alignLayout->addWidget(alignButton);
157
158     alignButton = new QToolButton;
159     alignButton->setDefaultAction(alignright);
160     alignButton->setAutoRaise(true);
161     alignLayout->addWidget(alignButton);
162
163     alignButton = new QToolButton;
164     alignButton->setDefaultAction(aligntop);
165     alignButton->setAutoRaise(true);
166     alignLayout->addWidget(alignButton);
167
168     alignButton = new QToolButton;
169     alignButton->setDefaultAction(alignvcenter);
170     alignButton->setAutoRaise(true);
171     alignLayout->addWidget(alignButton);
172
173     alignButton = new QToolButton;
174     alignButton->setDefaultAction(alignbottom);
175     alignButton->setAutoRaise(true);
176     alignLayout->addWidget(alignButton);
177     alignLayout->addStretch(10);
178
179     m_ui.horizontalLayout->addLayout(alignLayout, 1, 0, 1, 4);
180     //m_ui.horizontalLayout->addStretch(10);
181     
182     m_spinSize = new DragValue(i18n("Size"), 100, 2, 1, 99000, -1, i18n("%"), false, this);
183     m_ui.horizontalLayout2->addWidget(m_spinSize);
184     
185     m_opacity = new DragValue(i18n("Opacity"), 100, 0, 0, 100, -1, i18n("%"), true, this);
186     m_ui.horizontalLayout2->addWidget(m_opacity);
187
188
189     if (showRotation) {
190         m_rotateX = new DragValue(i18n("Rotate X"), 0, 0, -1800, 1800, -1, QString(), true, this);
191         m_rotateX->setObjectName("rotate_x");
192         m_ui.horizontalLayout3->addWidget(m_rotateX);
193         m_rotateY = new DragValue(i18n("Rotate Y"), 0, 0, -1800, 1800,  -1, QString(), true, this);
194         m_rotateY->setObjectName("rotate_y");
195         m_ui.horizontalLayout3->addWidget(m_rotateY);
196         m_rotateZ = new DragValue(i18n("Rotate Z"), 0, 0, -1800, 1800,  -1, QString(), true, this);
197         m_rotateZ->setObjectName("rotate_z");
198         m_ui.horizontalLayout3->addWidget(m_rotateZ);
199         connect(m_rotateX,            SIGNAL(valueChanged(double)), this, SLOT(slotUpdateGeometry()));
200         connect(m_rotateY,            SIGNAL(valueChanged(double)), this, SLOT(slotUpdateGeometry()));
201         connect(m_rotateZ,            SIGNAL(valueChanged(double)), this, SLOT(slotUpdateGeometry()));
202     }
203     
204     /*
205         Setup of geometry controls
206     */
207
208     connect(m_spinX,            SIGNAL(valueChanged(double)), this, SLOT(slotSetX(double)));
209     connect(m_spinY,            SIGNAL(valueChanged(double)), this, SLOT(slotSetY(double)));
210     connect(m_spinWidth,        SIGNAL(valueChanged(double)), this, SLOT(slotSetWidth(double)));
211     connect(m_spinHeight,       SIGNAL(valueChanged(double)), this, SLOT(slotSetHeight(double)));
212
213     connect(m_spinSize,         SIGNAL(valueChanged(double)), this, SLOT(slotResize(double)));
214
215     connect(m_opacity, SIGNAL(valueChanged(double)), this, SLOT(slotSetOpacity(double)));
216     
217     /*connect(m_ui.buttonMoveLeft,   SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
218     connect(m_ui.buttonCenterH,    SIGNAL(clicked()), this, SLOT(slotCenterH()));
219     connect(m_ui.buttonMoveRight,  SIGNAL(clicked()), this, SLOT(slotMoveRight()));
220     connect(m_ui.buttonMoveTop,    SIGNAL(clicked()), this, SLOT(slotMoveTop()));
221     connect(m_ui.buttonCenterV,    SIGNAL(clicked()), this, SLOT(slotCenterV()));
222     connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));*/
223
224
225     /*
226         Setup of configuration controls
227     */
228
229     connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
230
231     connect(m_scene, SIGNAL(addKeyframe()),    this, SLOT(slotAddKeyframe()));
232     connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
233 }
234
235 GeometryWidget::~GeometryWidget()
236 {
237     m_scene->setEnabled(true);
238     delete m_timePos;
239     delete m_timeline;
240     delete m_spinX;
241     delete m_spinY;
242     delete m_spinWidth;
243     delete m_spinHeight;
244     delete m_opacity;
245     m_scene->removeItem(m_rect);
246     if (m_rect) delete m_rect;
247     if (m_previous) delete m_previous;
248     delete m_geometry;
249     m_extraGeometryNames.clear();
250     m_extraFactors.clear();
251     while (!m_extraGeometries.isEmpty()) {
252         Mlt::Geometry *g = m_extraGeometries.takeFirst();
253         delete g;
254     }
255     if (m_monitor) {
256         m_monitor->getEffectEdit()->showVisibilityButton(false);
257     }
258 }
259
260 void GeometryWidget::slotShowPreviousKeyFrame(bool show)
261 {
262     KdenliveSettings::setOnmonitoreffects_geometryshowprevious(show);
263     slotPositionChanged(-1, false);
264 }
265
266 void GeometryWidget::updateTimecodeFormat()
267 {
268     m_timePos->slotUpdateTimeCodeFormat();
269 }
270
271 QString GeometryWidget::getValue() const
272 {
273     return m_geometry->serialise();
274 }
275
276 QString GeometryWidget::getExtraValue(const QString &name) const
277 {
278     int ix = m_extraGeometryNames.indexOf(name);
279     QString val = m_extraGeometries.at(ix)->serialise();
280     if (!val.contains("=")) val = val.section('/', 0, 0);
281     else {
282         QStringList list = val.split(';', QString::SkipEmptyParts);
283         val.clear();
284         val.append(list.takeFirst().section('/', 0, 0));
285         foreach (const QString value, list) {
286             val.append(';' + value.section('/', 0, 0));
287         }
288     }
289     return val;
290 }
291
292 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
293 {
294     m_inPoint = minframe;
295     m_outPoint = maxframe;
296     m_scene->cleanup();
297
298     if (m_geometry)
299         m_geometry->parse(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
300     else
301         m_geometry = new Mlt::Geometry(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
302
303     if (elem.attribute("fixed") == "1" || maxframe < minframe) {
304         // Keyframes are disabled
305         m_ui.widgetTimeWrapper->setHidden(true);
306     } else {
307         m_ui.widgetTimeWrapper->setHidden(false);
308         m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint);
309         m_timeline->update();
310         m_timePos->setRange(0, m_outPoint - m_inPoint);
311     }
312
313     // no opacity
314     if (elem.attribute("opacity") == "false") {
315         m_opacity->setHidden(true);
316         m_ui.horizontalLayout2->addStretch(2);
317     }
318
319     Mlt::GeometryItem item;
320
321     m_geometry->fetch(&item, 0);
322     delete m_rect;
323     m_rect = new OnMonitorRectItem(QRectF(0, 0, item.w(), item.h()), m_monitor->render->dar());
324     m_rect->setPos(item.x(), item.y());
325     m_rect->setZValue(0);
326     m_scene->addItem(m_rect);
327     connect(m_rect, SIGNAL(changed()), this, SLOT(slotUpdateGeometry()));
328     m_scene->centerView();
329     slotPositionChanged(0, false);
330     slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
331 }
332
333 void GeometryWidget::addParameter(const QDomElement elem)
334 {
335     Mlt::Geometry *geometry = new Mlt::Geometry(elem.attribute("value").toUtf8().data(), m_outPoint - m_inPoint, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
336     m_extraGeometries.append(geometry);
337     m_timeline->addGeometry(geometry);
338     m_extraFactors.append(elem.attribute("factor", "1"));
339     m_extraGeometryNames.append(elem.attribute("name"));
340     //kDebug()<<"ADDED PARAM: "<<elem.attribute("value");
341 }
342
343 void GeometryWidget::slotSyncPosition(int relTimelinePos)
344 {
345     // do only sync if this effect is keyframable
346     if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
347         relTimelinePos = qBound(0, relTimelinePos, m_timePos->maximum());
348         if (relTimelinePos != m_timePos->getValue())
349             slotPositionChanged(relTimelinePos, false);
350     }
351 }
352
353
354 void GeometryWidget::slotPositionChanged(int pos, bool seek)
355 {
356     if (pos == -1)
357         pos = m_timePos->getValue();
358     else
359         m_timePos->setValue(pos);
360
361     m_timeline->blockSignals(true);
362     m_timeline->setValue(pos);
363     m_timeline->blockSignals(false);
364
365     Mlt::GeometryItem item;
366     Mlt::GeometryItem previousItem;
367     if (m_geometry->fetch(&item, pos) || item.key() == false) {
368         // no keyframe
369         m_rect->setEnabled(false);
370         m_scene->setEnabled(false);
371         m_ui.widgetGeometry->setEnabled(false);
372         m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
373         m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
374     } else {
375         // keyframe
376         m_rect->setEnabled(true);
377         m_scene->setEnabled(true);
378         m_ui.widgetGeometry->setEnabled(true);
379         m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
380         m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
381     }
382     
383     if (KdenliveSettings::onmonitoreffects_geometryshowprevious() == false || m_geometry->prev_key(&previousItem, pos - 1) || previousItem.frame() == item.frame()) {
384         if (m_previous) {
385             m_scene->removeItem(m_previous);
386         }
387     }
388     else if (m_previous && m_previous->scene() && m_previous->data(Qt::UserRole).toInt() == previousItem.frame()) {
389         // previous frame already here, do nothing
390     }
391     else {
392         if (m_previous == NULL) {
393             m_previous = new QGraphicsRectItem(0, 0, previousItem.w(), previousItem.h());
394             m_previous->setBrush(QColor(200, 200, 0, 20));
395             m_previous->setPen(QPen(Qt::white, 0, Qt::DotLine));
396             m_previous->setPos(previousItem.x(), previousItem.y());
397             m_previous->setZValue(-1);
398             m_previous->setEnabled(false);
399         }
400         else {
401             m_previous->setPos(previousItem.x(), previousItem.y());
402             m_previous->setRect(0, 0, previousItem.w(), previousItem.h());
403         }
404         m_previous->setData(Qt::UserRole, previousItem.frame());
405         if (m_previous->scene() == 0) m_scene->addItem(m_previous);
406     }
407
408     m_rect->setPos(item.x(), item.y());
409     m_rect->setRect(0, 0, item.w(), item.h());
410
411     m_opacity->blockSignals(true);
412     m_opacity->setValue(item.mix());
413     m_opacity->blockSignals(false);
414
415     for (int i = 0; i < m_extraGeometries.count(); i++) {
416         Mlt::Geometry *geom = m_extraGeometries.at(i);
417         QString name = m_extraGeometryNames.at(i);
418         if (!geom->fetch(&item, pos)) {
419             DragValue *widget = findChild<DragValue *>(name);
420             if (widget) {
421                 widget->blockSignals(true);
422                 widget->setValue(item.x() * m_extraFactors.at(i).toInt());
423                 widget->blockSignals(false);
424             }
425         }
426     }
427
428     slotUpdateProperties();
429
430     if (seek && KdenliveSettings::transitionfollowcursor())
431         emit seekToPos(m_clipPos + pos);
432 }
433
434 void GeometryWidget::slotKeyframeMoved(int pos)
435 {
436     slotPositionChanged(pos);
437     slotUpdateGeometry();
438     QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
439 }
440
441 void GeometryWidget::slotAddKeyframe(int pos)
442 {
443     Mlt::GeometryItem item;
444     if (pos == -1)
445         pos = m_timePos->getValue();
446     item.frame(pos);
447     QRectF r = m_rect->rect().normalized();
448     QPointF rectpos = m_rect->pos();
449     item.x(rectpos.x());
450     item.y(rectpos.y());
451     item.w(r.width());
452     item.h(r.height());
453     item.mix(m_opacity->value());
454     m_geometry->insert(item);
455
456     for (int i = 0; i < m_extraGeometries.count(); i++) {
457         Mlt::Geometry *geom = m_extraGeometries.at(i);
458         QString name = m_extraGeometryNames.at(i);
459         DragValue *widget = findChild<DragValue *>(name);
460         if (widget) {
461             Mlt::GeometryItem item2;
462             item2.frame(pos);
463             item2.x((double) widget->value() / m_extraFactors.at(i).toInt());
464             geom->insert(item2);
465         }
466     }
467     
468     m_timeline->update();
469     slotPositionChanged(pos, false);
470     emit parameterChanged();
471 }
472
473 void GeometryWidget::slotDeleteKeyframe(int pos)
474 {
475     Mlt::GeometryItem item;
476     if (pos == -1)
477         pos = m_timePos->getValue();
478     // check there is more than one keyframe, do not allow to delete last one
479     if (m_geometry->next_key(&item, pos + 1)) {
480         if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
481             return;
482     }
483     m_geometry->remove(pos);
484
485     for (int i = 0; i < m_extraGeometries.count(); i++) {
486         Mlt::Geometry *geom = m_extraGeometries.at(i);
487         geom->remove(pos);
488     }
489
490     m_timeline->update();
491     slotPositionChanged(pos, false);
492     emit parameterChanged();
493 }
494
495 void GeometryWidget::slotPreviousKeyframe()
496 {
497     Mlt::GeometryItem item;
498     // Go to start if no keyframe is found
499     int currentPos = m_timePos->getValue();
500     int pos = 0;
501     if (!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
502         pos = item.frame();
503
504     slotPositionChanged(pos);
505 }
506
507 void GeometryWidget::slotNextKeyframe()
508 {
509     Mlt::GeometryItem item;
510     // Go to end if no keyframe is found
511     int pos = m_timeline->frameLength;
512     if (!m_geometry->next_key(&item, m_timeline->value() + 1))
513         pos = item.frame();
514
515     slotPositionChanged(pos);
516 }
517
518 void GeometryWidget::slotAddDeleteKeyframe()
519 {
520     Mlt::GeometryItem item;
521     if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
522         slotAddKeyframe();
523     else
524         slotDeleteKeyframe();
525 }
526
527
528 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
529 {
530     if (m_showScene) {
531         /*
532             We do only get the position in timeline if this geometry belongs to a transition,
533             therefore we need two ways here.
534         */
535         if (m_isEffect) {
536             emit checkMonitorPosition(renderPos);
537         } else {
538             if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
539                 if (!m_scene->views().at(0)->isVisible())
540                     m_monitor->slotShowEffectScene(true);
541             } else {
542                 m_monitor->slotShowEffectScene(false);
543             }
544         }
545     }
546 }
547
548
549 void GeometryWidget::slotUpdateGeometry()
550 {
551     Mlt::GeometryItem item;
552     int pos = m_timePos->getValue();
553
554     // get keyframe and make sure it is the correct one
555     if (m_geometry->next_key(&item, pos) || item.frame() != pos)
556         return;
557
558     QRectF rectSize = m_rect->rect().normalized();
559     QPointF rectPos = m_rect->pos();
560     item.x(rectPos.x());
561     item.y(rectPos.y());
562     item.w(rectSize.width());
563     item.h(rectSize.height());
564     m_geometry->insert(item);
565
566     for (int i = 0; i < m_extraGeometries.count(); i++) {
567         Mlt::Geometry *geom = m_extraGeometries.at(i);
568         QString name = m_extraGeometryNames.at(i);
569         Mlt::GeometryItem item2;
570         DragValue *widget = findChild<DragValue *>(name);
571         if (widget && !geom->next_key(&item2, pos) && item2.frame() == pos) {
572             item2.x((double) widget->value() / m_extraFactors.at(i).toInt());
573             geom->insert(item2);
574         }
575     }
576     emit parameterChanged();
577 }
578
579 void GeometryWidget::slotUpdateProperties()
580 {
581     QRectF rectSize = m_rect->rect().normalized();
582     QPointF rectPos = m_rect->pos();
583     double size;
584     if (rectSize.width() / m_monitor->render->dar() > rectSize.height())
585         size = rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth();
586     else
587         size = rectSize.height() * 100.0 / m_monitor->render->renderHeight();
588
589     m_spinX->blockSignals(true);
590     m_spinY->blockSignals(true);
591     m_spinWidth->blockSignals(true);
592     m_spinHeight->blockSignals(true);
593     m_spinSize->blockSignals(true);
594
595     m_spinX->setValue(rectPos.x());
596     m_spinY->setValue(rectPos.y());
597     m_spinWidth->setValue(rectSize.width());
598     m_spinHeight->setValue(rectSize.height());
599     m_spinSize->setValue(size);
600
601     m_spinX->blockSignals(false);
602     m_spinY->blockSignals(false);
603     m_spinWidth->blockSignals(false);
604     m_spinHeight->blockSignals(false);
605     m_spinSize->blockSignals(false);
606 }
607
608
609 void GeometryWidget::slotSetX(double value)
610 {
611     m_rect->setPos(value, m_spinY->value());
612     slotUpdateGeometry();
613 }
614
615 void GeometryWidget::slotSetY(double value)
616 {
617     m_rect->setPos(m_spinX->value(), value);
618     slotUpdateGeometry();
619 }
620
621 void GeometryWidget::slotSetWidth(double value)
622 {
623     m_rect->setRect(0, 0, value, m_spinHeight->value());
624     slotUpdateGeometry();
625 }
626
627 void GeometryWidget::slotSetHeight(double value)
628 {
629     m_rect->setRect(0, 0, m_spinWidth->value(), value);
630     slotUpdateGeometry();
631 }
632
633 void GeometryWidget::updateMonitorGeometry()
634 {
635     m_rect->setRect(0, 0, m_spinWidth->value(), m_spinHeight->value());
636     slotUpdateGeometry();
637 }
638
639
640 void GeometryWidget::slotResize(double value)
641 {
642     m_rect->setRect(0, 0,
643                     (int)((m_monitor->render->frameRenderWidth() * value / 100.0) + 0.5),
644                     (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
645     slotUpdateGeometry();
646 }
647
648
649 void GeometryWidget::slotSetOpacity(double value)
650 {
651     int pos = m_timePos->getValue();
652     Mlt::GeometryItem item;
653     if (m_geometry->fetch(&item, pos) || item.key() == false)
654         return;
655     item.mix(value);
656     m_geometry->insert(item);
657     emit parameterChanged();
658 }
659
660
661 void GeometryWidget::slotMoveLeft()
662 {
663     m_rect->setPos(0, m_rect->pos().y());
664     slotUpdateGeometry();
665 }
666
667 void GeometryWidget::slotCenterH()
668 {
669     m_rect->setPos((m_monitor->render->frameRenderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
670     slotUpdateGeometry();
671 }
672
673 void GeometryWidget::slotMoveRight()
674 {
675     m_rect->setPos(m_monitor->render->frameRenderWidth() - m_rect->rect().width(), m_rect->pos().y());
676     slotUpdateGeometry();
677 }
678
679 void GeometryWidget::slotMoveTop()
680 {
681     m_rect->setPos(m_rect->pos().x(), 0);
682     slotUpdateGeometry();
683 }
684
685 void GeometryWidget::slotCenterV()
686 {
687     m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
688     slotUpdateGeometry();
689 }
690
691 void GeometryWidget::slotMoveBottom()
692 {
693     m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
694     slotUpdateGeometry();
695 }
696
697
698 void GeometryWidget::slotSetSynchronize(bool sync)
699 {
700     KdenliveSettings::setTransitionfollowcursor(sync);
701     if (sync)
702         emit seekToPos(m_clipPos + m_timePos->getValue());
703 }
704
705 void GeometryWidget::slotShowScene(bool show)
706 {
707     m_showScene = show;
708     if (!m_showScene)
709         m_monitor->slotShowEffectScene(false);
710     else
711         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
712 }
713
714 void GeometryWidget::setFrameSize(QPoint size)
715 {
716     m_frameSize = size;
717 }
718
719 void GeometryWidget::slotAdjustToFrameSize()
720 {
721     if (m_frameSize == QPoint() || m_frameSize.x() == 0 || m_frameSize.y() == 0) {
722         m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
723     }
724     m_spinWidth->blockSignals(true);
725     m_spinHeight->blockSignals(true);
726     m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() + 0.5));
727     m_spinHeight->setValue(m_frameSize.y());
728     m_spinWidth->blockSignals(false);
729     m_spinHeight->blockSignals(false);
730     updateMonitorGeometry();
731 }
732
733 void GeometryWidget::slotFitToWidth()
734 {
735     if (m_frameSize == QPoint() || m_frameSize.x() == 0 || m_frameSize.y() == 0) {
736         m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
737     }
738     double factor = (double) m_monitor->render->frameRenderWidth() / m_frameSize.x() * m_monitor->render->sar();
739     m_spinWidth->blockSignals(true);
740     m_spinHeight->blockSignals(true);
741     m_spinHeight->setValue((int) (m_frameSize.y() * factor + 0.5));
742     m_spinWidth->setValue(m_monitor->render->frameRenderWidth());
743     m_spinWidth->blockSignals(false);
744     m_spinHeight->blockSignals(false);
745     updateMonitorGeometry();
746 }
747
748 void GeometryWidget::slotFitToHeight()
749 {
750     if (m_frameSize == QPoint() || m_frameSize.x() == 0 || m_frameSize.y() == 0) {
751         m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
752     }
753     double factor = (double) m_monitor->render->renderHeight() / m_frameSize.y();
754     m_spinWidth->blockSignals(true);
755     m_spinHeight->blockSignals(true);
756     m_spinHeight->setValue(m_monitor->render->renderHeight());
757     m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() * factor + 0.5));
758     m_spinWidth->blockSignals(false);
759     m_spinHeight->blockSignals(false);
760     updateMonitorGeometry();
761 }
762
763 #include "geometrywidget.moc"