]> git.sesse.net Git - kdenlive/blob - src/beziercurve/beziersplinewidget.cpp
Bezier Spline: Allow to link the handles of a point. They will then always remain...
[kdenlive] / src / beziercurve / beziersplinewidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *   This file is part of Kdenlive (www.kdenlive.org).                     *
4  *                                                                         *
5  *   Kdenlive is free software: you can redistribute it and/or modify      *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation, either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   Kdenlive is distributed in the hope that it will be useful,           *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with Kdenlive.  If not, see <http://www.gnu.org/licenses/>.     *
17  ***************************************************************************/
18
19 #include "beziersplinewidget.h"
20 #include "colortools.h"
21 #include "kdenlivesettings.h"
22
23 #include <QVBoxLayout>
24
25 #include <KIcon>
26
27 BezierSplineWidget::BezierSplineWidget(const QString& spline, QWidget* parent) :
28         QWidget(parent),
29         m_mode(ModeRGB)
30 {
31     QVBoxLayout *layout = new QVBoxLayout(this);
32     layout->addWidget(&m_edit);
33     QWidget *widget = new QWidget(this);
34     m_ui.setupUi(widget);
35     layout->addWidget(widget);
36
37     m_ui.buttonLinkHandles->setIcon(KIcon("insert-link"));
38     m_ui.buttonZoomIn->setIcon(KIcon("zoom-in"));
39     m_ui.buttonZoomOut->setIcon(KIcon("zoom-out"));
40     m_ui.buttonGridChange->setIcon(KIcon("view-grid"));
41     m_ui.buttonShowPixmap->setIcon(QIcon(QPixmap::fromImage(ColorTools::rgbCurvePlane(QSize(16, 16), ColorTools::COL_Luma, 0.8))));
42     m_ui.buttonResetSpline->setIcon(KIcon("view-refresh"));
43     m_ui.widgetPoint->setEnabled(false);
44
45     CubicBezierSpline s;
46     s.fromString(spline);
47     m_edit.setSpline(s);
48
49     connect(&m_edit, SIGNAL(modified()), this, SIGNAL(modified()));
50     connect(&m_edit, SIGNAL(currentPoint(const BPoint&)), this, SLOT(slotUpdatePoint(const BPoint&)));
51
52     connect(m_ui.spinPX, SIGNAL(editingFinished()), this, SLOT(slotUpdatePointP()));
53     connect(m_ui.spinPY, SIGNAL(editingFinished()), this, SLOT(slotUpdatePointP()));
54     connect(m_ui.spinH1X, SIGNAL(editingFinished()), this, SLOT(slotUpdatePointH1()));
55     connect(m_ui.spinH1Y, SIGNAL(editingFinished()), this, SLOT(slotUpdatePointH1()));
56     connect(m_ui.spinH2X, SIGNAL(editingFinished()), this, SLOT(slotUpdatePointH2()));
57     connect(m_ui.spinH2Y, SIGNAL(editingFinished()), this, SLOT(slotUpdatePointH2()));
58
59     connect(m_ui.buttonLinkHandles, SIGNAL(toggled(bool)), this, SLOT(slotSetHandlesLinked(bool)));
60     connect(m_ui.buttonZoomIn, SIGNAL(clicked()), &m_edit, SLOT(slotZoomIn()));
61     connect(m_ui.buttonZoomOut, SIGNAL(clicked()), &m_edit, SLOT(slotZoomOut()));
62     connect(m_ui.buttonGridChange, SIGNAL(clicked()), this, SLOT(slotGridChange()));
63     connect(m_ui.buttonShowPixmap, SIGNAL(toggled(bool)), this, SLOT(slotShowPixmap(bool)));
64     connect(m_ui.buttonResetSpline, SIGNAL(clicked()), this, SLOT(slotResetSpline()));
65
66     m_edit.setGridLines(KdenliveSettings::bezier_gridlines());
67     m_ui.buttonShowPixmap->setChecked(KdenliveSettings::bezier_showpixmap());
68 }
69
70 QString BezierSplineWidget::spline()
71 {
72     return m_edit.spline().toString();
73 }
74
75 void BezierSplineWidget::setMode(BezierSplineWidget::CurveModes mode)
76 {
77     if (m_mode != mode) {
78         m_mode = mode;
79         if (m_showPixmap)
80             slotShowPixmap();
81     }
82 }
83
84 void BezierSplineWidget::slotGridChange()
85 {
86     m_edit.setGridLines((m_edit.gridLines() + 1) % 9);
87     KdenliveSettings::setBezier_gridlines(m_edit.gridLines());
88 }
89
90 void BezierSplineWidget::slotShowPixmap(bool show)
91 {
92     m_showPixmap = show;
93     KdenliveSettings::setBezier_showpixmap(show);
94     if (show && m_mode != ModeAlpha && m_mode != ModeRGB)
95         m_edit.setPixmap(QPixmap::fromImage(ColorTools::rgbCurvePlane(m_edit.size(), (ColorTools::ColorsRGB)((int)(m_mode)))));
96     else
97         m_edit.setPixmap(QPixmap());
98 }
99
100 void BezierSplineWidget::slotUpdatePoint(const BPoint &p)
101 {
102     blockSignals(true);
103     if (p == BPoint()) {
104         m_ui.widgetPoint->setEnabled(false);
105     } else {
106         m_ui.widgetPoint->setEnabled(true);
107         m_ui.spinPX->setValue(qRound(p.p.x() * 255));
108         m_ui.spinPY->setValue(qRound(p.p.y() * 255));
109         m_ui.spinH1X->setValue(qRound(p.h1.x() * 255));
110         m_ui.spinH1Y->setValue(qRound(p.h1.y() * 255));
111         m_ui.spinH2X->setValue(qRound(p.h2.x() * 255));
112         m_ui.spinH2Y->setValue(qRound(p.h2.y() * 255));
113         m_ui.buttonLinkHandles->setChecked(p.handlesLinked);
114     }
115     blockSignals(false);
116 }
117
118 void BezierSplineWidget::slotUpdatePointP()
119 {
120     BPoint p = m_edit.getCurrentPoint();
121
122     p.setP(QPointF(m_ui.spinPX->value() / 255., m_ui.spinPY->value() / 255.));
123
124     m_edit.updateCurrentPoint(p);
125 }
126
127 void BezierSplineWidget::slotUpdatePointH1()
128 {
129     BPoint p = m_edit.getCurrentPoint();
130
131     p.setH1(QPointF(m_ui.spinH1X->value() / 255., m_ui.spinH1Y->value() / 255.));
132
133     m_edit.updateCurrentPoint(p);
134 }
135
136 void BezierSplineWidget::slotUpdatePointH2()
137 {
138     BPoint p = m_edit.getCurrentPoint();
139
140     p.setH2(QPointF(m_ui.spinH2X->value() / 255., m_ui.spinH2Y->value() / 255.));
141
142     m_edit.updateCurrentPoint(p);
143 }
144
145 void BezierSplineWidget::slotSetHandlesLinked(bool linked)
146 {
147     BPoint p = m_edit.getCurrentPoint();
148     p.handlesLinked = linked;
149     m_edit.updateCurrentPoint(p);
150 }
151
152 void BezierSplineWidget::slotResetSpline()
153 {
154     m_edit.setSpline(CubicBezierSpline());
155 }
156
157 #include "beziersplinewidget.moc"