cornerswidget.cpp 8.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/***************************************************************************
 *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

#include "cornerswidget.h"
#include "monitor.h"
#include "monitorscene.h"
23
#include "monitoreditwidget.h"
24
25
#include "onmonitoritems/onmonitorcornersitem.h"
#include "renderer.h"
Till Theato's avatar
Till Theato committed
26
#include "kdenlivesettings.h"
27
28

#include <QGraphicsView>
Till Theato's avatar
Till Theato committed
29
#include <QGridLayout>
30
#include <QToolButton>
31
32
33

#include <KIcon>

34
35
36
37
38
inline int lerp( const int a, const int b, double t )
{
    return a + (b - a) * t;
}

Till Theato's avatar
Till Theato committed
39
40
CornersWidget::CornersWidget(Monitor *monitor, QDomElement e, int minFrame, int maxFrame, Timecode tc, int activeKeyframe, QWidget* parent) :
        KeyframeEdit(e, minFrame, maxFrame, tc, activeKeyframe, parent),
41
        m_monitor(monitor),
42
43
        m_showScene(true),
        m_pos(0)
44
{
45
46
47
    MonitorEditWidget *edit = monitor->getEffectEdit();
    edit->showVisibilityButton(true);
    m_scene = edit->getScene();
48

Till Theato's avatar
Till Theato committed
49
    m_item = new OnMonitorCornersItem();
50
51
    m_scene->addItem(m_item);

52
    // TODO: Better Icons
53
    edit->removeCustomControls();
Till Theato's avatar
Till Theato committed
54
55
56
57
    edit->addCustomButton(KIcon("transform-move"), i18n("Show additional controls"), this, SLOT(slotShowControls(bool)),
                          true, KdenliveSettings::onmonitoreffects_cornersshowcontrols());
    edit->addCustomButton(KIcon("insert-horizontal-rule"), i18n("Show/Hide the lines connecting the corners"), this, SLOT(slotShowLines(bool)),
                          true, KdenliveSettings::onmonitoreffects_cornersshowlines());
58
59

    connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
60
    connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
Till Theato's avatar
Till Theato committed
61
    connect(m_item, SIGNAL(changed()), this, SLOT(slotUpdateProperties()));
62
    connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotInsertKeyframe()));
63
64

    connect(keyframe_list, SIGNAL(cellChanged(int, int)), this, SLOT(slotUpdateItem()));
65
66
67
68
69
70
}

CornersWidget::~CornersWidget()
{
    m_scene->removeItem(m_item);
    delete m_item;
71
72
73
74
    if (m_monitor) {
        MonitorEditWidget *edit = m_monitor->getEffectEdit();
        edit->showVisibilityButton(false);
        edit->removeCustomControls();
75
        m_monitor->slotEffectScene(false);
76
    }
77
78
}

79
80
81
82
83
84
85
86
void CornersWidget::addParameter(QDomElement e, int activeKeyframe)
{
    KeyframeEdit::addParameter(e, activeKeyframe);

    if (!m_item->polygon().count())
        slotUpdateItem();
}

87
88
void CornersWidget::slotUpdateItem()
{
89
90
    if (keyframe_list->columnCount() < 8)
        return;
Till Theato's avatar
Till Theato committed
91

92
    QTableWidgetItem *keyframe, *keyframeOld;
93
    keyframe = keyframeOld = keyframe_list->item(0, 0);
94
95
96
97
98
99
100
101
102
103
104
    for (int row = 0; row < keyframe_list->rowCount(); ++row) {
        keyframeOld = keyframe;
        keyframe = keyframe_list->item(row, 0);
        if (getPos(row) >= m_pos)
            break;
    }

    QList<QPointF> points, pointsPrev, pointsNext;
    pointsPrev = getPoints(keyframeOld);
    pointsNext = getPoints(keyframe);
    if (pointsPrev.count() != 4 || pointsNext.count() != 4)
Till Theato's avatar
Till Theato committed
105
106
        return;

107
108
109
110
111
112
113
    qreal position = (m_pos - getPos(keyframeOld->row())) / (qreal)( getPos(keyframe->row()) - getPos(keyframeOld->row()) + 1 );

    if (keyframeOld  == keyframe) {
        points = pointsNext;
    } else {
        for (int i = 0; i < 4; ++i)
            points.append(QLineF(pointsPrev.at(i), pointsNext.at(i)).pointAt(position));
Till Theato's avatar
Till Theato committed
114
    }
115

Till Theato's avatar
Till Theato committed
116
117
118
    m_scene->blockSignals(true);
    m_item->setPolygon(QPolygonF() << points.at(0) << points.at(1) << points.at(2) << points.at(3));
    m_scene->blockSignals(false);
119

120
121
122
    bool enable = getPos(keyframe->row()) == m_pos || keyframe_list->rowCount() == 1;
    m_item->setEnabled(enable);
    m_scene->setEnabled(enable);
123
124
}

Till Theato's avatar
Till Theato committed
125
void CornersWidget::slotUpdateProperties()
126
{
Till Theato's avatar
Till Theato committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    if (keyframe_list->columnCount() < 8)
        return;

    QPolygonF pol = m_item->polygon();

    QTableWidgetItem *item = keyframe_list->currentItem();
    double val;
    for (int col = 0; col < 8; col++) {
        if (col % 2 == 0)
            val = pol.at(col / 2).x() / (double)m_monitor->render->frameRenderWidth();
        else
            val = pol.at(col / 2).y() / (double)m_monitor->render->renderHeight();
        val *= 2000;
        val += 2000;
        QTableWidgetItem *nitem = keyframe_list->item(item->row(), col);
        if (nitem->text().toInt() != (int)val)
            nitem->setText(QString::number((int)val));
144
145
    }

Till Theato's avatar
Till Theato committed
146
    slotAdjustKeyframeInfo(false);
147
148
}

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
QList<QPointF> CornersWidget::getPoints(QTableWidgetItem* keyframe)
{
    QList<QPointF> points;

    if (!keyframe)
        return points;

    double val;
    for (int col = 0; col < 8; col++) {
        if (!keyframe_list->item(keyframe->row(), col))
            return QList<QPointF>();
        val = (keyframe_list->item(keyframe->row(), col)->text().toInt() - 2000) / 2000.;
        if (col % 2 == 0)
            points << QPointF(val * m_monitor->render->frameRenderWidth(), 0);
        else
            points[col / 2].setY(val * m_monitor->render->renderHeight());
    }
    return points;
}

169
170
void CornersWidget::slotCheckMonitorPosition(int renderPos)
{
Till Theato's avatar
Till Theato committed
171
172
    if (m_showScene)
        emit checkMonitorPosition(renderPos);
173
174
175
176
177
178
179
180
181
182
183
}

void CornersWidget::slotShowScene(bool show)
{
    m_showScene = show;
    if (!m_showScene)
        m_monitor->slotEffectScene(false);
    else
        slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
}

Till Theato's avatar
Till Theato committed
184
185
186
187
188
189
void CornersWidget::slotShowLines(bool show)
{
    KdenliveSettings::setOnmonitoreffects_cornersshowlines(show);
    m_item->update();
}

190
191
192
193
194
195
void CornersWidget::slotShowControls(bool show)
{
    KdenliveSettings::setOnmonitoreffects_cornersshowcontrols(show);
    m_item->update();
}

196
197
198
199
void CornersWidget::slotSyncPosition(int relTimelinePos)
{
    if (keyframe_list->rowCount()) {
        relTimelinePos = qBound(0, relTimelinePos, m_max);
200
201
        if (relTimelinePos != m_pos - m_min) {
            m_pos = relTimelinePos + m_min;
202
203
204
205
206
            slotUpdateItem();
        }
    }
}

207
208
209
210
211
212
void CornersWidget::slotInsertKeyframe()
{
    keyframe_list->blockSignals(true);

    int row;
    QTableWidgetItem *keyframe, *keyframeOld;
213
    keyframe = keyframeOld = keyframe_list->item(0, 0);
214
215
216
217
218
219
220
    for (row = 0; row < keyframe_list->rowCount(); ++row) {
        keyframeOld = keyframe;
        keyframe = keyframe_list->item(row, 0);
        if (getPos(row) >= m_pos)
            break;
    }

221
222
223
224
225
226
227
228
229

    int pos2;
    if (row == keyframe_list->rowCount()) {
        pos2 = m_max;
    } else {
        pos2 = getPos(row);
        if (pos2 == m_pos)
            return;
    }
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

    int pos1 = 0;
    if (row > 0)
        pos1 = getPos(row - 1);

    int col = keyframe_list->currentColumn();
    double pos = (m_pos - pos1) / (double)(pos2 - pos1 + 1);

    keyframe_list->insertRow(row);
    keyframe_list->setVerticalHeaderItem(row, new QTableWidgetItem(getPosString(m_pos)));

    QPolygonF pol = m_item->polygon();
    double val;
    for (int i = 0; i < keyframe_list->columnCount(); i++) {
        if (i < 8) {
            if (i % 2 == 0)
                val = pol.at(i / 2).x() / (double)m_monitor->render->frameRenderWidth();
            else
                val = pol.at(i / 2).y() / (double)m_monitor->render->renderHeight();
            val *= 2000;
            val += 2000;
            keyframe_list->setItem(row, i, new QTableWidgetItem(QString::number((int)val)));
        } else {
            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))));
        }
    }

    keyframe_list->resizeRowsToContents();
    slotAdjustKeyframeInfo();
    keyframe_list->blockSignals(false);
    generateAllParams();
    button_delete->setEnabled(true);
    keyframe_list->setCurrentCell(row, col);
    keyframe_list->selectRow(row);
}

266
#include "cornerswidget.moc"