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 "kdenlivesettings.h"
30 #include <QGraphicsView>
31 #include <QGraphicsRectItem>
32 #include <QVBoxLayout>
33 #include <QGridLayout>
37 GeometryWidget::GeometryWidget(Monitor* monitor, Timecode timecode, int clipPos, bool isEffect, QWidget* parent):
40 m_timePos(new TimecodeDisplay(timecode)),
50 m_scene = monitor->getEffectScene();
54 Setup of timeline and keyframe controls
57 ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 6);
59 QVBoxLayout *layout = new QVBoxLayout(m_ui.frameTimeline);
60 m_timeline = new KeyframeHelper(m_ui.frameTimeline);
61 layout->addWidget(m_timeline);
62 layout->setContentsMargins(0, 0, 0, 0);
64 m_ui.buttonPrevious->setIcon(KIcon("media-skip-backward"));
65 m_ui.buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
66 m_ui.buttonNext->setIcon(KIcon("media-skip-forward"));
67 m_ui.buttonNext->setToolTip(i18n("Go to next keyframe"));
68 m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
69 m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
71 m_ui.buttonSync->setIcon(KIcon("insert-link"));
72 m_ui.buttonSync->setToolTip(i18n("Synchronize with timeline cursor"));
73 m_ui.buttonSync->setChecked(KdenliveSettings::transitionfollowcursor());
75 connect(m_timeline, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
76 connect(m_timeline, SIGNAL(keyframeMoved(int)), this, SLOT(slotKeyframeMoved(int)));
77 connect(m_timeline, SIGNAL(addKeyframe(int)), this, SLOT(slotAddKeyframe(int)));
78 connect(m_timeline, SIGNAL(removeKeyframe(int)), this, SLOT(slotDeleteKeyframe(int)));
79 connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotPositionChanged()));
80 connect(m_ui.buttonPrevious, SIGNAL(clicked()), this, SLOT(slotPreviousKeyframe()));
81 connect(m_ui.buttonNext, SIGNAL(clicked()), this, SLOT(slotNextKeyframe()));
82 connect(m_ui.buttonAddDelete, SIGNAL(clicked()), this, SLOT(slotAddDeleteKeyframe()));
83 connect(m_ui.buttonSync, SIGNAL(toggled(bool)), this, SLOT(slotSetSynchronize(bool)));
87 Setup of geometry controls
90 m_ui.buttonMoveLeft->setIcon(KIcon("kdenlive-align-left"));
91 m_ui.buttonMoveLeft->setToolTip(i18n("Move to left"));
92 m_ui.buttonCenterH->setIcon(KIcon("kdenlive-align-hor"));
93 m_ui.buttonCenterH->setToolTip(i18n("Center horizontally"));
94 m_ui.buttonMoveRight->setIcon(KIcon("kdenlive-align-right"));
95 m_ui.buttonMoveRight->setToolTip(i18n("Move to right"));
96 m_ui.buttonMoveTop->setIcon(KIcon("kdenlive-align-top"));
97 m_ui.buttonMoveTop->setToolTip(i18n("Move to top"));
98 m_ui.buttonCenterV->setIcon(KIcon("kdenlive-align-vert"));
99 m_ui.buttonCenterV->setToolTip(i18n("Center vertically"));
100 m_ui.buttonMoveBottom->setIcon(KIcon("kdenlive-align-bottom"));
101 m_ui.buttonMoveBottom->setToolTip(i18n("Move to bottom"));
103 connect(m_ui.spinX, SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
104 connect(m_ui.spinY, SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
105 connect(m_ui.spinWidth, SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
106 connect(m_ui.spinHeight, SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
108 connect(m_ui.spinSize, SIGNAL(valueChanged(int)), this, SLOT(slotResize(int)));
110 connect(m_ui.spinOpacity, SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
111 connect(m_ui.sliderOpacity, SIGNAL(valueChanged(int)), m_ui.spinOpacity, SLOT(setValue(int)));
113 connect(m_ui.buttonMoveLeft, SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
114 connect(m_ui.buttonCenterH, SIGNAL(clicked()), this, SLOT(slotCenterH()));
115 connect(m_ui.buttonMoveRight, SIGNAL(clicked()), this, SLOT(slotMoveRight()));
116 connect(m_ui.buttonMoveTop, SIGNAL(clicked()), this, SLOT(slotMoveTop()));
117 connect(m_ui.buttonCenterV, SIGNAL(clicked()), this, SLOT(slotCenterV()));
118 connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));
122 Setup of configuration controls
125 m_ui.buttonConfig->setIcon(KIcon("system-run"));
126 m_ui.buttonConfig->setToolTip(i18n("Show/Hide settings"));
127 m_ui.groupSettings->setHidden(true);
129 m_ui.buttonShowScene->setIcon(KIcon("video-display"));
130 m_ui.buttonShowScene->setToolTip(i18n("Show monitor scene"));
131 m_ui.buttonDirectUpdate->setIcon(KIcon("transform-scale"));
132 m_ui.buttonDirectUpdate->setToolTip(i18n("Update parameters while monitor scene changes"));
133 m_ui.buttonDirectUpdate->setChecked(KdenliveSettings::monitorscene_directupdate());
135 m_ui.buttonZoomFit->setIcon(KIcon("zoom-fit-best"));
136 m_ui.buttonZoomFit->setToolTip(i18n("Fit zoom to monitor size"));
137 m_ui.buttonZoomOriginal->setIcon(KIcon("zoom-original"));
138 m_ui.buttonZoomOriginal->setToolTip(i18n("Original size"));
139 m_ui.buttonZoomIn->setIcon(KIcon("zoom-in"));
140 m_ui.buttonZoomIn->setToolTip(i18n("Zoom in"));
141 m_ui.buttonZoomOut->setIcon(KIcon("zoom-out"));
142 m_ui.buttonZoomOut->setToolTip(i18n("Zoom out"));
144 connect(m_ui.buttonConfig, SIGNAL(toggled(bool)), m_ui.groupSettings, SLOT(setVisible(bool)));
146 connect(m_ui.sliderZoom, SIGNAL(valueChanged(int)), m_scene, SLOT(slotZoom(int)));
147 connect(m_scene, SIGNAL(zoomChanged(int)), m_ui.sliderZoom, SLOT(setValue(int)));
148 connect(m_ui.buttonZoomFit, SIGNAL(clicked()), m_scene, SLOT(slotZoomFit()));
149 connect(m_ui.buttonZoomOriginal, SIGNAL(clicked()), m_scene, SLOT(slotZoomOriginal()));
150 connect(m_ui.buttonZoomIn, SIGNAL(clicked()), m_scene, SLOT(slotZoomIn()));
151 connect(m_ui.buttonZoomOut, SIGNAL(clicked()), m_scene, SLOT(slotZoomOut()));
152 m_scene->slotZoomFit();
154 connect(m_ui.buttonShowScene, SIGNAL(toggled(bool)), this, SLOT(slotShowScene(bool)));
155 connect(m_ui.buttonDirectUpdate, SIGNAL(toggled(bool)), m_scene, SLOT(slotSetDirectUpdate(bool)));
158 connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
159 connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
160 connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
161 connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
164 GeometryWidget::~GeometryWidget()
168 m_scene->removeItem(m_rect);
171 m_monitor->slotEffectScene(false);
174 void GeometryWidget::updateTimecodeFormat()
176 m_timePos->slotUpdateTimeCodeFormat();
179 QString GeometryWidget::getValue() const
181 return m_geometry->serialise();
184 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
186 m_inPoint = minframe;
187 m_outPoint = maxframe;
190 m_geometry->parse(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
192 m_geometry = new Mlt::Geometry(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->frameRenderWidth(), m_monitor->render->renderHeight());
194 if (elem.attribute("fixed") == "1" || maxframe < minframe) {
195 // Keyframes are disabled
196 m_ui.widgetTimeWrapper->setHidden(true);
198 m_ui.widgetTimeWrapper->setHidden(false);
199 m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint - 1);
200 m_timeline->update();
201 m_timePos->setRange(0, m_outPoint - m_inPoint - 1);
205 if (elem.attribute("opacity") == "false")
206 m_ui.widgetOpacity->setHidden(true);
208 Mlt::GeometryItem item;
210 m_geometry->fetch(&item, 0);
212 m_rect = new QGraphicsRectHandleItem(QRectF(0, 0, item.w(), item.h()));
213 m_rect->setPos(item.x(), item.y());
214 m_rect->setZValue(0);
215 m_rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
217 QPen framepen(Qt::SolidLine);
218 framepen.setColor(Qt::yellow);
219 m_rect->setPen(framepen);
220 m_rect->setBrush(Qt::transparent);
221 m_scene->addItem(m_rect);
223 slotPositionChanged(0, false);
224 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
227 void GeometryWidget::slotSyncPosition(int relTimelinePos)
229 // do only sync if this effect is keyframable
230 if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
231 relTimelinePos = qMax(0, relTimelinePos);
232 relTimelinePos = qMin(relTimelinePos, m_timePos->maximum());
233 if (relTimelinePos != m_timePos->getValue())
234 slotPositionChanged(relTimelinePos, false);
239 void GeometryWidget::slotPositionChanged(int pos, bool seek)
242 pos = m_timePos->getValue();
244 m_timePos->setValue(pos);
246 m_timeline->blockSignals(true);
247 m_timeline->setValue(pos);
248 m_timeline->blockSignals(false);
250 Mlt::GeometryItem item;
251 if (m_geometry->fetch(&item, pos) || item.key() == false) {
253 m_rect->drawHandles = false;
254 m_scene->setEnabled(false);
255 m_ui.widgetGeometry->setEnabled(false);
256 m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
257 m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
260 m_rect->drawHandles = true;
261 m_scene->setEnabled(true);
262 m_ui.widgetGeometry->setEnabled(true);
263 m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
264 m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
267 m_rect->setPos(item.x(), item.y());
268 m_rect->setRect(0, 0, item.w(), item.h());
270 m_ui.spinOpacity->blockSignals(true);
271 m_ui.sliderOpacity->blockSignals(true);
272 m_ui.spinOpacity->setValue(item.mix());
273 m_ui.sliderOpacity->setValue(item.mix());
274 m_ui.spinOpacity->blockSignals(false);
275 m_ui.sliderOpacity->blockSignals(false);
277 slotUpdateProperties();
279 if (seek && KdenliveSettings::transitionfollowcursor())
280 emit seekToPos(m_clipPos + pos);
283 void GeometryWidget::slotKeyframeMoved(int pos)
285 slotPositionChanged(pos);
286 slotUpdateGeometry();
287 QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
290 void GeometryWidget::slotAddKeyframe(int pos)
292 Mlt::GeometryItem item;
294 pos = m_timePos->getValue();
296 QRectF r = m_rect->rect().normalized();
297 QPointF rectpos = m_rect->pos();
302 item.mix(m_ui.spinOpacity->value());
303 m_geometry->insert(item);
305 m_timeline->update();
306 slotPositionChanged(pos, false);
307 emit parameterChanged();
310 void GeometryWidget::slotDeleteKeyframe(int pos)
312 Mlt::GeometryItem item;
314 pos = m_timePos->getValue();
315 // check there is more than one keyframe, do not allow to delete last one
316 if (m_geometry->next_key(&item, pos + 1)) {
317 if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
320 m_geometry->remove(pos);
322 m_timeline->update();
323 slotPositionChanged(pos, false);
324 emit parameterChanged();
327 void GeometryWidget::slotPreviousKeyframe()
329 Mlt::GeometryItem item;
330 // Go to start if no keyframe is found
331 int currentPos = m_timePos->getValue();
333 if (!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
336 slotPositionChanged(pos);
339 void GeometryWidget::slotNextKeyframe()
341 Mlt::GeometryItem item;
342 // Go to end if no keyframe is found
343 int pos = m_timeline->frameLength;
344 if (!m_geometry->next_key(&item, m_timeline->value() + 1))
347 slotPositionChanged(pos);
350 void GeometryWidget::slotAddDeleteKeyframe()
352 Mlt::GeometryItem item;
353 if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
356 slotDeleteKeyframe();
360 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
364 We do only get the position in timeline if this geometry belongs to a transition,
365 therefore we need two ways here.
368 emit checkMonitorPosition(renderPos);
370 if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
371 if (!m_scene->views().at(0)->isVisible())
372 m_monitor->slotEffectScene(true);
374 m_monitor->slotEffectScene(false);
381 void GeometryWidget::slotUpdateGeometry()
383 Mlt::GeometryItem item;
384 int pos = m_timePos->getValue();
386 // get keyframe and make sure it is the correct one
387 if (m_geometry->next_key(&item, pos) || item.frame() != pos)
390 QRectF rectSize = m_rect->rect().normalized();
391 QPointF rectPos = m_rect->pos();
394 item.w(rectSize.width());
395 item.h(rectSize.height());
396 m_geometry->insert(item);
397 emit parameterChanged();
400 void GeometryWidget::slotUpdateProperties()
402 QRectF rectSize = m_rect->rect().normalized();
403 QPointF rectPos = m_rect->pos();
405 if (rectSize.width() / m_monitor->render->dar() < rectSize.height())
406 size = (int)((rectSize.width() * 100.0 / m_monitor->render->frameRenderWidth()) + 0.5);
408 size = (int)((rectSize.height() * 100.0 / m_monitor->render->renderHeight()) + 0.5);
410 m_ui.spinX->blockSignals(true);
411 m_ui.spinY->blockSignals(true);
412 m_ui.spinWidth->blockSignals(true);
413 m_ui.spinHeight->blockSignals(true);
414 m_ui.spinSize->blockSignals(true);
416 m_ui.spinX->setValue(rectPos.x());
417 m_ui.spinY->setValue(rectPos.y());
418 m_ui.spinWidth->setValue(rectSize.width());
419 m_ui.spinHeight->setValue(rectSize.height());
420 m_ui.spinSize->setValue(size);
422 m_ui.spinX->blockSignals(false);
423 m_ui.spinY->blockSignals(false);
424 m_ui.spinWidth->blockSignals(false);
425 m_ui.spinHeight->blockSignals(false);
426 m_ui.spinSize->blockSignals(false);
430 void GeometryWidget::slotSetX(int value)
432 m_rect->setPos(value, m_ui.spinY->value());
433 slotUpdateGeometry();
436 void GeometryWidget::slotSetY(int value)
438 m_rect->setPos(m_ui.spinX->value(), value);
439 slotUpdateGeometry();
442 void GeometryWidget::slotSetWidth(int value)
444 m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
445 slotUpdateGeometry();
448 void GeometryWidget::slotSetHeight(int value)
450 m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
451 slotUpdateGeometry();
455 void GeometryWidget::slotResize(int value)
457 m_rect->setRect(0, 0,
458 (int)((m_monitor->render->frameRenderWidth() * value / 100.0) + 0.5),
459 (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
460 slotUpdateGeometry();
464 void GeometryWidget::slotSetOpacity(int value)
466 m_ui.sliderOpacity->blockSignals(true);
467 m_ui.sliderOpacity->setValue(value);
468 m_ui.sliderOpacity->blockSignals(false);
470 int pos = m_timePos->getValue();
471 Mlt::GeometryItem item;
472 if (m_geometry->fetch(&item, pos) || item.key() == false)
475 m_geometry->insert(item);
477 emit parameterChanged();
481 void GeometryWidget::slotMoveLeft()
483 m_rect->setPos(0, m_rect->pos().y());
484 slotUpdateGeometry();
487 void GeometryWidget::slotCenterH()
489 m_rect->setPos((m_monitor->render->frameRenderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
490 slotUpdateGeometry();
493 void GeometryWidget::slotMoveRight()
495 m_rect->setPos(m_monitor->render->frameRenderWidth() - m_rect->rect().width(), m_rect->pos().y());
496 slotUpdateGeometry();
499 void GeometryWidget::slotMoveTop()
501 m_rect->setPos(m_rect->pos().x(), 0);
502 slotUpdateGeometry();
505 void GeometryWidget::slotCenterV()
507 m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
508 slotUpdateGeometry();
511 void GeometryWidget::slotMoveBottom()
513 m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
514 slotUpdateGeometry();
518 void GeometryWidget::slotSetSynchronize(bool sync)
520 KdenliveSettings::setTransitionfollowcursor(sync);
522 emit seekToPos(m_clipPos + m_timePos->getValue());
525 void GeometryWidget::slotShowScene(bool show)
529 m_monitor->slotEffectScene(false);
531 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
534 #include "geometrywidget.moc"