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