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>
32 Geometryval::Geometryval(const MltVideoProfile &profile, const Timecode &t, const QPoint &frame_size, int startPoint, QWidget* parent) :
39 m_frameSize(frame_size),
40 m_startPoint(startPoint),
44 toolbarlayout->addWidget(&m_timePos);
45 toolbarlayout->insertStretch(-1);
47 QVBoxLayout* vbox = new QVBoxLayout(widget);
48 m_sceneview = new QGraphicsView(this);
49 m_sceneview->setBackgroundBrush(QBrush(Qt::black));
50 vbox->addWidget(m_sceneview);
51 vbox->setContentsMargins(0, 0, 0, 0);
53 QVBoxLayout* vbox2 = new QVBoxLayout(keyframeWidget);
54 m_helper = new KeyframeHelper(this);
55 vbox2->addWidget(m_helper);
56 vbox2->setContentsMargins(0, 0, 0, 0);
58 connect(m_helper, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
59 connect(m_helper, SIGNAL(keyframeMoved(int)), this, SLOT(slotKeyframeMoved(int)));
60 connect(m_helper, SIGNAL(addKeyframe(int)), this, SLOT(slotAddFrame(int)));
61 connect(m_helper, SIGNAL(removeKeyframe(int)), this, SLOT(slotDeleteFrame(int)));
63 m_scene = new GraphicsSceneRectMove(this);
64 m_scene->setTool(TITLE_SELECT);
65 m_sceneview->setScene(m_scene);
66 m_dar = (m_profile.height * m_profile.display_aspect_num / (double) m_profile.display_aspect_den) / (double) m_profile.width;
68 m_realWidth = (int)(profile.height * profile.display_aspect_num / (double) profile.display_aspect_den + 0.5);
69 QGraphicsRectItem *frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_realWidth, profile.height));
70 frameBorder->setZValue(-1100);
71 frameBorder->setBrush(QColor(255, 255, 0, 30));
72 frameBorder->setPen(QPen(QBrush(QColor(255, 255, 255, 255)), 1.0, Qt::DashLine));
73 m_scene->addItem(frameBorder);
75 buttonNext->setIcon(KIcon("media-skip-forward"));
76 buttonNext->setToolTip(i18n("Go to next keyframe"));
77 buttonPrevious->setIcon(KIcon("media-skip-backward"));
78 buttonPrevious->setToolTip(i18n("Go to previous keyframe"));
79 buttonAdd->setIcon(KIcon("document-new"));
80 buttonAdd->setToolTip(i18n("Add keyframe"));
81 buttonDelete->setIcon(KIcon("edit-delete"));
82 buttonDelete->setToolTip(i18n("Delete keyframe"));
84 m_configMenu = new QMenu(i18n("Misc..."), this);
85 buttonMenu->setMenu(m_configMenu);
86 buttonMenu->setPopupMode(QToolButton::MenuButtonPopup);
88 m_editOptions = m_configMenu->addAction(KIcon("system-run"), i18n("Show/Hide options"));
89 m_editOptions->setCheckable(true);
90 buttonMenu->setDefaultAction(m_editOptions);
91 connect(m_editOptions, SIGNAL(triggered()), this, SLOT(slotSwitchOptions()));
94 m_reset = m_configMenu->addAction(KIcon("view-refresh"), i18n("Reset"), this, SLOT(slotResetPosition()));
96 m_syncAction = m_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 = m_sceneview->sceneRect();
104 m_sceneview->setSceneRect(srect.x(), -srect.height() / 3 + 10, srect.width(), srect.height() + srect.height() / 3 * 2 - 10);
105 m_scene->setZoom(sc);
106 m_sceneview->centerOn(frameBorder);
107 m_sceneview->setMouseTracking(true);
108 connect(buttonNext , SIGNAL(clicked()) , this , SLOT(slotNextFrame()));
109 connect(buttonPrevious , SIGNAL(clicked()) , this , SLOT(slotPreviousFrame()));
110 connect(buttonDelete , SIGNAL(clicked()) , this , SLOT(slotDeleteFrame()));
111 connect(buttonAdd , SIGNAL(clicked()) , this , SLOT(slotAddFrame()));
112 connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateTransitionProperties()));
114 buttonhcenter->setIcon(KIcon("kdenlive-align-hor"));
115 buttonhcenter->setToolTip(i18n("Align item horizontally"));
116 buttonvcenter->setIcon(KIcon("kdenlive-align-vert"));
117 buttonvcenter->setToolTip(i18n("Align item vertically"));
118 buttontop->setIcon(KIcon("kdenlive-align-top"));
119 buttontop->setToolTip(i18n("Align item to top"));
120 buttonbottom->setIcon(KIcon("kdenlive-align-bottom"));
121 buttonbottom->setToolTip(i18n("Align item to bottom"));
122 buttonright->setIcon(KIcon("kdenlive-align-right"));
123 buttonright->setToolTip(i18n("Align item to right"));
124 buttonleft->setIcon(KIcon("kdenlive-align-left"));
125 buttonleft->setToolTip(i18n("Align item to left"));
127 connect(buttonhcenter, SIGNAL(clicked()), this, SLOT(slotAlignHCenter()));
128 connect(buttonvcenter, SIGNAL(clicked()), this, SLOT(slotAlignVCenter()));
129 connect(buttontop, SIGNAL(clicked()), this, SLOT(slotAlignTop()));
130 connect(buttonbottom, SIGNAL(clicked()), this, SLOT(slotAlignBottom()));
131 connect(buttonright, SIGNAL(clicked()), this, SLOT(slotAlignRight()));
132 connect(buttonleft, SIGNAL(clicked()), this, SLOT(slotAlignLeft()));
133 connect(spinX, SIGNAL(valueChanged(int)), this, SLOT(slotGeometryX(int)));
134 connect(spinY, SIGNAL(valueChanged(int)), this, SLOT(slotGeometryY(int)));
135 connect(spinWidth, SIGNAL(valueChanged(int)), this, SLOT(slotGeometryWidth(int)));
136 connect(spinHeight, SIGNAL(valueChanged(int)), this, SLOT(slotGeometryHeight(int)));
137 connect(spinResize, SIGNAL(editingFinished()), this, SLOT(slotResizeCustom()));
138 connect(buttonResize, SIGNAL(clicked()), this, SLOT(slotResizeOriginal()));
140 connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateGeometry()));
144 Geometryval::~Geometryval()
146 m_scene->disconnect();
158 void Geometryval::slotAlignHCenter()
160 if (!keyframeSelected())
162 m_paramRect->setPos((m_realWidth - m_paramRect->rect().width()) / 2, m_paramRect->pos().y());
163 slotUpdateTransitionProperties();
166 void Geometryval::slotAlignVCenter()
168 if (!keyframeSelected())
170 m_paramRect->setPos(m_paramRect->pos().x(), (m_profile.height - m_paramRect->rect().height()) / 2);
171 slotUpdateTransitionProperties();
174 void Geometryval::slotAlignTop()
176 if (!keyframeSelected())
178 m_paramRect->setPos(m_paramRect->pos().x(), 0);
179 slotUpdateTransitionProperties();
182 void Geometryval::slotAlignBottom()
184 if (!keyframeSelected())
186 m_paramRect->setPos(m_paramRect->pos().x(), m_profile.height - m_paramRect->rect().height());
187 slotUpdateTransitionProperties();
190 void Geometryval::slotAlignLeft()
192 if (!keyframeSelected())
194 m_paramRect->setPos(0, m_paramRect->pos().y());
195 slotUpdateTransitionProperties();
198 void Geometryval::slotAlignRight()
200 if (!keyframeSelected())
202 m_paramRect->setPos(m_realWidth - m_paramRect->rect().width(), m_paramRect->pos().y());
203 slotUpdateTransitionProperties();
206 void Geometryval::slotResizeOriginal()
208 if (!keyframeSelected())
210 if (m_frameSize.isNull())
211 m_paramRect->setRect(0, 0, m_realWidth, m_profile.height);
213 m_paramRect->setRect(0, 0, m_frameSize.x(), m_frameSize.y());
214 slotUpdateTransitionProperties();
217 void Geometryval::slotResizeCustom()
219 if (!keyframeSelected())
221 int value = spinResize->value();
222 m_paramRect->setRect(0, 0, m_realWidth * value / 100, m_profile.height * value / 100);
223 slotUpdateTransitionProperties();
226 void Geometryval::slotTransparencyChanged(int transp)
228 int pos = m_timePos.getValue();
229 Mlt::GeometryItem item;
230 int error = m_geom->fetch(&item, pos);
231 if (error || item.key() == false) {
232 // no keyframe under cursor
236 m_paramRect->setBrush(QColor(255, 0, 0, transp));
237 m_geom->insert(item);
238 emit parameterChanged();
241 void Geometryval::slotSyncCursor()
243 KdenliveSettings::setTransitionfollowcursor(m_syncAction->isChecked());
246 void Geometryval::updateTimecodeFormat()
248 m_timePos.slotUpdateTimeCodeFormat();
251 void Geometryval::slotPositionChanged(int pos, bool seek)
254 pos = m_timePos.getValue();
256 if (seek && KdenliveSettings::transitionfollowcursor()) emit seekToPos(pos + m_startPoint);
257 m_timePos.setValue(pos);
258 //spinPos->setValue(pos);
259 m_helper->blockSignals(true);
260 m_helper->setValue(pos);
261 m_helper->blockSignals(false);
262 Mlt::GeometryItem item;
263 int error = m_geom->fetch(&item, pos);
264 if (error || item.key() == false) {
265 // no keyframe under cursor, adjust buttons
266 buttonAdd->setEnabled(true);
267 buttonDelete->setEnabled(false);
268 widget->setEnabled(false);
269 spinTransp->setEnabled(false);
270 frameOptions->setEnabled(false);
271 m_reset->setEnabled(false);
273 buttonAdd->setEnabled(false);
274 buttonDelete->setEnabled(true);
275 widget->setEnabled(true);
276 spinTransp->setEnabled(true);
277 frameOptions->setEnabled(true);
278 m_reset->setEnabled(true);
281 m_paramRect->setPos(item.x() * m_dar, item.y());
282 m_paramRect->setRect(0, 0, item.w() * m_dar, item.h());
283 spinTransp->setValue(item.mix());
284 m_paramRect->setBrush(QColor(255, 0, 0, item.mix()));
285 slotUpdateGeometry();
288 void Geometryval::slotDeleteFrame(int pos)
290 // check there is more than one keyframe
291 Mlt::GeometryItem item;
292 int frame = m_timePos.getValue();
294 if (pos == -1) pos = frame;
295 int error = m_geom->next_key(&item, pos + 1);
297 error = m_geom->prev_key(&item, pos - 1);
298 if (error || item.frame() == pos) return;
301 m_geom->remove(frame);
302 buttonAdd->setEnabled(true);
303 buttonDelete->setEnabled(false);
304 widget->setEnabled(false);
305 spinTransp->setEnabled(false);
306 frameOptions->setEnabled(false);
307 m_reset->setEnabled(false);
309 slotPositionChanged(pos, false);
310 updateTransitionPath();
311 emit parameterChanged();
314 void Geometryval::slotAddFrame(int pos)
316 int frame = m_timePos.getValue();
317 if (pos == -1) pos = frame;
318 Mlt::GeometryItem item;
320 QRectF r = m_paramRect->rect().normalized();
321 QPointF rectpos = m_paramRect->pos();
322 item.x(rectpos.x() / m_dar);
324 item.w(r.width() / m_dar);
326 item.mix(spinTransp->value());
327 m_geom->insert(item);
328 buttonAdd->setEnabled(false);
329 buttonDelete->setEnabled(true);
330 widget->setEnabled(true);
331 spinTransp->setEnabled(true);
332 frameOptions->setEnabled(true);
333 m_reset->setEnabled(true);
335 emit parameterChanged();
338 void Geometryval::slotNextFrame()
340 Mlt::GeometryItem item;
341 int error = m_geom->next_key(&item, m_helper->value() + 1);
343 kDebug() << "// SEEK TO NEXT KFR: " << error;
346 pos = m_helper->frameLength;
347 } else pos = item.frame();
348 m_timePos.setValue(pos);
349 slotPositionChanged();
352 void Geometryval::slotPreviousFrame()
354 Mlt::GeometryItem item;
355 int error = m_geom->prev_key(&item, m_helper->value() - 1);
356 kDebug() << "// SEEK TO NEXT KFR: " << error;
358 int pos = item.frame();
359 m_timePos.setValue(pos);
360 slotPositionChanged();
364 QString Geometryval::getValue() const
366 return m_geom->serialise();
369 void Geometryval::setupParam(const QDomElement par, int minFrame, int maxFrame)
371 QString val = par.attribute("value");
372 if (par.attribute("fixed") == "1") {
374 buttonPrevious->setHidden(true);
375 buttonNext->setHidden(true);
376 buttonDelete->setHidden(true);
377 buttonAdd->setHidden(true);
378 spinTransp->setMaximum(500);
379 label_pos->setHidden(true);
380 m_helper->setHidden(true);
381 m_timePos.setHidden(true);
383 if (par.attribute("opacity") == "false") {
384 label_opacity->setHidden(true);
385 spinTransp->setHidden(true);
388 m_geom->parse(val.toUtf8().data(), maxFrame - minFrame, m_profile.width, m_profile.height);
390 m_geom = new Mlt::Geometry(val.toUtf8().data(), maxFrame - minFrame, m_profile.width, m_profile.height);
392 //kDebug() << " / / UPDATING TRANSITION VALUE: " << m_geom->serialise();
393 //read param her and set rect
395 m_helper->setKeyGeometry(m_geom, maxFrame - minFrame - 1);
398 doc.appendChild(doc.importNode(par, true));
399 kDebug() << "IMPORTED TRANS: " << doc.toString();*/
400 if (m_path == NULL) {
401 m_path = new QGraphicsPathItem();
402 m_path->setPen(QPen(Qt::red));
403 m_scene->addItem(m_path);
405 updateTransitionPath();
407 Mlt::GeometryItem item;
409 m_geom->fetch(&item, 0);
411 m_paramRect = new QGraphicsRectItem(QRectF(0, 0, item.w() * m_dar, item.h()));
412 m_paramRect->setPos(item.x() * m_dar, item.y());
413 m_paramRect->setZValue(0);
414 m_paramRect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
416 m_paramRect->setPen(QPen(QBrush(QColor(255, 0, 0, 255)), 1.0));
417 m_scene->addItem(m_paramRect);
418 slotPositionChanged(0, false);
419 slotUpdateGeometry();
421 m_timePos.setRange(0, maxFrame - minFrame - 1);
422 connect(&m_timePos, SIGNAL(timeCodeEditingFinished()), this , SLOT(slotPositionChanged()));
424 connect(spinTransp, SIGNAL(valueChanged(int)), this , SLOT(slotTransparencyChanged(int)));
427 void Geometryval::slotSyncPosition(int relTimelinePos)
429 if (m_timePos.maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
430 relTimelinePos = qMax(0, relTimelinePos);
431 relTimelinePos = qMin(relTimelinePos, m_timePos.maximum());
432 if (relTimelinePos != m_timePos.getValue())
433 slotPositionChanged(relTimelinePos, false);
437 void Geometryval::updateTransitionPath()
439 if (m_fixedMode) return;
440 Mlt::GeometryItem item;
445 if (m_geom->next_key(&item, pos) == 1) break;
448 path.moveTo(item.x() * m_dar + item.w() * m_dar / 2, item.y() + item.h() / 2);
450 path.lineTo(item.x() * m_dar + item.w() * m_dar / 2, item.y() + item.h() / 2);
455 m_path->setPath(path);
458 void Geometryval::slotUpdateTransitionProperties()
460 int pos = m_timePos.getValue();
461 Mlt::GeometryItem item;
462 int error = m_geom->next_key(&item, pos);
463 if (error || item.frame() != pos) {
464 // no keyframe under cursor
467 QRectF r = m_paramRect->rect().normalized();
468 QPointF rectpos = m_paramRect->pos();
469 item.x(rectpos.x() / m_dar);
471 item.w(r.width() / m_dar);
473 m_geom->insert(item);
474 updateTransitionPath();
475 emit parameterChanged();
478 void Geometryval::slotResetPosition()
483 if (m_frameSize.isNull()) {
484 spinWidth->setValue(m_realWidth);
485 spinHeight->setValue(m_profile.height);
487 spinWidth->setValue(m_frameSize.x());
488 spinHeight->setValue(m_frameSize.y());
492 void Geometryval::setFrameSize(const QPoint &p)
498 void Geometryval::slotKeyframeMoved(int pos)
500 slotPositionChanged(pos);
501 slotUpdateTransitionProperties();
502 QTimer::singleShot(100, this, SIGNAL(parameterChanged()));
505 void Geometryval::slotSwitchOptions()
507 if (frameOptions->isHidden()) {
508 frameOptions->setHidden(false);
509 m_editOptions->setChecked(true);
511 frameOptions->setHidden(true);
512 m_editOptions->setChecked(false);
517 void Geometryval::slotGeometryX(int value)
519 if (!keyframeSelected())
521 m_paramRect->setPos(value, spinY->value());
522 slotUpdateTransitionProperties();
525 void Geometryval::slotGeometryY(int value)
527 if (!keyframeSelected())
529 m_paramRect->setPos(spinX->value(), value);
530 slotUpdateTransitionProperties();
533 void Geometryval::slotGeometryWidth(int value)
535 if (!keyframeSelected())
537 m_paramRect->setRect(0, 0, value, spinHeight->value());
538 slotUpdateTransitionProperties();
541 void Geometryval::slotGeometryHeight(int value)
543 if (!keyframeSelected())
545 m_paramRect->setRect(0, 0, spinWidth->value(), value);
546 slotUpdateTransitionProperties();
549 void Geometryval::slotUpdateGeometry()
551 QRectF r = m_paramRect->rect().normalized();
553 spinX->blockSignals(true);
554 spinY->blockSignals(true);
555 spinWidth->blockSignals(true);
556 spinHeight->blockSignals(true);
557 spinResize->blockSignals(true);
559 spinX->setValue(m_paramRect->pos().x());
560 spinY->setValue(m_paramRect->pos().y());
561 spinWidth->setValue(r.width());
562 spinHeight->setValue(r.height());
563 spinResize->setValue(m_paramRect->rect().width() * 100 / m_realWidth);
565 spinX->blockSignals(false);
566 spinY->blockSignals(false);
567 spinWidth->blockSignals(false);
568 spinHeight->blockSignals(false);
569 spinResize->blockSignals(false);
572 bool Geometryval::keyframeSelected()
574 Mlt::GeometryItem item;
575 int pos = m_timePos.getValue();
576 if (m_geom->fetch(&item, pos) || item.key() == false) return false;
581 void Geometryval::slotUpdateRange(int inPoint, int outPoint)
583 m_helper->setKeyGeometry(m_geom, outPoint - inPoint - 1);
585 m_timePos.setRange(0, outPoint - inPoint - 1);
588 #include "geometryval.moc"