1 /***************************************************************************
2 * Copyright (C) 2010 by Till Theato (root@ttill.de) *
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. *
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. *
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 ***************************************************************************/
20 #include "cornerswidget.h"
22 #include "monitorscene.h"
23 #include "monitoreditwidget.h"
24 #include "onmonitoritems/onmonitorcornersitem.h"
26 #include "kdenlivesettings.h"
28 #include <QGraphicsView>
29 #include <QGridLayout>
30 #include <QToolButton>
34 inline int lerp( const int a, const int b, double t )
36 return a + (b - a) * t;
39 CornersWidget::CornersWidget(Monitor *monitor, QDomElement e, int minFrame, int maxFrame, Timecode tc, int activeKeyframe, QWidget* parent) :
40 KeyframeEdit(e, minFrame, maxFrame, tc, activeKeyframe, parent),
45 MonitorEditWidget *edit = monitor->getEffectEdit();
46 edit->showVisibilityButton(true);
47 m_scene = edit->getScene();
49 m_item = new OnMonitorCornersItem(m_scene);
50 m_scene->addItem(m_item);
53 QToolButton *buttonShowControls = new QToolButton();
54 buttonShowControls->setIcon(KIcon("transform-move"));
55 buttonShowControls->setToolTip(i18n("Show additional controls"));
56 buttonShowControls->setCheckable(true);
57 buttonShowControls->setAutoRaise(true);
58 buttonShowControls->setChecked(KdenliveSettings::onmonitoreffects_cornersshowcontrols());
59 connect(buttonShowControls, SIGNAL(toggled(bool)), this, SLOT(slotShowControls(bool)));
60 edit->addCustomControl(buttonShowControls);
62 QToolButton *buttonShowLines = new QToolButton();
63 buttonShowLines->setIcon(KIcon("insert-horizontal-rule"));
64 buttonShowLines->setToolTip(i18n("Show/Hide the lines connecting the corners"));
65 buttonShowLines->setCheckable(true);
66 buttonShowControls->setAutoRaise(true);
67 buttonShowLines->setChecked(KdenliveSettings::onmonitoreffects_cornersshowlines());
68 connect(buttonShowLines, SIGNAL(toggled(bool)), this, SLOT(slotShowLines(bool)));
69 edit->addCustomControl(buttonShowLines);
71 connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
72 connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
73 connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateProperties()));
74 connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotInsertKeyframe()));
76 connect(keyframe_list, SIGNAL(cellChanged(int, int)), this, SLOT(slotUpdateItem()));
79 CornersWidget::~CornersWidget()
81 m_scene->removeItem(m_item);
84 MonitorEditWidget *edit = m_monitor->getEffectEdit();
85 edit->showVisibilityButton(false);
86 edit->removeCustomControls();
87 m_monitor->slotEffectScene(false);
91 void CornersWidget::addParameter(QDomElement e, int activeKeyframe)
93 KeyframeEdit::addParameter(e, activeKeyframe);
95 if (!m_item->polygon().count())
99 void CornersWidget::slotUpdateItem()
101 if (keyframe_list->columnCount() < 8)
104 QTableWidgetItem *keyframe, *keyframeOld;
105 keyframe = keyframeOld = keyframe_list->item(0, 0);
106 for (int row = 0; row < keyframe_list->rowCount(); ++row) {
107 keyframeOld = keyframe;
108 keyframe = keyframe_list->item(row, 0);
109 if (getPos(row) >= m_pos)
113 QList<QPointF> points, pointsPrev, pointsNext;
114 pointsPrev = getPoints(keyframeOld);
115 pointsNext = getPoints(keyframe);
116 if (pointsPrev.count() != 4 || pointsNext.count() != 4)
119 qreal position = (m_pos - getPos(keyframeOld->row())) / (qreal)( getPos(keyframe->row()) - getPos(keyframeOld->row()) + 1 );
121 if (keyframeOld == keyframe) {
124 for (int i = 0; i < 4; ++i)
125 points.append(QLineF(pointsPrev.at(i), pointsNext.at(i)).pointAt(position));
128 m_scene->blockSignals(true);
129 m_item->setPolygon(QPolygonF() << points.at(0) << points.at(1) << points.at(2) << points.at(3));
130 m_scene->blockSignals(false);
132 bool enable = getPos(keyframe->row()) == m_pos || keyframe_list->rowCount() == 1;
133 m_item->setEnabled(enable);
134 m_scene->setEnabled(enable);
137 void CornersWidget::slotUpdateProperties()
139 if (keyframe_list->columnCount() < 8)
142 QPolygonF pol = m_item->polygon();
144 QTableWidgetItem *item = keyframe_list->currentItem();
146 for (int col = 0; col < 8; col++) {
148 val = pol.at(col / 2).x() / (double)m_monitor->render->frameRenderWidth();
150 val = pol.at(col / 2).y() / (double)m_monitor->render->renderHeight();
153 QTableWidgetItem *nitem = keyframe_list->item(item->row(), col);
154 if (nitem->text().toInt() != (int)val)
155 nitem->setText(QString::number((int)val));
158 slotAdjustKeyframeInfo(false);
161 QList<QPointF> CornersWidget::getPoints(QTableWidgetItem* keyframe)
163 QList<QPointF> points;
169 for (int col = 0; col < 8; col++) {
170 if (!keyframe_list->item(keyframe->row(), col))
171 return QList<QPointF>();
172 val = (keyframe_list->item(keyframe->row(), col)->text().toInt() - 2000) / 2000.;
174 points << QPointF(val * m_monitor->render->frameRenderWidth(), 0);
176 points[col / 2].setY(val * m_monitor->render->renderHeight());
181 void CornersWidget::slotCheckMonitorPosition(int renderPos)
184 emit checkMonitorPosition(renderPos);
187 void CornersWidget::slotShowScene(bool show)
191 m_monitor->slotEffectScene(false);
193 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
196 void CornersWidget::slotShowLines(bool show)
198 KdenliveSettings::setOnmonitoreffects_cornersshowlines(show);
202 void CornersWidget::slotShowControls(bool show)
204 KdenliveSettings::setOnmonitoreffects_cornersshowcontrols(show);
208 void CornersWidget::slotSyncPosition(int relTimelinePos)
210 if (keyframe_list->rowCount()) {
211 relTimelinePos = qBound(0, relTimelinePos, m_max);
212 if (relTimelinePos != m_pos) {
213 m_pos = relTimelinePos;
219 void CornersWidget::slotInsertKeyframe()
221 keyframe_list->blockSignals(true);
224 QTableWidgetItem *keyframe, *keyframeOld;
225 keyframe = keyframeOld = keyframe_list->item(0, 0);
226 for (row = 0; row < keyframe_list->rowCount(); ++row) {
227 keyframeOld = keyframe;
228 keyframe = keyframe_list->item(row, 0);
229 if (getPos(row) >= m_pos)
235 if (row == keyframe_list->rowCount()) {
245 pos1 = getPos(row - 1);
247 int col = keyframe_list->currentColumn();
248 double pos = (m_pos - pos1) / (double)(pos2 - pos1 + 1);
250 keyframe_list->insertRow(row);
251 keyframe_list->setVerticalHeaderItem(row, new QTableWidgetItem(getPosString(m_pos)));
253 QPolygonF pol = m_item->polygon();
255 for (int i = 0; i < keyframe_list->columnCount(); i++) {
258 val = pol.at(i / 2).x() / (double)m_monitor->render->frameRenderWidth();
260 val = pol.at(i / 2).y() / (double)m_monitor->render->renderHeight();
263 keyframe_list->setItem(row, i, new QTableWidgetItem(QString::number((int)val)));
265 keyframe_list->setItem(row, i, new QTableWidgetItem(QString::number(lerp(keyframe_list->item(keyframeOld->row(), i)->text().toInt(), keyframe_list->item(keyframe->row(), i)->text().toInt(), pos))));
269 keyframe_list->resizeRowsToContents();
270 slotAdjustKeyframeInfo();
271 keyframe_list->blockSignals(false);
273 button_delete->setEnabled(true);
274 keyframe_list->setCurrentCell(row, col);
275 keyframe_list->selectRow(row);
278 #include "cornerswidget.moc"