1 /************************************************************************
2  **
3  **  @file   vsplinepoint.cpp
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   November 15, 2013
6  **
7  **  @brief
8  **  @copyright
9  **  This source code is part of the Valentina project, a pattern making
10  **  program, whose allow create and modeling patterns of clothing.
11  **  Copyright (C) 2013-2015 Valentina project
12  **  <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
13  **
14  **  Valentina is free software: you can redistribute it and/or modify
15  **  it under the terms of the GNU General Public License as published by
16  **  the Free Software Foundation, either version 3 of the License, or
17  **  (at your option) any later version.
18  **
19  **  Valentina is distributed in the hope that it will be useful,
20  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  **  GNU General Public License for more details.
23  **
24  **  You should have received a copy of the GNU General Public License
25  **  along with Valentina.  If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #include "vsplinepoint.h"
30 
31 #include <QJsonObject>
32 #include <QLineF>
33 
34 #include "../qmuparser/qmutokenparser.h"
35 #include "vsplinepoint_p.h"
36 
37 //---------------------------------------------------------------------------------------------------------------------
38 /**
39  * @brief VFSplinePoint default constructor.
40  */
VFSplinePoint()41 VFSplinePoint::VFSplinePoint()
42     :d(new VFSplinePointData)
43 {}
44 
45 //---------------------------------------------------------------------------------------------------------------------
46 /**
47  * @brief VFSplinePoint constructor.
48  * @param pSpline spline point.
49  * @param kAsm1 coefficient of length first control line.
50  * @param angle1 first angle control line.
51  * @param kAsm2 coefficient of length second control line.
52  * @param angle2 second angle control line.
53  */
VFSplinePoint(const VPointF & pSpline,qreal kAsm1,qreal angle1,qreal kAsm2,qreal angle2)54 VFSplinePoint::VFSplinePoint(const VPointF &pSpline, qreal kAsm1, qreal angle1, qreal kAsm2, qreal angle2)
55     :d(new VFSplinePointData(pSpline, kAsm1, angle1, kAsm2, angle2))
56 {}
57 
58 //---------------------------------------------------------------------------------------------------------------------
59 /**
60  * @brief VFSplinePoint copy constructor
61  * @param point point
62  */
VFSplinePoint(const VFSplinePoint & point)63 VFSplinePoint::VFSplinePoint(const VFSplinePoint &point)
64     :d(point.d)
65 {}
66 
67 //---------------------------------------------------------------------------------------------------------------------
operator =(const VFSplinePoint & point)68 VFSplinePoint &VFSplinePoint::operator=(const VFSplinePoint &point)
69 {
70     if ( &point == this )
71     {
72         return *this;
73     }
74     d = point.d;
75     return *this;
76 }
77 
78 #ifdef Q_COMPILER_RVALUE_REFS
79 //---------------------------------------------------------------------------------------------------------------------
VFSplinePoint(const VFSplinePoint && point)80 VFSplinePoint::VFSplinePoint(const VFSplinePoint &&point) Q_DECL_NOTHROW
81     :d(point.d)
82 {}
83 
84 //---------------------------------------------------------------------------------------------------------------------
operator =(VFSplinePoint && point)85 VFSplinePoint &VFSplinePoint::operator=(VFSplinePoint &&point) Q_DECL_NOTHROW
86 {
87     std::swap(d, point.d);
88     return *this;
89 }
90 #endif
91 
92 //---------------------------------------------------------------------------------------------------------------------
~VFSplinePoint()93 VFSplinePoint::~VFSplinePoint()
94 {}
95 
96 //---------------------------------------------------------------------------------------------------------------------
97 /**
98  * @brief SetAngle1 set first angle of spline.
99  * @param value angle.
100  */
SetAngle1(const qreal & value)101 void VFSplinePoint::SetAngle1(const qreal &value)
102 {
103     QLineF line(0, 0, 100, 0);
104     line.setAngle(value);
105     d->angle1 = line.angle();
106 
107     line.setAngle(d->angle1+180);
108     d->angle2 = line.angle();
109 }
110 
111 //---------------------------------------------------------------------------------------------------------------------
112 /**
113  * @brief SetAngle2 set second angle of spline.
114  * @param value angle.
115  */
SetAngle2(const qreal & value)116 void VFSplinePoint::SetAngle2(const qreal &value)
117 {
118     QLineF line(0, 0, 100, 0);
119     line.setAngle(value);
120     d->angle2 = line.angle();
121 
122     line.setAngle(d->angle2+180);
123     d->angle1 = line.angle();
124 }
125 
126 //---------------------------------------------------------------------------------------------------------------------
127 /**
128  * @brief P return point.
129  * @return point.
130  */
P() const131 VPointF VFSplinePoint::P() const
132 {
133     return d->pSpline;
134 }
135 
136 //---------------------------------------------------------------------------------------------------------------------
137 /**
138  * @brief SetP set point.
139  * @param value point.
140  */
SetP(const VPointF & value)141 void VFSplinePoint::SetP(const VPointF &value)
142 {
143     d->pSpline = value;
144 }
145 
146 //---------------------------------------------------------------------------------------------------------------------
147 /**
148  * @brief Angle1 return first angle of spline.
149  * @return angle.
150  */
Angle1() const151 qreal VFSplinePoint::Angle1() const
152 {
153     return d->angle1;
154 }
155 
156 //---------------------------------------------------------------------------------------------------------------------
157 /**
158  * @brief Angle2 return second angle of spline.
159  * @return angle.
160  */
Angle2() const161 qreal VFSplinePoint::Angle2() const
162 {
163     return d->angle2;
164 }
165 
166 //---------------------------------------------------------------------------------------------------------------------
167 /**
168  * @brief KAsm1 return coefficient of length first control line.
169  * @return coefficient.
170  */
KAsm1() const171 qreal VFSplinePoint::KAsm1() const
172 {
173     return d->kAsm1;
174 }
175 
176 //---------------------------------------------------------------------------------------------------------------------
177 /**
178  * @brief SetKAsm1 set coefficient of length first control line.
179  * @param value coefficient.
180  */
SetKAsm1(const qreal & value)181 void VFSplinePoint::SetKAsm1(const qreal &value)
182 {
183     d->kAsm1 = value;
184 }
185 
186 //---------------------------------------------------------------------------------------------------------------------
187 /**
188  * @brief KAsm2 return coefficient of length second control line.
189  * @return coefficient.
190  */
KAsm2() const191 qreal VFSplinePoint::KAsm2() const
192 {
193     return d->kAsm2;
194 }
195 
196 //---------------------------------------------------------------------------------------------------------------------
197 /**
198  * @brief SetKAsm2 set coefficient of length second control line.
199  * @param value coefficient.
200  */
SetKAsm2(const qreal & value)201 void VFSplinePoint::SetKAsm2(const qreal &value)
202 {
203     d->kAsm2 = value;
204 }
205 
206 //------------------------------------------VSplinePoint---------------------------------------------------------------
VSplinePoint()207 VSplinePoint::VSplinePoint()
208     : d(new VSplinePointData)
209 {
210 }
211 
212 //---------------------------------------------------------------------------------------------------------------------
VSplinePoint(const VPointF & pSpline,qreal angle1,const QString & angle1F,qreal angle2,const QString & angle2F,qreal length1,const QString & length1F,qreal length2,const QString & length2F)213 VSplinePoint::VSplinePoint(const VPointF &pSpline, qreal angle1, const QString &angle1F, qreal angle2,
214                            const QString &angle2F, qreal length1, const QString &length1F, qreal length2,
215                            const QString &length2F)
216     : d(new VSplinePointData(pSpline, angle1, angle1F, angle2, angle2F, length1, length1F, length2, length2F))
217 {
218 }
219 
220 //---------------------------------------------------------------------------------------------------------------------
VSplinePoint(const VSplinePoint & point)221 VSplinePoint::VSplinePoint(const VSplinePoint &point)
222     : d(point.d)
223 {
224 }
225 
226 //---------------------------------------------------------------------------------------------------------------------
operator =(const VSplinePoint & point)227 VSplinePoint &VSplinePoint::operator=(const VSplinePoint &point)
228 {
229     if ( &point == this )
230     {
231         return *this;
232     }
233     d = point.d;
234     return *this;
235 }
236 
237 #ifdef Q_COMPILER_RVALUE_REFS
238 //---------------------------------------------------------------------------------------------------------------------
VSplinePoint(const VSplinePoint && point)239 VSplinePoint::VSplinePoint(const VSplinePoint &&point) Q_DECL_NOTHROW
240     : d(point.d)
241 {}
242 
243 //---------------------------------------------------------------------------------------------------------------------
operator =(VSplinePoint && point)244 VSplinePoint &VSplinePoint::operator=(VSplinePoint &&point) Q_DECL_NOTHROW
245 {
246     std::swap(d, point.d);
247     return *this;
248 }
249 #endif
250 
251 //---------------------------------------------------------------------------------------------------------------------
~VSplinePoint()252 VSplinePoint::~VSplinePoint()
253 {
254 }
255 
256 //---------------------------------------------------------------------------------------------------------------------
P() const257 VPointF VSplinePoint::P() const
258 {
259     return d->pSpline;
260 }
261 
262 //---------------------------------------------------------------------------------------------------------------------
SetP(const VPointF & value)263 void VSplinePoint::SetP(const VPointF &value)
264 {
265     d->pSpline = value;
266 }
267 
268 //---------------------------------------------------------------------------------------------------------------------
Angle1() const269 qreal VSplinePoint::Angle1() const
270 {
271     return d->angle1;
272 }
273 
274 //---------------------------------------------------------------------------------------------------------------------
Angle1Formula() const275 QString VSplinePoint::Angle1Formula() const
276 {
277     return d->angle1F;
278 }
279 
280 //---------------------------------------------------------------------------------------------------------------------
SetAngle1(const qreal & value,const QString & angle1F)281 void VSplinePoint::SetAngle1(const qreal &value, const QString &angle1F)
282 {
283     d->angle1F = angle1F;
284 
285     QLineF line(0, 0, 100, 0);
286     line.setAngle(value);
287     d->angle1 = line.angle();
288 
289     line.setAngle(d->angle1+180);
290     d->angle2 = line.angle();
291     d->angle2F = QString().number(d->angle2);
292 }
293 
294 //---------------------------------------------------------------------------------------------------------------------
Angle2() const295 qreal VSplinePoint::Angle2() const
296 {
297     return d->angle2;
298 }
299 
300 //---------------------------------------------------------------------------------------------------------------------
Angle2Formula() const301 QString VSplinePoint::Angle2Formula() const
302 {
303     return d->angle2F;
304 }
305 
306 //---------------------------------------------------------------------------------------------------------------------
SetAngle2(const qreal & value,const QString & angle2F)307 void VSplinePoint::SetAngle2(const qreal &value, const QString &angle2F)
308 {
309     d->angle2F = angle2F;
310 
311     QLineF line(0, 0, 100, 0);
312     line.setAngle(value);
313     d->angle2 = line.angle();
314 
315     line.setAngle(d->angle2+180);
316     d->angle1 = line.angle();
317     d->angle1F = QString().number(d->angle1);
318 }
319 
320 //---------------------------------------------------------------------------------------------------------------------
Length1() const321 qreal VSplinePoint::Length1() const
322 {
323     return d->length1;
324 }
325 
326 //---------------------------------------------------------------------------------------------------------------------
Length1Formula() const327 QString VSplinePoint::Length1Formula() const
328 {
329     return d->length1F;
330 }
331 
332 //---------------------------------------------------------------------------------------------------------------------
SetLength1(const qreal & value,const QString & length1F)333 void VSplinePoint::SetLength1(const qreal &value, const QString &length1F)
334 {
335     d->length1 = value;
336     d->length1F = length1F;
337 }
338 
339 //---------------------------------------------------------------------------------------------------------------------
Length2() const340 qreal VSplinePoint::Length2() const
341 {
342     return d->length2;
343 }
344 
345 //---------------------------------------------------------------------------------------------------------------------
Length2Formula() const346 QString VSplinePoint::Length2Formula() const
347 {
348     return d->length2F;
349 }
350 
351 //---------------------------------------------------------------------------------------------------------------------
SetLength2(const qreal & value,const QString & length2F)352 void VSplinePoint::SetLength2(const qreal &value, const QString &length2F)
353 {
354     d->length2 = value;
355     d->length2F = length2F;
356 }
357 
358 //---------------------------------------------------------------------------------------------------------------------
IsMovable() const359 bool VSplinePoint::IsMovable() const
360 {
361     return qmu::QmuTokenParser::IsSingle(d->angle1F) && qmu::QmuTokenParser::IsSingle(d->angle2F) &&
362            qmu::QmuTokenParser::IsSingle(d->length1F) && qmu::QmuTokenParser::IsSingle(d->length2F);
363 }
364 
365 //---------------------------------------------------------------------------------------------------------------------
ToJson() const366 QJsonObject VSplinePoint::ToJson() const
367 {
368     QJsonObject object
369     {
370         {"point", d->pSpline.ToJson()},
371         {"angle1", d->angle1},
372         {"angle1Formula", d->angle1F},
373         {"angle2", d->angle2},
374         {"angle2Formula", d->angle2F},
375         {"length1", d->length1},
376         {"length1Formula", d->length1F},
377         {"length2", d->length2},
378         {"length2Formula", d->length2F},
379     };
380     return object;
381 }
382