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 "monitorscenecontrolwidget.h"
24 #include "onmonitoritems/onmonitorcornersitem.h"
26 #include "kdenlivesettings.h"
28 #include <QGraphicsView>
29 #include <QGridLayout>
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),
39 m_scene = monitor->getEffectScene();
41 m_item = new OnMonitorCornersItem(m_scene);
42 m_scene->addItem(m_item);
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);
49 QToolButton *buttonShowLines = new QToolButton(m_config);
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);
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()));
69 connect(keyframe_list, SIGNAL(cellChanged(int, int)), this, SLOT(slotUpdateItem()));
72 CornersWidget::~CornersWidget()
75 m_scene->removeItem(m_item);
78 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 = 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 m_item->setEnabled(getPos(keyframe->row()) == m_pos || keyframe_list->rowCount() == 1);
125 void CornersWidget::slotUpdateProperties()
127 if (keyframe_list->columnCount() < 8)
130 QPolygonF pol = m_item->polygon();
132 QTableWidgetItem *item = keyframe_list->currentItem();
134 for (int col = 0; col < 8; col++) {
136 val = pol.at(col / 2).x() / (double)m_monitor->render->frameRenderWidth();
138 val = pol.at(col / 2).y() / (double)m_monitor->render->renderHeight();
141 QTableWidgetItem *nitem = keyframe_list->item(item->row(), col);
142 if (nitem->text().toInt() != (int)val)
143 nitem->setText(QString::number((int)val));
146 slotAdjustKeyframeInfo(false);
149 QList<QPointF> CornersWidget::getPoints(QTableWidgetItem* keyframe)
151 QList<QPointF> points;
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.;
162 points << QPointF(val * m_monitor->render->frameRenderWidth(), 0);
164 points[col / 2].setY(val * m_monitor->render->renderHeight());
169 void CornersWidget::slotCheckMonitorPosition(int renderPos)
172 emit checkMonitorPosition(renderPos);
175 void CornersWidget::slotShowScene(bool show)
179 m_monitor->slotEffectScene(false);
181 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
184 void CornersWidget::slotShowLines(bool show)
186 KdenliveSettings::setOnmonitoreffects_cornersshowlines(show);
190 void CornersWidget::slotShowControls(bool show)
192 KdenliveSettings::setOnmonitoreffects_cornersshowcontrols(show);
196 void CornersWidget::slotSyncPosition(int relTimelinePos)
198 if (keyframe_list->rowCount()) {
199 relTimelinePos = qBound(0, relTimelinePos, m_max);
200 if (relTimelinePos != m_pos) {
201 m_pos = relTimelinePos;
207 #include "cornerswidget.moc"