]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
Add settings to new geometry widget:
[kdenlive] / src / geometrywidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include "geometrywidget.h"
22 #include "monitor.h"
23 #include "renderer.h"
24 #include "keyframehelper.h"
25 #include "timecodedisplay.h"
26 #include "monitorscene.h"
27 #include "kdenlivesettings.h"
28
29 #include <QtCore>
30 #include <QGraphicsView>
31 #include <QGraphicsRectItem>
32 #include <QVBoxLayout>
33 #include <QGridLayout>
34
35 GeometryWidget::GeometryWidget(Monitor* monitor, Timecode timecode, int clipPos, bool isEffect, QWidget* parent ):
36         QWidget(parent),
37         m_monitor(monitor),
38         m_timePos(new TimecodeDisplay(timecode)),
39         m_clipPos(clipPos),
40         m_inPoint(0),
41         m_outPoint(1),
42         m_isEffect(isEffect),
43         m_rect(NULL),
44         m_geometry(NULL),
45         m_showScene(true)
46 {
47     m_ui.setupUi(this);
48     m_scene = monitor->getEffectScene();
49
50
51     /*
52         Setup of timeline and keyframe controls
53     */
54
55     ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 4);
56
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);
61
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"));
68
69     connect(m_timeline, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
70     connect(m_timeline, SIGNAL(keyframeMoved(int)),   this, SLOT(slotKeyframeMoved(int)));
71     connect(m_timeline, SIGNAL(addKeyframe(int)),     this, SLOT(slotAddKeyframe(int)));
72     connect(m_timeline, SIGNAL(removeKeyframe(int)),  this, SLOT(slotDeleteKeyframe(int)));
73     connect(m_timePos, SIGNAL(editingFinished()), this , SLOT(slotPositionChanged()));
74     connect(m_ui.buttonPrevious,  SIGNAL(clicked()), this, SLOT(slotPreviousKeyframe()));
75     connect(m_ui.buttonNext,      SIGNAL(clicked()), this, SLOT(slotNextKeyframe()));
76     connect(m_ui.buttonAddDelete, SIGNAL(clicked()), this, SLOT(slotAddDeleteKeyframe()));
77
78
79     /*
80         Setup of geometry controls
81     */
82
83     m_ui.buttonMoveLeft->setIcon(KIcon("kdenlive-align-left"));
84     m_ui.buttonMoveLeft->setToolTip(i18n("Move to left"));
85     m_ui.buttonCenterH->setIcon(KIcon("kdenlive-align-hor"));
86     m_ui.buttonCenterH->setToolTip(i18n("Center horizontally"));
87     m_ui.buttonMoveRight->setIcon(KIcon("kdenlive-align-right"));
88     m_ui.buttonMoveRight->setToolTip(i18n("Move to right"));
89     m_ui.buttonMoveTop->setIcon(KIcon("kdenlive-align-top"));
90     m_ui.buttonMoveTop->setToolTip(i18n("Move to top"));
91     m_ui.buttonCenterV->setIcon(KIcon("kdenlive-align-vert"));
92     m_ui.buttonCenterV->setToolTip(i18n("Center vertically"));
93     m_ui.buttonMoveBottom->setIcon(KIcon("kdenlive-align-bottom"));
94     m_ui.buttonMoveBottom->setToolTip(i18n("Move to bottom"));
95
96     connect(m_ui.spinX,            SIGNAL(valueChanged(int)), this, SLOT(slotSetX(int)));
97     connect(m_ui.spinY,            SIGNAL(valueChanged(int)), this, SLOT(slotSetY(int)));
98     connect(m_ui.spinWidth,        SIGNAL(valueChanged(int)), this, SLOT(slotSetWidth(int)));
99     connect(m_ui.spinHeight,       SIGNAL(valueChanged(int)), this, SLOT(slotSetHeight(int)));
100
101     connect(m_ui.spinSize,         SIGNAL(valueChanged(int)), this, SLOT(slotResize(int)));
102
103     connect(m_ui.spinOpacity,      SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
104     connect(m_ui.sliderOpacity,    SIGNAL(valueChanged(int)), m_ui.spinOpacity, SLOT(setValue(int)));
105
106     connect(m_ui.buttonMoveLeft,   SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
107     connect(m_ui.buttonCenterH,    SIGNAL(clicked()), this, SLOT(slotCenterH()));
108     connect(m_ui.buttonMoveRight,  SIGNAL(clicked()), this, SLOT(slotMoveRight()));
109     connect(m_ui.buttonMoveTop,    SIGNAL(clicked()), this, SLOT(slotMoveTop()));
110     connect(m_ui.buttonCenterV,    SIGNAL(clicked()), this, SLOT(slotCenterV()));
111     connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));
112
113
114     /*
115         Setup of configuration controls
116     */
117
118     m_ui.buttonConfig->setIcon(KIcon("system-run"));
119     m_ui.buttonConfig->setToolTip(i18n("Show/Hide settings"));
120     m_ui.buttonConfig->setCheckable(true);
121     m_ui.groupSettings->setHidden(true);
122     m_ui.checkSync->setChecked(KdenliveSettings::transitionfollowcursor());
123     m_ui.checkDirectUpdate->setChecked(m_scene->getDirectUpdate());
124
125     connect(m_ui.buttonConfig, SIGNAL(toggled(bool)), m_ui.groupSettings, SLOT(setVisible(bool)));
126
127     connect(m_ui.checkSync,         SIGNAL(toggled(bool)), this, SLOT(slotSetSynchronize(bool)));
128     connect(m_ui.checkShowScene,    SIGNAL(toggled(bool)), this, SLOT(slotShowScene(bool)));
129     connect(m_ui.checkDirectUpdate, SIGNAL(toggled(bool)), m_scene, SLOT(slotSetDirectUpdate(bool)));
130
131
132     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
133     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
134     connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
135 }
136
137 GeometryWidget::~GeometryWidget()
138 {
139     delete m_timePos;
140     delete m_timeline;
141     m_scene->removeItem(m_rect);
142     delete m_geometry;
143     m_monitor->slotEffectScene(false);
144 }
145
146 void GeometryWidget::updateTimecodeFormat()
147 {
148     m_timePos->slotUpdateTimeCodeFormat();
149 }
150
151 QString GeometryWidget::getValue() const
152 {
153     return m_geometry->serialise();
154 }
155
156 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
157 {
158     m_inPoint = minframe;
159     m_outPoint = maxframe;
160
161     char *tmp = (char *) qstrdup(elem.attribute("value").toUtf8().data());
162     if (m_geometry)
163         m_geometry->parse(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
164     else
165         m_geometry = new Mlt::Geometry(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
166     delete[] tmp;
167
168     if (elem.attribute("fixed") == "1") {
169         // Keyframes are disabled
170         m_ui.widgetTimeWrapper->setHidden(true);
171     } else {
172         m_ui.widgetTimeWrapper->setHidden(false);
173         m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint - 1);
174         m_timeline->update();
175         m_timePos->setRange(0, m_outPoint - m_inPoint - 1);
176     }
177
178     // no opacity
179     if (elem.attribute("opacity") == "false")
180         m_ui.widgetOpacity->setHidden(true);
181
182     Mlt::GeometryItem item;
183
184     m_geometry->fetch(&item, 0);
185     delete m_rect;
186     m_rect = new QGraphicsRectItem(QRectF(0, 0, item.w(), item.h()));
187     m_rect->setPos(item.x(), item.y());
188     m_rect->setZValue(0);
189     m_rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
190
191     QPen framepen(Qt::SolidLine);
192     framepen.setColor(Qt::yellow);
193     m_rect->setPen(framepen);
194     m_rect->setBrush(Qt::transparent);
195     m_scene->addItem(m_rect);
196
197     slotPositionChanged(0, false);
198     slotUpdateProperties();
199     slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
200 }
201
202 void GeometryWidget::slotSyncPosition(int relTimelinePos)
203 {
204     if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
205         relTimelinePos = qMax(0, relTimelinePos);
206         relTimelinePos = qMin(relTimelinePos, m_timePos->maximum());
207         if (relTimelinePos != m_timePos->getValue())
208             slotPositionChanged(relTimelinePos, false);
209     }
210 }
211
212
213 void GeometryWidget::slotPositionChanged(int pos, bool seek)
214 {
215     if (pos == -1)
216         pos = m_timePos->getValue();
217
218     m_timePos->setValue(pos);
219     m_timeline->blockSignals(true);
220     m_timeline->setValue(pos);
221     m_timeline->blockSignals(false);
222
223     Mlt::GeometryItem item;
224     if (m_geometry->fetch(&item, pos) || item.key() == false) {
225         // no keyframe
226         m_scene->setEnabled(false);
227         m_ui.widgetGeometry->setEnabled(false);
228         m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
229         m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
230     } else {
231         // keyframe
232         m_scene->setEnabled(true);
233         m_ui.widgetGeometry->setEnabled(true);
234         m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
235         m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
236     }
237
238     m_rect->setPos(item.x(), item.y());
239     m_rect->setRect(0, 0, item.w(), item.h());
240
241     m_ui.spinOpacity->blockSignals(true);
242     m_ui.sliderOpacity->blockSignals(true);
243     m_ui.spinOpacity->setValue(item.mix());
244     m_ui.sliderOpacity->setValue(item.mix());
245     m_ui.spinOpacity->blockSignals(false);
246     m_ui.sliderOpacity->blockSignals(false);
247
248     slotUpdateProperties();
249
250     if (seek && KdenliveSettings::transitionfollowcursor())
251         emit seekToPos(m_clipPos + pos);
252 }
253
254 void GeometryWidget::slotKeyframeMoved(int pos)
255 {
256     slotPositionChanged(pos);
257     slotUpdateGeometry();
258 }
259
260 void GeometryWidget::slotAddKeyframe(int pos)
261 {
262     Mlt::GeometryItem item;
263     if (pos == -1)
264         pos = m_timePos->getValue();
265     item.frame(pos);
266     QRectF r = m_rect->rect().normalized();
267     QPointF rectpos = m_rect->pos();
268     item.x(rectpos.x());
269     item.y(rectpos.y());
270     item.w(r.width());
271     item.h(r.height());
272     item.mix(m_ui.spinOpacity->value());
273     m_geometry->insert(item);
274
275     m_timeline->update();
276     slotPositionChanged(pos, false);
277     emit parameterChanged();
278 }
279
280 void GeometryWidget::slotDeleteKeyframe(int pos)
281 {
282     Mlt::GeometryItem item;
283     if (pos == -1)
284         pos = m_timePos->getValue();
285     // check there is more than one keyframe, do not allow to delete last one
286     if (m_geometry->next_key(&item, pos + 1)) {
287         if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
288             return;
289     }
290     m_geometry->remove(pos);
291
292     m_timeline->update();
293     slotPositionChanged(pos, false);
294     emit parameterChanged();
295 }
296
297 void GeometryWidget::slotPreviousKeyframe()
298 {
299     Mlt::GeometryItem item;
300     // Go to start if no keyframe is found
301     int pos = 0;
302     if(!m_geometry->prev_key(&item, m_timeline->value() - 1))
303         pos = item.frame();
304
305     slotPositionChanged(pos);
306 }
307
308 void GeometryWidget::slotNextKeyframe()
309 {
310     Mlt::GeometryItem item;
311     // Go to end if no keyframe is found
312     int pos = m_timeline->frameLength;
313     if (!m_geometry->next_key(&item, m_timeline->value() + 1))
314         pos = item.frame();
315
316     slotPositionChanged(pos);
317 }
318
319 void GeometryWidget::slotAddDeleteKeyframe()
320 {
321     Mlt::GeometryItem item;
322     if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
323         slotAddKeyframe();
324     else
325         slotDeleteKeyframe();
326 }
327
328
329 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
330 {
331     if (m_showScene) {
332         /*
333             We do only get the position in timeline if this geometry belongs to a transition,
334             therefore we need to ways here.
335         */
336         if (m_isEffect) {
337             emit checkMonitorPosition(renderPos);
338         } else {
339             if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
340                 if (!m_scene->views().at(0)->isVisible())
341                     m_monitor->slotEffectScene(true);
342             } else {
343                 m_monitor->slotEffectScene(false);
344             }
345         }
346     }
347 }
348
349
350 void GeometryWidget::slotUpdateGeometry()
351 {
352     Mlt::GeometryItem item;
353     int pos = m_timePos->getValue();
354     // get keyframe and make sure it is the correct one
355     if (m_geometry->next_key(&item, pos) || item.frame() != pos)
356         return;
357
358     QRectF rectSize = m_rect->rect().normalized();
359     QPointF rectPos = m_rect->pos();
360     item.x(rectPos.x());
361     item.y(rectPos.y());
362     item.w(rectSize.width());
363     item.h(rectSize.height());
364     m_geometry->insert(item);
365     emit parameterChanged();
366 }
367
368 void GeometryWidget::slotUpdateProperties()
369 {
370     QRectF rectSize = m_rect->rect().normalized();
371     QPointF rectPos = m_rect->pos();
372     int size;
373     if (rectSize.width() / m_monitor->render->dar() < rectSize.height())
374         size = (int)(rectSize.width() * 100 / m_monitor->render->renderWidth());
375     else
376         size = (int)(rectSize.height() * 100 / m_monitor->render->renderHeight());
377
378     m_ui.spinX->blockSignals(true);
379     m_ui.spinY->blockSignals(true);
380     m_ui.spinWidth->blockSignals(true);
381     m_ui.spinHeight->blockSignals(true);
382     m_ui.spinSize->blockSignals(true);
383
384     m_ui.spinX->setValue(rectPos.x());
385     m_ui.spinY->setValue(rectPos.y());
386     m_ui.spinWidth->setValue(rectSize.width());
387     m_ui.spinHeight->setValue(rectSize.height());
388     m_ui.spinSize->setValue(size);
389
390     m_ui.spinX->blockSignals(false);
391     m_ui.spinY->blockSignals(false);
392     m_ui.spinWidth->blockSignals(false);
393     m_ui.spinHeight->blockSignals(false);
394     m_ui.spinSize->blockSignals(false);
395 }
396
397
398 void GeometryWidget::slotSetX(int value)
399 {
400     m_rect->setPos(value, m_ui.spinY->value());
401     slotUpdateGeometry();
402 }
403
404 void GeometryWidget::slotSetY(int value)
405 {
406     m_rect->setPos(m_ui.spinX->value(), value);
407     slotUpdateGeometry();
408 }
409
410 void GeometryWidget::slotSetWidth(int value)
411 {
412     m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
413     slotUpdateGeometry();
414 }
415
416 void GeometryWidget::slotSetHeight(int value)
417 {
418     m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
419     slotUpdateGeometry();
420 }
421
422
423 void GeometryWidget::slotResize(int value)
424 {
425     m_rect->setRect(0, 0, m_monitor->render->renderWidth() * value / 100, m_monitor->render->renderHeight() * value / 100);
426     slotUpdateGeometry();
427 }
428
429
430 void GeometryWidget::slotSetOpacity(int value)
431 {
432     m_ui.sliderOpacity->blockSignals(true);
433     m_ui.sliderOpacity->setValue(value);
434     m_ui.sliderOpacity->blockSignals(false);
435
436     int pos = m_timePos->getValue();
437     Mlt::GeometryItem item;
438     if (m_geometry->fetch(&item, pos) || item.key() == false)
439         return;
440     item.mix(value);
441     m_geometry->insert(item);
442
443     emit parameterChanged();
444 }
445
446
447 void GeometryWidget::slotMoveLeft()
448 {
449     m_rect->setPos(0, m_rect->pos().y());
450     slotUpdateGeometry();
451 }
452
453 void GeometryWidget::slotCenterH()
454 {
455     m_rect->setPos((m_monitor->render->renderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
456     slotUpdateGeometry();
457 }
458
459 void GeometryWidget::slotMoveRight()
460 {
461     m_rect->setPos(m_monitor->render->renderWidth() - m_rect->rect().width(), m_rect->pos().y());
462     slotUpdateGeometry();
463 }
464
465 void GeometryWidget::slotMoveTop()
466 {
467     m_rect->setPos(m_rect->pos().x(), 0);
468     slotUpdateGeometry();
469 }
470
471 void GeometryWidget::slotCenterV()
472 {
473     m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
474     slotUpdateGeometry();
475 }
476
477 void GeometryWidget::slotMoveBottom()
478 {
479     m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
480     slotUpdateGeometry();
481 }
482
483
484 void GeometryWidget::slotSetSynchronize(bool sync)
485 {
486     KdenliveSettings::setTransitionfollowcursor(sync);
487 }
488
489 void GeometryWidget::slotShowScene(bool show)
490 {
491     m_showScene = show;
492     if (!m_showScene)
493         m_monitor->slotEffectScene(false);
494     else
495         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
496 }
497
498 #include "geometrywidget.moc"