]> git.sesse.net Git - kdenlive/blob - src/beziercurve/cubicbezierspline.cpp
45cca94ce1649da2f04b0dfd7a1c036438a853ca
[kdenlive] / src / beziercurve / cubicbezierspline.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 "cubicbezierspline.h"
20 #include <KDebug>
21
22 /** @brief For sorting a Bezier spline. Whether a is before b. */
23 static bool pointLessThan(const BPoint &a, const BPoint &b)
24 {
25     return a.p.x() < b.p.x();
26 }
27
28 CubicBezierSpline::CubicBezierSpline(QObject* parent) :
29         QObject(parent)
30 {
31     m_points.append(BPoint(QPointF(0, 0), QPointF(0, 0), QPointF(.1, .1)));
32     m_points.append(BPoint(QPointF(.9, .9), QPointF(1, 1), QPointF(1, 1)));
33 }
34
35 CubicBezierSpline::CubicBezierSpline(const CubicBezierSpline& spline, QObject* parent) :
36         QObject(parent)
37 {
38     m_points = spline.m_points;
39 }
40
41 CubicBezierSpline& CubicBezierSpline::operator=(const CubicBezierSpline& spline)
42 {
43     m_points = spline.m_points;
44     return *this;
45 }
46
47 void CubicBezierSpline::fromString(const QString& spline)
48 {
49     m_points.clear();
50
51     const QStringList bpoints = spline.split(QLatin1Char('|'));
52     foreach(const QString &bpoint, bpoints) {
53         const QStringList points = bpoint.split(QLatin1Char('#'));
54         QList <QPointF> values;
55         foreach(const QString &point, points) {
56             QStringList xy = point.split(QLatin1Char(';'));
57             if (xy.count() == 2)
58                 values.append(QPointF(xy.at(0).toDouble(), xy.at(1).toDouble()));
59         }
60         if (values.count() == 3) {
61             m_points.append(BPoint(values.at(0), values.at(1), values.at(2)));
62         }
63     }
64
65     keepSorted();
66     validatePoints();
67 }
68
69 QString CubicBezierSpline::toString() const
70 {
71     QStringList spline;
72     QLocale locale;
73     foreach(const BPoint &p, m_points) {
74         spline << QString("%1;%2#%3;%4#%5;%6").arg(locale.toString(p.h1.x())).arg(locale.toString(p.h1.y()))
75                                               .arg(locale.toString(p.p.x())).arg(locale.toString(p.p.y()))
76                                               .arg(locale.toString(p.h2.x())).arg(locale.toString(p.h2.y()));
77     }
78     return spline.join("|");
79 }
80
81 int CubicBezierSpline::setPoint(int ix, const BPoint& point)
82 {
83     m_points[ix] = point;
84     keepSorted();
85     validatePoints();
86     return indexOf(point); // in case it changed
87 }
88
89 QList <BPoint> CubicBezierSpline::points() const
90 {
91     return m_points;
92 }
93
94 void CubicBezierSpline::removePoint(int ix)
95 {
96     m_points.removeAt(ix);
97 }
98
99 int CubicBezierSpline::addPoint(const BPoint& point)
100 {
101     m_points.append(point);
102     keepSorted();
103     validatePoints();
104     return indexOf(point);
105 }
106
107 BPoint CubicBezierSpline::getPoint(int ix, int normalisedWidth, int normalisedHeight, bool invertHeight)
108 {
109     BPoint p = m_points.at(ix);
110     for (int i = 0; i < 3; ++i) {
111         p[i].rx() *= normalisedWidth;
112         p[i].ry() *= normalisedHeight;
113         if (invertHeight)
114             p[i].ry() = normalisedHeight - p[i].y();
115     }
116     return p;
117 }
118
119 void CubicBezierSpline::validatePoints()
120 {
121     BPoint p1, p2;
122     for (int i = 0; i < m_points.count() - 1; ++i) {
123         p1 = m_points.at(i);
124         p2 = m_points.at(i+1);
125         p1.h2.setX(qBound(p1.p.x(), p1.h2.x(), p2.p.x()));
126         p2.h1.setX(qBound(p1.p.x(), p2.h1.x(), p2.p.x()));
127         m_points[i] = p1;
128         m_points[i+1] = p2;
129     }
130 }
131
132 void CubicBezierSpline::keepSorted()
133 {
134     qSort(m_points.begin(), m_points.end(), pointLessThan);
135 }
136
137 int CubicBezierSpline::indexOf(const BPoint& p)
138 {
139     if (m_points.indexOf(p) == -1) {
140         // point changed during validation process
141         for (int i = 0; i < m_points.count(); ++i) {
142             // this condition should be sufficient, too
143             if (m_points.at(i).p == p.p)
144                 return i;
145         }
146     } else {
147         return m_points.indexOf(p);
148     }
149     return -1;
150 }
151
152 #include "cubicbezierspline.moc"