]> git.sesse.net Git - kdenlive/blob - src/beziercurve/beziersplinewidget.cpp
Bézier spline:
[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
21 #include <QVBoxLayout>
22
23 #include <KIcon>
24
25 BezierSplineWidget::BezierSplineWidget(const QString& spline, QWidget* parent) :
26         QWidget(parent)
27 {
28     QVBoxLayout *layout = new QVBoxLayout(this);
29     layout->addWidget(&m_edit);
30     QWidget *widget = new QWidget(this);
31     m_ui.setupUi(widget);
32     layout->addWidget(widget);
33
34     m_ui.buttonLinkHandles->setIcon(KIcon("insert-link"));
35     m_ui.buttonLinkHandles->setEnabled(false);
36     m_ui.widgetPoint->setEnabled(false);
37
38     CubicBezierSpline s;
39     s.fromString(spline);
40     m_edit.setSpline(s);
41
42     connect(&m_edit, SIGNAL(modified()), this, SIGNAL(modified()));
43     connect(&m_edit, SIGNAL(currentPoint(const BPoint&)), this, SLOT(slotUpdatePoint(const BPoint&)));
44
45     connect(m_ui.spinPX, SIGNAL(editingFinished()), this, SLOT(slotUpdateSpline()));
46     connect(m_ui.spinPY, SIGNAL(editingFinished()), this, SLOT(slotUpdateSpline()));
47     connect(m_ui.spinH1X, SIGNAL(editingFinished()), this, SLOT(slotUpdateSpline()));
48     connect(m_ui.spinH1Y, SIGNAL(editingFinished()), this, SLOT(slotUpdateSpline()));
49     connect(m_ui.spinH2X, SIGNAL(editingFinished()), this, SLOT(slotUpdateSpline()));
50     connect(m_ui.spinH2Y, SIGNAL(editingFinished()), this, SLOT(slotUpdateSpline()));
51 }
52
53 QString BezierSplineWidget::spline()
54 {
55     return m_edit.spline().toString();
56 }
57
58 void BezierSplineWidget::slotUpdatePoint(const BPoint &p)
59 {
60     blockSignals(true);
61     if (p == BPoint()) {
62         m_ui.widgetPoint->setEnabled(false);
63     } else {
64         m_ui.widgetPoint->setEnabled(true);
65         m_ui.spinPX->setValue(qRound(p.p.x() * 255));
66         m_ui.spinPY->setValue(qRound(p.p.y() * 255));
67         m_ui.spinH1X->setValue(qRound(p.h1.x() * 255));
68         m_ui.spinH1Y->setValue(qRound(p.h1.y() * 255));
69         m_ui.spinH2X->setValue(qRound(p.h2.x() * 255));
70         m_ui.spinH2Y->setValue(qRound(p.h2.y() * 255));
71     }
72     blockSignals(false);
73 }
74
75 void BezierSplineWidget::slotUpdateSpline()
76 {
77     BPoint p = m_edit.getCurrentPoint();
78
79     // check for every value, so we do not lose too much info through rounding
80     if (m_ui.spinPX->value() != qRound(p.p.x() * 255))
81         p.p.setX(m_ui.spinPX->value() / 255.);
82     if (m_ui.spinPY->value() != qRound(p.p.y() * 255))
83         p.p.setY(m_ui.spinPY->value() / 255.);
84
85     if (m_ui.spinH1X->value() != qRound(p.h1.x() * 255))
86         p.h1.setX(m_ui.spinH1X->value() / 255.);
87     if (m_ui.spinH1Y->value() != qRound(p.h1.y() * 255))
88         p.h1.setY(m_ui.spinH1Y->value() / 255.);
89
90     if (m_ui.spinH2X->value() != qRound(p.h2.x() * 255))
91         p.h2.setX(m_ui.spinH2X->value() / 255.);
92     if (m_ui.spinH2Y->value() != qRound(p.h2.y() * 255))
93         p.h2.setY(m_ui.spinH2Y->value() / 255.);
94
95     m_edit.updateCurrentPoint(p);
96     emit modified();
97 }
98
99 #include "beziersplinewidget.moc"