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>
35 GeometryWidget::GeometryWidget(Monitor* monitor, Timecode timecode, int clipPos, bool isEffect, QWidget* parent ):
38 m_timePos(new TimecodeDisplay(timecode)),
48 m_scene = monitor->getEffectScene();
52 Setup of timeline and keyframe controls
55 ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 6);
57 QVBoxLayout *layout = new QVBoxLayout(m_ui.frameTimeline);
58 m_timeline = new KeyframeHelper(m_ui.frameTimeline);
59 layout->addWidget(m_timeline);
60 layout->setContentsMargins(0, 0, 0, 0);
62 m_ui.buttonPrevious->setIcon(KIcon("media-skip-backward"));
63 m_ui.buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
64 m_ui.buttonNext->setIcon(KIcon("media-skip-forward"));
65 m_ui.buttonNext->setToolTip(i18n("Go to next keyframe"));
66 m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
67 m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
69 m_ui.buttonSync->setIcon(KIcon("insert-link"));
70 m_ui.buttonSync->setToolTip(i18n("Synchronize with timeline cursor"));
71 m_ui.buttonSync->setChecked(KdenliveSettings::transitionfollowcursor());
73 connect(m_timeline, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
74 connect(m_timeline, SIGNAL(keyframeMoved(int)), this, SLOT(slotKeyframeMoved(int)));
75 connect(m_timeline, SIGNAL(addKeyframe(int)), this, SLOT(slotAddKeyframe(int)));
76 connect(m_timeline, SIGNAL(removeKeyframe(int)), this, SLOT(slotDeleteKeyframe(int)));
77 connect(m_timePos, SIGNAL(editingFinished()), this, SLOT(slotPositionChanged()));
78 connect(m_ui.buttonPrevious, SIGNAL(clicked()), this, SLOT(slotPreviousKeyframe()));
79 connect(m_ui.buttonNext, SIGNAL(clicked()), this, SLOT(slotNextKeyframe()));
80 connect(m_ui.buttonAddDelete, SIGNAL(clicked()), this, SLOT(slotAddDeleteKeyframe()));
81 connect(m_ui.buttonSync, SIGNAL(toggled(bool)), this, SLOT(slotSetSynchronize(bool)));
85 Setup of geometry controls
88 m_ui.buttonMoveLeft->setIcon(KIcon("kdenlive-align-left"));
89 m_ui.buttonMoveLeft->setToolTip(i18n("Move to left"));
90 m_ui.buttonCenterH->setIcon(KIcon("kdenlive-align-hor"));
91 m_ui.buttonCenterH->setToolTip(i18n("Center horizontally"));
92 m_ui.buttonMoveRight->setIcon(KIcon("kdenlive-align-right"));
93 m_ui.buttonMoveRight->setToolTip(i18n("Move to right"));
94 m_ui.buttonMoveTop->setIcon(KIcon("kdenlive-align-top"));
95 m_ui.buttonMoveTop->setToolTip(i18n("Move to top"));
96 m_ui.buttonCenterV->setIcon(KIcon("kdenlive-align-vert"));
97 m_ui.buttonCenterV->setToolTip(i18n("Center vertically"));
98 m_ui.buttonMoveBottom->setIcon(KIcon("kdenlive-align-bottom"));
99 m_ui.buttonMoveBottom->setToolTip(i18n("Move to bottom"));
101 connect(m_ui.spinX, SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
102 connect(m_ui.spinY, SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
103 connect(m_ui.spinWidth, SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
104 connect(m_ui.spinHeight, SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
106 connect(m_ui.spinSize, SIGNAL(valueChanged(int)), this, SLOT(slotResize(int)));
108 connect(m_ui.spinOpacity, SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
109 connect(m_ui.sliderOpacity, SIGNAL(valueChanged(int)), m_ui.spinOpacity, SLOT(setValue(int)));
111 connect(m_ui.buttonMoveLeft, SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
112 connect(m_ui.buttonCenterH, SIGNAL(clicked()), this, SLOT(slotCenterH()));
113 connect(m_ui.buttonMoveRight, SIGNAL(clicked()), this, SLOT(slotMoveRight()));
114 connect(m_ui.buttonMoveTop, SIGNAL(clicked()), this, SLOT(slotMoveTop()));
115 connect(m_ui.buttonCenterV, SIGNAL(clicked()), this, SLOT(slotCenterV()));
116 connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));
120 Setup of configuration controls
123 m_ui.buttonConfig->setIcon(KIcon("system-run"));
124 m_ui.buttonConfig->setToolTip(i18n("Show/Hide settings"));
125 m_ui.groupSettings->setHidden(true);
127 m_ui.buttonShowScene->setIcon(KIcon("video-display"));
128 m_ui.buttonShowScene->setToolTip(i18n("Show monitor scene"));
129 m_ui.buttonDirectUpdate->setIcon(KIcon("transform-scale"));
130 m_ui.buttonDirectUpdate->setToolTip(i18n("Update parameters while monitor scene changes"));
131 m_ui.buttonDirectUpdate->setChecked(KdenliveSettings::monitorscene_directupdate());
133 m_ui.buttonZoomFit->setIcon(KIcon("zoom-fit-best"));
134 m_ui.buttonZoomFit->setToolTip(i18n("Fit zoom to monitor size"));
135 m_ui.buttonZoomOriginal->setIcon(KIcon("zoom-original"));
136 m_ui.buttonZoomOriginal->setToolTip(i18n("Original size"));
137 m_ui.buttonZoomIn->setIcon(KIcon("zoom-in"));
138 m_ui.buttonZoomIn->setToolTip(i18n("Zoom in"));
139 m_ui.buttonZoomOut->setIcon(KIcon("zoom-out"));
140 m_ui.buttonZoomOut->setToolTip(i18n("Zoom out"));
142 connect(m_ui.buttonConfig, SIGNAL(toggled(bool)), m_ui.groupSettings, SLOT(setVisible(bool)));
144 connect(m_ui.sliderZoom, SIGNAL(valueChanged(int)), m_scene, SLOT(slotZoom(int)));
145 connect(m_scene, SIGNAL(zoomChanged(int)), m_ui.sliderZoom, SLOT(setValue(int)));
146 connect(m_ui.buttonZoomFit, SIGNAL(clicked()), m_scene, SLOT(slotZoomFit()));
147 connect(m_ui.buttonZoomOriginal, SIGNAL(clicked()), m_scene, SLOT(slotZoomOriginal()));
148 connect(m_ui.buttonZoomIn, SIGNAL(clicked()), m_scene, SLOT(slotZoomIn()));
149 connect(m_ui.buttonZoomOut, SIGNAL(clicked()), m_scene, SLOT(slotZoomOut()));
150 m_scene->slotZoomFit();
152 connect(m_ui.buttonShowScene, SIGNAL(toggled(bool)), this, SLOT(slotShowScene(bool)));
153 connect(m_ui.buttonDirectUpdate, SIGNAL(toggled(bool)), m_scene, SLOT(slotSetDirectUpdate(bool)));
156 connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
157 connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
158 connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
161 GeometryWidget::~GeometryWidget()
165 m_scene->removeItem(m_rect);
168 m_monitor->slotEffectScene(false);
171 void GeometryWidget::updateTimecodeFormat()
173 m_timePos->slotUpdateTimeCodeFormat();
176 QString GeometryWidget::getValue() const
178 return m_geometry->serialise();
181 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
183 m_inPoint = minframe;
184 m_outPoint = maxframe;
187 m_geometry->parse(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
189 m_geometry = new Mlt::Geometry(elem.attribute("value").toUtf8().data(), maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
191 if (elem.attribute("fixed") == "1" || maxframe < minframe) {
192 // Keyframes are disabled
193 m_ui.widgetTimeWrapper->setHidden(true);
195 m_ui.widgetTimeWrapper->setHidden(false);
196 m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint - 1);
197 m_timeline->update();
198 m_timePos->setRange(0, m_outPoint - m_inPoint - 1);
202 if (elem.attribute("opacity") == "false")
203 m_ui.widgetOpacity->setHidden(true);
205 Mlt::GeometryItem item;
207 m_geometry->fetch(&item, 0);
209 m_rect = new QGraphicsRectItem(QRectF(0, 0, item.w(), item.h()));
210 m_rect->setPos(item.x(), item.y());
211 m_rect->setZValue(0);
212 m_rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
214 QPen framepen(Qt::SolidLine);
215 framepen.setColor(Qt::yellow);
216 m_rect->setPen(framepen);
217 m_rect->setBrush(Qt::transparent);
218 m_scene->addItem(m_rect);
220 slotPositionChanged(0, false);
221 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
224 void GeometryWidget::slotSyncPosition(int relTimelinePos)
226 // do only sync if this effect is keyframable
227 if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
228 relTimelinePos = qMax(0, relTimelinePos);
229 relTimelinePos = qMin(relTimelinePos, m_timePos->maximum());
230 if (relTimelinePos != m_timePos->getValue())
231 slotPositionChanged(relTimelinePos, false);
236 void GeometryWidget::slotPositionChanged(int pos, bool seek)
239 pos = m_timePos->getValue();
241 m_timePos->setValue(pos);
243 m_timeline->blockSignals(true);
244 m_timeline->setValue(pos);
245 m_timeline->blockSignals(false);
247 Mlt::GeometryItem item;
248 if (m_geometry->fetch(&item, pos) || item.key() == false) {
250 m_scene->setEnabled(false);
251 m_ui.widgetGeometry->setEnabled(false);
252 m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
253 m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
256 m_scene->setEnabled(true);
257 m_ui.widgetGeometry->setEnabled(true);
258 m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
259 m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
262 m_rect->setPos(item.x(), item.y());
263 m_rect->setRect(0, 0, item.w(), item.h());
265 m_ui.spinOpacity->blockSignals(true);
266 m_ui.sliderOpacity->blockSignals(true);
267 m_ui.spinOpacity->setValue(item.mix());
268 m_ui.sliderOpacity->setValue(item.mix());
269 m_ui.spinOpacity->blockSignals(false);
270 m_ui.sliderOpacity->blockSignals(false);
272 slotUpdateProperties();
274 if (seek && KdenliveSettings::transitionfollowcursor())
275 emit seekToPos(m_clipPos + pos);
278 void GeometryWidget::slotKeyframeMoved(int pos)
280 slotPositionChanged(pos);
281 slotUpdateGeometry();
282 QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
285 void GeometryWidget::slotAddKeyframe(int pos)
287 Mlt::GeometryItem item;
289 pos = m_timePos->getValue();
291 QRectF r = m_rect->rect().normalized();
292 QPointF rectpos = m_rect->pos();
297 item.mix(m_ui.spinOpacity->value());
298 m_geometry->insert(item);
300 m_timeline->update();
301 slotPositionChanged(pos, false);
302 emit parameterChanged();
305 void GeometryWidget::slotDeleteKeyframe(int pos)
307 Mlt::GeometryItem item;
309 pos = m_timePos->getValue();
310 // check there is more than one keyframe, do not allow to delete last one
311 if (m_geometry->next_key(&item, pos + 1)) {
312 if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
315 m_geometry->remove(pos);
317 m_timeline->update();
318 slotPositionChanged(pos, false);
319 emit parameterChanged();
322 void GeometryWidget::slotPreviousKeyframe()
324 Mlt::GeometryItem item;
325 // Go to start if no keyframe is found
326 int currentPos = m_timePos->getValue();
328 if(!m_geometry->prev_key(&item, currentPos - 1) && item.frame() < currentPos)
331 slotPositionChanged(pos);
334 void GeometryWidget::slotNextKeyframe()
336 Mlt::GeometryItem item;
337 // Go to end if no keyframe is found
338 int pos = m_timeline->frameLength;
339 if (!m_geometry->next_key(&item, m_timeline->value() + 1))
342 slotPositionChanged(pos);
345 void GeometryWidget::slotAddDeleteKeyframe()
347 Mlt::GeometryItem item;
348 if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
351 slotDeleteKeyframe();
355 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
359 We do only get the position in timeline if this geometry belongs to a transition,
360 therefore we need to ways here.
363 emit checkMonitorPosition(renderPos);
365 if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
366 if (!m_scene->views().at(0)->isVisible())
367 m_monitor->slotEffectScene(true);
369 m_monitor->slotEffectScene(false);
376 void GeometryWidget::slotUpdateGeometry()
378 Mlt::GeometryItem item;
379 int pos = m_timePos->getValue();
380 // get keyframe and make sure it is the correct one
381 if (m_geometry->next_key(&item, pos) || item.frame() != pos)
384 QRectF rectSize = m_rect->rect().normalized();
385 QPointF rectPos = m_rect->pos();
388 item.w(rectSize.width());
389 item.h(rectSize.height());
390 m_geometry->insert(item);
391 emit parameterChanged();
394 void GeometryWidget::slotUpdateProperties()
396 QRectF rectSize = m_rect->rect().normalized();
397 QPointF rectPos = m_rect->pos();
399 if (rectSize.width() / m_monitor->render->dar() < rectSize.height())
400 size = (int)((rectSize.width() * 100.0 / m_monitor->render->renderWidth()) + 0.5);
402 size = (int)((rectSize.height() * 100.0 / m_monitor->render->renderHeight()) + 0.5);
404 m_ui.spinX->blockSignals(true);
405 m_ui.spinY->blockSignals(true);
406 m_ui.spinWidth->blockSignals(true);
407 m_ui.spinHeight->blockSignals(true);
408 m_ui.spinSize->blockSignals(true);
410 m_ui.spinX->setValue(rectPos.x());
411 m_ui.spinY->setValue(rectPos.y());
412 m_ui.spinWidth->setValue(rectSize.width());
413 m_ui.spinHeight->setValue(rectSize.height());
414 m_ui.spinSize->setValue(size);
416 m_ui.spinX->blockSignals(false);
417 m_ui.spinY->blockSignals(false);
418 m_ui.spinWidth->blockSignals(false);
419 m_ui.spinHeight->blockSignals(false);
420 m_ui.spinSize->blockSignals(false);
424 void GeometryWidget::slotSetX(int value)
426 m_rect->setPos(value, m_ui.spinY->value());
427 slotUpdateGeometry();
430 void GeometryWidget::slotSetY(int value)
432 m_rect->setPos(m_ui.spinX->value(), value);
433 slotUpdateGeometry();
436 void GeometryWidget::slotSetWidth(int value)
438 m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
439 slotUpdateGeometry();
442 void GeometryWidget::slotSetHeight(int value)
444 m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
445 slotUpdateGeometry();
449 void GeometryWidget::slotResize(int value)
451 m_rect->setRect(0, 0,
452 (int)((m_monitor->render->renderWidth() * value / 100.0) + 0.5),
453 (int)((m_monitor->render->renderHeight() * value / 100.0) + 0.5));
454 slotUpdateGeometry();
458 void GeometryWidget::slotSetOpacity(int value)
460 m_ui.sliderOpacity->blockSignals(true);
461 m_ui.sliderOpacity->setValue(value);
462 m_ui.sliderOpacity->blockSignals(false);
464 int pos = m_timePos->getValue();
465 Mlt::GeometryItem item;
466 if (m_geometry->fetch(&item, pos) || item.key() == false)
469 m_geometry->insert(item);
471 emit parameterChanged();
475 void GeometryWidget::slotMoveLeft()
477 m_rect->setPos(0, m_rect->pos().y());
478 slotUpdateGeometry();
481 void GeometryWidget::slotCenterH()
483 m_rect->setPos((m_monitor->render->renderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
484 slotUpdateGeometry();
487 void GeometryWidget::slotMoveRight()
489 m_rect->setPos(m_monitor->render->renderWidth() - m_rect->rect().width(), m_rect->pos().y());
490 slotUpdateGeometry();
493 void GeometryWidget::slotMoveTop()
495 m_rect->setPos(m_rect->pos().x(), 0);
496 slotUpdateGeometry();
499 void GeometryWidget::slotCenterV()
501 m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
502 slotUpdateGeometry();
505 void GeometryWidget::slotMoveBottom()
507 m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
508 slotUpdateGeometry();
512 void GeometryWidget::slotSetSynchronize(bool sync)
514 KdenliveSettings::setTransitionfollowcursor(sync);
516 emit seekToPos(m_clipPos + m_timePos->getValue());
519 void GeometryWidget::slotShowScene(bool show)
523 m_monitor->slotEffectScene(false);
525 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
528 #include "geometrywidget.moc"