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);
304 if (m_previous == NULL) {
305 m_previous = new QGraphicsRectItem(0, 0, previousItem.w(), previousItem.h());
306 m_previous->setBrush(QColor(200, 200, 0, 20));
307 m_previous->setPen(QPen(Qt::white, 0, Qt::DotLine));
309 m_previous->setPos(previousItem.x(), previousItem.y());
310 m_previous->setZValue(-1);
311 m_previous->setEnabled(false);
314 m_previous->setPos(previousItem.x(), previousItem.y());
315 m_previous->setRect(0, 0, previousItem.w(), previousItem.h());
317 m_scene->addItem(m_previous);
320 m_rect->setPos(item.x(), item.y());
321 m_rect->setRect(0, 0, item.w(), item.h());
323 m_opacity->blockSignals(true);
324 m_opacity->setValue(item.mix());
325 m_opacity->blockSignals(false);
327 slotUpdateProperties();
329 if (seek && KdenliveSettings::transitionfollowcursor())
330 emit seekToPos(m_clipPos + pos);
333 void GeometryWidget::slotKeyframeMoved(int pos)
335 slotPositionChanged(pos);
336 slotUpdateGeometry();
337 QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
340 void GeometryWidget::slotAddKeyframe(int pos)
342 Mlt::GeometryItem item;
344 pos = m_timePos->getValue();
346 QRectF r = m_rect->rect().normalized();
347 QPointF rectpos = m_rect->pos();
352 item.mix(m_opacity->value());
353 m_geometry->insert(item);
355 m_timeline->update();
356 slotPositionChanged(pos, false);
357 emit parameterChanged();
360 void GeometryWidget::slotDeleteKeyframe(int pos)
362 Mlt::GeometryItem item;
364 pos = m_timePos->getValue();
365 // check there is more than one keyframe, do not allow to delete last one
366 if (m_geometry->next_key(&item, pos + 1)) {
367 if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
370 m_geometry->remove(pos);
372 m_timeline->update();
373 slotPositionChanged(pos, false);
374 emit parameterChanged();
377 void GeometryWidget::slotPreviousKeyframe()
379 Mlt::GeometryItem item;
380 // Go to start if no keyframe is found
381 int currentPos = m_timePos->getValue();
383 if (!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
386 slotPositionChanged(pos);
389 void GeometryWidget::slotNextKeyframe()
391 Mlt::GeometryItem item;
392 // Go to end if no keyframe is found
393 int pos = m_timeline->frameLength;
394 if (!m_geometry->next_key(&item, m_timeline->value() + 1))
397 slotPositionChanged(pos);
400 void GeometryWidget::slotAddDeleteKeyframe()
402 Mlt::GeometryItem item;
403 if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
406 slotDeleteKeyframe();
410 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
414 We do only get the position in timeline if this geometry belongs to a transition,
415 therefore we need two ways here.
418 emit checkMonitorPosition(renderPos);
420 if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
421 if (!m_scene->views().at(0)->isVisible())
422 m_monitor->slotEffectScene(true);
424 m_monitor->slotEffectScene(false);
431 void GeometryWidget::slotUpdateGeometry()
433 Mlt::GeometryItem item;
434 int pos = m_timePos->getValue();
436 // get keyframe and make sure it is the correct one
437 if (m_geometry->next_key(&item, pos) || item.frame() != pos)
440 QRectF rectSize = m_rect->rect().normalized();
441 QPointF rectPos = m_rect->pos();
444 item.w(rectSize.width());
445 item.h(rectSize.height());
446 m_geometry->insert(item);
447 emit parameterChanged();
450 void GeometryWidget::slotUpdateProperties()
452 QRectF rectSize = m_rect->rect().normalized();
453 QPointF rectPos = m_rect->pos();
455 if (rectSize.width() / m_monitor->render->dar() > rectSize.height())
456 size = rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth();
458 size = rectSize.height() * 100.0 / m_monitor->render->renderHeight();
460 m_spinX->blockSignals(true);
461 m_spinY->blockSignals(true);
462 m_spinWidth->blockSignals(true);
463 m_spinHeight->blockSignals(true);
464 m_spinSize->blockSignals(true);
466 m_spinX->setValue(rectPos.x());
467 m_spinY->setValue(rectPos.y());
468 m_spinWidth->setValue(rectSize.width());
469 m_spinHeight->setValue(rectSize.height());
470 m_spinSize->setValue(size);
472 m_spinX->blockSignals(false);
473 m_spinY->blockSignals(false);
474 m_spinWidth->blockSignals(false);
475 m_spinHeight->blockSignals(false);
476 m_spinSize->blockSignals(false);
480 void GeometryWidget::slotSetX(int value)
482 m_rect->setPos(value, m_spinY->value());
483 slotUpdateGeometry();
486 void GeometryWidget::slotSetY(int value)
488 m_rect->setPos(m_spinX->value(), value);
489 slotUpdateGeometry();
492 void GeometryWidget::slotSetWidth(int value)
494 m_rect->setRect(0, 0, value, m_spinHeight->value());
495 slotUpdateGeometry();
498 void GeometryWidget::slotSetHeight(int value)
500 m_rect->setRect(0, 0, m_spinWidth->value(), value);
501 slotUpdateGeometry();
505 void GeometryWidget::slotResize(double value)
507 m_rect->setRect(0, 0,
508 (int)((m_monitor->render->frameRenderWidth() * value / 100.0) + 0.5),
509 (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
510 slotUpdateGeometry();
514 void GeometryWidget::slotSetOpacity(int value)
516 int pos = m_timePos->getValue();
517 Mlt::GeometryItem item;
518 if (m_geometry->fetch(&item, pos) || item.key() == false)
521 m_geometry->insert(item);
522 emit parameterChanged();
526 void GeometryWidget::slotMoveLeft()
528 m_rect->setPos(0, m_rect->pos().y());
529 slotUpdateGeometry();
532 void GeometryWidget::slotCenterH()
534 m_rect->setPos((m_monitor->render->frameRenderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
535 slotUpdateGeometry();
538 void GeometryWidget::slotMoveRight()
540 m_rect->setPos(m_monitor->render->frameRenderWidth() - m_rect->rect().width(), m_rect->pos().y());
541 slotUpdateGeometry();
544 void GeometryWidget::slotMoveTop()
546 m_rect->setPos(m_rect->pos().x(), 0);
547 slotUpdateGeometry();
550 void GeometryWidget::slotCenterV()
552 m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
553 slotUpdateGeometry();
556 void GeometryWidget::slotMoveBottom()
558 m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
559 slotUpdateGeometry();
563 void GeometryWidget::slotSetSynchronize(bool sync)
565 KdenliveSettings::setTransitionfollowcursor(sync);
567 emit seekToPos(m_clipPos + m_timePos->getValue());
570 void GeometryWidget::slotShowScene(bool show)
574 m_monitor->slotEffectScene(false);
576 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
579 void GeometryWidget::setFrameSize(QPoint size)
584 void GeometryWidget::slotAdjustToFrameSize()
586 if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
587 m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() + 0.5));
588 m_spinHeight->setValue(m_frameSize.y());
591 void GeometryWidget::slotFitToWidth()
593 if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
594 double factor = (double) m_monitor->render->frameRenderWidth() / m_frameSize.x() * m_monitor->render->sar();
595 m_spinHeight->setValue((int) (m_frameSize.y() * factor + 0.5));
596 m_spinWidth->setValue(m_monitor->render->frameRenderWidth());
599 void GeometryWidget::slotFitToHeight()
601 if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
602 double factor = (double) m_monitor->render->renderHeight() / m_frameSize.y();
603 m_spinHeight->setValue(m_monitor->render->renderHeight());
604 m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() * factor + 0.5));
607 #include "geometrywidget.moc"