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