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();
50 m_item = new OnMonitorCornersItem();
51 m_scene->addItem(m_item);
54 edit->removeCustomControls();
55 edit->addCustomButton(KIcon("transform-move"), i18n("Show additional controls"), this, SLOT(slotShowControls(bool)),
56 true, KdenliveSettings::onmonitoreffects_cornersshowcontrols());
57 edit->addCustomButton(KIcon("insert-horizontal-rule"), i18n("Show/Hide the lines connecting the corners"), this, SLOT(slotShowLines(bool)),
58 true, KdenliveSettings::onmonitoreffects_cornersshowlines());
60 connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
61 connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
62 connect(m_item, SIGNAL(changed()), this, SLOT(slotUpdateProperties()));
63 connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotInsertKeyframe()));
65 connect(keyframe_list, SIGNAL(cellChanged(int, int)), this, SLOT(slotUpdateItem()));
66 m_scene->centerView();
69 CornersWidget::~CornersWidget()
71 m_scene->removeItem(m_item);
74 MonitorEditWidget *edit = m_monitor->getEffectEdit();
75 edit->showVisibilityButton(false);
76 edit->removeCustomControls();
77 m_monitor->slotEffectScene(false);
81 void CornersWidget::addParameter(QDomElement e, int activeKeyframe)
83 KeyframeEdit::addParameter(e, activeKeyframe);
85 if (!m_item->polygon().count())
89 void CornersWidget::slotUpdateItem()
91 if (keyframe_list->columnCount() < 8)
94 QTableWidgetItem *keyframe, *keyframeOld;
95 keyframe = keyframeOld = 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)
103 QList<QPointF> points, pointsPrev, pointsNext;
104 pointsPrev = getPoints(keyframeOld);
105 pointsNext = getPoints(keyframe);
106 if (pointsPrev.count() != 4 || pointsNext.count() != 4)
109 qreal position = (m_pos - getPos(keyframeOld->row())) / (qreal)( getPos(keyframe->row()) - getPos(keyframeOld->row()) + 1 );
111 if (keyframeOld == keyframe) {
114 for (int i = 0; i < 4; ++i)
115 points.append(QLineF(pointsPrev.at(i), pointsNext.at(i)).pointAt(position));
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);
122 bool enable = getPos(keyframe->row()) == m_pos || keyframe_list->rowCount() == 1;
123 m_item->setEnabled(enable);
124 m_scene->setEnabled(enable);
127 void CornersWidget::slotUpdateProperties()
129 if (keyframe_list->columnCount() < 8)
132 QPolygonF pol = m_item->polygon();
134 QTableWidgetItem *item = keyframe_list->currentItem();
136 for (int col = 0; col < 8; col++) {
138 val = pol.at(col / 2).x() / (double)m_monitor->render->frameRenderWidth();
140 val = pol.at(col / 2).y() / (double)m_monitor->render->renderHeight();
143 QTableWidgetItem *nitem = keyframe_list->item(item->row(), col);
144 if (nitem->text().toInt() != (int)val)
145 nitem->setText(QString::number((int)val));
148 slotAdjustKeyframeInfo(false);
151 QList<QPointF> CornersWidget::getPoints(QTableWidgetItem* keyframe)
153 QList<QPointF> points;
159 for (int col = 0; col < 8; col++) {
160 if (!keyframe_list->item(keyframe->row(), col))
161 return QList<QPointF>();
162 val = (keyframe_list->item(keyframe->row(), col)->text().toInt() - 2000) / 2000.;
164 points << QPointF(val * m_monitor->render->frameRenderWidth(), 0);
166 points[col / 2].setY(val * m_monitor->render->renderHeight());
171 void CornersWidget::slotCheckMonitorPosition(int renderPos)
174 emit checkMonitorPosition(renderPos);
177 void CornersWidget::slotShowScene(bool show)
181 m_monitor->slotEffectScene(false);
183 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
186 void CornersWidget::slotShowLines(bool show)
188 KdenliveSettings::setOnmonitoreffects_cornersshowlines(show);
192 void CornersWidget::slotShowControls(bool show)
194 KdenliveSettings::setOnmonitoreffects_cornersshowcontrols(show);
198 void CornersWidget::slotSyncPosition(int relTimelinePos)
200 if (keyframe_list->rowCount()) {
201 relTimelinePos = qBound(0, relTimelinePos, m_max);
202 if (relTimelinePos != m_pos - m_min) {
203 m_pos = relTimelinePos + m_min;
209 void CornersWidget::slotInsertKeyframe()
211 keyframe_list->blockSignals(true);
214 QTableWidgetItem *keyframe, *keyframeOld;
215 keyframe = keyframeOld = keyframe_list->item(0, 0);
216 for (row = 0; row < keyframe_list->rowCount(); ++row) {
217 keyframeOld = keyframe;
218 keyframe = keyframe_list->item(row, 0);
219 if (getPos(row) >= m_pos)
225 if (row == keyframe_list->rowCount()) {
235 pos1 = getPos(row - 1);
237 int col = keyframe_list->currentColumn();
238 double pos = (m_pos - pos1) / (double)(pos2 - pos1 + 1);
240 keyframe_list->insertRow(row);
241 keyframe_list->setVerticalHeaderItem(row, new QTableWidgetItem(getPosString(m_pos)));
243 QPolygonF pol = m_item->polygon();
245 for (int i = 0; i < keyframe_list->columnCount(); i++) {
248 val = pol.at(i / 2).x() / (double)m_monitor->render->frameRenderWidth();
250 val = pol.at(i / 2).y() / (double)m_monitor->render->renderHeight();
253 keyframe_list->setItem(row, i, new QTableWidgetItem(QString::number((int)val)));
255 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))));
259 keyframe_list->resizeRowsToContents();
260 slotAdjustKeyframeInfo();
261 keyframe_list->blockSignals(false);
263 button_delete->setEnabled(true);
264 keyframe_list->setCurrentCell(row, col);
265 keyframe_list->selectRow(row);
268 #include "cornerswidget.moc"