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