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)),
53 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
54 MonitorEditWidget *edit = monitor->getEffectEdit();
55 edit->showVisibilityButton(true);
56 m_scene = edit->getScene();
60 Setup of timeline and keyframe controls
63 ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 6);
65 QVBoxLayout *layout = new QVBoxLayout(m_ui.frameTimeline);
66 m_timeline = new KeyframeHelper(m_ui.frameTimeline);
67 layout->addWidget(m_timeline);
68 layout->setContentsMargins(0, 0, 0, 0);
70 m_ui.buttonPrevious->setIcon(KIcon("media-skip-backward"));
71 m_ui.buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
72 m_ui.buttonNext->setIcon(KIcon("media-skip-forward"));
73 m_ui.buttonNext->setToolTip(i18n("Go to next keyframe"));
74 m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
75 m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
77 m_ui.buttonSync->setIcon(KIcon("insert-link"));
78 m_ui.buttonSync->setToolTip(i18n("Synchronize with timeline cursor"));
79 m_ui.buttonSync->setChecked(KdenliveSettings::transitionfollowcursor());
81 connect(m_timeline, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
82 connect(m_timeline, SIGNAL(keyframeMoved(int)), this, SLOT(slotKeyframeMoved(int)));
83 connect(m_timeline, SIGNAL(addKeyframe(int)), this, SLOT(slotAddKeyframe(int)));
84 connect(m_timeline, SIGNAL(removeKeyframe(int)), this, SLOT(slotDeleteKeyframe(int)));
85 connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotPositionChanged()));
86 connect(m_ui.buttonPrevious, SIGNAL(clicked()), this, SLOT(slotPreviousKeyframe()));
87 connect(m_ui.buttonNext, SIGNAL(clicked()), this, SLOT(slotNextKeyframe()));
88 connect(m_ui.buttonAddDelete, SIGNAL(clicked()), this, SLOT(slotAddDeleteKeyframe()));
89 connect(m_ui.buttonSync, SIGNAL(toggled(bool)), this, SLOT(slotSetSynchronize(bool)));
91 m_spinX = new DragValue(i18n("X"), 0, 0, -1, QString(), false, this);
92 m_spinX->setRange(-10000, 10000);
93 m_ui.horizontalLayout->addWidget(m_spinX);
95 m_spinY = new DragValue(i18n("Y"), 0, 0, -1, QString(), false, this);
96 m_spinY->setRange(-10000, 10000);
97 m_ui.horizontalLayout->addWidget(m_spinY);
99 m_spinWidth = new DragValue(i18n("W"), m_monitor->render->frameRenderWidth(), 0, -1, QString(), false, this);
100 m_spinWidth->setRange(1, 10000);
101 m_ui.horizontalLayout->addWidget(m_spinWidth);
103 m_spinHeight = new DragValue(i18n("H"), m_monitor->render->renderHeight(), 0, -1, QString(), false, this);
104 m_spinHeight->setRange(1, 10000);
105 m_ui.horizontalLayout->addWidget(m_spinHeight);
106 m_ui.horizontalLayout->addStretch(10);
108 m_spinSize = new DragValue(i18n("Size"), 100, 2, -1, i18n("%"), false, this);
109 m_spinSize->setRange(1, 10000);
110 m_ui.horizontalLayout2->addWidget(m_spinSize);
112 m_opacity = new DragValue(i18n("Opacity"), 100, 0, -1, i18n("%"), true, this);
113 m_ui.horizontalLayout2->addWidget(m_opacity);
116 Setup of geometry controls
119 connect(m_spinX, SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
120 connect(m_spinY, SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
121 connect(m_spinWidth, SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
122 connect(m_spinHeight, SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
124 connect(m_spinSize, SIGNAL(valueChanged(double)), this, SLOT(slotResize(double)));
126 connect(m_opacity, SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
128 QMenu *menu = new QMenu(this);
129 QAction *adjustSize = new QAction(i18n("Adjust to original size"), this);
130 connect(adjustSize, SIGNAL(triggered()), this, SLOT(slotAdjustToFrameSize()));
131 menu->addAction(adjustSize);
132 QAction *fitToWidth = new QAction(i18n("Fit to width"), this);
133 connect(fitToWidth, SIGNAL(triggered()), this, SLOT(slotFitToWidth()));
134 menu->addAction(fitToWidth);
135 QAction *fitToHeight = new QAction(i18n("Fit to height"), this);
136 connect(fitToHeight, SIGNAL(triggered()), this, SLOT(slotFitToHeight()));
137 menu->addAction(fitToHeight);
138 menu->addSeparator();
140 QAction *alignleft = new QAction(KIcon("kdenlive-align-left"), i18n("Align left"), this);
141 connect(alignleft, SIGNAL(triggered()), this, SLOT(slotMoveLeft()));
142 menu->addAction(alignleft);
143 QAction *alignhcenter = new QAction(KIcon("kdenlive-align-hor"), i18n("Center horizontally"), this);
144 connect(alignhcenter, SIGNAL(triggered()), this, SLOT(slotCenterH()));
145 menu->addAction(alignhcenter);
146 QAction *alignright = new QAction(KIcon("kdenlive-align-right"), i18n("Align right"), this);
147 connect(alignright, SIGNAL(triggered()), this, SLOT(slotMoveRight()));
148 menu->addAction(alignright);
149 QAction *aligntop = new QAction(KIcon("kdenlive-align-top"), i18n("Align top"), this);
150 connect(aligntop, SIGNAL(triggered()), this, SLOT(slotMoveTop()));
151 menu->addAction(aligntop);
152 QAction *alignvcenter = new QAction(KIcon("kdenlive-align-vert"), i18n("Center vertically"), this);
153 connect(alignvcenter, SIGNAL(triggered()), this, SLOT(slotCenterV()));
154 menu->addAction(alignvcenter);
155 QAction *alignbottom = new QAction(KIcon("kdenlive-align-bottom"), i18n("Align bottom"), this);
156 connect(alignbottom, SIGNAL(triggered()), this, SLOT(slotMoveBottom()));
157 menu->addAction(alignbottom);
158 m_ui.buttonOptions->setMenu(menu);
160 /*connect(m_ui.buttonMoveLeft, SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
161 connect(m_ui.buttonCenterH, SIGNAL(clicked()), this, SLOT(slotCenterH()));
162 connect(m_ui.buttonMoveRight, SIGNAL(clicked()), this, SLOT(slotMoveRight()));
163 connect(m_ui.buttonMoveTop, SIGNAL(clicked()), this, SLOT(slotMoveTop()));
164 connect(m_ui.buttonCenterV, SIGNAL(clicked()), this, SLOT(slotCenterV()));
165 connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));*/
169 Setup of configuration controls
172 connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
174 connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
175 connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
176 connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
179 GeometryWidget::~GeometryWidget()
181 m_scene->setEnabled(true);
189 m_scene->removeItem(m_rect);
192 m_monitor->getEffectEdit()->showVisibilityButton(false);
193 m_monitor->slotEffectScene(false);
197 void GeometryWidget::updateTimecodeFormat()
199 m_timePos->slotUpdateTimeCodeFormat();
202 QString GeometryWidget::getValue() const
204 return m_geometry->serialise();
207 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
209 m_inPoint = minframe;
210 m_outPoint = maxframe;
213 m_geometry->parse(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
215 m_geometry = new Mlt::Geometry(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
217 if (elem.attribute("fixed") == "1" || maxframe < minframe) {
218 // Keyframes are disabled
219 m_ui.widgetTimeWrapper->setHidden(true);
221 m_ui.widgetTimeWrapper->setHidden(false);
222 m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint - 1);
223 m_timeline->update();
224 m_timePos->setRange(0, m_outPoint - m_inPoint - 1);
228 if (elem.attribute("opacity") == "false") {
229 m_opacity->setHidden(true);
230 m_ui.horizontalLayout2->addStretch(2);
233 Mlt::GeometryItem item;
235 m_geometry->fetch(&item, 0);
237 m_rect = new OnMonitorRectItem(QRectF(0, 0, item.w(), item.h()), m_monitor->render->dar());
238 m_rect->setPos(item.x(), item.y());
239 m_rect->setZValue(0);
240 m_scene->addItem(m_rect);
241 connect(m_rect, SIGNAL(changed()), this, SLOT(slotUpdateGeometry()));
243 slotPositionChanged(0, false);
244 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
247 void GeometryWidget::slotSyncPosition(int relTimelinePos)
249 // do only sync if this effect is keyframable
250 if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
251 relTimelinePos = qBound(0, relTimelinePos, m_timePos->maximum());
252 if (relTimelinePos != m_timePos->getValue())
253 slotPositionChanged(relTimelinePos, false);
258 void GeometryWidget::slotPositionChanged(int pos, bool seek)
261 pos = m_timePos->getValue();
263 m_timePos->setValue(pos);
265 m_timeline->blockSignals(true);
266 m_timeline->setValue(pos);
267 m_timeline->blockSignals(false);
269 Mlt::GeometryItem item;
270 if (m_geometry->fetch(&item, pos) || item.key() == false) {
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"));
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"));
286 m_rect->setPos(item.x(), item.y());
287 m_rect->setRect(0, 0, item.w(), item.h());
289 m_opacity->blockSignals(true);
290 m_opacity->setValue(item.mix());
291 m_opacity->blockSignals(false);
293 slotUpdateProperties();
295 if (seek && KdenliveSettings::transitionfollowcursor())
296 emit seekToPos(m_clipPos + pos);
299 void GeometryWidget::slotKeyframeMoved(int pos)
301 slotPositionChanged(pos);
302 slotUpdateGeometry();
303 QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
306 void GeometryWidget::slotAddKeyframe(int pos)
308 Mlt::GeometryItem item;
310 pos = m_timePos->getValue();
312 QRectF r = m_rect->rect().normalized();
313 QPointF rectpos = m_rect->pos();
318 item.mix(m_opacity->value());
319 m_geometry->insert(item);
321 m_timeline->update();
322 slotPositionChanged(pos, false);
323 emit parameterChanged();
326 void GeometryWidget::slotDeleteKeyframe(int pos)
328 Mlt::GeometryItem item;
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)
336 m_geometry->remove(pos);
338 m_timeline->update();
339 slotPositionChanged(pos, false);
340 emit parameterChanged();
343 void GeometryWidget::slotPreviousKeyframe()
345 Mlt::GeometryItem item;
346 // Go to start if no keyframe is found
347 int currentPos = m_timePos->getValue();
349 if (!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
352 slotPositionChanged(pos);
355 void GeometryWidget::slotNextKeyframe()
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))
363 slotPositionChanged(pos);
366 void GeometryWidget::slotAddDeleteKeyframe()
368 Mlt::GeometryItem item;
369 if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
372 slotDeleteKeyframe();
376 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
380 We do only get the position in timeline if this geometry belongs to a transition,
381 therefore we need two ways here.
384 emit checkMonitorPosition(renderPos);
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);
390 m_monitor->slotEffectScene(false);
397 void GeometryWidget::slotUpdateGeometry()
399 Mlt::GeometryItem item;
400 int pos = m_timePos->getValue();
402 // get keyframe and make sure it is the correct one
403 if (m_geometry->next_key(&item, pos) || item.frame() != pos)
406 QRectF rectSize = m_rect->rect().normalized();
407 QPointF rectPos = m_rect->pos();
410 item.w(rectSize.width());
411 item.h(rectSize.height());
412 m_geometry->insert(item);
413 emit parameterChanged();
416 void GeometryWidget::slotUpdateProperties()
418 QRectF rectSize = m_rect->rect().normalized();
419 QPointF rectPos = m_rect->pos();
421 if (rectSize.width() / m_monitor->render->dar() > rectSize.height())
422 size = rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth();
424 size = rectSize.height() * 100.0 / m_monitor->render->renderHeight();
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);
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);
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);
446 void GeometryWidget::slotSetX(int value)
448 m_rect->setPos(value, m_spinY->value());
449 slotUpdateGeometry();
452 void GeometryWidget::slotSetY(int value)
454 m_rect->setPos(m_spinX->value(), value);
455 slotUpdateGeometry();
458 void GeometryWidget::slotSetWidth(int value)
460 m_rect->setRect(0, 0, value, m_spinHeight->value());
461 slotUpdateGeometry();
464 void GeometryWidget::slotSetHeight(int value)
466 m_rect->setRect(0, 0, m_spinWidth->value(), value);
467 slotUpdateGeometry();
471 void GeometryWidget::slotResize(double value)
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();
480 void GeometryWidget::slotSetOpacity(int value)
482 int pos = m_timePos->getValue();
483 Mlt::GeometryItem item;
484 if (m_geometry->fetch(&item, pos) || item.key() == false)
487 m_geometry->insert(item);
488 emit parameterChanged();
492 void GeometryWidget::slotMoveLeft()
494 m_rect->setPos(0, m_rect->pos().y());
495 slotUpdateGeometry();
498 void GeometryWidget::slotCenterH()
500 m_rect->setPos((m_monitor->render->frameRenderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
501 slotUpdateGeometry();
504 void GeometryWidget::slotMoveRight()
506 m_rect->setPos(m_monitor->render->frameRenderWidth() - m_rect->rect().width(), m_rect->pos().y());
507 slotUpdateGeometry();
510 void GeometryWidget::slotMoveTop()
512 m_rect->setPos(m_rect->pos().x(), 0);
513 slotUpdateGeometry();
516 void GeometryWidget::slotCenterV()
518 m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
519 slotUpdateGeometry();
522 void GeometryWidget::slotMoveBottom()
524 m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
525 slotUpdateGeometry();
529 void GeometryWidget::slotSetSynchronize(bool sync)
531 KdenliveSettings::setTransitionfollowcursor(sync);
533 emit seekToPos(m_clipPos + m_timePos->getValue());
536 void GeometryWidget::slotShowScene(bool show)
540 m_monitor->slotEffectScene(false);
542 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
545 void GeometryWidget::setFrameSize(QPoint size)
550 void GeometryWidget::slotAdjustToFrameSize()
552 if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
553 m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() + 0.5));
554 m_spinHeight->setValue(m_frameSize.y());
557 void GeometryWidget::slotFitToWidth()
559 if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
560 double factor = (double) m_monitor->render->frameRenderWidth() / m_frameSize.x() * m_monitor->render->sar();
561 m_spinHeight->setValue((int) (m_frameSize.y() * factor + 0.5));
562 m_spinWidth->setValue(m_monitor->render->frameRenderWidth());
565 void GeometryWidget::slotFitToHeight()
567 if (m_frameSize == QPoint()) m_frameSize = QPoint(m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
568 double factor = (double) m_monitor->render->renderHeight() / m_frameSize.y();
569 m_spinHeight->setValue(m_monitor->render->renderHeight());
570 m_spinWidth->setValue((int) (m_frameSize.x() / m_monitor->render->sar() * factor + 0.5));
573 #include "geometrywidget.moc"