1 /***************************************************************************
2 geomeytrval.cpp - description
5 copyright : (C) 2008 by Marco Gittler
6 email : g.marco@freenet.de
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
18 #include "geometryval.h"
19 #include "graphicsscenerectmove.h"
20 #include "kdenlivesettings.h"
24 #include <QGraphicsView>
25 #include <QVBoxLayout>
26 #include <QGraphicsRectItem>
28 #include <QInputDialog>
31 Geometryval::Geometryval(const MltVideoProfile profile, QWidget* parent) :
40 QVBoxLayout* vbox = new QVBoxLayout(m_ui.widget);
41 QGraphicsView *view = new QGraphicsView(this);
42 view->setBackgroundBrush(QBrush(Qt::black));
43 vbox->addWidget(view);
44 vbox->setContentsMargins(0, 0, 0, 0);
46 QVBoxLayout* vbox2 = new QVBoxLayout(m_ui.keyframeWidget);
47 m_helper = new KeyframeHelper(this);
48 vbox2->addWidget(m_helper);
49 vbox2->setContentsMargins(0, 0, 0, 0);
51 connect(m_helper, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
53 m_scene = new GraphicsSceneRectMove(this);
54 m_scene->setTool(TITLE_SELECT);
55 view->setScene(m_scene);
56 QGraphicsRectItem *m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, profile.width, profile.height));
57 m_frameBorder->setZValue(-1100);
58 m_frameBorder->setBrush(QColor(255, 255, 0, 30));
59 m_frameBorder->setPen(QPen(QBrush(QColor(255, 255, 255, 255)), 1.0, Qt::DashLine));
60 m_scene->addItem(m_frameBorder);
62 m_ui.buttonNext->setIcon(KIcon("media-skip-forward"));
63 m_ui.buttonNext->setToolTip(i18n("Go to next keyframe"));
64 m_ui.buttonPrevious->setIcon(KIcon("media-skip-backward"));
65 m_ui.buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
66 m_ui.buttonAdd->setIcon(KIcon("document-new"));
67 m_ui.buttonAdd->setToolTip(i18n("Add keyframe"));
68 m_ui.buttonDelete->setIcon(KIcon("edit-delete"));
69 m_ui.buttonDelete->setToolTip(i18n("Delete keyframe"));
71 QMenu *configMenu = new QMenu(i18n("Misc..."), this);
72 m_ui.buttonMenu->setIcon(KIcon("system-run"));
73 m_ui.buttonMenu->setMenu(configMenu);
74 m_ui.buttonMenu->setPopupMode(QToolButton::QToolButton::InstantPopup);
77 m_scaleMenu = new QMenu(i18n("Resize..."), this);
78 configMenu->addMenu(m_scaleMenu);
79 m_scaleMenu->addAction(i18n("50%"), this, SLOT(slotResize50()));
80 m_scaleMenu->addAction(i18n("100%"), this, SLOT(slotResize100()));
81 m_scaleMenu->addAction(i18n("200%"), this, SLOT(slotResize200()));
82 m_scaleMenu->addAction(i18n("Custom"), this, SLOT(slotResizeCustom()));
85 m_alignMenu = new QMenu(i18n("Align..."), this);
86 configMenu->addMenu(m_alignMenu);
87 m_alignMenu->addAction(i18n("Center"), this, SLOT(slotAlignCenter()));
88 m_alignMenu->addAction(i18n("Hor. Center"), this, SLOT(slotAlignHCenter()));
89 m_alignMenu->addAction(i18n("Vert. Center"), this, SLOT(slotAlignVCenter()));
90 m_alignMenu->addAction(i18n("Right"), this, SLOT(slotAlignRight()));
91 m_alignMenu->addAction(i18n("Left"), this, SLOT(slotAlignLeft()));
92 m_alignMenu->addAction(i18n("Top"), this, SLOT(slotAlignTop()));
93 m_alignMenu->addAction(i18n("Bottom"), this, SLOT(slotAlignBottom()));
96 m_syncAction = configMenu->addAction(i18n("Sync timeline cursor"), this, SLOT(slotSyncCursor()));
97 m_syncAction->setCheckable(true);
98 m_syncAction->setChecked(KdenliveSettings::transitionfollowcursor());
100 //scene->setSceneRect(0, 0, profile.width * 2, profile.height * 2);
101 //view->fitInView(m_frameBorder, Qt::KeepAspectRatio);
102 const double sc = 100.0 / profile.height * 0.8;
103 QRectF srect = view->sceneRect();
104 view->setSceneRect(srect.x(), -srect.height() / 3 + 10, srect.width(), srect.height() + srect.height() / 3 * 2 - 10);
105 m_scene->setZoom(sc);
106 view->centerOn(m_frameBorder);
107 connect(m_ui.buttonNext , SIGNAL(clicked()) , this , SLOT(slotNextFrame()));
108 connect(m_ui.buttonPrevious , SIGNAL(clicked()) , this , SLOT(slotPreviousFrame()));
109 connect(m_ui.buttonDelete , SIGNAL(clicked()) , this , SLOT(slotDeleteFrame()));
110 connect(m_ui.buttonAdd , SIGNAL(clicked()) , this , SLOT(slotAddFrame()));
111 connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateTransitionProperties()));
114 void Geometryval::slotAlignCenter()
116 int pos = m_ui.spinPos->value();
117 Mlt::GeometryItem item;
118 int error = m_geom->fetch(&item, pos);
119 if (error || item.key() == false) {
120 // no keyframe under cursor
123 m_paramRect->setPos((m_profile.width - m_paramRect->rect().width()) / 2, (m_profile.height - m_paramRect->rect().height()) / 2);
124 slotUpdateTransitionProperties();
127 void Geometryval::slotAlignHCenter()
129 int pos = m_ui.spinPos->value();
130 Mlt::GeometryItem item;
131 int error = m_geom->fetch(&item, pos);
132 if (error || item.key() == false) {
133 // no keyframe under cursor
136 m_paramRect->setPos((m_profile.width - m_paramRect->rect().width()) / 2, m_paramRect->pos().y());
137 slotUpdateTransitionProperties();
140 void Geometryval::slotAlignVCenter()
142 int pos = m_ui.spinPos->value();
143 Mlt::GeometryItem item;
144 int error = m_geom->fetch(&item, pos);
145 if (error || item.key() == false) {
146 // no keyframe under cursor
149 m_paramRect->setPos(m_paramRect->pos().x(), (m_profile.height - m_paramRect->rect().height()) / 2);
150 slotUpdateTransitionProperties();
153 void Geometryval::slotAlignTop()
155 int pos = m_ui.spinPos->value();
156 Mlt::GeometryItem item;
157 int error = m_geom->fetch(&item, pos);
158 if (error || item.key() == false) {
159 // no keyframe under cursor
162 m_paramRect->setPos(m_paramRect->pos().x(), 0);
163 slotUpdateTransitionProperties();
166 void Geometryval::slotAlignBottom()
168 int pos = m_ui.spinPos->value();
169 Mlt::GeometryItem item;
170 int error = m_geom->fetch(&item, pos);
171 if (error || item.key() == false) {
172 // no keyframe under cursor
175 m_paramRect->setPos(m_paramRect->pos().x(), m_profile.height - m_paramRect->rect().height());
176 slotUpdateTransitionProperties();
179 void Geometryval::slotAlignLeft()
181 int pos = m_ui.spinPos->value();
182 Mlt::GeometryItem item;
183 int error = m_geom->fetch(&item, pos);
184 if (error || item.key() == false) {
185 // no keyframe under cursor
188 m_paramRect->setPos(0, m_paramRect->pos().y());
189 slotUpdateTransitionProperties();
192 void Geometryval::slotAlignRight()
194 int pos = m_ui.spinPos->value();
195 Mlt::GeometryItem item;
196 int error = m_geom->fetch(&item, pos);
197 if (error || item.key() == false) {
198 // no keyframe under cursor
201 m_paramRect->setPos(m_profile.width - m_paramRect->rect().width(), m_paramRect->pos().y());
202 slotUpdateTransitionProperties();
205 void Geometryval::slotResize50()
207 int pos = m_ui.spinPos->value();
208 Mlt::GeometryItem item;
209 int error = m_geom->fetch(&item, pos);
210 if (error || item.key() == false) {
211 // no keyframe under cursor
214 m_paramRect->setRect(0, 0, m_profile.width / 2, m_profile.height / 2);
215 slotUpdateTransitionProperties();
218 void Geometryval::slotResize100()
220 int pos = m_ui.spinPos->value();
221 Mlt::GeometryItem item;
222 int error = m_geom->fetch(&item, pos);
223 if (error || item.key() == false) {
224 // no keyframe under cursor
227 m_paramRect->setRect(0, 0, m_profile.width, m_profile.height);
228 slotUpdateTransitionProperties();
231 void Geometryval::slotResize200()
233 int pos = m_ui.spinPos->value();
234 Mlt::GeometryItem item;
235 int error = m_geom->fetch(&item, pos);
236 if (error || item.key() == false) {
237 // no keyframe under cursor
240 m_paramRect->setRect(0, 0, m_profile.width * 2, m_profile.height * 2);
241 slotUpdateTransitionProperties();
244 void Geometryval::slotResizeCustom()
246 int pos = m_ui.spinPos->value();
247 Mlt::GeometryItem item;
248 int error = m_geom->fetch(&item, pos);
249 if (error || item.key() == false) {
250 // no keyframe under cursor
253 int scale = m_paramRect->rect().width() * 100 / m_profile.width;
255 scale = QInputDialog::getInteger(this, i18n("Resize..."), i18n("Scale"), scale, 1, 2147483647, 10, &ok);
257 m_paramRect->setRect(0, 0, m_profile.width * scale / 100, m_profile.height * scale / 100);
258 slotUpdateTransitionProperties();
261 void Geometryval::slotTransparencyChanged(int transp)
263 int pos = m_ui.spinPos->value();
264 Mlt::GeometryItem item;
265 int error = m_geom->fetch(&item, pos);
266 if (error || item.key() == false) {
267 // no keyframe under cursor
271 m_paramRect->setBrush(QColor(255, 0, 0, transp));
272 m_geom->insert(item);
273 emit parameterChanged();
276 void Geometryval::slotSyncCursor()
278 KdenliveSettings::setTransitionfollowcursor(m_syncAction->isChecked());
281 void Geometryval::slotPositionChanged(int pos, bool seek)
283 if (seek && KdenliveSettings::transitionfollowcursor()) emit seekToPos(pos);
284 m_ui.spinPos->setValue(pos);
285 m_helper->setValue(pos);
286 Mlt::GeometryItem item;
287 int error = m_geom->fetch(&item, pos);
288 if (error || item.key() == false) {
289 // no keyframe under cursor, adjust buttons
290 m_ui.buttonAdd->setEnabled(true);
291 m_ui.buttonDelete->setEnabled(false);
292 m_ui.widget->setEnabled(false);
293 m_ui.spinTransp->setEnabled(false);
294 m_scaleMenu->setEnabled(false);
295 m_alignMenu->setEnabled(false);
297 m_ui.buttonAdd->setEnabled(false);
298 m_ui.buttonDelete->setEnabled(true);
299 m_ui.widget->setEnabled(true);
300 m_ui.spinTransp->setEnabled(true);
301 m_scaleMenu->setEnabled(true);
302 m_alignMenu->setEnabled(true);
304 m_paramRect->setPos(item.x(), item.y());
305 m_paramRect->setRect(0, 0, item.w(), item.h());
306 m_ui.spinTransp->setValue(item.mix());
307 m_paramRect->setBrush(QColor(255, 0, 0, item.mix()));
310 void Geometryval::slotDeleteFrame()
312 // check there is more than one keyframe
313 Mlt::GeometryItem item;
314 const int pos = m_ui.spinPos->value();
315 int error = m_geom->next_key(&item, pos + 1);
317 error = m_geom->prev_key(&item, pos - 1);
318 if (error || item.frame() == pos) return;
321 m_geom->remove(m_ui.spinPos->value());
322 m_ui.buttonAdd->setEnabled(true);
323 m_ui.buttonDelete->setEnabled(false);
324 m_ui.widget->setEnabled(false);
325 m_ui.spinTransp->setEnabled(false);
326 m_scaleMenu->setEnabled(false);
327 m_alignMenu->setEnabled(false);
329 slotPositionChanged(pos, false);
330 updateTransitionPath();
331 emit parameterChanged();
334 void Geometryval::slotAddFrame()
336 int pos = m_ui.spinPos->value();
337 Mlt::GeometryItem item;
339 item.x(m_paramRect->pos().x());
340 item.y(m_paramRect->pos().y());
341 item.w(m_paramRect->rect().width());
342 item.h(m_paramRect->rect().height());
343 item.mix(m_ui.spinTransp->value());
344 m_geom->insert(item);
345 m_ui.buttonAdd->setEnabled(false);
346 m_ui.buttonDelete->setEnabled(true);
347 m_ui.widget->setEnabled(true);
348 m_ui.spinTransp->setEnabled(true);
349 m_scaleMenu->setEnabled(true);
350 m_alignMenu->setEnabled(true);
352 emit parameterChanged();
355 void Geometryval::slotNextFrame()
357 Mlt::GeometryItem item;
358 int error = m_geom->next_key(&item, m_helper->value() + 1);
359 kDebug() << "// SEEK TO NEXT KFR: " << error;
362 m_ui.spinPos->setValue(m_ui.spinPos->maximum());
365 int pos = item.frame();
366 m_ui.spinPos->setValue(pos);
369 void Geometryval::slotPreviousFrame()
371 Mlt::GeometryItem item;
372 int error = m_geom->prev_key(&item, m_helper->value() - 1);
373 kDebug() << "// SEEK TO NEXT KFR: " << error;
375 int pos = item.frame();
376 m_ui.spinPos->setValue(pos);
380 QDomElement Geometryval::getParamDesc()
382 m_param.setAttribute("value", m_geom->serialise());
383 kDebug() << " / / UPDATING TRANSITION VALUE: " << m_param.attribute("value");
387 void Geometryval::setupParam(const QDomElement& par, int minFrame, int maxFrame)
390 QString val = par.attribute("value");
391 if (par.attribute("fixed") == "1") {
393 m_ui.buttonPrevious->setHidden(true);
394 m_ui.buttonNext->setHidden(true);
395 m_ui.buttonDelete->setHidden(true);
396 m_ui.buttonAdd->setHidden(true);
397 m_ui.spinTransp->setMaximum(500);
398 m_ui.label_pos->setHidden(true);
399 m_helper->setHidden(true);
400 m_ui.spinPos->setHidden(true);
402 char *tmp = (char *) qstrdup(val.toUtf8().data());
403 if (m_geom) m_geom->parse(tmp, maxFrame - minFrame, m_profile.width, m_profile.height);
404 else m_geom = new Mlt::Geometry(tmp, maxFrame - minFrame, m_profile.width, m_profile.height);
407 //kDebug() << " / / UPDATING TRANSITION VALUE: " << m_geom->serialise();
408 //read param her and set rect
410 m_helper->setKeyGeometry(m_geom, maxFrame - minFrame - 1);
413 doc.appendChild(doc.importNode(par, true));
414 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
415 m_ui.spinPos->setMaximum(maxFrame - minFrame - 1);
416 if (m_path == NULL) {
417 m_path = new QGraphicsPathItem();
418 m_path->setPen(QPen(Qt::red));
419 m_scene->addItem(m_path);
421 updateTransitionPath();
423 Mlt::GeometryItem item;
425 m_geom->fetch(&item, 0);
427 m_paramRect = new QGraphicsRectItem(QRectF(0, 0, item.w(), item.h()));
428 m_paramRect->setPos(item.x(), item.y());
429 m_paramRect->setZValue(0);
431 m_paramRect->setPen(QPen(QBrush(QColor(255, 0, 0, 255)), 1.0));
432 m_scene->addItem(m_paramRect);
433 slotPositionChanged(0, false);
435 connect(m_ui.spinPos, SIGNAL(valueChanged(int)), this , SLOT(slotPositionChanged(int)));
437 connect(m_ui.spinTransp, SIGNAL(valueChanged(int)), this , SLOT(slotTransparencyChanged(int)));
440 void Geometryval::updateTransitionPath()
442 if (m_fixedMode) return;
443 Mlt::GeometryItem item;
448 if (m_geom->next_key(&item, pos) == 1) break;
451 path.moveTo(item.x() + item.w() / 2, item.y() + item.h() / 2);
453 path.lineTo(item.x() + item.w() / 2, item.y() + item.h() / 2);
458 m_path->setPath(path);
461 void Geometryval::slotUpdateTransitionProperties()
463 int pos = m_ui.spinPos->value();
464 Mlt::GeometryItem item;
465 int error = m_geom->next_key(&item, pos);
466 if (error || item.frame() != pos) {
467 // no keyframe under cursor
470 QRectF r = m_paramRect->rect().normalized();
471 item.x(m_paramRect->pos().x());
472 item.y(m_paramRect->pos().y());
475 m_geom->insert(item);
476 updateTransitionPath();
477 emit parameterChanged();