]> git.sesse.net Git - kdenlive/blob - src/cornerswidget.cpp
fix regression: corners on-monitor item not updated when changing values in keyframe...
[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         m_pos(0)
38 {
39     m_scene = monitor->getEffectScene();
40
41     m_item = new OnMonitorCornersItem(m_scene);
42     m_scene->addItem(m_item);
43
44     m_config = new MonitorSceneControlWidget(m_scene, this);
45     QGridLayout *l = static_cast<QGridLayout *>(layout());
46     l->addWidget(m_config->getShowHideButton(), 1, 1);
47     l->addWidget(m_config, 1, 2);
48
49     QToolButton *buttonShowLines = new QToolButton(m_config);
50     // TODO: Better Icons
51     buttonShowLines->setIcon(KIcon("insert-horizontal-rule"));
52     buttonShowLines->setToolTip(i18n("Show/Hide the lines connecting the corners"));
53     buttonShowLines->setCheckable(true);
54     buttonShowLines->setChecked(KdenliveSettings::onmonitoreffects_cornersshowlines());
55     connect(buttonShowLines, SIGNAL(toggled(bool)), this, SLOT(slotShowLines(bool)));
56     m_config->addWidget(buttonShowLines, 0, 2);
57     QToolButton *buttonShowControls = new QToolButton(m_config);
58     buttonShowControls->setIcon(KIcon("transform-move"));
59     buttonShowControls->setToolTip(i18n("Show additional controls"));
60     buttonShowControls->setCheckable(true);
61     buttonShowControls->setChecked(KdenliveSettings::onmonitoreffects_cornersshowcontrols());
62     connect(buttonShowControls, SIGNAL(toggled(bool)), this, SLOT(slotShowControls(bool)));
63     m_config->addWidget(buttonShowControls, 0, 3);
64
65     connect(m_config, SIGNAL(showScene(bool)), this, SLOT(slotShowScene(bool)));
66     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
67     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateProperties()));
68
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::addParameter(QDomElement e, int activeKeyframe)
82 {
83     KeyframeEdit::addParameter(e, activeKeyframe);
84
85     if (!m_item->polygon().count())
86         slotUpdateItem();
87 }
88
89 void CornersWidget::slotUpdateItem()
90 {
91     if (keyframe_list->columnCount() < 8)
92         return;
93
94     QTableWidgetItem *keyframe, *keyframeOld;
95     keyframe = keyframe_list->item(0, 0);
96     for (int row = 0; row < keyframe_list->rowCount(); ++row) {
97         keyframeOld = keyframe;
98         keyframe = keyframe_list->item(row, 0);
99         if (getPos(row) >= m_pos)
100             break;
101     }
102
103     QList<QPointF> points, pointsPrev, pointsNext;
104     pointsPrev = getPoints(keyframeOld);
105     pointsNext = getPoints(keyframe);
106     if (pointsPrev.count() != 4 || pointsNext.count() != 4)
107         return;
108
109     qreal position = (m_pos - getPos(keyframeOld->row())) / (qreal)( getPos(keyframe->row()) - getPos(keyframeOld->row()) + 1 );
110
111     if (keyframeOld  == keyframe) {
112         points = pointsNext;
113     } else {
114         for (int i = 0; i < 4; ++i)
115             points.append(QLineF(pointsPrev.at(i), pointsNext.at(i)).pointAt(position));
116     }
117
118     m_scene->blockSignals(true);
119     m_item->setPolygon(QPolygonF() << points.at(0) << points.at(1) << points.at(2) << points.at(3));
120     m_scene->blockSignals(false);
121
122     m_item->setEnabled(getPos(keyframe->row()) == m_pos || keyframe_list->rowCount() == 1);
123 }
124
125 void CornersWidget::slotUpdateProperties()
126 {
127     if (keyframe_list->columnCount() < 8)
128         return;
129
130     QPolygonF pol = m_item->polygon();
131
132     QTableWidgetItem *item = keyframe_list->currentItem();
133     double val;
134     for (int col = 0; col < 8; col++) {
135         if (col % 2 == 0)
136             val = pol.at(col / 2).x() / (double)m_monitor->render->frameRenderWidth();
137         else
138             val = pol.at(col / 2).y() / (double)m_monitor->render->renderHeight();
139         val *= 2000;
140         val += 2000;
141         QTableWidgetItem *nitem = keyframe_list->item(item->row(), col);
142         if (nitem->text().toInt() != (int)val)
143             nitem->setText(QString::number((int)val));
144     }
145
146     slotAdjustKeyframeInfo(false);
147 }
148
149 QList<QPointF> CornersWidget::getPoints(QTableWidgetItem* keyframe)
150 {
151     QList<QPointF> points;
152
153     if (!keyframe)
154         return points;
155
156     double val;
157     for (int col = 0; col < 8; col++) {
158         if (!keyframe_list->item(keyframe->row(), col))
159             return QList<QPointF>();
160         val = (keyframe_list->item(keyframe->row(), col)->text().toInt() - 2000) / 2000.;
161         if (col % 2 == 0)
162             points << QPointF(val * m_monitor->render->frameRenderWidth(), 0);
163         else
164             points[col / 2].setY(val * m_monitor->render->renderHeight());
165     }
166     return points;
167 }
168
169 void CornersWidget::slotCheckMonitorPosition(int renderPos)
170 {
171     if (m_showScene)
172         emit checkMonitorPosition(renderPos);
173 }
174
175 void CornersWidget::slotShowScene(bool show)
176 {
177     m_showScene = show;
178     if (!m_showScene)
179         m_monitor->slotEffectScene(false);
180     else
181         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
182 }
183
184 void CornersWidget::slotShowLines(bool show)
185 {
186     KdenliveSettings::setOnmonitoreffects_cornersshowlines(show);
187     m_item->update();
188 }
189
190 void CornersWidget::slotShowControls(bool show)
191 {
192     KdenliveSettings::setOnmonitoreffects_cornersshowcontrols(show);
193     m_item->update();
194 }
195
196 void CornersWidget::slotSyncPosition(int relTimelinePos)
197 {
198     if (keyframe_list->rowCount()) {
199         relTimelinePos = qBound(0, relTimelinePos, m_max);
200         if (relTimelinePos != m_pos) {
201             m_pos = relTimelinePos;
202             slotUpdateItem();
203         }
204     }
205 }
206
207 #include "cornerswidget.moc"