Seamly2D
Code documentation
vcontainer.h
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 vcontainer.h
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date November 15, 2013
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) 2013-2015 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 #ifndef VCONTAINER_H
53 #define VCONTAINER_H
54 
55 #include <qcompilerdetection.h>
56 #include <QCoreApplication>
57 #include <QHash>
58 #include <QMap>
59 #include <QMessageLogger>
60 #include <QSet>
61 #include <QSharedPointer>
62 #include <QSharedData>
63 #include <QSharedDataPointer>
64 #include <QString>
65 #include <QStringList>
66 #include <QTypeInfo>
67 #include <QtGlobal>
68 #include <new>
69 
70 #include "../vmisc/def.h"
71 #include "../ifc/exception/vexceptionbadid.h"
72 #include "../ifc/ifcdef.h"
73 #include "../vgeometry/vabstractcubicbezierpath.h"
74 #include "../vgeometry/vabstractcurve.h"
75 #include "../vgeometry/vgobject.h"
76 #include "../vmisc/diagnostic.h"
77 #include "variables.h"
79 #include "vpiece.h"
80 #include "vpiecepath.h"
81 #include "vtranslatevars.h"
82 
83 class VEllipticalArc;
84 
85 QT_WARNING_PUSH
86 QT_WARNING_DISABLE_GCC("-Weffc++")
87 QT_WARNING_DISABLE_INTEL(2021)
88 QT_WARNING_DISABLE_GCC("-Wnon-virtual-dtor")
89 
90 class VContainerData : public QSharedData //-V690
91 {
92 public:
93 
94  VContainerData(const VTranslateVars *trVars, const Unit *patternUnit)
95  : gObjects(QHash<quint32, QSharedPointer<VGObject> >()),
96  variables(QHash<QString, QSharedPointer<VInternalVariable> > ()),
97  pieces(QSharedPointer<QHash<quint32, VPiece>>(new QHash<quint32, VPiece>())),
98  piecePaths(QSharedPointer<QHash<quint32, VPiecePath>>(new QHash<quint32, VPiecePath>())),
99  trVars(trVars),
100  patternUnit(patternUnit)
101  {}
102 
104  : QSharedData(data),
105  gObjects(data.gObjects),
106  variables(data.variables),
107  pieces(data.pieces),
108  piecePaths(data.piecePaths),
109  trVars(data.trVars),
110  patternUnit(data.patternUnit)
111  {}
112 
113  virtual ~VContainerData();
114 
115  /**
116  * @brief gObjects graphicals objects of pattern.
117  */
119 
120  /**
121  * @brief variables container for measurements, increments, lines lengths, lines angles, arcs lengths, curve lengths
122  */
124 
127 
130 
131 private:
132  VContainerData &operator=(const VContainerData &) Q_DECL_EQ_DELETE;
133 };
134 
136 
137 /**
138  * @brief The VContainer class container of all variables.
139  */
141 {
142  Q_DECLARE_TR_FUNCTIONS(VContainer)
143 public:
144  VContainer(const VTranslateVars *trVars, const Unit *patternUnit);
145  VContainer(const VContainer &data);
146  ~VContainer();
147 
148  VContainer &operator=(const VContainer &data);
149 #ifdef Q_COMPILER_RVALUE_REFS
150  VContainer &operator=(VContainer &&data) Q_DECL_NOTHROW;
151 #endif
152 
153  void Swap(VContainer &data) Q_DECL_NOTHROW;
154 
155  template <typename T>
156  const QSharedPointer<T> GeometricObject(const quint32 &id) const;
157  const QSharedPointer<VGObject> GetGObject(quint32 id) const;
158  static const QSharedPointer<VGObject> GetFakeGObject(quint32 id);
159  VPiece GetPiece(quint32 id) const;
160  VPiecePath GetPiecePath(quint32 id) const;
161  template <typename T>
162  QSharedPointer<T> GetVariable(QString name) const;
163  static quint32 getId();
164  static quint32 getNextId();
165  static void UpdateId(quint32 newId);
166 
167  quint32 AddGObject(VGObject *obj);
168  quint32 AddPiece(const VPiece &piece);
169  quint32 AddPiecePath(const VPiecePath &path);
170  void AddLine(const quint32 &firstPointId, const quint32 &secondPointId);
171  void AddArc(const QSharedPointer<VAbstractCurve> &arc, const quint32 &id,
172  const quint32 &parentId = NULL_ID);
173  void AddSpline(const QSharedPointer<VAbstractBezier> &curve, quint32 id, quint32 parentId = NULL_ID);
174  void AddCurveWithSegments(const QSharedPointer<VAbstractCubicBezierPath> &curve, const quint32 &id,
175  quint32 parentId = NULL_ID);
176 
177  template <typename T>
178  void AddVariable(const QString& name, T *var);
179  template <typename T>
180  void AddVariable(const QString& name, const QSharedPointer<T> &var);
181  void RemoveVariable(const QString& name);
182  void RemovePiece(quint32 id);
183 
184  template <class T>
185  void UpdateGObject(quint32 id, T* obj);
186  template <class T>
187  void UpdateGObject(quint32 id, const QSharedPointer<T> &obj);
188 
189  void UpdatePiece(quint32 id, const VPiece &piece);
190  void UpdatePiecePath(quint32 id, const VPiecePath &path);
191 
192  void Clear();
193  void ClearForFullParse();
194  void ClearGObjects();
196  void ClearVariables(const VarType &type = VarType::Unknown);
197  static void ClearUniqueNames();
198  static void ClearUniqueIncrementNames();
199 
200  static void SetSize(qreal size);
201  static void SetHeight(qreal height);
202  static qreal size();
203  static qreal *rsize();
204  static qreal height();
205  static qreal *rheight();
206 
207  void removeCustomVariable(const QString& name);
208 
210  const QHash<quint32, VPiece> *DataPieces() const;
212 
221 
222  static bool IsUnique(const QString &name);
223  static QStringList AllUniqueNames();
224 
225  const Unit *GetPatternUnit() const;
226  const VTranslateVars *GetTrVars() const;
227 
228 private:
229  /**
230  * @brief _id current id. New object will have value +1. For empty class equal 0.
231  */
232  static quint32 _id;
233  static qreal _size;
234  static qreal _height;
235  static QSet<QString> uniqueNames;
236 
237  QSharedDataPointer<VContainerData> d;
238 
239  void AddCurve(const QSharedPointer<VAbstractCurve> &curve, const quint32 &id, quint32 parentId = NULL_ID);
240 
241  template <class T>
242  uint qHash( const QSharedPointer<T> &p );
243 
244  template <typename key, typename val>
245  // cppcheck-suppress functionStatic
246  const val GetObject(const QHash<key, val> &obj, key id) const;
247 
248  template <typename T>
249  void UpdateObject(const quint32 &id, const QSharedPointer<T> &point);
250 
251  template <typename key, typename val>
252  static quint32 AddObject(QHash<key, val> &obj, val value);
253 
254  template <typename T>
255  const QMap<QString, QSharedPointer<T> > DataVar(const VarType &type) const;
256 };
257 
259 
260 /*
261 * Defintion of templated member functions of VContainer
262 */
263 
264 //---------------------------------------------------------------------------------------------------------------------
265 template <typename T>
266 const QSharedPointer<T> VContainer::GeometricObject(const quint32 &id) const
267 {
268 
269  if (id == NULL_ID)
270  {
271  throw VExceptionBadId(tr("Can't find object"), id);
272  }
273 
275  if (d->gObjects.contains(id))
276  {
277  gObj = d->gObjects.value(id);
278  }
279  else
280  {
281  throw VExceptionBadId(tr("Can't find object Id: "), id);
282  }
283  try
284  {
285  QSharedPointer<T> obj = qSharedPointerDynamicCast<T>(gObj);
286  SCASSERT(obj.isNull() == false)
287  return obj;
288  }
289  catch (const std::bad_alloc &)
290  {
291  throw VExceptionBadId(tr("Can't cast object"), id);
292  }
293 }
294 
295 
296 //---------------------------------------------------------------------------------------------------------------------
297 /**
298 * @brief GetVariable return varible by name
299 * @param name variable's name
300 * @return variable
301 */
302 template <typename T>
304 {
305  SCASSERT(name.isEmpty()==false)
306  if (d->variables.contains(name))
307  {
308  try
309  {
310  QSharedPointer<T> value = qSharedPointerDynamicCast<T>(d->variables.value(name));
311  SCASSERT(value.isNull() == false)
312  return value;
313  }
314  catch (const std::bad_alloc &)
315  {
316  throw VExceptionBadId(tr("Can't cast object"), name);
317  }
318  }
319  else
320  {
321  throw VExceptionBadId(tr("Can't find object"), name);
322  }
323 }
324 
325 //---------------------------------------------------------------------------------------------------------------------
326 template <typename T>
327 void VContainer::AddVariable(const QString& name, T *var)
328 {
329  AddVariable(name, QSharedPointer<T>(var));
330 }
331 
332 //---------------------------------------------------------------------------------------------------------------------
333 template <typename T>
334 void VContainer::AddVariable(const QString& name, const QSharedPointer<T> &var)
335 {
336  if (d->variables.contains(name))
337  {
338  if (d->variables.value(name)->GetType() == var->GetType())
339  {
340  QSharedPointer<T> v = qSharedPointerDynamicCast<T>(d->variables.value(name));
341  if (v.isNull())
342  {
343  throw VExceptionBadId(tr("Can't cast object."), name);
344  }
345  *v = *var;
346  }
347  else
348  {
349  throw VExceptionBadId(tr("Can't find object. Type mismatch."), name);
350  }
351  }
352  else
353  {
354  d->variables.insert(name, var);
355  }
356 
357  uniqueNames.insert(name);
358 }
359 
360 //---------------------------------------------------------------------------------------------------------------------
361 template <class T>
363 {
364  return qHash( p.data() );
365 }
366 
367 //---------------------------------------------------------------------------------------------------------------------
368 /**
369  * @brief UpdateGObject update GObject by id
370  * @param id id of existing GObject
371  * @param obj object
372  */
373 template <class T>
374 void VContainer::UpdateGObject(quint32 id, T* obj)
375 {
376  SCASSERT(obj != nullptr)
378 }
379 
380 //---------------------------------------------------------------------------------------------------------------------
381 template <class T>
382 void VContainer::UpdateGObject(quint32 id, const QSharedPointer<T> &obj)
383 {
384  SCASSERT(not obj.isNull())
385  UpdateObject(id, obj);
386  uniqueNames.insert(obj->name());
387 }
388 
389 //---------------------------------------------------------------------------------------------------------------------
390 /**
391  * @brief UpdateObject update object in container
392  * @param id id of existing object
393  * @param point object
394  */
395 template <typename T>
396 void VContainer::UpdateObject(const quint32 &id, const QSharedPointer<T> &point)
397 {
398  Q_ASSERT_X(id != NULL_ID, Q_FUNC_INFO, "id == 0"); //-V654 //-V712
399  SCASSERT(point.isNull() == false)
400  point->setId(id);
401  if (d->gObjects.contains(id))
402  {
403  QSharedPointer<T> obj = qSharedPointerDynamicCast<T>(d->gObjects.value(id));
404  if (obj.isNull())
405  {
406  throw VExceptionBadId(tr("Can't cast object"), id);
407  }
408  *obj = *point;
409  }
410  else
411  {
412  d->gObjects.insert(id, point);
413  }
414  UpdateId(id);
415 }
416 #endif // VCONTAINER_H
QSharedPointer< QHash< quint32, VPiecePath > > piecePaths
Definition: vcontainer.h:126
QSharedPointer< QHash< quint32, VPiece > > pieces
Definition: vcontainer.h:125
VContainerData(const VContainerData &data)
Definition: vcontainer.h:103
VContainerData & operator=(const VContainerData &) Q_DECL_EQ_DELETE
VContainerData(const VTranslateVars *trVars, const Unit *patternUnit)
Definition: vcontainer.h:94
QHash< quint32, QSharedPointer< VGObject > > gObjects
gObjects graphicals objects of pattern.
Definition: vcontainer.h:118
QHash< QString, QSharedPointer< VInternalVariable > > variables
variables container for measurements, increments, lines lengths, lines angles, arcs lengths,...
Definition: vcontainer.h:123
const VTranslateVars * trVars
Definition: vcontainer.h:128
const Unit * patternUnit
Definition: vcontainer.h:129
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QMap< QString, QSharedPointer< VArcRadius > > arcRadiusesData() const
Definition: vcontainer.cpp:573
const QMap< QString, QSharedPointer< VLineAngle > > lineAnglesData() const
Definition: vcontainer.cpp:567
const QMap< QString, QSharedPointer< VCurveAngle > > curveAnglesData() const
Definition: vcontainer.cpp:579
void AddVariable(const QString &name, T *var)
Definition: vcontainer.h:327
static qreal height()
height return height
Definition: vcontainer.cpp:690
static void SetSize(qreal size)
SetSize set value of size.
Definition: vcontainer.cpp:654
static QSet< QString > uniqueNames
Definition: vcontainer.h:235
const VTranslateVars * GetTrVars() const
Definition: vcontainer.cpp:605
const QHash< quint32, VPiece > * DataPieces() const
Definition: vcontainer.cpp:712
static quint32 _id
_id current id. New object will have value +1. For empty class equal 0.
Definition: vcontainer.h:232
const QMap< QString, QSharedPointer< VCurveCLength > > controlPointLengthsData() const
Definition: vcontainer.cpp:561
void UpdateObject(const quint32 &id, const QSharedPointer< T > &point)
UpdateObject update object in container.
Definition: vcontainer.h:396
void AddArc(const QSharedPointer< VAbstractCurve > &arc, const quint32 &id, const quint32 &parentId=null_id)
Definition: vcontainer.cpp:395
VContainer & operator=(const VContainer &data)
operator = copy constructor
Definition: vcontainer.cpp:117
static void ClearUniqueNames()
Definition: vcontainer.cpp:629
void UpdatePiece(quint32 id, const VPiece &piece)
Definition: vcontainer.cpp:510
void RemovePiece(quint32 id)
Definition: vcontainer.cpp:487
void RemoveVariable(const QString &name)
Definition: vcontainer.cpp:481
static quint32 getNextId()
getNextId generate next unique id
Definition: vcontainer.cpp:251
void AddLine(const quint32 &firstPointId, const quint32 &secondPointId)
AddLine add line to container.
Definition: vcontainer.cpp:382
static void UpdateId(quint32 newId)
UpdateId update id. If new id bigger when current save new like current.
Definition: vcontainer.cpp:269
VContainer(const VTranslateVars *trVars, const Unit *patternUnit)
VContainer create empty container.
Definition: vcontainer.cpp:107
void UpdatePiecePath(quint32 id, const VPiecePath &path)
Definition: vcontainer.cpp:518
void ClearCalculationGObjects()
Definition: vcontainer.cpp:322
static bool IsUnique(const QString &name)
Definition: vcontainer.cpp:585
void ClearVariables(const VarType &type=VarType::Unknown)
Definition: vcontainer.cpp:348
static qreal * rsize()
Definition: vcontainer.cpp:680
void Clear()
Clear clear data in container. Id will be 0.
Definition: vcontainer.cpp:281
void AddCurve(const QSharedPointer< VAbstractCurve > &curve, const quint32 &id, quint32 parentId=null_id)
Definition: vcontainer.cpp:419
void ClearGObjects()
ClearObject points, splines, arcs, spline paths will be cleared.
Definition: vcontainer.cpp:316
const QMap< QString, QSharedPointer< VCurveLength > > curveLengthsData() const
Definition: vcontainer.cpp:555
const QMap< QString, QSharedPointer< VLengthLine > > lineLengthsData() const
Definition: vcontainer.cpp:549
const QHash< QString, QSharedPointer< VInternalVariable > > * DataVariables() const
Definition: vcontainer.cpp:718
static qreal size()
size return size
Definition: vcontainer.cpp:674
static quint32 getId()
Definition: vcontainer.cpp:241
static quint32 AddObject(QHash< key, val > &obj, val value)
AddObject add object to container.
Definition: vcontainer.cpp:500
const QSharedPointer< VGObject > GetGObject(quint32 id) const
GetGObject returns a point by id.
Definition: vcontainer.cpp:150
static qreal _size
Definition: vcontainer.h:233
quint32 AddPiece(const VPiece &piece)
Definition: vcontainer.cpp:225
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
const val GetObject(const QHash< key, val > &obj, key id) const
GetObject return object from container.
Definition: vcontainer.cpp:172
void AddSpline(const QSharedPointer< VAbstractBezier > &curve, quint32 id, quint32 parentId=null_id)
Definition: vcontainer.cpp:440
static qreal * rheight()
Definition: vcontainer.cpp:696
static QStringList AllUniqueNames()
Definition: vcontainer.cpp:591
const QMap< QString, QSharedPointer< VIncrement > > variablesData() const
Definition: vcontainer.cpp:543
VPiecePath GetPiecePath(quint32 id) const
Definition: vcontainer.cpp:198
const QHash< quint32, QSharedPointer< VGObject > > * DataGObjects() const
data container with datagObjects return container of gObjects
Definition: vcontainer.cpp:706
void AddCurveWithSegments(const QSharedPointer< VAbstractCubicBezierPath > &curve, const quint32 &id, quint32 parentId=null_id)
Definition: vcontainer.cpp:452
uint qHash(const QSharedPointer< T > &p)
Definition: vcontainer.h:362
QSharedPointer< T > GetVariable(QString name) const
GetVariable return varible by name.
Definition: vcontainer.h:303
quint32 AddGObject(VGObject *obj)
AddGObject add new GObject to container.
Definition: vcontainer.cpp:216
QSharedDataPointer< VContainerData > d
Definition: vcontainer.h:237
const Unit * GetPatternUnit() const
Definition: vcontainer.cpp:599
static void SetHeight(qreal height)
SetGrowth set value of growth.
Definition: vcontainer.cpp:664
static const QSharedPointer< VGObject > GetFakeGObject(quint32 id)
Definition: vcontainer.cpp:156
void ClearForFullParse()
Definition: vcontainer.cpp:294
void Swap(VContainer &data) Q_DECL_NOTHROW
Definition: vcontainer.cpp:100
quint32 AddPiecePath(const VPiecePath &path)
Definition: vcontainer.cpp:233
void removeCustomVariable(const QString &name)
removeCustomVariable remove increment by name from increment table
Definition: vcontainer.cpp:530
static void ClearUniqueIncrementNames()
Definition: vcontainer.cpp:635
const QMap< QString, QSharedPointer< VMeasurement > > DataMeasurements() const
Definition: vcontainer.cpp:537
void UpdateGObject(quint32 id, T *obj)
UpdateGObject update GObject by id.
Definition: vcontainer.h:374
static qreal _height
Definition: vcontainer.h:234
const QMap< QString, QSharedPointer< T > > DataVar(const VarType &type) const
Definition: vcontainer.cpp:612
VPiece GetPiece(quint32 id) const
Definition: vcontainer.cpp:185
The VExceptionBadId class for exception bad id.
The VGObject class keep information graphical objects.
Definition: vgobject.h:74
Definition: vpiece.h:88
#define SCASSERT(cond)
Definition: def.h:317
VarType
Definition: def.h:276
Unit
Definition: def.h:105
#define NULL_ID
Definition: ifcdef.h:76
Q_DECLARE_TYPEINFO(VContainer, Q_MOVABLE_TYPE)