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)),
50 Setup of timeline and keyframe controls
53 ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 4);
55 QVBoxLayout *layout = new QVBoxLayout(m_ui.frameTimeline);
56 m_timeline = new KeyframeHelper(m_ui.frameTimeline);
57 layout->addWidget(m_timeline);
58 layout->setContentsMargins(0, 0, 0, 0);
60 m_ui.buttonPrevious->setIcon(KIcon("media-skip-backward"));
61 m_ui.buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
62 m_ui.buttonNext->setIcon(KIcon("media-skip-forward"));
63 m_ui.buttonNext->setToolTip(i18n("Go to next keyframe"));
64 m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
65 m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
67 connect(m_timeline, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
68 connect(m_timeline, SIGNAL(keyframeMoved(int)), this, SLOT(slotKeyframeMoved(int)));
69 connect(m_timeline, SIGNAL(addKeyframe(int)), this, SLOT(slotAddKeyframe(int)));
70 connect(m_timeline, SIGNAL(removeKeyframe(int)), this, SLOT(slotDeleteKeyframe(int)));
71 connect(m_timePos, SIGNAL(editingFinished()), this , SLOT(slotPositionChanged()));
72 connect(m_ui.buttonPrevious, SIGNAL(clicked()), this, SLOT(slotPreviousKeyframe()));
73 connect(m_ui.buttonNext, SIGNAL(clicked()), this, SLOT(slotNextKeyframe()));
74 connect(m_ui.buttonAddDelete, SIGNAL(clicked()), this, SLOT(slotAddDeleteKeyframe()));
78 Setup of geometry controls
81 m_ui.buttonMoveLeft->setIcon(KIcon("kdenlive-align-left"));
82 m_ui.buttonMoveLeft->setToolTip(i18n("Move to left"));
83 m_ui.buttonCenterH->setIcon(KIcon("kdenlive-align-hor"));
84 m_ui.buttonCenterH->setToolTip(i18n("Center horizontally"));
85 m_ui.buttonMoveRight->setIcon(KIcon("kdenlive-align-right"));
86 m_ui.buttonMoveRight->setToolTip(i18n("Move to right"));
87 m_ui.buttonMoveTop->setIcon(KIcon("kdenlive-align-top"));
88 m_ui.buttonMoveTop->setToolTip(i18n("Move to top"));
89 m_ui.buttonCenterV->setIcon(KIcon("kdenlive-align-vert"));
90 m_ui.buttonCenterV->setToolTip(i18n("Center vertically"));
91 m_ui.buttonMoveBottom->setIcon(KIcon("kdenlive-align-bottom"));
92 m_ui.buttonMoveBottom->setToolTip(i18n("Move to bottom"));
94 connect(m_ui.spinX, SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
95 connect(m_ui.spinY, SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
96 connect(m_ui.spinWidth, SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
97 connect(m_ui.spinHeight, SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
99 connect(m_ui.spinSize, SIGNAL(valueChanged(int)), this, SLOT(slotResize(int)));
101 connect(m_ui.buttonMoveLeft, SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
102 connect(m_ui.buttonCenterH, SIGNAL(clicked()), this, SLOT(slotCenterH()));
103 connect(m_ui.buttonMoveRight, SIGNAL(clicked()), this, SLOT(slotMoveRight()));
104 connect(m_ui.buttonMoveTop, SIGNAL(clicked()), this, SLOT(slotMoveTop()));
105 connect(m_ui.buttonCenterV, SIGNAL(clicked()), this, SLOT(slotCenterV()));
106 connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));
109 m_scene = monitor->getEffectScene();
110 connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
111 connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
112 connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
115 GeometryWidget::~GeometryWidget()
119 m_scene->removeItem(m_rect);
121 m_monitor->slotEffectScene(false);
124 void GeometryWidget::updateTimecodeFormat()
126 m_timePos->slotUpdateTimeCodeFormat();
129 QString GeometryWidget::getValue() const
131 return m_geometry->serialise();
134 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
136 m_inPoint = minframe;
137 m_outPoint = maxframe;
139 char *tmp = (char *) qstrdup(elem.attribute("value").toUtf8().data());
141 m_geometry->parse(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
143 m_geometry = new Mlt::Geometry(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
146 if (elem.attribute("fixed") == "1") {
147 // Keyframes are disabled
148 m_ui.widgetTimeWrapper->setHidden(true);
150 m_ui.widgetTimeWrapper->setHidden(false);
151 m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint - 1);
152 m_timeline->update();
153 m_timePos->setRange(0, m_outPoint - m_inPoint - 1);
156 Mlt::GeometryItem item;
158 m_geometry->fetch(&item, 0);
160 m_rect = new QGraphicsRectItem(QRectF(0, 0, item.w(), item.h()));
161 m_rect->setPos(item.x(), item.y());
162 m_rect->setZValue(0);
163 m_rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
165 QPen framepen(Qt::SolidLine);
166 framepen.setColor(Qt::yellow);
167 m_rect->setPen(framepen);
168 m_rect->setBrush(Qt::transparent);
169 m_scene->addItem(m_rect);
171 slotPositionChanged(0, false);
172 slotUpdateProperties();
173 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
176 void GeometryWidget::slotSyncPosition(int relTimelinePos)
178 if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
179 relTimelinePos = qMax(0, relTimelinePos);
180 relTimelinePos = qMin(relTimelinePos, m_timePos->maximum());
181 if (relTimelinePos != m_timePos->getValue())
182 slotPositionChanged(relTimelinePos, false);
187 void GeometryWidget::slotPositionChanged(int pos, bool seek)
190 pos = m_timePos->getValue();
192 m_timePos->setValue(pos);
193 m_timeline->blockSignals(true);
194 m_timeline->setValue(pos);
195 m_timeline->blockSignals(false);
197 Mlt::GeometryItem item;
198 if (m_geometry->fetch(&item, pos) || item.key() == false) {
200 m_scene->setEnabled(false);
201 m_ui.widgetGeometry->setEnabled(false);
202 m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
203 m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
206 m_scene->setEnabled(true);
207 m_ui.widgetGeometry->setEnabled(true);
208 m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
209 m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
212 m_rect->setPos(item.x(), item.y());
213 m_rect->setRect(0, 0, item.w(), item.h());
214 slotUpdateProperties();
216 if (seek && KdenliveSettings::transitionfollowcursor())
217 emit seekToPos(m_clipPos + pos);
220 void GeometryWidget::slotKeyframeMoved(int pos)
222 slotPositionChanged(pos);
223 slotUpdateGeometry();
226 void GeometryWidget::slotAddKeyframe(int pos)
228 Mlt::GeometryItem item;
230 pos = m_timePos->getValue();
232 QRectF r = m_rect->rect().normalized();
233 QPointF rectpos = m_rect->pos();
238 m_geometry->insert(item);
240 m_timeline->update();
241 slotPositionChanged(pos, false);
242 emit parameterChanged();
245 void GeometryWidget::slotDeleteKeyframe(int pos)
247 Mlt::GeometryItem item;
249 pos = m_timePos->getValue();
250 // check there is more than one keyframe, do not allow to delete last one
251 if (m_geometry->next_key(&item, pos + 1)) {
252 if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
255 m_geometry->remove(pos);
257 m_timeline->update();
258 slotPositionChanged(pos, false);
259 emit parameterChanged();
262 void GeometryWidget::slotPreviousKeyframe()
264 Mlt::GeometryItem item;
265 // Go to start if no keyframe is found
267 if(!m_geometry->prev_key(&item, m_timeline->value() - 1))
270 slotPositionChanged(pos);
273 void GeometryWidget::slotNextKeyframe()
275 Mlt::GeometryItem item;
276 // Go to end if no keyframe is found
277 int pos = m_timeline->frameLength;
278 if (!m_geometry->next_key(&item, m_timeline->value() + 1))
281 slotPositionChanged(pos);
284 void GeometryWidget::slotAddDeleteKeyframe()
286 Mlt::GeometryItem item;
287 if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
290 slotDeleteKeyframe();
294 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
297 We do only get the position in timeline if this geometry belongs to a transition,
298 therefore we need to ways here.
301 emit checkMonitorPosition(renderPos);
303 if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
304 if (!m_scene->views().at(0)->isVisible())
305 m_monitor->slotEffectScene(true);
307 m_monitor->slotEffectScene(false);
313 void GeometryWidget::slotUpdateGeometry()
315 Mlt::GeometryItem item;
316 int pos = m_timePos->getValue();
317 // get keyframe and make sure it is the correct one
318 if (m_geometry->next_key(&item, pos) || item.frame() != pos)
321 QRectF rectSize = m_rect->rect().normalized();
322 QPointF rectPos = m_rect->pos();
325 item.w(rectSize.width());
326 item.h(rectSize.height());
327 m_geometry->insert(item);
328 emit parameterChanged();
331 void GeometryWidget::slotUpdateProperties()
333 QRectF rectSize = m_rect->rect().normalized();
334 QPointF rectPos = m_rect->pos();
336 if (rectSize.width() / m_monitor->render->dar() < rectSize.height())
337 size = (int)(rectSize.width() * 100 / m_monitor->render->renderWidth());
339 size = (int)(rectSize.height() * 100 / m_monitor->render->renderHeight());
341 m_ui.spinX->blockSignals(true);
342 m_ui.spinY->blockSignals(true);
343 m_ui.spinWidth->blockSignals(true);
344 m_ui.spinHeight->blockSignals(true);
345 m_ui.spinSize->blockSignals(true);
347 m_ui.spinX->setValue(rectPos.x());
348 m_ui.spinY->setValue(rectPos.y());
349 m_ui.spinWidth->setValue(rectSize.width());
350 m_ui.spinHeight->setValue(rectSize.height());
351 m_ui.spinSize->setValue(size);
353 m_ui.spinX->blockSignals(false);
354 m_ui.spinY->blockSignals(false);
355 m_ui.spinWidth->blockSignals(false);
356 m_ui.spinHeight->blockSignals(false);
357 m_ui.spinSize->blockSignals(false);
361 void GeometryWidget::slotSetX(int value)
363 m_rect->setPos(value, m_ui.spinY->value());
364 slotUpdateGeometry();
367 void GeometryWidget::slotSetY(int value)
369 m_rect->setPos(m_ui.spinX->value(), value);
370 slotUpdateGeometry();
373 void GeometryWidget::slotSetWidth(int value)
375 m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
376 slotUpdateGeometry();
379 void GeometryWidget::slotSetHeight(int value)
381 m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
382 slotUpdateGeometry();
386 void GeometryWidget::slotResize(int value)
388 m_rect->setRect(0, 0, m_monitor->render->renderWidth() * value / 100, m_monitor->render->renderHeight() * value / 100);
389 slotUpdateGeometry();
393 void GeometryWidget::slotMoveLeft()
395 m_rect->setPos(0, m_rect->pos().y());
396 slotUpdateGeometry();
399 void GeometryWidget::slotCenterH()
401 m_rect->setPos((m_monitor->render->renderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
402 slotUpdateGeometry();
405 void GeometryWidget::slotMoveRight()
407 m_rect->setPos(m_monitor->render->renderWidth() - m_rect->rect().width(), m_rect->pos().y());
408 slotUpdateGeometry();
411 void GeometryWidget::slotMoveTop()
413 m_rect->setPos(m_rect->pos().x(), 0);
414 slotUpdateGeometry();
417 void GeometryWidget::slotCenterV()
419 m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
420 slotUpdateGeometry();
423 void GeometryWidget::slotMoveBottom()
425 m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
426 slotUpdateGeometry();
429 #include "geometrywidget.moc"