Seamly2D
Code documentation
vpiecenode.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 3 11, 2016
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2016 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "vpiecenode.h"
53 #include "vpiecenode_p.h"
54 #include "vcontainer.h"
55 #include "calculator.h"
56 
57 #include <QDataStream>
58 #include <QtNumeric>
59 
60 namespace
61 {
62 //---------------------------------------------------------------------------------------------------------------------
63 qreal EvalFormula(const VContainer *data, QString formula)
64 {
65  if (formula.isEmpty())
66  {
67  return -1;
68  }
69  else
70  {
71  try
72  {
73  // Replace line return character with spaces for calc if exist
74  formula.replace("\n", " ");
75  QScopedPointer<Calculator> cal(new Calculator());
76  const qreal result = cal->EvalFormula(data->DataVariables(), formula);
77 
78  if (qIsInf(result) || qIsNaN(result))
79  {
80  return -1;
81  }
82  return result;
83  }
84  catch (qmu::QmuParserError &e)
85  {
86  Q_UNUSED(e)
87  return -1;
88  }
89  }
90 }
91 }
92 
93 //---------------------------------------------------------------------------------------------------------------------
94 
95 #ifdef Q_COMPILER_RVALUE_REFS
96 VPieceNode &VPieceNode::operator=(VPieceNode &&node) Q_DECL_NOTHROW
97 { Swap(node); return *this; }
98 #endif
99 
100 void VPieceNode::Swap(VPieceNode &node) Q_DECL_NOTHROW
101 { std::swap(d, node.d); }
102 
103 //---------------------------------------------------------------------------------------------------------------------
105  : d(new VPieceNodeData)
106 {}
107 
108 //---------------------------------------------------------------------------------------------------------------------
109 VPieceNode::VPieceNode(quint32 id, Tool typeTool, bool reverse)
110  : d(new VPieceNodeData(id, typeTool, reverse))
111 {}
112 
113 //---------------------------------------------------------------------------------------------------------------------
115  : d (node.d)
116 {}
117 
118 //---------------------------------------------------------------------------------------------------------------------
120 {
121  if ( &node == this )
122  {
123  return *this;
124  }
125  d = node.d;
126  return *this;
127 }
128 
129 //---------------------------------------------------------------------------------------------------------------------
131 {}
132 
133 // Friend functions
134 //---------------------------------------------------------------------------------------------------------------------
135 QDataStream &operator<<(QDataStream &out, const VPieceNode &p)
136 {
137  out << p.d;
138  return out;
139 }
140 
141 //---------------------------------------------------------------------------------------------------------------------
142 QDataStream &operator>>(QDataStream &in, VPieceNode &p)
143 {
144  in >> *p.d;
145  return in;
146 }
147 
148 //---------------------------------------------------------------------------------------------------------------------
149 quint32 VPieceNode::GetId() const
150 {
151  return d->m_id;
152 }
153 
154 //---------------------------------------------------------------------------------------------------------------------
155 void VPieceNode::SetId(quint32 id)
156 {
157  d->m_id = id;
158 }
159 
160 //---------------------------------------------------------------------------------------------------------------------
162 {
163  return d->m_typeTool;
164 }
165 
166 //---------------------------------------------------------------------------------------------------------------------
168 {
169  d->m_typeTool = value;
170 }
171 
172 //---------------------------------------------------------------------------------------------------------------------
174 {
175  return d->m_reverse;
176 }
177 
178 //---------------------------------------------------------------------------------------------------------------------
179 void VPieceNode::SetReverse(bool reverse)
180 {
181  if (d->m_typeTool != Tool::NodePoint)
182  {
183  d->m_reverse = reverse;
184  }
185 }
186 
187 //---------------------------------------------------------------------------------------------------------------------
188 qreal VPieceNode::GetSABefore(const VContainer *data) const
189 {
190  if (d->m_beforeWidthFormula == currentSeamAllowance)
191  {
192  return -1;
193  }
194 
195  return EvalFormula(data, d->m_beforeWidthFormula);
196 }
197 
198 //---------------------------------------------------------------------------------------------------------------------
199 qreal VPieceNode::GetSABefore(const VContainer *data, Unit unit) const
200 {
201  if (d->m_beforeWidthFormula == currentSeamAllowance)
202  {
203  return -1;
204  }
205 
206  qreal value = EvalFormula(data, d->m_beforeWidthFormula);
207  if (value >= 0)
208  {
209  value = ToPixel(value, unit);
210  }
211  return value;
212 }
213 
214 //---------------------------------------------------------------------------------------------------------------------
216 {
217  return d->m_beforeWidthFormula;
218 }
219 
220 //---------------------------------------------------------------------------------------------------------------------
221 void VPieceNode::setBeforeSAFormula(const QString &formula)
222 {
223  if (d->m_typeTool == Tool::NodePoint)
224  {
225  d->m_beforeWidthFormula = formula;
226  }
227 }
228 
229 //---------------------------------------------------------------------------------------------------------------------
230 qreal VPieceNode::GetSAAfter(const VContainer *data) const
231 {
232  if (d->m_afterWidthFormula == currentSeamAllowance)
233  {
234  return -1;
235  }
236 
237  return EvalFormula(data, d->m_afterWidthFormula);
238 }
239 
240 //---------------------------------------------------------------------------------------------------------------------
241 qreal VPieceNode::GetSAAfter(const VContainer *data, Unit unit) const
242 {
243  if (d->m_afterWidthFormula == currentSeamAllowance)
244  {
245  return -1;
246  }
247 
248  qreal value = EvalFormula(data, d->m_afterWidthFormula);
249  if (value >= 0)
250  {
251  value = ToPixel(value, unit);
252  }
253  return value;
254 }
255 
256 //---------------------------------------------------------------------------------------------------------------------
258 {
259  return d->m_afterWidthFormula;
260 }
261 
262 //---------------------------------------------------------------------------------------------------------------------
263 void VPieceNode::setAfterSAFormula(const QString &formula)
264 {
265  if (d->m_typeTool == Tool::NodePoint)
266  {
267  d->m_afterWidthFormula = formula;
268  }
269 }
270 
271 //---------------------------------------------------------------------------------------------------------------------
273 {
274  return d->m_angleType;
275 }
276 
277 //---------------------------------------------------------------------------------------------------------------------
279 {
280  if (d->m_typeTool == Tool::NodePoint)
281  {
282  d->m_angleType = type;
283  }
284 }
285 
286 //---------------------------------------------------------------------------------------------------------------------
288 {
289  return d->m_isNotch;
290 }
291 
292 //---------------------------------------------------------------------------------------------------------------------
293 void VPieceNode::setNotch(bool notch)
294 {
295  if (GetTypeTool() == Tool::NodePoint)
296  {
297  d->m_isNotch = notch;
298  }
299 }
300 
301 //---------------------------------------------------------------------------------------------------------------------
303 {
304  return d->m_isMainPathNode;
305 }
306 
307 //---------------------------------------------------------------------------------------------------------------------
309 {
310  d->m_isMainPathNode = value;
311 }
312 
313 //---------------------------------------------------------------------------------------------------------------------
315 {
316  return d->m_notchType;
317 }
318 
319 //---------------------------------------------------------------------------------------------------------------------
321 {
322  d->m_notchType = lineType;
323 }
324 
325 //---------------------------------------------------------------------------------------------------------------------
327 {
328  return d->m_notchSubType;
329 }
330 
331 //---------------------------------------------------------------------------------------------------------------------
333 {
334  d->m_notchSubType = notchSubType;
335 }
336 
337 //---------------------------------------------------------------------------------------------------------------------
339 {
340  return d->m_showNotch;
341 }
342 
343 //---------------------------------------------------------------------------------------------------------------------
344 void VPieceNode::setShowNotch(bool value)
345 {
346  d->m_showNotch = value;
347 }
348 
349 //---------------------------------------------------------------------------------------------------------------------
351 {
352  return d->m_showSeamlineNotch;
353 }
354 
355 //---------------------------------------------------------------------------------------------------------------------
357 {
358  d->m_showSeamlineNotch = value;
359 }
360 
361 
362 //---------------------------------------------------------------------------------------------------------------------
363 void VPieceNode::setNotchLength(qreal notchLength)
364 {
365  d->m_notchLength = notchLength;
366 }
367 
368 //---------------------------------------------------------------------------------------------------------------------
370 {
371  return d->m_notchLength;
372 }
373 
374 //---------------------------------------------------------------------------------------------------------------------
375 void VPieceNode::setNotchWidth(qreal notchWidth)
376 {
377  d->m_notchWidth = notchWidth;
378 }
379 
380 //---------------------------------------------------------------------------------------------------------------------
382 {
383  return d->m_notchWidth;
384 }
385 
386 //---------------------------------------------------------------------------------------------------------------------
387 void VPieceNode::setNotchAngle(qreal notchAngle)
388 {
389  d->m_notchAngle = notchAngle;
390 }
391 
392 //---------------------------------------------------------------------------------------------------------------------
394 {
395  return d->m_notchAngle;
396 }
397 
398 //---------------------------------------------------------------------------------------------------------------------
399 void VPieceNode::setNotchCount(int notchCount)
400 {
401  d->m_notchCount = notchCount;
402 }
403 
404 //---------------------------------------------------------------------------------------------------------------------
406 {
407  return d->m_notchCount;
408 }
409 
410 //---------------------------------------------------------------------------------------------------------------------
412 {
413  return d->m_excluded;
414 }
415 
416 //---------------------------------------------------------------------------------------------------------------------
417 void VPieceNode::SetExcluded(bool exclude)
418 {
419  d->m_excluded = exclude;
420 }
The Calculator class for calculation formula.
Definition: calculator.h:84
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QHash< QString, QSharedPointer< VInternalVariable > > * DataVariables() const
Definition: vcontainer.cpp:718
void setBeforeSAFormula(const QString &formula)
Definition: vpiecenode.cpp:221
void setShowNotch(bool value)
Definition: vpiecenode.cpp:344
qreal GetSABefore(const VContainer *data) const
Definition: vpiecenode.cpp:188
void setNotchCount(int notchCount)
Definition: vpiecenode.cpp:399
void SetExcluded(bool exclude)
Definition: vpiecenode.cpp:417
void setNotch(bool notch)
Definition: vpiecenode.cpp:293
Tool GetTypeTool() const
Definition: vpiecenode.cpp:161
bool showNotch() const
Definition: vpiecenode.cpp:338
quint32 GetId() const
Definition: vpiecenode.cpp:149
bool isExcluded() const
Definition: vpiecenode.cpp:411
void setNotchType(NotchType notchType)
Definition: vpiecenode.cpp:320
void setAfterSAFormula(const QString &formula)
Definition: vpiecenode.cpp:263
void SetTypeTool(Tool value)
Definition: vpiecenode.cpp:167
PieceNodeAngle GetAngleType() const
Definition: vpiecenode.cpp:272
void setNotchLength(qreal notchLength)
Definition: vpiecenode.cpp:363
NotchType getNotchType() const
Definition: vpiecenode.cpp:314
void Swap(VPieceNode &node) Q_DECL_NOTHROW
Definition: vpiecenode.cpp:100
QString GetFormulaSAAfter() const
Definition: vpiecenode.cpp:257
void setNotchWidth(qreal notchWidth)
Definition: vpiecenode.cpp:375
qreal getNotchAngle() const
Definition: vpiecenode.cpp:393
void SetMainPathNode(bool value)
Definition: vpiecenode.cpp:308
QString GetFormulaSABefore() const
Definition: vpiecenode.cpp:215
QSharedDataPointer< VPieceNodeData > d
Definition: vpiecenode.h:142
bool GetReverse() const
Definition: vpiecenode.cpp:173
void setShowSeamlineNotch(bool value)
Definition: vpiecenode.cpp:356
void SetAngleType(PieceNodeAngle type)
Definition: vpiecenode.cpp:278
qreal GetSAAfter(const VContainer *data) const
Definition: vpiecenode.cpp:230
bool IsMainPathNode() const
Definition: vpiecenode.cpp:302
qreal getNotchLength() const
Definition: vpiecenode.cpp:369
VPieceNode & operator=(const VPieceNode &node)
Definition: vpiecenode.cpp:119
bool isNotch() const
Definition: vpiecenode.cpp:287
void SetReverse(bool reverse)
Definition: vpiecenode.cpp:179
int getNotchCount() const
Definition: vpiecenode.cpp:405
void setNotchAngle(qreal notchAngle)
Definition: vpiecenode.cpp:387
NotchSubType getNotchSubType() const
Definition: vpiecenode.cpp:326
void SetId(quint32 id)
Definition: vpiecenode.cpp:155
qreal getNotchWidth() const
Definition: vpiecenode.cpp:381
bool showSeamlineNotch() const
Definition: vpiecenode.cpp:350
void setNotchSubType(NotchSubType notchSubType)
Definition: vpiecenode.cpp:332
Error class of the parser.
double ToPixel(double val, const Unit &unit)
Definition: def.cpp:231
NotchType
Definition: def.h:123
PieceNodeAngle
Definition: def.h:113
NotchSubType
Definition: def.h:137
Unit
Definition: def.h:105
Tool
Definition: def.h:161
@ NodePoint
const QString currentSeamAllowance
Definition: ifcdef.cpp:442
qreal EvalFormula(const VContainer *data, QString formula)
Definition: vpiecenode.cpp:63
QDataStream & operator>>(QDataStream &in, VPieceNode &p)
Definition: vpiecenode.cpp:142
QDataStream & operator<<(QDataStream &out, const VPieceNode &p)
Definition: vpiecenode.cpp:135