1 /*
2     SPDX-FileCopyrightText: 2010 Till Theato <root@ttill.de>
3     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
4 */
5 
6 #ifndef CUBICBEZIERSPLINE_H
7 #define CUBICBEZIERSPLINE_H
8 
9 #include "bpoint.h"
10 
11 #include <QList>
12 #include <QString>
13 
14 /** @class CubicBezierSpline
15     @brief \@todo Describe class CubicBezierSpline
16     @todo Describe class CubicBezierSpline
17  */
18 class CubicBezierSpline
19 {
20 
21 public:
22     using Point_t = BPoint;
23     explicit CubicBezierSpline();
24     CubicBezierSpline(const CubicBezierSpline &spline);
25     CubicBezierSpline &operator=(const CubicBezierSpline &spline);
26 
27     /** @brief Loads the points from the string @param spline.
28      *
29      * x, y values have to be separated with a ';'
30      * handles and points with a '#'
31      * and the nodes with a '|'
32      * So that you get: h1x;h1y#px;py#h2x;h2y|h1x;h1y#... */
33     void fromString(const QString &spline);
34     /** @brief Returns the points stored in a string.
35      *
36      * x, y values have are separated with a ';'
37      * handles and points with a '#'
38      * and the nodes with a '|'
39      * So that you get: h1x;h1y#px;py#h2x;h2y|h1x;h1y#... */
40     QString toString() const;
41 
42     /** @brief Returns a list of the points defining the spline. */
43     QList<BPoint> points() const;
44 
45     /** @brief Returns the number of points in the spline.*/
46     int count() const;
47 
48     /** @brief Sets the point at index @param ix to @param point and returns its index (it might have changed during validation). */
49     int setPoint(int ix, const BPoint &point);
50     /** @brief Adds @param point and returns its index. */
51     int addPoint(const BPoint &point);
52     /** @brief Add a point based on a position @param point only
53         This will try to compute relevant handles based on neihbouring points
54         Return the index of the added point.
55     */
56     int addPoint(const QPointF &point);
57     /** @brief Removes the point at @param ix. */
58     void removePoint(int ix);
59 
60     /** @brief Returns the point at @param ix.
61      * @param ix Index of the point
62      * @param normalisedWidth (default = 1) Will be multiplied will all x values to change the range from 0-1 into another one
63      * @param normalisedHeight (default = 1) Will be multiplied will all y values to change the range from 0-1 into another one
64      * @param invertHeight (default = false) true => y = 0 is at the very top
65      */
66     BPoint getPoint(int ix, int normalisedWidth = 1, int normalisedHeight = 1, bool invertHeight = false);
67 
68     /** @brief Returns the closest point to @param p, represented by its index and type (center or handle)
69      */
70     std::pair<int, BPoint::PointType> closestPoint(const QPointF &p) const;
71 
72     QList<BPoint> getPoints() const;
73 
74 private:
75     void validatePoints();
76     void keepSorted();
77     int indexOf(const BPoint &p);
78 
79     QList<BPoint> m_points;
80 };
81 
82 #endif
83