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