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