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 MonitorEditWidget *edit = monitor->getEffectEdit();
54 edit->showVisibilityButton(true);
55 m_scene = edit->getScene();
59 Setup of timeline and keyframe controls
62 ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 6);
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);
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"));
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());
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)));
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);
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);
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);
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);
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);
111 m_opacity = new DragValue(i18n("Opacity"), 100, -1, i18n("%"), true, this);
112 m_ui.horizontalLayout2->addWidget(m_opacity);
115 Setup of geometry controls
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)));
123 connect(m_spinSize, SIGNAL(valueChanged(int)), this, SLOT(slotResize(int)));
125 connect(m_opacity, SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
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();
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);
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()));*/
168 Setup of configuration controls
171 connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
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()));
178 GeometryWidget::~GeometryWidget()
180 m_scene->setEnabled(true);
188 m_scene->removeItem(m_rect);
191 m_monitor->getEffectEdit()->showVisibilityButton(false);
192 m_monitor->slotEffectScene(false);
196 void GeometryWidget::updateTimecodeFormat()
198 m_timePos->slotUpdateTimeCodeFormat();
201 QString GeometryWidget::getValue() const
203 return m_geometry->serialise();
206 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
208 m_inPoint = minframe;
209 m_outPoint = maxframe;
212 m_geometry->parse(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
214 m_geometry = new Mlt::Geometry(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
216 if (elem.attribute("fixed") == "1" || maxframe < minframe) {
217 // Keyframes are disabled
218 m_ui.widgetTimeWrapper->setHidden(true);
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);
227 if (elem.attribute("opacity") == "false") {
228 m_opacity->setHidden(true);
229 m_ui.horizontalLayout2->addStretch(2);
232 Mlt::GeometryItem item;
234 m_geometry->fetch(&item, 0);
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()));
242 slotPositionChanged(0, false);
243 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
246 void GeometryWidget::slotSyncPosition(int relTimelinePos)
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);
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 = (int)((rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth()) + 0.5);
424 size = (int)((rectSize.height() * 100.0 / m_monitor->render->renderHeight()) + 0.5);
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(int 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(m_frameSize.x());
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 = 100.0 * m_monitor->render->frameRenderWidth() / m_frameSize.x() + 0.5;
561 m_spinSize->setValue(factor);
564 void GeometryWidget::slotFitToHeight()
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);
571 #include "geometrywidget.moc"