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