1 /************************************************************************
2 **
3 ** @file vstandardtablecell.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 "vmeasurement.h"
30
31 #include <QMap>
32 #include <QMessageLogger>
33 #include <QtDebug>
34
35 #include "../ifc/ifcdef.h"
36 #include "vvariable.h"
37 #include "vmeasurement_p.h"
38
39 //---------------------------------------------------------------------------------------------------------------------
40 /**
41 * @brief VMeasurement create measurement for multisize table
42 * @param name measurement's name
43 * @param base measurement's base value
44 */
VMeasurement(quint32 index,const QString & name,qreal baseA,qreal baseB,qreal baseC,qreal base)45 VMeasurement::VMeasurement(quint32 index, const QString &name, qreal baseA, qreal baseB, qreal baseC, qreal base)
46 :VVariable(name),
47 d(new VMeasurementData(index, baseA, baseB, baseC, base))
48 {
49 SetType(VarType::Measurement);
50 VInternalVariable::SetValue(d->shiftBase);
51 }
52
53 //---------------------------------------------------------------------------------------------------------------------
54 /**
55 * @brief VMeasurement create measurement for individual table
56 * @param name measurement's base value
57 * @param base value in base size and height
58 */
VMeasurement(VContainer * data,quint32 index,const QString & name,const qreal & base,const QString & formula,bool ok)59 VMeasurement::VMeasurement(VContainer *data, quint32 index, const QString &name, const qreal &base,
60 const QString &formula, bool ok)
61 :VVariable(name), d(new VMeasurementData(data, index, formula, ok, base))
62 {
63 SetType(VarType::Measurement);
64 VInternalVariable::SetValue(base);
65 }
66
67 //---------------------------------------------------------------------------------------------------------------------
VMeasurement(const VMeasurement & m)68 VMeasurement::VMeasurement(const VMeasurement &m)
69 :VVariable(m), d(m.d)
70 {}
71
72 //---------------------------------------------------------------------------------------------------------------------
operator =(const VMeasurement & m)73 VMeasurement &VMeasurement::operator=(const VMeasurement &m)
74 {
75 if ( &m == this )
76 {
77 return *this;
78 }
79 VVariable::operator=(m);
80 d = m.d;
81 return *this;
82 }
83
84 #ifdef Q_COMPILER_RVALUE_REFS
85 //---------------------------------------------------------------------------------------------------------------------
VMeasurement(const VMeasurement && m)86 VMeasurement::VMeasurement(const VMeasurement &&m) Q_DECL_NOTHROW
87 :VVariable(m), d(m.d)
88 {}
89
90 //---------------------------------------------------------------------------------------------------------------------
operator =(VMeasurement && m)91 VMeasurement &VMeasurement::operator=(VMeasurement &&m) Q_DECL_NOTHROW
92 {
93 VVariable::operator=(m);
94 std::swap(d, m.d);
95 return *this;
96 }
97 #endif
98
99 //---------------------------------------------------------------------------------------------------------------------
~VMeasurement()100 VMeasurement::~VMeasurement()
101 {}
102
103 //---------------------------------------------------------------------------------------------------------------------
CorrectionHash(qreal baseA,qreal baseB,qreal baseC)104 QString VMeasurement::CorrectionHash(qreal baseA, qreal baseB, qreal baseC)
105 {
106 QStringList hashBlocks{QString::number(baseA)};
107
108 if (baseB > 0)
109 {
110 hashBlocks.append(QString::number(baseB));
111 }
112
113 if (baseC > 0)
114 {
115 hashBlocks.append(QString::number(baseC));
116 }
117 return hashBlocks.join(';');
118 }
119
120 //---------------------------------------------------------------------------------------------------------------------
CalcValue() const121 qreal VMeasurement::CalcValue() const
122 {
123 if (qFuzzyIsNull(d->currentBaseA))
124 {
125 return VInternalVariable::GetValue();
126 }
127
128 // Formula for calculation gradation
129 const qreal kA = d->stepA > 0 ? (d->currentBaseA - d->baseA) / d->stepA : 0;
130 const qreal kB = d->stepB > 0 ? (d->currentBaseB - d->baseB) / d->stepB : 0;
131 const qreal kC = d->stepC > 0 ? (d->currentBaseC - d->baseC) / d->stepC : 0;
132
133 return d->shiftBase + kA * d->shiftA + kB * d->shiftB + kC * d->shiftC + Correction();
134 }
135
136 //---------------------------------------------------------------------------------------------------------------------
Correction() const137 qreal VMeasurement::Correction() const
138 {
139 const QString hash = CorrectionHash(d->currentBaseA, d->currentBaseB, d->currentBaseC);
140 if (d->corrections.contains(hash))
141 {
142 return d->corrections.value(hash);
143 }
144
145 return 0;
146 }
147
148 //---------------------------------------------------------------------------------------------------------------------
149 /**
150 * @brief GetGuiText measurement name for tooltip
151 * @return measurement name
152 */
GetGuiText() const153 QString VMeasurement::GetGuiText() const
154 {
155 return d->gui_text;
156 }
157
158 //---------------------------------------------------------------------------------------------------------------------
SetGuiText(const QString & guiText)159 void VMeasurement::SetGuiText(const QString &guiText)
160 {
161 d->gui_text = guiText;
162 }
163
164 //---------------------------------------------------------------------------------------------------------------------
GetFormula() const165 QString VMeasurement::GetFormula() const
166 {
167 return d->formula;
168 }
169
170 //---------------------------------------------------------------------------------------------------------------------
IsCustom() const171 bool VMeasurement::IsCustom() const
172 {
173 return GetName().indexOf(CustomMSign) == 0;
174 }
175
176 //---------------------------------------------------------------------------------------------------------------------
Index() const177 int VMeasurement::Index() const
178 {
179 return static_cast<int>(d->index);
180 }
181
182 //---------------------------------------------------------------------------------------------------------------------
IsFormulaOk() const183 bool VMeasurement::IsFormulaOk() const
184 {
185 return d->formulaOk;
186 }
187
188 //---------------------------------------------------------------------------------------------------------------------
IsNotUsed() const189 bool VMeasurement::IsNotUsed() const
190 {
191 return qFuzzyIsNull(d->shiftBase) && qFuzzyIsNull(d->shiftB) && qFuzzyIsNull(d->shiftA);
192 }
193
194 //---------------------------------------------------------------------------------------------------------------------
GetValue() const195 qreal VMeasurement::GetValue() const
196 {
197 return CalcValue();
198 }
199
200 //---------------------------------------------------------------------------------------------------------------------
GetValue()201 qreal *VMeasurement::GetValue()
202 {
203 VInternalVariable::SetValue(CalcValue());
204 return VInternalVariable::GetValue();
205 }
206
207 //---------------------------------------------------------------------------------------------------------------------
GetData()208 VContainer *VMeasurement::GetData()
209 {
210 return d->data.data();
211 }
212
213 //---------------------------------------------------------------------------------------------------------------------
SetBaseA(qreal base)214 void VMeasurement::SetBaseA(qreal base)
215 {
216 d->currentBaseA = base;
217 }
218
219 //---------------------------------------------------------------------------------------------------------------------
SetBaseB(qreal base)220 void VMeasurement::SetBaseB(qreal base)
221 {
222 d->currentBaseB = base;
223 }
224
225 //---------------------------------------------------------------------------------------------------------------------
SetBaseC(qreal base)226 void VMeasurement::SetBaseC(qreal base)
227 {
228 d->currentBaseC = base;
229 }
230
231 //---------------------------------------------------------------------------------------------------------------------
232 /**
233 * @brief GetBase return value in base size and height
234 * @return value
235 */
GetBase() const236 qreal VMeasurement::GetBase() const
237 {
238 return d->shiftBase;
239 }
240
241 //---------------------------------------------------------------------------------------------------------------------
SetBase(qreal value)242 void VMeasurement::SetBase(qreal value)
243 {
244 d->shiftBase = value;
245 }
246
247 //---------------------------------------------------------------------------------------------------------------------
248 /**
249 * @brief GetKheight return increment in heights
250 * @return increment
251 */
GetShiftA() const252 qreal VMeasurement::GetShiftA() const
253 {
254 return d->shiftA;
255 }
256
257 //---------------------------------------------------------------------------------------------------------------------
258 // cppcheck-suppress unusedFunction
SetShiftA(qreal value)259 void VMeasurement::SetShiftA(qreal value)
260 {
261 d->shiftA = value;
262 }
263
264 //---------------------------------------------------------------------------------------------------------------------
265 /**
266 * @brief GetKsize return increment in sizes
267 * @return increment
268 */
GetShiftB() const269 qreal VMeasurement::GetShiftB() const
270 {
271 return d->shiftB;
272 }
273
274 //---------------------------------------------------------------------------------------------------------------------
275 // cppcheck-suppress unusedFunction
SetShiftB(qreal value)276 void VMeasurement::SetShiftB(qreal value)
277 {
278 d->shiftB = value;
279 }
280
281 //---------------------------------------------------------------------------------------------------------------------
GetShiftC() const282 qreal VMeasurement::GetShiftC() const
283 {
284 return d->shiftC;
285 }
286
287 //---------------------------------------------------------------------------------------------------------------------
SetShiftC(qreal value)288 void VMeasurement::SetShiftC(qreal value)
289 {
290 d->shiftC = value;
291 }
292
293 //---------------------------------------------------------------------------------------------------------------------
GetStepA() const294 qreal VMeasurement::GetStepA() const
295 {
296 return d->shiftA;
297 }
298
299 //---------------------------------------------------------------------------------------------------------------------
SetStepA(qreal value)300 void VMeasurement::SetStepA(qreal value)
301 {
302 d->stepA = value;
303 }
304
305 //---------------------------------------------------------------------------------------------------------------------
GetStepB() const306 qreal VMeasurement::GetStepB() const
307 {
308 return d->stepB;
309 }
310
311 //---------------------------------------------------------------------------------------------------------------------
SetStepB(qreal value)312 void VMeasurement::SetStepB(qreal value)
313 {
314 d->stepB = value;
315 }
316
317 //---------------------------------------------------------------------------------------------------------------------
GetStepC() const318 qreal VMeasurement::GetStepC() const
319 {
320 return d->stepC;
321 }
322
323 //---------------------------------------------------------------------------------------------------------------------
SetStepC(qreal value)324 void VMeasurement::SetStepC(qreal value)
325 {
326 d->stepC = value;
327 }
328
329 //---------------------------------------------------------------------------------------------------------------------
IsSpecialUnits() const330 bool VMeasurement::IsSpecialUnits() const
331 {
332 return d->specialUnits;
333 }
334
335 //---------------------------------------------------------------------------------------------------------------------
SetSpecialUnits(bool special)336 void VMeasurement::SetSpecialUnits(bool special)
337 {
338 d->specialUnits = special;
339 }
340
341 //---------------------------------------------------------------------------------------------------------------------
GetDimension() const342 IMD VMeasurement::GetDimension() const
343 {
344 return d->dimension;
345 }
346
347 //---------------------------------------------------------------------------------------------------------------------
SetDimension(IMD type)348 void VMeasurement::SetDimension(IMD type)
349 {
350 d->dimension = type;
351 }
352
353 //---------------------------------------------------------------------------------------------------------------------
GetCorrection(qreal baseA,qreal baseB,qreal baseC) const354 qreal VMeasurement::GetCorrection(qreal baseA, qreal baseB, qreal baseC) const
355 {
356 return d->corrections.value(VMeasurement::CorrectionHash(baseA, baseB, baseC), 0);
357 }
358
359 //---------------------------------------------------------------------------------------------------------------------
GetCorrections() const360 QMap<QString, qreal> VMeasurement::GetCorrections() const
361 {
362 return d->corrections;
363 }
364
365 //---------------------------------------------------------------------------------------------------------------------
SetCorrections(const QMap<QString,qreal> & corrections)366 void VMeasurement::SetCorrections(const QMap<QString, qreal> &corrections)
367 {
368 d->corrections = corrections;
369 }
370