]> git.sesse.net Git - kdenlive/blob - src/cornerswidget.cpp
corners widget: fix on-monitor controls not showing up when effect is added
[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 {
38     m_scene = monitor->getEffectScene();
39
40     m_item = new OnMonitorCornersItem(m_scene);
41     m_scene->addItem(m_item);
42
43     m_config = new MonitorSceneControlWidget(m_scene, this);
44     QGridLayout *l = static_cast<QGridLayout *>(layout());
45     l->addWidget(m_config->getShowHideButton(), 1, 1);
46     l->addWidget(m_config, 1, 2);
47
48     QToolButton *buttonShowLines = new QToolButton(m_config);
49     // TODO: Better Icons
50     buttonShowLines->setIcon(KIcon("insert-horizontal-rule"));
51     buttonShowLines->setToolTip(i18n("Show/Hide the lines connecting the corners"));
52     buttonShowLines->setCheckable(true);
53     buttonShowLines->setChecked(KdenliveSettings::onmonitoreffects_cornersshowlines());
54     connect(buttonShowLines, SIGNAL(toggled(bool)), this, SLOT(slotShowLines(bool)));
55     m_config->addWidget(buttonShowLines, 0, 2);
56     QToolButton *buttonShowControls = new QToolButton(m_config);
57     buttonShowControls->setIcon(KIcon("transform-move"));
58     buttonShowControls->setToolTip(i18n("Show additional controls"));
59     buttonShowControls->setCheckable(true);
60     buttonShowControls->setChecked(KdenliveSettings::onmonitoreffects_cornersshowcontrols());
61     connect(buttonShowControls, SIGNAL(toggled(bool)), this, SLOT(slotShowControls(bool)));
62     m_config->addWidget(buttonShowControls, 0, 3);
63
64     connect(m_config, SIGNAL(showScene(bool)), this, SLOT(slotShowScene(bool)));
65     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
66     connect(m_scene, SIGNAL(actionFinished()), this, SLOT(slotUpdateProperties()));
67
68     connect(keyframe_list, SIGNAL(itemSelectionChanged()), this, SLOT(slotUpdateItem()));
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 *item = keyframe_list->currentItem();
95     if (!item)
96         return;
97
98     QList<QPointF> points;
99     double val;
100     for (int col = 0; col < 8; col++) {
101         if (!keyframe_list->item(item->row(), col))
102             return;
103         val = (keyframe_list->item(item->row(), col)->text().toInt() - 2000) / 2000.;
104         if (col % 2 == 0)
105             points << QPointF(val * m_monitor->render->frameRenderWidth(), 0);
106         else
107             points[col / 2].setY(val * m_monitor->render->renderHeight());
108     }
109
110     m_scene->blockSignals(true);
111     m_item->setPolygon(QPolygonF() << points.at(0) << points.at(1) << points.at(2) << points.at(3));
112     m_scene->blockSignals(false);
113 }
114
115 void CornersWidget::slotUpdateProperties()
116 {
117     if (keyframe_list->columnCount() < 8)
118         return;
119
120     QPolygonF pol = m_item->polygon();
121
122     QTableWidgetItem *item = keyframe_list->currentItem();
123     double val;
124     for (int col = 0; col < 8; col++) {
125         if (col % 2 == 0)
126             val = pol.at(col / 2).x() / (double)m_monitor->render->frameRenderWidth();
127         else
128             val = pol.at(col / 2).y() / (double)m_monitor->render->renderHeight();
129         val *= 2000;
130         val += 2000;
131         QTableWidgetItem *nitem = keyframe_list->item(item->row(), col);
132         if (nitem->text().toInt() != (int)val)
133             nitem->setText(QString::number((int)val));
134     }
135
136     slotAdjustKeyframeInfo(false);
137 }
138
139 void CornersWidget::slotCheckMonitorPosition(int renderPos)
140 {
141     if (m_showScene)
142         emit checkMonitorPosition(renderPos);
143 }
144
145 void CornersWidget::slotShowScene(bool show)
146 {
147     m_showScene = show;
148     if (!m_showScene)
149         m_monitor->slotEffectScene(false);
150     else
151         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
152 }
153
154 void CornersWidget::slotShowLines(bool show)
155 {
156     KdenliveSettings::setOnmonitoreffects_cornersshowlines(show);
157     m_item->update();
158 }
159
160 void CornersWidget::slotShowControls(bool show)
161 {
162     KdenliveSettings::setOnmonitoreffects_cornersshowcontrols(show);
163     m_item->update();
164 }
165
166 #include "cornerswidget.moc"