]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
Merge branch 'master' into feature/pkey
[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_geomPath->setPoints(m_geometry);
357     m_scene->addItem(m_geomPath);
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) {
527         m_scene->removeItem(m_geomPath);
528         m_geomPath->setPoints(m_geometry);
529         m_scene->addItem(m_geomPath);
530     }
531     slotPositionChanged(pos, false);
532     emit parameterChanged();
533 }
534
535 void GeometryWidget::slotPreviousKeyframe()
536 {
537     Mlt::GeometryItem item;
538     // Go to start if no keyframe is found
539     int currentPos = m_timePos->getValue();
540     int pos = 0;
541     if (!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
542         pos = item.frame();
543
544     slotPositionChanged(pos);
545 }
546
547 void GeometryWidget::slotNextKeyframe()
548 {
549     Mlt::GeometryItem item;
550     // Go to end if no keyframe is found
551     int pos = m_timeline->frameLength;
552     if (!m_geometry->next_key(&item, m_timeline->value() + 1))
553         pos = item.frame();
554
555     slotPositionChanged(pos);
556 }
557
558 void GeometryWidget::slotAddDeleteKeyframe()
559 {
560     Mlt::GeometryItem item;
561     if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
562         slotAddKeyframe();
563     else
564         slotDeleteKeyframe();
565 }
566
567 void GeometryWidget::slotUpdatePath()
568 {
569     if (!m_geomPath) return;
570     QList <QPointF> points = m_geomPath->points();
571     Mlt::GeometryItem item;
572     int pos = 0;
573     int ix = 0;
574     while (ix < points.count() && !m_geometry->next_key(&item, pos)) {
575         QPointF center = points.at(ix);
576         QSizeF size(item.w(), item.h());
577         item.x(center.x() - size.width()/2);
578         item.y(center.y() - size.height()/2);
579         m_geometry->insert(item);
580         pos = item.frame() + 1;
581         ix++;
582     }
583     slotPositionChanged(-1, false);
584     emit parameterChanged();
585 }
586
587
588 void GeometryWidget::slotUpdateGeometry()
589 {
590     Mlt::GeometryItem item;
591     int pos = m_timePos->getValue();
592
593     // get keyframe and make sure it is the correct one
594     if (m_geometry->next_key(&item, pos) || item.frame() != pos)
595         return;
596
597     QRectF rectSize = m_rect->rect().normalized();
598     QPointF rectPos = m_rect->pos();
599     item.x(rectPos.x());
600     item.y(rectPos.y());
601     item.w(rectSize.width());
602     item.h(rectSize.height());
603     m_geometry->insert(item);
604
605     for (int i = 0; i < m_extraGeometries.count(); i++) {
606         Mlt::Geometry *geom = m_extraGeometries.at(i);
607         QString name = m_extraGeometryNames.at(i);
608         Mlt::GeometryItem item2;
609         DragValue *widget = findChild<DragValue *>(name);
610         if (widget && !geom->next_key(&item2, pos) && item2.frame() == pos) {
611             item2.x((double) widget->value() / m_extraFactors.at(i).toInt());
612             geom->insert(item2);
613         }
614     }
615     if (m_geomPath) {
616         m_scene->removeItem(m_geomPath);
617         m_geomPath->setPoints(m_geometry);
618         m_scene->addItem(m_geomPath);
619     }
620     emit parameterChanged();
621 }
622
623 void GeometryWidget::slotUpdateProperties()
624 {
625     QRectF rectSize = m_rect->rect().normalized();
626     QPointF rectPos = m_rect->pos();
627     double size;
628     if (rectSize.width() / m_monitor->render->dar() > rectSize.height())
629         size = rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth();
630     else
631         size = rectSize.height() * 100.0 / m_monitor->render->renderHeight();
632
633     m_spinX->blockSignals(true);
634     m_spinY->blockSignals(true);
635     m_spinWidth->blockSignals(true);
636     m_spinHeight->blockSignals(true);
637     m_spinSize->blockSignals(true);
638
639     m_spinX->setValue(rectPos.x());
640     m_spinY->setValue(rectPos.y());
641     m_spinWidth->setValue(rectSize.width());
642     m_spinHeight->setValue(rectSize.height());
643     m_spinSize->setValue(size);
644
645     m_spinX->blockSignals(false);
646     m_spinY->blockSignals(false);
647     m_spinWidth->blockSignals(false);
648     m_spinHeight->blockSignals(false);
649     m_spinSize->blockSignals(false);
650 }
651
652
653 void GeometryWidget::slotSetX(double value)
654 {
655     m_rect->setPos(value, m_spinY->value());
656     slotUpdateGeometry();
657 }
658
659 void GeometryWidget::slotSetY(double value)
660 {
661     m_rect->setPos(m_spinX->value(), value);
662     slotUpdateGeometry();
663 }
664
665 void GeometryWidget::slotSetWidth(double value)
666 {
667     m_rect->setRect(0, 0, value, m_spinHeight->value());
668     slotUpdateGeometry();
669 }
670
671 void GeometryWidget::slotSetHeight(double value)
672 {
673     m_rect->setRect(0, 0, m_spinWidth->value(), value);
674     slotUpdateGeometry();
675 }
676
677 void GeometryWidget::updateMonitorGeometry()
678 {
679     m_rect->setRect(0, 0, m_spinWidth->value(), m_spinHeight->value());
680     slotUpdateGeometry();
681 }
682
683
684 void GeometryWidget::slotResize(double value)
685 {
686     m_rect->setRect(0, 0,
687                     (int)((m_monitor->render->frameRenderWidth() * value / 100.0) + 0.5),
688                     (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
689     slotUpdateGeometry();
690 }
691
692
693 void GeometryWidget::slotSetOpacity(double value)
694 {
695     int pos = m_timePos->getValue();
696     Mlt::GeometryItem item;
697     if (m_geometry->fetch(&item, pos) || item.key() == false)
698         return;
699     item.mix(value);
700     m_geometry->insert(item);
701     emit parameterChanged();
702 }
703
704
705 void GeometryWidget::slotMoveLeft()
706 {
707     m_rect->setPos(0, m_rect->pos().y());
708     slotUpdateGeometry();
709 }
710
711 void GeometryWidget::slotCenterH()
712 {
713     m_rect->setPos((m_monitor->render->frameRenderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
714     slotUpdateGeometry();
715 }
716
717 void GeometryWidget::slotMoveRight()
718 {
719     m_rect->setPos(m_monitor->render->frameRenderWidth() - m_rect->rect().width(), m_rect->pos().y());
720     slotUpdateGeometry();
721 }
722
723 void GeometryWidget::slotMoveTop()
724 {
725     m_rect->setPos(m_rect->pos().x(), 0);
726     slotUpdateGeometry();
727 }
728
729 void GeometryWidget::slotCenterV()
730 {
731     m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
732     slotUpdateGeometry();
733 }
734
735 void GeometryWidget::slotMoveBottom()
736 {
737     m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
738     slotUpdateGeometry();
739 }
740
741
742 void GeometryWidget::slotSetSynchronize(bool sync)
743 {
744     KdenliveSettings::setTransitionfollowcursor(sync);
745     if (sync)
746         emit seekToPos(m_clipPos + m_timePos->getValue());
747 }
748
749 void GeometryWidget::setFrameSize(QPoint size)
750 {
751     m_frameSize = size;
752 }
753
754 void GeometryWidget::slotAdjustToFrameSize()
755 {
756     if (m_frameSize == QPoint() || m_frameSize.x() == 0 || m_frameSize.y() == 0) {
757         m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
758     }
759     m_spinWidth->blockSignals(true);
760     m_spinHeight->blockSignals(true);
761     m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() + 0.5));
762     m_spinHeight->setValue(m_frameSize.y());
763     m_spinWidth->blockSignals(false);
764     m_spinHeight->blockSignals(false);
765     updateMonitorGeometry();
766 }
767
768 void GeometryWidget::slotFitToWidth()
769 {
770     if (m_frameSize == QPoint() || m_frameSize.x() == 0 || m_frameSize.y() == 0) {
771         m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
772     }
773     double factor = (double) m_monitor->render->frameRenderWidth() / m_frameSize.x() * m_monitor->render->sar();
774     m_spinWidth->blockSignals(true);
775     m_spinHeight->blockSignals(true);
776     m_spinHeight->setValue((int) (m_frameSize.y() * factor + 0.5));
777     m_spinWidth->setValue(m_monitor->render->frameRenderWidth());
778     m_spinWidth->blockSignals(false);
779     m_spinHeight->blockSignals(false);
780     updateMonitorGeometry();
781 }
782
783 void GeometryWidget::slotFitToHeight()
784 {
785     if (m_frameSize == QPoint() || m_frameSize.x() == 0 || m_frameSize.y() == 0) {
786         m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
787     }
788     double factor = (double) m_monitor->render->renderHeight() / m_frameSize.y();
789     m_spinWidth->blockSignals(true);
790     m_spinHeight->blockSignals(true);
791     m_spinHeight->setValue(m_monitor->render->renderHeight());
792     m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() * factor + 0.5));
793     m_spinWidth->blockSignals(false);
794     m_spinHeight->blockSignals(false);
795     updateMonitorGeometry();
796 }
797
798 void GeometryWidget::slotResetKeyframes()
799 {
800     // Delete existing keyframes
801     Mlt::GeometryItem item;
802     while (!m_geometry->next_key(&item, 1)) {
803         m_geometry->remove(item.frame());
804     }
805
806     // Create neutral first keyframe
807     item.frame(0);
808     item.x(0);
809     item.y(0);
810     item.w(m_monitor->render->frameRenderWidth());
811     item.h(m_monitor->render->renderHeight());
812     item.mix(100);
813     m_geometry->insert(item);
814     m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint);
815     if (m_geomPath) {
816         m_scene->removeItem(m_geomPath);
817         m_geomPath->setPoints(m_geometry);
818         m_scene->addItem(m_geomPath);
819     }
820     slotPositionChanged(-1, false);
821     emit parameterChanged();
822     
823 }
824
825 void GeometryWidget::importKeyframes(const QString &data, int maximum)
826 {
827     QStringList list = data.split(';', QString::SkipEmptyParts);
828     if (list.isEmpty()) return;
829     QPoint screenSize = m_frameSize;
830     if (screenSize == QPoint() || screenSize.x() == 0 || screenSize.y() == 0) {
831         screenSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
832     }
833     // Clear existing keyframes
834     Mlt::GeometryItem item;
835     
836     while (!m_geometry->next_key(&item, 0)) {
837         m_geometry->remove(item.frame());
838     }
839     
840     int offset = 1;
841     if (maximum > 0 && list.count() > maximum) {
842         offset = list.count() / maximum;
843     }
844     for (int i = 0; i < list.count(); i += offset) {
845         QString geom = list.at(i);
846         if (geom.contains('=')) {
847             item.frame(geom.section('=', 0, 0).toInt());
848             geom = geom.section('=', 1);
849         }
850         else item.frame(0);
851         if (geom.contains('/')) {
852             item.x(geom.section('/', 0, 0).toDouble());
853             item.y(geom.section('/', 1, 1).section(':', 0, 0).toDouble());
854         }
855         else {
856             item.x(0);
857             item.y(0);
858         }
859         if (geom.contains('x')) {
860             item.w(geom.section('x', 0, 0).section(':', 1, 1).toDouble());
861             item.h(geom.section('x', 1, 1).section(':', 0, 0).toDouble());
862         }
863         else {
864             item.w(screenSize.x());
865             item.h(screenSize.y());
866         }
867         //TODO: opacity
868         item.mix(100);
869         m_geometry->insert(item);
870     }
871     m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint);
872     if (m_geomPath) {
873         m_scene->removeItem(m_geomPath);
874         m_geomPath->setPoints(m_geometry);
875         m_scene->addItem(m_geomPath);
876     }
877     slotPositionChanged(-1, false);
878     emit parameterChanged();
879 }
880
881
882 #include "geometrywidget.moc"