]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
Improve update of effect stack when resizing clip, start implementing merge of analys...
[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     }
330     m_timePos->setRange(0, m_outPoint - m_inPoint);
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 int GeometryWidget::currentPosition() const
383 {
384     return m_inPoint + m_timePos->getValue();
385 }
386
387 void GeometryWidget::slotRequestSeek(int pos)
388 {
389     if (KdenliveSettings::transitionfollowcursor())
390         emit seekToPos(m_clipPos + pos);
391 }
392
393
394 void GeometryWidget::slotPositionChanged(int pos, bool seek)
395 {
396     if (pos == -1)
397         pos = m_timePos->getValue();
398     else
399         m_timePos->setValue(pos);
400
401     //m_timeline->blockSignals(true);
402     m_timeline->setValue(pos);
403     //m_timeline->blockSignals(false);
404
405     Mlt::GeometryItem item;
406     Mlt::GeometryItem previousItem;
407     if (m_geometry->fetch(&item, pos) || item.key() == false) {
408         // no keyframe
409         m_rect->setEnabled(false);
410         m_scene->setEnabled(false);
411         m_ui.widgetGeometry->setEnabled(false);
412         m_ui.buttonAddDelete->setIcon(KIcon("list-add"));
413         m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
414     } else {
415         // keyframe
416         m_rect->setEnabled(true);
417         m_scene->setEnabled(true);
418         m_ui.widgetGeometry->setEnabled(true);
419         m_ui.buttonAddDelete->setIcon(KIcon("list-remove"));
420         m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
421     }
422     
423     if (KdenliveSettings::onmonitoreffects_geometryshowprevious() == false || m_geometry->prev_key(&previousItem, pos - 1) || previousItem.frame() == item.frame()) {
424         if (m_previous) {
425             m_scene->removeItem(m_previous);
426         }
427     }
428     else if (m_previous && m_previous->scene() && m_previous->data(Qt::UserRole).toInt() == previousItem.frame()) {
429         // previous frame already here, do nothing
430     }
431     else {
432         if (m_previous == NULL) {
433             m_previous = new QGraphicsRectItem(0, 0, previousItem.w(), previousItem.h());
434             m_previous->setBrush(QColor(200, 200, 0, 20));
435             m_previous->setPen(QPen(Qt::white, 0, Qt::DotLine));
436             m_previous->setPos(previousItem.x(), previousItem.y());
437             m_previous->setZValue(-1);
438             m_previous->setEnabled(false);
439         }
440         else {
441             m_previous->setPos(previousItem.x(), previousItem.y());
442             m_previous->setRect(0, 0, previousItem.w(), previousItem.h());
443         }
444         m_previous->setData(Qt::UserRole, previousItem.frame());
445         if (m_previous->scene() == 0) m_scene->addItem(m_previous);
446     }
447
448     m_rect->setPos(item.x(), item.y());
449     m_rect->setRect(0, 0, item.w(), item.h());
450
451     m_opacity->blockSignals(true);
452     m_opacity->setValue(item.mix());
453     m_opacity->blockSignals(false);
454
455     for (int i = 0; i < m_extraGeometries.count(); i++) {
456         Mlt::Geometry *geom = m_extraGeometries.at(i);
457         QString name = m_extraGeometryNames.at(i);
458         if (!geom->fetch(&item, pos)) {
459             DragValue *widget = findChild<DragValue *>(name);
460             if (widget) {
461                 widget->blockSignals(true);
462                 widget->setValue(item.x() * m_extraFactors.at(i).toInt());
463                 widget->blockSignals(false);
464             }
465         }
466     }
467
468     slotUpdateProperties();
469
470     if (seek && KdenliveSettings::transitionfollowcursor())
471         emit seekToPos(m_clipPos + pos);
472 }
473
474 void GeometryWidget::slotKeyframeMoved(int pos)
475 {
476     slotPositionChanged(pos);
477     slotUpdateGeometry();
478     QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
479 }
480
481 void GeometryWidget::slotAddKeyframe(int pos)
482 {
483     Mlt::GeometryItem item;
484     if (pos == -1)
485         pos = m_timePos->getValue();
486     item.frame(pos);
487     QRectF r = m_rect->rect().normalized();
488     QPointF rectpos = m_rect->pos();
489     item.x(rectpos.x());
490     item.y(rectpos.y());
491     item.w(r.width());
492     item.h(r.height());
493     item.mix(m_opacity->value());
494     m_geometry->insert(item);
495
496     for (int i = 0; i < m_extraGeometries.count(); i++) {
497         Mlt::Geometry *geom = m_extraGeometries.at(i);
498         QString name = m_extraGeometryNames.at(i);
499         DragValue *widget = findChild<DragValue *>(name);
500         if (widget) {
501             Mlt::GeometryItem item2;
502             item2.frame(pos);
503             item2.x((double) widget->value() / m_extraFactors.at(i).toInt());
504             geom->insert(item2);
505         }
506     }
507     
508     m_timeline->update();
509     slotPositionChanged(pos, false);
510     emit parameterChanged();
511 }
512
513 void GeometryWidget::slotDeleteKeyframe(int pos)
514 {
515     Mlt::GeometryItem item;
516     if (pos == -1)
517         pos = m_timePos->getValue();
518     // check there is more than one keyframe, do not allow to delete last one
519     if (m_geometry->next_key(&item, pos + 1)) {
520         if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
521             return;
522     }
523     m_geometry->remove(pos);
524
525     for (int i = 0; i < m_extraGeometries.count(); i++) {
526         Mlt::Geometry *geom = m_extraGeometries.at(i);
527         geom->remove(pos);
528     }
529
530     m_timeline->update();
531     if (m_geomPath) {
532         m_scene->removeItem(m_geomPath);
533         m_geomPath->setPoints(m_geometry);
534         m_scene->addItem(m_geomPath);
535     }
536     slotPositionChanged(pos, false);
537     emit parameterChanged();
538 }
539
540 void GeometryWidget::slotPreviousKeyframe()
541 {
542     Mlt::GeometryItem item;
543     // Go to start if no keyframe is found
544     int currentPos = m_timePos->getValue();
545     int pos = 0;
546     if (!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
547         pos = item.frame();
548
549     slotPositionChanged(pos);
550 }
551
552 void GeometryWidget::slotNextKeyframe()
553 {
554     Mlt::GeometryItem item;
555     // Go to end if no keyframe is found
556     int pos = m_timeline->frameLength;
557     if (!m_geometry->next_key(&item, m_timeline->value() + 1))
558         pos = item.frame();
559
560     slotPositionChanged(pos);
561 }
562
563 void GeometryWidget::slotAddDeleteKeyframe()
564 {
565     Mlt::GeometryItem item;
566     if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
567         slotAddKeyframe();
568     else
569         slotDeleteKeyframe();
570 }
571
572 void GeometryWidget::slotUpdatePath()
573 {
574     if (!m_geomPath) return;
575     QList <QPointF> points = m_geomPath->points();
576     Mlt::GeometryItem item;
577     int pos = 0;
578     int ix = 0;
579     while (ix < points.count() && !m_geometry->next_key(&item, pos)) {
580         QPointF center = points.at(ix);
581         QSizeF size(item.w(), item.h());
582         item.x(center.x() - size.width()/2);
583         item.y(center.y() - size.height()/2);
584         m_geometry->insert(item);
585         pos = item.frame() + 1;
586         ix++;
587     }
588     slotPositionChanged(-1, false);
589     emit parameterChanged();
590 }
591
592
593 void GeometryWidget::slotUpdateGeometry()
594 {
595     Mlt::GeometryItem item;
596     int pos = m_timePos->getValue();
597
598     // get keyframe and make sure it is the correct one
599     if (m_geometry->next_key(&item, pos) || item.frame() != pos)
600         return;
601
602     QRectF rectSize = m_rect->rect().normalized();
603     QPointF rectPos = m_rect->pos();
604     item.x(rectPos.x());
605     item.y(rectPos.y());
606     item.w(rectSize.width());
607     item.h(rectSize.height());
608     m_geometry->insert(item);
609
610     for (int i = 0; i < m_extraGeometries.count(); i++) {
611         Mlt::Geometry *geom = m_extraGeometries.at(i);
612         QString name = m_extraGeometryNames.at(i);
613         Mlt::GeometryItem item2;
614         DragValue *widget = findChild<DragValue *>(name);
615         if (widget && !geom->next_key(&item2, pos) && item2.frame() == pos) {
616             item2.x((double) widget->value() / m_extraFactors.at(i).toInt());
617             geom->insert(item2);
618         }
619     }
620     if (m_geomPath) {
621         m_scene->removeItem(m_geomPath);
622         m_geomPath->setPoints(m_geometry);
623         m_scene->addItem(m_geomPath);
624     }
625     emit parameterChanged();
626 }
627
628 void GeometryWidget::slotUpdateProperties()
629 {
630     QRectF rectSize = m_rect->rect().normalized();
631     QPointF rectPos = m_rect->pos();
632     double size;
633     if (rectSize.width() / m_monitor->render->dar() > rectSize.height())
634         size = rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth();
635     else
636         size = rectSize.height() * 100.0 / m_monitor->render->renderHeight();
637
638     m_spinX->blockSignals(true);
639     m_spinY->blockSignals(true);
640     m_spinWidth->blockSignals(true);
641     m_spinHeight->blockSignals(true);
642     m_spinSize->blockSignals(true);
643
644     m_spinX->setValue(rectPos.x());
645     m_spinY->setValue(rectPos.y());
646     m_spinWidth->setValue(rectSize.width());
647     m_spinHeight->setValue(rectSize.height());
648     m_spinSize->setValue(size);
649
650     m_spinX->blockSignals(false);
651     m_spinY->blockSignals(false);
652     m_spinWidth->blockSignals(false);
653     m_spinHeight->blockSignals(false);
654     m_spinSize->blockSignals(false);
655 }
656
657
658 void GeometryWidget::slotSetX(double value)
659 {
660     m_rect->setPos(value, m_spinY->value());
661     slotUpdateGeometry();
662 }
663
664 void GeometryWidget::slotSetY(double value)
665 {
666     m_rect->setPos(m_spinX->value(), value);
667     slotUpdateGeometry();
668 }
669
670 void GeometryWidget::slotSetWidth(double value)
671 {
672     m_rect->setRect(0, 0, value, m_spinHeight->value());
673     slotUpdateGeometry();
674 }
675
676 void GeometryWidget::slotSetHeight(double value)
677 {
678     m_rect->setRect(0, 0, m_spinWidth->value(), value);
679     slotUpdateGeometry();
680 }
681
682 void GeometryWidget::updateMonitorGeometry()
683 {
684     m_rect->setRect(0, 0, m_spinWidth->value(), m_spinHeight->value());
685     slotUpdateGeometry();
686 }
687
688
689 void GeometryWidget::slotResize(double value)
690 {
691     m_rect->setRect(0, 0,
692                     (int)((m_monitor->render->frameRenderWidth() * value / 100.0) + 0.5),
693                     (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
694     slotUpdateGeometry();
695 }
696
697
698 void GeometryWidget::slotSetOpacity(double value)
699 {
700     int pos = m_timePos->getValue();
701     Mlt::GeometryItem item;
702     if (m_geometry->fetch(&item, pos) || item.key() == false)
703         return;
704     item.mix(value);
705     m_geometry->insert(item);
706     emit parameterChanged();
707 }
708
709
710 void GeometryWidget::slotMoveLeft()
711 {
712     m_rect->setPos(0, m_rect->pos().y());
713     slotUpdateGeometry();
714 }
715
716 void GeometryWidget::slotCenterH()
717 {
718     m_rect->setPos((m_monitor->render->frameRenderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
719     slotUpdateGeometry();
720 }
721
722 void GeometryWidget::slotMoveRight()
723 {
724     m_rect->setPos(m_monitor->render->frameRenderWidth() - m_rect->rect().width(), m_rect->pos().y());
725     slotUpdateGeometry();
726 }
727
728 void GeometryWidget::slotMoveTop()
729 {
730     m_rect->setPos(m_rect->pos().x(), 0);
731     slotUpdateGeometry();
732 }
733
734 void GeometryWidget::slotCenterV()
735 {
736     m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
737     slotUpdateGeometry();
738 }
739
740 void GeometryWidget::slotMoveBottom()
741 {
742     m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
743     slotUpdateGeometry();
744 }
745
746
747 void GeometryWidget::slotSetSynchronize(bool sync)
748 {
749     KdenliveSettings::setTransitionfollowcursor(sync);
750     if (sync)
751         emit seekToPos(m_clipPos + m_timePos->getValue());
752 }
753
754 void GeometryWidget::setFrameSize(QPoint size)
755 {
756     m_frameSize = size;
757 }
758
759 void GeometryWidget::slotAdjustToFrameSize()
760 {
761     if (m_frameSize == QPoint() || m_frameSize.x() == 0 || m_frameSize.y() == 0) {
762         m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
763     }
764     m_spinWidth->blockSignals(true);
765     m_spinHeight->blockSignals(true);
766     m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() + 0.5));
767     m_spinHeight->setValue(m_frameSize.y());
768     m_spinWidth->blockSignals(false);
769     m_spinHeight->blockSignals(false);
770     updateMonitorGeometry();
771 }
772
773 void GeometryWidget::slotFitToWidth()
774 {
775     if (m_frameSize == QPoint() || m_frameSize.x() == 0 || m_frameSize.y() == 0) {
776         m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
777     }
778     double factor = (double) m_monitor->render->frameRenderWidth() / m_frameSize.x() * m_monitor->render->sar();
779     m_spinWidth->blockSignals(true);
780     m_spinHeight->blockSignals(true);
781     m_spinHeight->setValue((int) (m_frameSize.y() * factor + 0.5));
782     m_spinWidth->setValue(m_monitor->render->frameRenderWidth());
783     m_spinWidth->blockSignals(false);
784     m_spinHeight->blockSignals(false);
785     updateMonitorGeometry();
786 }
787
788 void GeometryWidget::slotFitToHeight()
789 {
790     if (m_frameSize == QPoint() || m_frameSize.x() == 0 || m_frameSize.y() == 0) {
791         m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
792     }
793     double factor = (double) m_monitor->render->renderHeight() / m_frameSize.y();
794     m_spinWidth->blockSignals(true);
795     m_spinHeight->blockSignals(true);
796     m_spinHeight->setValue(m_monitor->render->renderHeight());
797     m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() * factor + 0.5));
798     m_spinWidth->blockSignals(false);
799     m_spinHeight->blockSignals(false);
800     updateMonitorGeometry();
801 }
802
803 void GeometryWidget::slotResetKeyframes()
804 {
805     // Delete existing keyframes
806     Mlt::GeometryItem item;
807     while (!m_geometry->next_key(&item, 1)) {
808         m_geometry->remove(item.frame());
809     }
810
811     // Create neutral first keyframe
812     item.frame(0);
813     item.x(0);
814     item.y(0);
815     item.w(m_monitor->render->frameRenderWidth());
816     item.h(m_monitor->render->renderHeight());
817     item.mix(100);
818     m_geometry->insert(item);
819     m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint);
820     if (m_geomPath) {
821         m_scene->removeItem(m_geomPath);
822         m_geomPath->setPoints(m_geometry);
823         m_scene->addItem(m_geomPath);
824     }
825     slotPositionChanged(-1, false);
826     emit parameterChanged();
827     
828 }
829
830 void GeometryWidget::importKeyframes(const QString &data, int maximum)
831 {
832     QStringList list = data.split(';', QString::SkipEmptyParts);
833     if (list.isEmpty()) return;
834     QPoint screenSize = m_frameSize;
835     if (screenSize == QPoint() || screenSize.x() == 0 || screenSize.y() == 0) {
836         screenSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
837     }
838     // Clear existing keyframes
839     Mlt::GeometryItem item;
840     
841     while (!m_geometry->next_key(&item, 0)) {
842         m_geometry->remove(item.frame());
843     }
844     
845     int offset = 1;
846     if (maximum > 0 && list.count() > maximum) {
847         offset = list.count() / maximum;
848     }
849     for (int i = 0; i < list.count(); i += offset) {
850         QString geom = list.at(i);
851         if (geom.contains('=')) {
852             item.frame(geom.section('=', 0, 0).toInt());
853             geom = geom.section('=', 1);
854         }
855         else item.frame(0);
856         if (geom.contains('/')) {
857             item.x(geom.section('/', 0, 0).toDouble());
858             item.y(geom.section('/', 1, 1).section(':', 0, 0).toDouble());
859         }
860         else {
861             item.x(0);
862             item.y(0);
863         }
864         if (geom.contains('x')) {
865             item.w(geom.section('x', 0, 0).section(':', 1, 1).toDouble());
866             item.h(geom.section('x', 1, 1).section(':', 0, 0).toDouble());
867         }
868         else {
869             item.w(screenSize.x());
870             item.h(screenSize.y());
871         }
872         //TODO: opacity
873         item.mix(100);
874         m_geometry->insert(item);
875     }
876     m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint);
877     if (m_geomPath) {
878         m_scene->removeItem(m_geomPath);
879         m_geomPath->setPoints(m_geometry);
880         m_scene->addItem(m_geomPath);
881     }
882     slotPositionChanged(-1, false);
883     emit parameterChanged();
884 }
885
886 void GeometryWidget::slotUpdateRange(int inPoint, int outPoint)
887 {
888     m_inPoint = inPoint;
889     m_outPoint = outPoint;
890     m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint);
891     m_timePos->setRange(0, m_outPoint - m_inPoint);    
892 }
893
894 #include "geometrywidget.moc"