1 /***************************************************************************
2 * Copyright (C) 2010 by Till Theato (root@ttill.de) *
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. *
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. *
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 ***************************************************************************/
21 #include "geometrywidget.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"
33 #include <QGraphicsView>
34 #include <QVBoxLayout>
35 #include <QGridLayout>
40 GeometryWidget::GeometryWidget(Monitor* monitor, Timecode timecode, int clipPos, bool isEffect, QWidget* parent):
43 m_timePos(new TimecodeDisplay(timecode)),
54 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
55 MonitorEditWidget *edit = monitor->getEffectEdit();
56 edit->removeCustomControls();
57 edit->addCustomButton(KIcon("transform-crop"), i18n("Show previous keyframe"), this, SLOT(slotShowPreviousKeyFrame(bool)), true, KdenliveSettings::onmonitoreffects_geometryshowprevious());
58 edit->showVisibilityButton(true);
59 m_scene = edit->getScene();
63 Setup of timeline and keyframe controls
66 ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 6);
68 QVBoxLayout *layout = new QVBoxLayout(m_ui.frameTimeline);
69 m_timeline = new KeyframeHelper(m_ui.frameTimeline);
70 layout->addWidget(m_timeline);
71 layout->setContentsMargins(0, 0, 0, 0);
73 m_ui.buttonPrevious->setIcon(KIcon("media-skip-backward"));
74 m_ui.buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
75 m_ui.buttonNext->setIcon(KIcon("media-skip-forward"));
76 m_ui.buttonNext->setToolTip(i18n("Go to next keyframe"));
77 m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
78 m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
80 m_ui.buttonSync->setIcon(KIcon("insert-link"));
81 m_ui.buttonSync->setToolTip(i18n("Synchronize with timeline cursor"));
82 m_ui.buttonSync->setChecked(KdenliveSettings::transitionfollowcursor());
84 connect(m_timeline, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
85 connect(m_timeline, SIGNAL(keyframeMoved(int)), this, SLOT(slotKeyframeMoved(int)));
86 connect(m_timeline, SIGNAL(addKeyframe(int)), this, SLOT(slotAddKeyframe(int)));
87 connect(m_timeline, SIGNAL(removeKeyframe(int)), this, SLOT(slotDeleteKeyframe(int)));
88 connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotPositionChanged()));
89 connect(m_ui.buttonPrevious, SIGNAL(clicked()), this, SLOT(slotPreviousKeyframe()));
90 connect(m_ui.buttonNext, SIGNAL(clicked()), this, SLOT(slotNextKeyframe()));
91 connect(m_ui.buttonAddDelete, SIGNAL(clicked()), this, SLOT(slotAddDeleteKeyframe()));
92 connect(m_ui.buttonSync, SIGNAL(toggled(bool)), this, SLOT(slotSetSynchronize(bool)));
94 m_spinX = new DragValue(i18n("X"), 0, 0, -1, QString(), false, this);
95 m_spinX->setRange(-10000, 10000);
96 m_ui.horizontalLayout->addWidget(m_spinX);
98 m_spinY = new DragValue(i18n("Y"), 0, 0, -1, QString(), false, this);
99 m_spinY->setRange(-10000, 10000);
100 m_ui.horizontalLayout->addWidget(m_spinY);
102 m_spinWidth = new DragValue(i18n("W"), m_monitor->render->frameRenderWidth(), 0, -1, QString(), false, this);
103 m_spinWidth->setRange(1, 10000);
104 m_ui.horizontalLayout->addWidget(m_spinWidth);
106 m_spinHeight = new DragValue(i18n("H"), m_monitor->render->renderHeight(), 0, -1, QString(), false, this);
107 m_spinHeight->setRange(1, 10000);
108 m_ui.horizontalLayout->addWidget(m_spinHeight);
109 m_ui.horizontalLayout->addStretch(10);
111 m_spinSize = new DragValue(i18n("Size"), 100, 2, -1, i18n("%"), false, this);
112 m_spinSize->setRange(1, 10000);
113 m_ui.horizontalLayout2->addWidget(m_spinSize);
115 m_opacity = new DragValue(i18n("Opacity"), 100, 0, -1, i18n("%"), true, this);
116 m_ui.horizontalLayout2->addWidget(m_opacity);
119 Setup of geometry controls
122 connect(m_spinX, SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
123 connect(m_spinY, SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
124 connect(m_spinWidth, SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
125 connect(m_spinHeight, SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
127 connect(m_spinSize, SIGNAL(valueChanged(double)), this, SLOT(slotResize(double)));
129 connect(m_opacity, SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
131 QMenu *menu = new QMenu(this);
132 QAction *adjustSize = new QAction(i18n("Adjust to original size"), this);
133 connect(adjustSize, SIGNAL(triggered()), this, SLOT(slotAdjustToFrameSize()));
134 menu->addAction(adjustSize);
135 QAction *fitToWidth = new QAction(i18n("Fit to width"), this);
136 connect(fitToWidth, SIGNAL(triggered()), this, SLOT(slotFitToWidth()));
137 menu->addAction(fitToWidth);
138 QAction *fitToHeight = new QAction(i18n("Fit to height"), this);
139 connect(fitToHeight, SIGNAL(triggered()), this, SLOT(slotFitToHeight()));
140 menu->addAction(fitToHeight);
141 menu->addSeparator();
143 QAction *alignleft = new QAction(KIcon("kdenlive-align-left"), i18n("Align left"), this);
144 connect(alignleft, SIGNAL(triggered()), this, SLOT(slotMoveLeft()));
145 menu->addAction(alignleft);
146 QAction *alignhcenter = new QAction(KIcon("kdenlive-align-hor"), i18n("Center horizontally"), this);
147 connect(alignhcenter, SIGNAL(triggered()), this, SLOT(slotCenterH()));
148 menu->addAction(alignhcenter);
149 QAction *alignright = new QAction(KIcon("kdenlive-align-right"), i18n("Align right"), this);
150 connect(alignright, SIGNAL(triggered()), this, SLOT(slotMoveRight()));
151 menu->addAction(alignright);
152 QAction *aligntop = new QAction(KIcon("kdenlive-align-top"), i18n("Align top"), this);
153 connect(aligntop, SIGNAL(triggered()), this, SLOT(slotMoveTop()));
154 menu->addAction(aligntop);
155 QAction *alignvcenter = new QAction(KIcon("kdenlive-align-vert"), i18n("Center vertically"), this);
156 connect(alignvcenter, SIGNAL(triggered()), this, SLOT(slotCenterV()));
157 menu->addAction(alignvcenter);
158 QAction *alignbottom = new QAction(KIcon("kdenlive-align-bottom"), i18n("Align bottom"), this);
159 connect(alignbottom, SIGNAL(triggered()), this, SLOT(slotMoveBottom()));
160 menu->addAction(alignbottom);
161 m_ui.buttonOptions->setMenu(menu);
163 /*connect(m_ui.buttonMoveLeft, SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
164 connect(m_ui.buttonCenterH, SIGNAL(clicked()), this, SLOT(slotCenterH()));
165 connect(m_ui.buttonMoveRight, SIGNAL(clicked()), this, SLOT(slotMoveRight()));
166 connect(m_ui.buttonMoveTop, SIGNAL(clicked()), this, SLOT(slotMoveTop()));
167 connect(m_ui.buttonCenterV, SIGNAL(clicked()), this, SLOT(slotCenterV()));
168 connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));*/
172 Setup of configuration controls
175 connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
177 connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
178 connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
179 connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
182 GeometryWidget::~GeometryWidget()
184 m_scene->setEnabled(true);
192 m_scene->removeItem(m_rect);
193 if (m_rect) delete m_rect;
194 if (m_previous) delete m_previous;
197 m_monitor->getEffectEdit()->showVisibilityButton(false);
198 m_monitor->slotEffectScene(false);
202 void GeometryWidget::slotShowPreviousKeyFrame(bool show)
204 KdenliveSettings::setOnmonitoreffects_geometryshowprevious(show);
205 slotPositionChanged(-1, false);
208 void GeometryWidget::updateTimecodeFormat()
210 m_timePos->slotUpdateTimeCodeFormat();
213 QString GeometryWidget::getValue() const
215 return m_geometry->serialise();
218 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
220 m_inPoint = minframe;
221 m_outPoint = maxframe;
224 m_geometry->parse(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
226 m_geometry = new Mlt::Geometry(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
228 if (elem.attribute("fixed") == "1" || maxframe < minframe) {
229 // Keyframes are disabled
230 m_ui.widgetTimeWrapper->setHidden(true);
232 m_ui.widgetTimeWrapper->setHidden(false);
233 m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint - 1);
234 m_timeline->update();
235 m_timePos->setRange(0, m_outPoint - m_inPoint - 1);
239 if (elem.attribute("opacity") == "false") {
240 m_opacity->setHidden(true);
241 m_ui.horizontalLayout2->addStretch(2);
244 Mlt::GeometryItem item;
246 m_geometry->fetch(&item, 0);
248 m_rect = new OnMonitorRectItem(QRectF(0, 0, item.w(), item.h()), m_monitor->render->dar());
249 m_rect->setPos(item.x(), item.y());
250 m_rect->setZValue(0);
251 m_scene->addItem(m_rect);
252 connect(m_rect, SIGNAL(changed()), this, SLOT(slotUpdateGeometry()));
254 slotPositionChanged(0, false);
255 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
258 void GeometryWidget::slotSyncPosition(int relTimelinePos)
260 // do only sync if this effect is keyframable
261 if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
262 relTimelinePos = qBound(0, relTimelinePos, m_timePos->maximum());
263 if (relTimelinePos != m_timePos->getValue())
264 slotPositionChanged(relTimelinePos, false);
269 void GeometryWidget::slotPositionChanged(int pos, bool seek)
272 pos = m_timePos->getValue();
274 m_timePos->setValue(pos);
276 m_timeline->blockSignals(true);
277 m_timeline->setValue(pos);
278 m_timeline->blockSignals(false);
280 Mlt::GeometryItem item;
281 Mlt::GeometryItem previousItem;
282 if (m_geometry->fetch(&item, pos) || item.key() == false) {
284 m_rect->setEnabled(false);
285 m_scene->setEnabled(false);
286 m_ui.widgetGeometry->setEnabled(false);
287 m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
288 m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
291 m_rect->setEnabled(true);
292 m_scene->setEnabled(true);
293 m_ui.widgetGeometry->setEnabled(true);
294 m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
295 m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
298 if (KdenliveSettings::onmonitoreffects_geometryshowprevious() == false || m_geometry->prev_key(&previousItem, pos - 1) || previousItem.frame() == item.frame()) {
300 m_scene->removeItem(m_previous);
303 else if (m_previous && m_previous->scene() && m_previous->data(Qt::UserRole).toInt() == previousItem.frame()) {
304 // previous frame already here, do nothing
307 if (m_previous == NULL) {
308 m_previous = new QGraphicsRectItem(0, 0, previousItem.w(), previousItem.h());
309 m_previous->setBrush(QColor(200, 200, 0, 20));
310 m_previous->setPen(QPen(Qt::white, 0, Qt::DotLine));
311 m_previous->setPos(previousItem.x(), previousItem.y());
312 m_previous->setZValue(-1);
313 m_previous->setEnabled(false);
316 m_previous->setPos(previousItem.x(), previousItem.y());
317 m_previous->setRect(0, 0, previousItem.w(), previousItem.h());
319 m_previous->setData(Qt::UserRole, previousItem.frame());
320 if (m_previous->scene() == 0) m_scene->addItem(m_previous);
323 m_rect->setPos(item.x(), item.y());
324 m_rect->setRect(0, 0, item.w(), item.h());
326 m_opacity->blockSignals(true);
327 m_opacity->setValue(item.mix());
328 m_opacity->blockSignals(false);
330 slotUpdateProperties();
332 if (seek && KdenliveSettings::transitionfollowcursor())
333 emit seekToPos(m_clipPos + pos);
336 void GeometryWidget::slotKeyframeMoved(int pos)
338 slotPositionChanged(pos);
339 slotUpdateGeometry();
340 QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
343 void GeometryWidget::slotAddKeyframe(int pos)
345 Mlt::GeometryItem item;
347 pos = m_timePos->getValue();
349 QRectF r = m_rect->rect().normalized();
350 QPointF rectpos = m_rect->pos();
355 item.mix(m_opacity->value());
356 m_geometry->insert(item);
358 m_timeline->update();
359 slotPositionChanged(pos, false);
360 emit parameterChanged();
363 void GeometryWidget::slotDeleteKeyframe(int pos)
365 Mlt::GeometryItem item;
367 pos = m_timePos->getValue();
368 // check there is more than one keyframe, do not allow to delete last one
369 if (m_geometry->next_key(&item, pos + 1)) {
370 if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
373 m_geometry->remove(pos);
375 m_timeline->update();
376 slotPositionChanged(pos, false);
377 emit parameterChanged();
380 void GeometryWidget::slotPreviousKeyframe()
382 Mlt::GeometryItem item;
383 // Go to start if no keyframe is found
384 int currentPos = m_timePos->getValue();
386 if (!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
389 slotPositionChanged(pos);
392 void GeometryWidget::slotNextKeyframe()
394 Mlt::GeometryItem item;
395 // Go to end if no keyframe is found
396 int pos = m_timeline->frameLength;
397 if (!m_geometry->next_key(&item, m_timeline->value() + 1))
400 slotPositionChanged(pos);
403 void GeometryWidget::slotAddDeleteKeyframe()
405 Mlt::GeometryItem item;
406 if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
409 slotDeleteKeyframe();
413 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
417 We do only get the position in timeline if this geometry belongs to a transition,
418 therefore we need two ways here.
421 emit checkMonitorPosition(renderPos);
423 if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
424 if (!m_scene->views().at(0)->isVisible())
425 m_monitor->slotEffectScene(true);
427 m_monitor->slotEffectScene(false);
434 void GeometryWidget::slotUpdateGeometry()
436 Mlt::GeometryItem item;
437 int pos = m_timePos->getValue();
439 // get keyframe and make sure it is the correct one
440 if (m_geometry->next_key(&item, pos) || item.frame() != pos)
443 QRectF rectSize = m_rect->rect().normalized();
444 QPointF rectPos = m_rect->pos();
447 item.w(rectSize.width());
448 item.h(rectSize.height());
449 m_geometry->insert(item);
450 emit parameterChanged();
453 void GeometryWidget::slotUpdateProperties()
455 QRectF rectSize = m_rect->rect().normalized();
456 QPointF rectPos = m_rect->pos();
458 if (rectSize.width() / m_monitor->render->dar() > rectSize.height())
459 size = rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth();
461 size = rectSize.height() * 100.0 / m_monitor->render->renderHeight();
463 m_spinX->blockSignals(true);
464 m_spinY->blockSignals(true);
465 m_spinWidth->blockSignals(true);
466 m_spinHeight->blockSignals(true);
467 m_spinSize->blockSignals(true);
469 m_spinX->setValue(rectPos.x());
470 m_spinY->setValue(rectPos.y());
471 m_spinWidth->setValue(rectSize.width());
472 m_spinHeight->setValue(rectSize.height());
473 m_spinSize->setValue(size);
475 m_spinX->blockSignals(false);
476 m_spinY->blockSignals(false);
477 m_spinWidth->blockSignals(false);
478 m_spinHeight->blockSignals(false);
479 m_spinSize->blockSignals(false);
483 void GeometryWidget::slotSetX(int value)
485 m_rect->setPos(value, m_spinY->value());
486 slotUpdateGeometry();
489 void GeometryWidget::slotSetY(int value)
491 m_rect->setPos(m_spinX->value(), value);
492 slotUpdateGeometry();
495 void GeometryWidget::slotSetWidth(int value)
497 m_rect->setRect(0, 0, value, m_spinHeight->value());
498 slotUpdateGeometry();
501 void GeometryWidget::slotSetHeight(int value)
503 m_rect->setRect(0, 0, m_spinWidth->value(), value);
504 slotUpdateGeometry();
508 void GeometryWidget::slotResize(double value)
510 m_rect->setRect(0, 0,
511 (int)((m_monitor->render->frameRenderWidth() * value / 100.0) + 0.5),
512 (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
513 slotUpdateGeometry();
517 void GeometryWidget::slotSetOpacity(int value)
519 int pos = m_timePos->getValue();
520 Mlt::GeometryItem item;
521 if (m_geometry->fetch(&item, pos) || item.key() == false)
524 m_geometry->insert(item);
525 emit parameterChanged();
529 void GeometryWidget::slotMoveLeft()
531 m_rect->setPos(0, m_rect->pos().y());
532 slotUpdateGeometry();
535 void GeometryWidget::slotCenterH()
537 m_rect->setPos((m_monitor->render->frameRenderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
538 slotUpdateGeometry();
541 void GeometryWidget::slotMoveRight()
543 m_rect->setPos(m_monitor->render->frameRenderWidth() - m_rect->rect().width(), m_rect->pos().y());
544 slotUpdateGeometry();
547 void GeometryWidget::slotMoveTop()
549 m_rect->setPos(m_rect->pos().x(), 0);
550 slotUpdateGeometry();
553 void GeometryWidget::slotCenterV()
555 m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
556 slotUpdateGeometry();
559 void GeometryWidget::slotMoveBottom()
561 m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
562 slotUpdateGeometry();
566 void GeometryWidget::slotSetSynchronize(bool sync)
568 KdenliveSettings::setTransitionfollowcursor(sync);
570 emit seekToPos(m_clipPos + m_timePos->getValue());
573 void GeometryWidget::slotShowScene(bool show)
577 m_monitor->slotEffectScene(false);
579 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
582 void GeometryWidget::setFrameSize(QPoint size)
587 void GeometryWidget::slotAdjustToFrameSize()
589 if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
590 m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() + 0.5));
591 m_spinHeight->setValue(m_frameSize.y());
594 void GeometryWidget::slotFitToWidth()
596 if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
597 double factor = (double) m_monitor->render->frameRenderWidth() / m_frameSize.x() * m_monitor->render->sar();
598 m_spinHeight->setValue((int) (m_frameSize.y() * factor + 0.5));
599 m_spinWidth->setValue(m_monitor->render->frameRenderWidth());
602 void GeometryWidget::slotFitToHeight()
604 if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
605 double factor = (double) m_monitor->render->renderHeight() / m_frameSize.y();
606 m_spinHeight->setValue(m_monitor->render->renderHeight());
607 m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() * factor + 0.5));
610 #include "geometrywidget.moc"