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