]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
Geometry Widget:
[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 "kdenlivesettings.h"
28
29 #include <QtCore>
30 #include <QGraphicsView>
31 #include <QGraphicsRectItem>
32 #include <QVBoxLayout>
33 #include <QGridLayout>
34
35 GeometryWidget::GeometryWidget(Monitor* monitor, Timecode timecode, int clipPos, bool isEffect, QWidget* parent ):
36         QWidget(parent),
37         m_monitor(monitor),
38         m_timePos(new TimecodeDisplay(timecode)),
39         m_clipPos(clipPos),
40         m_inPoint(0),
41         m_outPoint(1),
42         m_isEffect(isEffect),
43         m_rect(NULL),
44         m_geometry(NULL),
45         m_showScene(true)
46 {
47     m_ui.setupUi(this);
48     m_scene = monitor->getEffectScene();
49
50
51     /*
52         Setup of timeline and keyframe controls
53     */
54
55     ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 6);
56
57     QVBoxLayout *layout = new QVBoxLayout(m_ui.frameTimeline);
58     m_timeline = new KeyframeHelper(m_ui.frameTimeline);
59     layout->addWidget(m_timeline);
60     layout->setContentsMargins(0, 0, 0, 0);
61
62     m_ui.buttonPrevious->setIcon(KIcon("media-skip-backward"));
63     m_ui.buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
64     m_ui.buttonNext->setIcon(KIcon("media-skip-forward"));
65     m_ui.buttonNext->setToolTip(i18n("Go to next keyframe"));
66     m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
67     m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
68
69     m_ui.buttonSync->setIcon(KIcon("insert-link"));
70     m_ui.buttonSync->setToolTip(i18n("Synchronize with timeline cursor"));
71     m_ui.buttonSync->setCheckable(true);
72     m_ui.buttonSync->setChecked(KdenliveSettings::transitionfollowcursor());
73
74     connect(m_timeline, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
75     connect(m_timeline, SIGNAL(keyframeMoved(int)),   this, SLOT(slotKeyframeMoved(int)));
76     connect(m_timeline, SIGNAL(addKeyframe(int)),     this, SLOT(slotAddKeyframe(int)));
77     connect(m_timeline, SIGNAL(removeKeyframe(int)),  this, SLOT(slotDeleteKeyframe(int)));
78     connect(m_timePos, SIGNAL(editingFinished()), this , SLOT(slotPositionChanged()));
79     connect(m_ui.buttonPrevious,  SIGNAL(clicked()), this, SLOT(slotPreviousKeyframe()));
80     connect(m_ui.buttonNext,      SIGNAL(clicked()), this, SLOT(slotNextKeyframe()));
81     connect(m_ui.buttonAddDelete, SIGNAL(clicked()), this, SLOT(slotAddDeleteKeyframe()));
82     connect(m_ui.buttonSync,      SIGNAL(toggled(bool)), this, SLOT(slotSetSynchronize(bool)));
83
84
85     /*
86         Setup of geometry controls
87     */
88
89     m_ui.buttonMoveLeft->setIcon(KIcon("kdenlive-align-left"));
90     m_ui.buttonMoveLeft->setToolTip(i18n("Move to left"));
91     m_ui.buttonCenterH->setIcon(KIcon("kdenlive-align-hor"));
92     m_ui.buttonCenterH->setToolTip(i18n("Center horizontally"));
93     m_ui.buttonMoveRight->setIcon(KIcon("kdenlive-align-right"));
94     m_ui.buttonMoveRight->setToolTip(i18n("Move to right"));
95     m_ui.buttonMoveTop->setIcon(KIcon("kdenlive-align-top"));
96     m_ui.buttonMoveTop->setToolTip(i18n("Move to top"));
97     m_ui.buttonCenterV->setIcon(KIcon("kdenlive-align-vert"));
98     m_ui.buttonCenterV->setToolTip(i18n("Center vertically"));
99     m_ui.buttonMoveBottom->setIcon(KIcon("kdenlive-align-bottom"));
100     m_ui.buttonMoveBottom->setToolTip(i18n("Move to bottom"));
101
102     connect(m_ui.spinX,            SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
103     connect(m_ui.spinY,            SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
104     connect(m_ui.spinWidth,        SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
105     connect(m_ui.spinHeight,       SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
106
107     connect(m_ui.spinSize,         SIGNAL(valueChanged(int)), this, SLOT(slotResize(int)));
108
109     connect(m_ui.spinOpacity,      SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
110     connect(m_ui.sliderOpacity,    SIGNAL(valueChanged(int)), m_ui.spinOpacity, SLOT(setValue(int)));
111
112     connect(m_ui.buttonMoveLeft,   SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
113     connect(m_ui.buttonCenterH,    SIGNAL(clicked()), this, SLOT(slotCenterH()));
114     connect(m_ui.buttonMoveRight,  SIGNAL(clicked()), this, SLOT(slotMoveRight()));
115     connect(m_ui.buttonMoveTop,    SIGNAL(clicked()), this, SLOT(slotMoveTop()));
116     connect(m_ui.buttonCenterV,    SIGNAL(clicked()), this, SLOT(slotCenterV()));
117     connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));
118
119
120     /*
121         Setup of configuration controls
122     */
123
124     m_ui.buttonConfig->setIcon(KIcon("system-run"));
125     m_ui.buttonConfig->setToolTip(i18n("Show/Hide settings"));
126     m_ui.buttonConfig->setCheckable(true);
127     m_ui.groupSettings->setHidden(true);
128     m_ui.checkDirectUpdate->setChecked(m_scene->getDirectUpdate());
129
130     connect(m_ui.buttonConfig, SIGNAL(toggled(bool)), m_ui.groupSettings, SLOT(setVisible(bool)));
131
132     connect(m_ui.checkShowScene,    SIGNAL(toggled(bool)), this, SLOT(slotShowScene(bool)));
133     connect(m_ui.checkDirectUpdate, SIGNAL(toggled(bool)), m_scene, SLOT(slotSetDirectUpdate(bool)));
134
135
136     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
137     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
138     connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
139 }
140
141 GeometryWidget::~GeometryWidget()
142 {
143     delete m_timePos;
144     delete m_timeline;
145     m_scene->removeItem(m_rect);
146     delete m_geometry;
147     if (m_monitor) m_monitor->slotEffectScene(false);
148 }
149
150 void GeometryWidget::updateTimecodeFormat()
151 {
152     m_timePos->slotUpdateTimeCodeFormat();
153 }
154
155 QString GeometryWidget::getValue() const
156 {
157     return m_geometry->serialise();
158 }
159
160 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
161 {
162     m_inPoint = minframe;
163     m_outPoint = maxframe;
164
165     char *tmp = (char *) qstrdup(elem.attribute("value").toUtf8().data());
166     if (m_geometry)
167         m_geometry->parse(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
168     else
169         m_geometry = new Mlt::Geometry(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
170     delete[] tmp;
171
172     if (elem.attribute("fixed") == "1") {
173         // Keyframes are disabled
174         m_ui.widgetTimeWrapper->setHidden(true);
175     } else {
176         m_ui.widgetTimeWrapper->setHidden(false);
177         m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint - 1);
178         m_timeline->update();
179         m_timePos->setRange(0, m_outPoint - m_inPoint - 1);
180     }
181
182     // no opacity
183     if (elem.attribute("opacity") == "false")
184         m_ui.widgetOpacity->setHidden(true);
185
186     Mlt::GeometryItem item;
187
188     m_geometry->fetch(&item, 0);
189     delete m_rect;
190     m_rect = new QGraphicsRectItem(QRectF(0, 0, item.w(), item.h()));
191     m_rect->setPos(item.x(), item.y());
192     m_rect->setZValue(0);
193     m_rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
194
195     QPen framepen(Qt::SolidLine);
196     framepen.setColor(Qt::yellow);
197     m_rect->setPen(framepen);
198     m_rect->setBrush(Qt::transparent);
199     m_scene->addItem(m_rect);
200
201     slotPositionChanged(0, false);
202     slotUpdateProperties();
203     slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
204 }
205
206 void GeometryWidget::slotSyncPosition(int relTimelinePos)
207 {
208     // do only sync if this effect is keyframable
209     if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
210         relTimelinePos = qMax(0, relTimelinePos);
211         relTimelinePos = qMin(relTimelinePos, m_timePos->maximum());
212         if (relTimelinePos != m_timePos->getValue())
213             slotPositionChanged(relTimelinePos, false);
214     }
215 }
216
217
218 void GeometryWidget::slotPositionChanged(int pos, bool seek)
219 {
220     if (pos == -1)
221         pos = m_timePos->getValue();
222
223     m_timePos->setValue(pos);
224     m_timeline->blockSignals(true);
225     m_timeline->setValue(pos);
226     m_timeline->blockSignals(false);
227
228     Mlt::GeometryItem item;
229     if (m_geometry->fetch(&item, pos) || item.key() == false) {
230         // no keyframe
231         m_scene->setEnabled(false);
232         m_ui.widgetGeometry->setEnabled(false);
233         m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
234         m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
235     } else {
236         // keyframe
237         m_scene->setEnabled(true);
238         m_ui.widgetGeometry->setEnabled(true);
239         m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
240         m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
241     }
242
243     m_rect->setPos(item.x(), item.y());
244     m_rect->setRect(0, 0, item.w(), item.h());
245
246     m_ui.spinOpacity->blockSignals(true);
247     m_ui.sliderOpacity->blockSignals(true);
248     m_ui.spinOpacity->setValue(item.mix());
249     m_ui.sliderOpacity->setValue(item.mix());
250     m_ui.spinOpacity->blockSignals(false);
251     m_ui.sliderOpacity->blockSignals(false);
252
253     slotUpdateProperties();
254
255     if (seek && KdenliveSettings::transitionfollowcursor())
256         emit seekToPos(m_clipPos + pos);
257 }
258
259 void GeometryWidget::slotKeyframeMoved(int pos)
260 {
261     slotPositionChanged(pos);
262     slotUpdateGeometry();
263 }
264
265 void GeometryWidget::slotAddKeyframe(int pos)
266 {
267     Mlt::GeometryItem item;
268     if (pos == -1)
269         pos = m_timePos->getValue();
270     item.frame(pos);
271     QRectF r = m_rect->rect().normalized();
272     QPointF rectpos = m_rect->pos();
273     item.x(rectpos.x());
274     item.y(rectpos.y());
275     item.w(r.width());
276     item.h(r.height());
277     item.mix(m_ui.spinOpacity->value());
278     m_geometry->insert(item);
279
280     m_timeline->update();
281     slotPositionChanged(pos, false);
282     emit parameterChanged();
283 }
284
285 void GeometryWidget::slotDeleteKeyframe(int pos)
286 {
287     Mlt::GeometryItem item;
288     if (pos == -1)
289         pos = m_timePos->getValue();
290     // check there is more than one keyframe, do not allow to delete last one
291     if (m_geometry->next_key(&item, pos + 1)) {
292         if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
293             return;
294     }
295     m_geometry->remove(pos);
296
297     m_timeline->update();
298     slotPositionChanged(pos, false);
299     emit parameterChanged();
300 }
301
302 void GeometryWidget::slotPreviousKeyframe()
303 {
304     Mlt::GeometryItem item;
305     // Go to start if no keyframe is found
306     int currentPos = m_timePos->getValue();
307     int pos = 0;
308     if(!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
309         pos = item.frame();
310
311     slotPositionChanged(pos);
312 }
313
314 void GeometryWidget::slotNextKeyframe()
315 {
316     Mlt::GeometryItem item;
317     // Go to end if no keyframe is found
318     int pos = m_timeline->frameLength;
319     if (!m_geometry->next_key(&item, m_timeline->value() + 1))
320         pos = item.frame();
321
322     slotPositionChanged(pos);
323 }
324
325 void GeometryWidget::slotAddDeleteKeyframe()
326 {
327     Mlt::GeometryItem item;
328     if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
329         slotAddKeyframe();
330     else
331         slotDeleteKeyframe();
332 }
333
334
335 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
336 {
337     if (m_showScene) {
338         /*
339             We do only get the position in timeline if this geometry belongs to a transition,
340             therefore we need to ways here.
341         */
342         if (m_isEffect) {
343             emit checkMonitorPosition(renderPos);
344         } else {
345             if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
346                 if (!m_scene->views().at(0)->isVisible())
347                     m_monitor->slotEffectScene(true);
348             } else {
349                 m_monitor->slotEffectScene(false);
350             }
351         }
352     }
353 }
354
355
356 void GeometryWidget::slotUpdateGeometry()
357 {
358     Mlt::GeometryItem item;
359     int pos = m_timePos->getValue();
360     // get keyframe and make sure it is the correct one
361     if (m_geometry->next_key(&item, pos) || item.frame() != pos)
362         return;
363
364     QRectF rectSize = m_rect->rect().normalized();
365     QPointF rectPos = m_rect->pos();
366     item.x(rectPos.x());
367     item.y(rectPos.y());
368     item.w(rectSize.width());
369     item.h(rectSize.height());
370     m_geometry->insert(item);
371     emit parameterChanged();
372 }
373
374 void GeometryWidget::slotUpdateProperties()
375 {
376     QRectF rectSize = m_rect->rect().normalized();
377     QPointF rectPos = m_rect->pos();
378     int size;
379     if (rectSize.width() / m_monitor->render->dar() < rectSize.height())
380         size = (int)((rectSize.width() * 100.0 / m_monitor->render->renderWidth()) + 0.5);
381     else
382         size = (int)((rectSize.height() * 100.0 / m_monitor->render->renderHeight()) + 0.5);
383
384     m_ui.spinX->blockSignals(true);
385     m_ui.spinY->blockSignals(true);
386     m_ui.spinWidth->blockSignals(true);
387     m_ui.spinHeight->blockSignals(true);
388     m_ui.spinSize->blockSignals(true);
389
390     m_ui.spinX->setValue(rectPos.x());
391     m_ui.spinY->setValue(rectPos.y());
392     m_ui.spinWidth->setValue(rectSize.width());
393     m_ui.spinHeight->setValue(rectSize.height());
394     m_ui.spinSize->setValue(size);
395
396     m_ui.spinX->blockSignals(false);
397     m_ui.spinY->blockSignals(false);
398     m_ui.spinWidth->blockSignals(false);
399     m_ui.spinHeight->blockSignals(false);
400     m_ui.spinSize->blockSignals(false);
401 }
402
403
404 void GeometryWidget::slotSetX(int value)
405 {
406     m_rect->setPos(value, m_ui.spinY->value());
407     slotUpdateGeometry();
408 }
409
410 void GeometryWidget::slotSetY(int value)
411 {
412     m_rect->setPos(m_ui.spinX->value(), value);
413     slotUpdateGeometry();
414 }
415
416 void GeometryWidget::slotSetWidth(int value)
417 {
418     m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
419     slotUpdateGeometry();
420 }
421
422 void GeometryWidget::slotSetHeight(int value)
423 {
424     m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
425     slotUpdateGeometry();
426 }
427
428
429 void GeometryWidget::slotResize(int value)
430 {
431     m_rect->setRect(0, 0,
432                     (int)((m_monitor->render->renderWidth() * value / 100.0) + 0.5),
433                     (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
434     slotUpdateGeometry();
435 }
436
437
438 void GeometryWidget::slotSetOpacity(int value)
439 {
440     m_ui.sliderOpacity->blockSignals(true);
441     m_ui.sliderOpacity->setValue(value);
442     m_ui.sliderOpacity->blockSignals(false);
443
444     int pos = m_timePos->getValue();
445     Mlt::GeometryItem item;
446     if (m_geometry->fetch(&item, pos) || item.key() == false)
447         return;
448     item.mix(value);
449     m_geometry->insert(item);
450
451     emit parameterChanged();
452 }
453
454
455 void GeometryWidget::slotMoveLeft()
456 {
457     m_rect->setPos(0, m_rect->pos().y());
458     slotUpdateGeometry();
459 }
460
461 void GeometryWidget::slotCenterH()
462 {
463     m_rect->setPos((m_monitor->render->renderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
464     slotUpdateGeometry();
465 }
466
467 void GeometryWidget::slotMoveRight()
468 {
469     m_rect->setPos(m_monitor->render->renderWidth() - m_rect->rect().width(), m_rect->pos().y());
470     slotUpdateGeometry();
471 }
472
473 void GeometryWidget::slotMoveTop()
474 {
475     m_rect->setPos(m_rect->pos().x(), 0);
476     slotUpdateGeometry();
477 }
478
479 void GeometryWidget::slotCenterV()
480 {
481     m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
482     slotUpdateGeometry();
483 }
484
485 void GeometryWidget::slotMoveBottom()
486 {
487     m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
488     slotUpdateGeometry();
489 }
490
491
492 void GeometryWidget::slotSetSynchronize(bool sync)
493 {
494     KdenliveSettings::setTransitionfollowcursor(sync);
495 }
496
497 void GeometryWidget::slotShowScene(bool show)
498 {
499     m_showScene = show;
500     if (!m_showScene)
501         m_monitor->slotEffectScene(false);
502     else
503         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
504 }
505
506 #include "geometrywidget.moc"