]> git.sesse.net Git - kdenlive/blob - src/cornerswidget.cpp
Make c0rners keyframable
[kdenlive] / src / cornerswidget.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 #include "cornerswidget.h"
21 #include "monitor.h"
22 #include "monitorscene.h"
23 #include "monitorscenecontrolwidget.h"
24 #include "onmonitoritems/onmonitorcornersitem.h"
25 #include "renderer.h"
26 #include "kdenlivesettings.h"
27
28 #include <QGraphicsView>
29 #include <QGridLayout>
30
31 #include <KIcon>
32
33 CornersWidget::CornersWidget(Monitor *monitor, QDomElement e, int minFrame, int maxFrame, Timecode tc, int activeKeyframe, QWidget* parent) :
34         KeyframeEdit(e, minFrame, maxFrame, tc, activeKeyframe, parent),
35         m_monitor(monitor),
36         m_showScene(true)
37 {
38     m_scene = monitor->getEffectScene();
39
40     m_item = new OnMonitorCornersItem(m_scene);
41     m_scene->addItem(m_item);
42
43     m_config = new MonitorSceneControlWidget(m_scene, this);
44     QGridLayout *l = static_cast<QGridLayout *>(layout());
45     l->addWidget(m_config->getShowHideButton(), 1, 1);
46     l->addWidget(m_config, 1, 2);
47
48     QToolButton *buttonShowLines = new QToolButton(m_config);
49     // TODO: Better Icons
50     buttonShowLines->setIcon(KIcon("insert-horizontal-rule"));
51     buttonShowLines->setToolTip(i18n("Show/Hide the lines connecting the corners"));
52     buttonShowLines->setCheckable(true);
53     buttonShowLines->setChecked(KdenliveSettings::onmonitoreffects_cornersshowlines());
54     connect(buttonShowLines, SIGNAL(toggled(bool)), this, SLOT(slotShowLines(bool)));
55     m_config->addWidget(buttonShowLines, 0, 2);
56     QToolButton *buttonShowControls = new QToolButton(m_config);
57     buttonShowControls->setIcon(KIcon("transform-move"));
58     buttonShowControls->setToolTip(i18n("Show additional controls"));
59     buttonShowControls->setCheckable(true);
60     buttonShowControls->setChecked(KdenliveSettings::onmonitoreffects_cornersshowcontrols());
61     connect(buttonShowControls, SIGNAL(toggled(bool)), this, SLOT(slotShowControls(bool)));
62     m_config->addWidget(buttonShowControls, 0, 3);
63
64     connect(m_config, SIGNAL(showScene(bool)), this, SLOT(slotShowScene(bool)));
65     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
66     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateProperties()));
67
68     connect(keyframe_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotUpdateItem()));
69     connect(keyframe_list, SIGNAL(cellChanged(int, int)), this, SLOT(slotUpdateItem()));
70 }
71
72 CornersWidget::~CornersWidget()
73 {
74     delete m_config;
75     m_scene->removeItem(m_item);
76     delete m_item;
77     if (m_monitor)
78         m_monitor->slotEffectScene(false);
79 }
80
81 void CornersWidget::slotUpdateItem()
82 {
83     QList<QPointF> points;
84
85     QTableWidgetItem *item = keyframe_list->currentItem();
86     if (!item || keyframe_list->columnCount() < 8)
87         return;
88
89     double val;
90     for (int col = 0; col < 8; col++) {
91         if (!keyframe_list->item(item->row(), col))
92             return;
93         val = (keyframe_list->item(item->row(), col)->text().toInt() - 2000) / 2000.;
94         if (col % 2 == 0)
95             points << QPointF(val * m_monitor->render->frameRenderWidth(), 0);
96         else
97             points[col / 2].setY(val * m_monitor->render->renderHeight());
98     }
99
100     m_scene->blockSignals(true);
101     m_item->setPolygon(QPolygonF() << points.at(0) << points.at(1) << points.at(2) << points.at(3));
102     m_scene->blockSignals(false);
103 }
104
105 void CornersWidget::slotUpdateProperties()
106 {
107     if (keyframe_list->columnCount() < 8)
108         return;
109
110     QPolygonF pol = m_item->polygon();
111
112     QTableWidgetItem *item = keyframe_list->currentItem();
113     double val;
114     for (int col = 0; col < 8; col++) {
115         if (col % 2 == 0)
116             val = pol.at(col / 2).x() / (double)m_monitor->render->frameRenderWidth();
117         else
118             val = pol.at(col / 2).y() / (double)m_monitor->render->renderHeight();
119         val *= 2000;
120         val += 2000;
121         QTableWidgetItem *nitem = keyframe_list->item(item->row(), col);
122         if (nitem->text().toInt() != (int)val)
123             nitem->setText(QString::number((int)val));
124     }
125
126     slotAdjustKeyframeInfo(false);
127
128     if (changed)
129         emit parameterChanged();
130 }
131
132 void CornersWidget::slotCheckMonitorPosition(int renderPos)
133 {
134     if (m_showScene)
135         emit checkMonitorPosition(renderPos);
136 }
137
138 void CornersWidget::slotShowScene(bool show)
139 {
140     m_showScene = show;
141     if (!m_showScene)
142         m_monitor->slotEffectScene(false);
143     else
144         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
145 }
146
147 void CornersWidget::slotShowLines(bool show)
148 {
149     KdenliveSettings::setOnmonitoreffects_cornersshowlines(show);
150     m_item->update();
151 }
152
153 void CornersWidget::slotShowControls(bool show)
154 {
155     KdenliveSettings::setOnmonitoreffects_cornersshowcontrols(show);
156     m_item->update();
157 }
158
159 #include "cornerswidget.moc"