Seamly2D
Code documentation
vdomdocument.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 vdomdocument.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 Seamly2D project, a pattern making
33  ** program that allows creating and modelling 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 VDOMDOCUMENT_H
53 #define VDOMDOCUMENT_H
54 
55 #include <QCoreApplication>
56 #include <QDomDocument>
57 #include <QDomElement>
58 #include <QDomNode>
59 #include <QHash>
60 #include <QLatin1String>
61 #include <QStaticStringData>
62 #include <QString>
63 #include <QStringData>
64 #include <QStringDataPtr>
65 #include <QtGlobal>
66 #include <QLocale>
67 
68 #include "../ifc/ifcdef.h"
69 #include "../vmisc/def.h"
70 #include "../vmisc/diagnostic.h"
71 #include "../vmisc/logging.h"
72 
73 class QDomElement;
74 class QDomNode;
75 template <typename T> class QVector;
76 
77 Q_DECLARE_LOGGING_CATEGORY(vXML)
78 
79 QT_WARNING_PUSH
80 QT_WARNING_DISABLE_GCC("-Weffc++")
81 QT_WARNING_DISABLE_GCC("-Wnon-virtual-dtor")
82 
83 /**
84  * @brief The VDomDocument class represents a Seamly2D document (.val file).
85  *
86  * A Seamly2D document describes the construction of a sewing pattern. The
87  * information is stored in XML format. By parsing a VDomDocument, the contained
88  * pattern is rendered to a Seamly2D graphics scene (VMainGraphicsScene).
89  *
90  * A sewing pattern consists of zero or more increments and one
91  * or more pattern pieces.
92  *
93  * An increment is an auxiliary variable that is calculated from regular measurement
94  * variables (that belong to the multisize measurements table). Increments are used to
95  * create a graduation schema for the sewing pattern.
96  *
97  * A pattern piece contains
98  * 1) auxiliary pattern construction elements (calculation),
99  * 2) pattern construction elements (modeling), and
100  * 3) special markers, e.g. seam allowances (details).
101  * Of these, 2) and 3) are visible in the final pattern (draw mode 'Modeling'),
102  * 1) is only displayed when editing (draw mode 'Calculation') the pattern.
103  */
104 class VDomDocument : public QDomDocument
105 {
106  Q_DECLARE_TR_FUNCTIONS(VDomDocument)
107 public:
108  static const QString AttrId;
109  static const QString AttrText;
110  static const QString AttrBold;
111  static const QString AttrItalic;
112  static const QString AttrAlignment;
113  static const QString AttrFSIncrement;
114 
115  static const QString TagVersion;
116  static const QString TagUnit;
117  static const QString TagLine;
118 
119  VDomDocument();
120  virtual ~VDomDocument() Q_DECL_EQ_DEFAULT;
121  QDomElement elementById(quint32 id, const QString &tagName = QString());
122 
123  template <typename T>
124  void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const;
125 
126  static quint32 GetParametrUInt(const QDomElement& domElement, const QString &name, const QString &defValue);
127  static bool getParameterBool(const QDomElement& domElement, const QString &name, const QString &defValue);
128 
129  static NodeUsage GetParametrUsage(const QDomElement& domElement, const QString &name);
130  static void SetParametrUsage(QDomElement& domElement, const QString &name, const NodeUsage &value);
131 
132  static QString GetParametrString(const QDomElement& domElement, const QString &name,
133  const QString &defValue = QString());
134  static QString GetParametrEmptyString(const QDomElement& domElement, const QString &name);
135  static qreal GetParametrDouble(const QDomElement& domElement, const QString &name, const QString &defValue);
136  static quint32 getParameterId(const QDomElement& domElement);
137 
138  Unit MUnit() const;
139 
140  static void ValidateXML(const QString &schema, const QString &fileName);
141  virtual void setXMLContent(const QString &fileName);
142  static QString UnitsHelpString();
143 
144  virtual bool SaveDocument(const QString &fileName, QString &error);
145  QString Major() const;
146  QString Minor() const;
147  QString Patch() const;
148  static void RemoveAllChildren(QDomElement &domElement);
149 
150  QDomNode ParentNodeById(const quint32 &nodeId);
151  QDomElement CloneNodeById(const quint32 &nodeId);
152  QDomElement NodeById(const quint32 &nodeId);
153 
154  static bool SafeCopy(const QString &source, const QString &destination, QString &error);
155 
156  QVector<VLabelTemplateLine> GetLabelTemplate(const QDomElement &element) const;
157  void SetLabelTemplate(QDomElement &element, const QVector<VLabelTemplateLine> &lines);
158 
159 protected:
160  bool setTagText(const QString &tag, const QString &text);
161  bool setTagText(const QDomElement &domElement, const QString &text);
162  QString UniqueTagText(const QString &tagName, const QString &defVal = QString()) const;
163 
164  void TestUniqueId() const;
165  void CollectId(const QDomElement &node, QVector<quint32> &vector)const;
166 
167 private:
168  Q_DISABLE_COPY(VDomDocument)
169  /** @brief Map used for finding element by id. */
170  QHash<quint32, QDomElement> map;
171 
172  bool find(const QDomElement &node, quint32 id);
173 
174  bool SaveCanonicalXML(QIODevice *file, int indent, QString &error) const;
175 };
176 
177 //---------------------------------------------------------------------------------------------------------------------
178 template <typename T>
179 /**
180  * @brief SetAttribute set attribute in pattern file. Replace "," by ".".
181  * @param domElement element in xml tree.
182  * @param name name of attribute.
183  * @param value value of attribute.
184  */
185 inline void VDomDocument::SetAttribute(QDomElement &domElement, const QString &name, const T &value) const
186 {
187  // See specification for xs:decimal
188  const QLocale locale = QLocale::c();
189  domElement.setAttribute(name, locale.toString(value).remove(locale.groupSeparator()));
190 }
191 
192 //---------------------------------------------------------------------------------------------------------------------
193 template <>
194 inline void VDomDocument::SetAttribute<QString>(QDomElement &domElement, const QString &name,
195  const QString &value) const
196 {
197  domElement.setAttribute(name, value);
198 }
199 
200 //---------------------------------------------------------------------------------------------------------------------
201 template <>
202 inline void VDomDocument::SetAttribute<bool>(QDomElement &domElement, const QString &name, const bool &value) const
203 {
204  domElement.setAttribute(name, value ? trueStr : falseStr);
205 }
206 
207 //---------------------------------------------------------------------------------------------------------------------
208 template <>
209 inline void VDomDocument::SetAttribute<MeasurementsType>(QDomElement &domElement, const QString &name,
210  const MeasurementsType &value) const
211 {
212  domElement.setAttribute(name, value == MeasurementsType::Multisize ? QStringLiteral("multisize") :
213  QStringLiteral("individual"));
214 }
215 
217 
218 #endif // VDOMDOCUMENT_H
The VDomDocument class represents a Seamly2D document (.val file).
Definition: vdomdocument.h:105
static const QString AttrItalic
Definition: vdomdocument.h:111
static const QString AttrBold
Definition: vdomdocument.h:110
static const QString AttrAlignment
Definition: vdomdocument.h:112
static const QString TagVersion
Definition: vdomdocument.h:115
static const QString TagUnit
Definition: vdomdocument.h:116
virtual ~VDomDocument() Q_DECL_EQ_DEFAULT
static const QString AttrId
Definition: vdomdocument.h:108
static const QString TagLine
Definition: vdomdocument.h:117
static const QString AttrText
Definition: vdomdocument.h:109
static const QString AttrFSIncrement
Definition: vdomdocument.h:113
const QString trueStr
Definition: def.cpp:197
const QString falseStr
Definition: def.cpp:198
MeasurementsType
Definition: def.h:104
Unit
Definition: def.h:105
NodeUsage
Definition: def.h:107