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