]> git.sesse.net Git - kdenlive/blob - src/geometrywidget.cpp
Add Opacity controls 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 {
46     m_ui.setupUi(this);
47
48
49     /*
50         Setup of timeline and keyframe controls
51     */
52
53     ((QGridLayout *)(m_ui.widgetTimeWrapper->layout()))->addWidget(m_timePos, 1, 4);
54
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);
59
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"));
66
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()));
75
76
77     /*
78         Setup of geometry controls
79     */
80
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"));
93
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)));
98
99     connect(m_ui.spinSize,         SIGNAL(valueChanged(int)), this, SLOT(slotResize(int)));
100
101     connect(m_ui.spinOpacity,      SIGNAL(valueChanged(int)), this, SLOT(slotSetOpacity(int)));
102     connect(m_ui.sliderOpacity,    SIGNAL(valueChanged(int)), m_ui.spinOpacity, SLOT(setValue(int)));
103
104     connect(m_ui.buttonMoveLeft,   SIGNAL(clicked()), this, SLOT(slotMoveLeft()));
105     connect(m_ui.buttonCenterH,    SIGNAL(clicked()), this, SLOT(slotCenterH()));
106     connect(m_ui.buttonMoveRight,  SIGNAL(clicked()), this, SLOT(slotMoveRight()));
107     connect(m_ui.buttonMoveTop,    SIGNAL(clicked()), this, SLOT(slotMoveTop()));
108     connect(m_ui.buttonCenterV,    SIGNAL(clicked()), this, SLOT(slotCenterV()));
109     connect(m_ui.buttonMoveBottom, SIGNAL(clicked()), this, SLOT(slotMoveBottom()));
110
111
112     m_scene = monitor->getEffectScene();
113     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateGeometry()));
114     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
115     connect(this, SIGNAL(parameterChanged()), this, SLOT(slotUpdateProperties()));
116 }
117
118 GeometryWidget::~GeometryWidget()
119 {
120     delete m_timePos;
121     delete m_timeline;
122     m_scene->removeItem(m_rect);
123     delete m_geometry;
124     m_monitor->slotEffectScene(false);
125 }
126
127 void GeometryWidget::updateTimecodeFormat()
128 {
129     m_timePos->slotUpdateTimeCodeFormat();
130 }
131
132 QString GeometryWidget::getValue() const
133 {
134     return m_geometry->serialise();
135 }
136
137 void GeometryWidget::setupParam(const QDomElement elem, int minframe, int maxframe)
138 {
139     m_inPoint = minframe;
140     m_outPoint = maxframe;
141
142     char *tmp = (char *) qstrdup(elem.attribute("value").toUtf8().data());
143     if (m_geometry)
144         m_geometry->parse(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
145     else
146         m_geometry = new Mlt::Geometry(tmp, maxframe - minframe, m_monitor->render->renderWidth(), m_monitor->render->renderHeight());
147     delete[] tmp;
148
149     if (elem.attribute("fixed") == "1") {
150         // Keyframes are disabled
151         m_ui.widgetTimeWrapper->setHidden(true);
152     } else {
153         m_ui.widgetTimeWrapper->setHidden(false);
154         m_timeline->setKeyGeometry(m_geometry, m_outPoint - m_inPoint - 1);
155         m_timeline->update();
156         m_timePos->setRange(0, m_outPoint - m_inPoint - 1);
157     }
158
159     // no opacity
160     if (elem.attribute("opacity") == "false")
161         m_ui.widgetOpacity->setHidden(true);
162
163     Mlt::GeometryItem item;
164
165     m_geometry->fetch(&item, 0);
166     delete m_rect;
167     m_rect = new QGraphicsRectItem(QRectF(0, 0, item.w(), item.h()));
168     m_rect->setPos(item.x(), item.y());
169     m_rect->setZValue(0);
170     m_rect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
171
172     QPen framepen(Qt::SolidLine);
173     framepen.setColor(Qt::yellow);
174     m_rect->setPen(framepen);
175     m_rect->setBrush(Qt::transparent);
176     m_scene->addItem(m_rect);
177
178     slotPositionChanged(0, false);
179     slotUpdateProperties();
180     slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
181 }
182
183 void GeometryWidget::slotSyncPosition(int relTimelinePos)
184 {
185     if (m_timePos->maximum() > 0 && KdenliveSettings::transitionfollowcursor()) {
186         relTimelinePos = qMax(0, relTimelinePos);
187         relTimelinePos = qMin(relTimelinePos, m_timePos->maximum());
188         if (relTimelinePos != m_timePos->getValue())
189             slotPositionChanged(relTimelinePos, false);
190     }
191 }
192
193
194 void GeometryWidget::slotPositionChanged(int pos, bool seek)
195 {
196     if (pos == -1)
197         pos = m_timePos->getValue();
198
199     m_timePos->setValue(pos);
200     m_timeline->blockSignals(true);
201     m_timeline->setValue(pos);
202     m_timeline->blockSignals(false);
203
204     Mlt::GeometryItem item;
205     if (m_geometry->fetch(&item, pos) || item.key() == false) {
206         // no keyframe
207         m_scene->setEnabled(false);
208         m_ui.widgetGeometry->setEnabled(false);
209         m_ui.buttonAddDelete->setIcon(KIcon("document-new"));
210         m_ui.buttonAddDelete->setToolTip(i18n("Add keyframe"));
211     } else {
212         // keyframe
213         m_scene->setEnabled(true);
214         m_ui.widgetGeometry->setEnabled(true);
215         m_ui.buttonAddDelete->setIcon(KIcon("edit-delete"));
216         m_ui.buttonAddDelete->setToolTip(i18n("Delete keyframe"));
217     }
218
219     m_rect->setPos(item.x(), item.y());
220     m_rect->setRect(0, 0, item.w(), item.h());
221
222     m_ui.spinOpacity->blockSignals(true);
223     m_ui.sliderOpacity->blockSignals(true);
224     m_ui.spinOpacity->setValue(item.mix());
225     m_ui.sliderOpacity->setValue(item.mix());
226     m_ui.spinOpacity->blockSignals(false);
227     m_ui.sliderOpacity->blockSignals(false);
228
229     slotUpdateProperties();
230
231     if (seek && KdenliveSettings::transitionfollowcursor())
232         emit seekToPos(m_clipPos + pos);
233 }
234
235 void GeometryWidget::slotKeyframeMoved(int pos)
236 {
237     slotPositionChanged(pos);
238     slotUpdateGeometry();
239 }
240
241 void GeometryWidget::slotAddKeyframe(int pos)
242 {
243     Mlt::GeometryItem item;
244     if (pos == -1)
245         pos = m_timePos->getValue();
246     item.frame(pos);
247     QRectF r = m_rect->rect().normalized();
248     QPointF rectpos = m_rect->pos();
249     item.x(rectpos.x());
250     item.y(rectpos.y());
251     item.w(r.width());
252     item.h(r.height());
253     item.mix(m_ui.spinOpacity->value());
254     m_geometry->insert(item);
255
256     m_timeline->update();
257     slotPositionChanged(pos, false);
258     emit parameterChanged();
259 }
260
261 void GeometryWidget::slotDeleteKeyframe(int pos)
262 {
263     Mlt::GeometryItem item;
264     if (pos == -1)
265         pos = m_timePos->getValue();
266     // check there is more than one keyframe, do not allow to delete last one
267     if (m_geometry->next_key(&item, pos + 1)) {
268         if (m_geometry->prev_key(&item, pos - 1) || item.frame() == pos)
269             return;
270     }
271     m_geometry->remove(pos);
272
273     m_timeline->update();
274     slotPositionChanged(pos, false);
275     emit parameterChanged();
276 }
277
278 void GeometryWidget::slotPreviousKeyframe()
279 {
280     Mlt::GeometryItem item;
281     // Go to start if no keyframe is found
282     int pos = 0;
283     if(!m_geometry->prev_key(&item, m_timeline->value() - 1))
284         pos = item.frame();
285
286     slotPositionChanged(pos);
287 }
288
289 void GeometryWidget::slotNextKeyframe()
290 {
291     Mlt::GeometryItem item;
292     // Go to end if no keyframe is found
293     int pos = m_timeline->frameLength;
294     if (!m_geometry->next_key(&item, m_timeline->value() + 1))
295         pos = item.frame();
296
297     slotPositionChanged(pos);
298 }
299
300 void GeometryWidget::slotAddDeleteKeyframe()
301 {
302     Mlt::GeometryItem item;
303     if (m_geometry->fetch(&item, m_timePos->getValue()) || item.key() == false)
304         slotAddKeyframe();
305     else
306         slotDeleteKeyframe();
307 }
308
309
310 void GeometryWidget::slotCheckMonitorPosition(int renderPos)
311 {
312     /*
313         We do only get the position in timeline if this geometry belongs to a transition,
314         therefore we need to ways here.
315     */
316     if (m_isEffect) {
317         emit checkMonitorPosition(renderPos);
318     } else {
319         if (renderPos >= m_clipPos && renderPos <= m_clipPos + m_outPoint - m_inPoint) {
320             if (!m_scene->views().at(0)->isVisible())
321                 m_monitor->slotEffectScene(true);
322         } else {
323             m_monitor->slotEffectScene(false);
324         }
325     }
326 }
327
328
329 void GeometryWidget::slotUpdateGeometry()
330 {
331     Mlt::GeometryItem item;
332     int pos = m_timePos->getValue();
333     // get keyframe and make sure it is the correct one
334     if (m_geometry->next_key(&item, pos) || item.frame() != pos)
335         return;
336
337     QRectF rectSize = m_rect->rect().normalized();
338     QPointF rectPos = m_rect->pos();
339     item.x(rectPos.x());
340     item.y(rectPos.y());
341     item.w(rectSize.width());
342     item.h(rectSize.height());
343     m_geometry->insert(item);
344     emit parameterChanged();
345 }
346
347 void GeometryWidget::slotUpdateProperties()
348 {
349     QRectF rectSize = m_rect->rect().normalized();
350     QPointF rectPos = m_rect->pos();
351     int size;
352     if (rectSize.width() / m_monitor->render->dar() < rectSize.height())
353         size = (int)(rectSize.width() * 100 / m_monitor->render->renderWidth());
354     else
355         size = (int)(rectSize.height() * 100 / m_monitor->render->renderHeight());
356
357     m_ui.spinX->blockSignals(true);
358     m_ui.spinY->blockSignals(true);
359     m_ui.spinWidth->blockSignals(true);
360     m_ui.spinHeight->blockSignals(true);
361     m_ui.spinSize->blockSignals(true);
362
363     m_ui.spinX->setValue(rectPos.x());
364     m_ui.spinY->setValue(rectPos.y());
365     m_ui.spinWidth->setValue(rectSize.width());
366     m_ui.spinHeight->setValue(rectSize.height());
367     m_ui.spinSize->setValue(size);
368
369     m_ui.spinX->blockSignals(false);
370     m_ui.spinY->blockSignals(false);
371     m_ui.spinWidth->blockSignals(false);
372     m_ui.spinHeight->blockSignals(false);
373     m_ui.spinSize->blockSignals(false);
374 }
375
376
377 void GeometryWidget::slotSetX(int value)
378 {
379     m_rect->setPos(value, m_ui.spinY->value());
380     slotUpdateGeometry();
381 }
382
383 void GeometryWidget::slotSetY(int value)
384 {
385     m_rect->setPos(m_ui.spinX->value(), value);
386     slotUpdateGeometry();
387 }
388
389 void GeometryWidget::slotSetWidth(int value)
390 {
391     m_rect->setRect(0, 0, value, m_ui.spinHeight->value());
392     slotUpdateGeometry();
393 }
394
395 void GeometryWidget::slotSetHeight(int value)
396 {
397     m_rect->setRect(0, 0, m_ui.spinWidth->value(), value);
398     slotUpdateGeometry();
399 }
400
401
402 void GeometryWidget::slotResize(int value)
403 {
404     m_rect->setRect(0, 0, m_monitor->render->renderWidth() * value / 100, m_monitor->render->renderHeight() * value / 100);
405     slotUpdateGeometry();
406 }
407
408
409 void GeometryWidget::slotSetOpacity(int value)
410 {
411     m_ui.sliderOpacity->blockSignals(true);
412     m_ui.sliderOpacity->setValue(value);
413     m_ui.sliderOpacity->blockSignals(false);
414
415     int pos = m_timePos->getValue();
416     Mlt::GeometryItem item;
417     if (m_geometry->fetch(&item, pos) || item.key() == false)
418         return;
419     item.mix(value);
420     m_geometry->insert(item);
421
422     emit parameterChanged();
423 }
424
425
426 void GeometryWidget::slotMoveLeft()
427 {
428     m_rect->setPos(0, m_rect->pos().y());
429     slotUpdateGeometry();
430 }
431
432 void GeometryWidget::slotCenterH()
433 {
434     m_rect->setPos((m_monitor->render->renderWidth() - m_rect->rect().width()) / 2, m_rect->pos().y());
435     slotUpdateGeometry();
436 }
437
438 void GeometryWidget::slotMoveRight()
439 {
440     m_rect->setPos(m_monitor->render->renderWidth() - m_rect->rect().width(), m_rect->pos().y());
441     slotUpdateGeometry();
442 }
443
444 void GeometryWidget::slotMoveTop()
445 {
446     m_rect->setPos(m_rect->pos().x(), 0);
447     slotUpdateGeometry();
448 }
449
450 void GeometryWidget::slotCenterV()
451 {
452     m_rect->setPos(m_rect->pos().x(), (m_monitor->render->renderHeight() - m_rect->rect().height()) / 2);
453     slotUpdateGeometry();
454 }
455
456 void GeometryWidget::slotMoveBottom()
457 {
458     m_rect->setPos(m_rect->pos().x(), m_monitor->render->renderHeight() - m_rect->rect().height());
459     slotUpdateGeometry();
460 }
461
462 #include "geometrywidget.moc"