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()));
70 CornersWidget::~CornersWidget()
73 m_scene->removeItem(m_item);
76 m_monitor->slotEffectScene(false);
79 void CornersWidget::addParameter(QDomElement e, int activeKeyframe)
81 KeyframeEdit::addParameter(e, activeKeyframe);
83 if (!m_item->polygon().count())
87 void CornersWidget::slotUpdateItem()
89 if (keyframe_list->columnCount() < 8)
92 QTableWidgetItem *keyframe, *keyframeOld;
93 keyframe = keyframe_list->item(0, 0);
94 for (int row = 0; row < keyframe_list->rowCount(); ++row) {
95 keyframeOld = keyframe;
96 keyframe = keyframe_list->item(row, 0);
97 if (getPos(row) >= m_pos)
101 QList<QPointF> points, pointsPrev, pointsNext;
102 pointsPrev = getPoints(keyframeOld);
103 pointsNext = getPoints(keyframe);
104 if (pointsPrev.count() != 4 || pointsNext.count() != 4)
107 qreal position = (m_pos - getPos(keyframeOld->row())) / (qreal)( getPos(keyframe->row()) - getPos(keyframeOld->row()) + 1 );
109 if (keyframeOld == keyframe) {
112 for (int i = 0; i < 4; ++i)
113 points.append(QLineF(pointsPrev.at(i), pointsNext.at(i)).pointAt(position));
116 m_scene->blockSignals(true);
117 m_item->setPolygon(QPolygonF() << points.at(0) << points.at(1) << points.at(2) << points.at(3));
118 m_scene->blockSignals(false);
120 m_item->setEnabled(getPos(keyframe->row()) == m_pos || keyframe_list->rowCount() == 1);
123 void CornersWidget::slotUpdateProperties()
125 if (keyframe_list->columnCount() < 8)
128 QPolygonF pol = m_item->polygon();
130 QTableWidgetItem *item = keyframe_list->currentItem();
132 for (int col = 0; col < 8; col++) {
134 val = pol.at(col / 2).x() / (double)m_monitor->render->frameRenderWidth();
136 val = pol.at(col / 2).y() / (double)m_monitor->render->renderHeight();
139 QTableWidgetItem *nitem = keyframe_list->item(item->row(), col);
140 if (nitem->text().toInt() != (int)val)
141 nitem->setText(QString::number((int)val));
144 slotAdjustKeyframeInfo(false);
147 QList<QPointF> CornersWidget::getPoints(QTableWidgetItem* keyframe)
149 QList<QPointF> points;
155 for (int col = 0; col < 8; col++) {
156 if (!keyframe_list->item(keyframe->row(), col))
157 return QList<QPointF>();
158 val = (keyframe_list->item(keyframe->row(), col)->text().toInt() - 2000) / 2000.;
160 points << QPointF(val * m_monitor->render->frameRenderWidth(), 0);
162 points[col / 2].setY(val * m_monitor->render->renderHeight());
167 void CornersWidget::slotCheckMonitorPosition(int renderPos)
170 emit checkMonitorPosition(renderPos);
173 void CornersWidget::slotShowScene(bool show)
177 m_monitor->slotEffectScene(false);
179 slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
182 void CornersWidget::slotShowLines(bool show)
184 KdenliveSettings::setOnmonitoreffects_cornersshowlines(show);
188 void CornersWidget::slotShowControls(bool show)
190 KdenliveSettings::setOnmonitoreffects_cornersshowcontrols(show);
194 void CornersWidget::slotSyncPosition(int relTimelinePos)
196 if (keyframe_list->rowCount()) {
197 relTimelinePos = qBound(0, relTimelinePos, m_max);
198 if (relTimelinePos != m_pos) {
199 m_pos = relTimelinePos;
205 #include "cornerswidget.moc"