]> git.sesse.net Git - kdenlive/blob - src/beziercurve/bpoint.cpp
Const'ref
[kdenlive] / src / beziercurve / bpoint.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 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 "bpoint.h"
20
21
22 BPoint::BPoint() :
23         h1(QPointF(-1, -1)),
24         p(QPointF(-1, -1)),
25         h2(QPointF(-1, -1)),
26         handlesLinked(true)
27 {
28 }
29
30 BPoint::BPoint(const QPointF &handle1, const QPointF &point, const QPointF &handle2) :
31         h1(handle1),
32         p(point),
33         h2(handle2)
34 {
35     autoSetLinked();
36 }
37
38 QPointF &BPoint::operator[](int i)
39 {
40     return i == 0 ? h1 : (i == 1 ? p : h2);
41 }
42
43 const QPointF &BPoint::operator[](int i) const
44 {
45     return i == 0 ? h1 : (i == 1 ? p : h2);
46 }
47
48 bool BPoint::operator==(const BPoint& point) const
49 {
50     return      point.h1 == h1 &&
51                 point.p  == p  &&
52                 point.h2 == h2;
53 }
54
55 void BPoint::setP(const QPointF &point, bool updateHandles)
56 {
57     QPointF offset = point - p;
58     p = point;
59     if (updateHandles) {
60         h1 += offset;
61         h2 += offset;
62     }
63 }
64
65 void BPoint::setH1(const QPointF &handle1)
66 {
67     h1 = handle1;
68     if (handlesLinked) {
69         qreal angle = QLineF(h1, p).angle();
70         QLineF l = QLineF(p, h2);
71         l.setAngle(angle);
72         h2 = l.p2();
73     }    
74 }
75
76 void BPoint::setH2(const QPointF &handle2)
77 {
78     h2 = handle2;
79     if (handlesLinked) {
80         qreal angle = QLineF(h2, p).angle();
81         QLineF l = QLineF(p, h1);
82         l.setAngle(angle);
83         h1 = l.p2();
84     }
85 }
86
87 void BPoint::keepInRange(qreal xMin, qreal xMax)
88 {
89     Q_UNUSED(xMin)
90     Q_UNUSED(xMax)
91 }
92
93 void BPoint::autoSetLinked()
94 {
95     // sometimes the angle is returned as 360°
96     // due to rounding problems the angle is sometimes not quite 0
97     qreal angle = QLineF(h1, p).angleTo(QLineF(p, h2));
98     handlesLinked = angle < 1e-3 || qRound(angle) == 360;
99 }