2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file vdomdocument.h
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @brief
|
|
|
|
** @copyright
|
2014-03-18 23:36:32 +01:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
|
|
|
** program that allows creating and modelling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2013-11-15 13:41:26 +01:00
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-09-18 21:16:19 +02:00
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
2013-10-27 13:36:29 +01:00
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
2013-09-18 21:16:19 +02:00
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-09-18 21:16:19 +02:00
|
|
|
|
2013-07-13 12:51:31 +02:00
|
|
|
#ifndef VDOMDOCUMENT_H
|
|
|
|
#define VDOMDOCUMENT_H
|
|
|
|
|
2014-12-10 19:33:20 +01:00
|
|
|
#include <QCoreApplication>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QDomDocument>
|
|
|
|
#include <QDomElement>
|
|
|
|
#include <QDomNode>
|
|
|
|
#include <QHash>
|
|
|
|
#include <QLatin1String>
|
|
|
|
#include <QStaticStringData>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringData>
|
|
|
|
#include <QStringDataPtr>
|
|
|
|
#include <QtGlobal>
|
2016-10-23 11:11:00 +02:00
|
|
|
#include <QLocale>
|
2019-04-05 14:42:22 +02:00
|
|
|
#include <QLoggingCategory>
|
2014-12-10 19:33:20 +01:00
|
|
|
|
2015-06-18 19:23:24 +02:00
|
|
|
#include "../ifc/ifcdef.h"
|
2015-06-18 10:45:38 +02:00
|
|
|
#include "../vmisc/def.h"
|
2016-08-06 20:42:40 +02:00
|
|
|
#include "../vmisc/diagnostic.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
class QDomElement;
|
|
|
|
class QDomNode;
|
|
|
|
template <typename T> class QVector;
|
2018-01-26 13:43:52 +01:00
|
|
|
template <typename T> class QFutureWatcher;
|
2014-11-24 14:03:34 +01:00
|
|
|
|
|
|
|
Q_DECLARE_LOGGING_CATEGORY(vXML)
|
2013-07-13 12:51:31 +02:00
|
|
|
|
2016-08-06 20:42:40 +02:00
|
|
|
QT_WARNING_PUSH
|
|
|
|
QT_WARNING_DISABLE_GCC("-Weffc++")
|
2017-05-30 17:44:16 +02:00
|
|
|
QT_WARNING_DISABLE_GCC("-Wnon-virtual-dtor")
|
2013-11-19 21:56:49 +01:00
|
|
|
|
|
|
|
/**
|
2014-03-18 23:36:32 +01:00
|
|
|
* @brief The VDomDocument class represents a Valentina document (.val file).
|
2014-05-01 13:33:40 +02:00
|
|
|
*
|
|
|
|
* A Valentina document describes the construction of a sewing pattern. The
|
2014-03-18 23:36:32 +01:00
|
|
|
* information is stored in XML format. By parsing a VDomDocument, the contained
|
|
|
|
* pattern is rendered to a Valentina graphics scene (VMainGraphicsScene).
|
|
|
|
*
|
2014-05-01 13:33:40 +02:00
|
|
|
* A sewing pattern consists of zero or more increments and one
|
2014-03-18 23:36:32 +01:00
|
|
|
* or more pattern pieces.
|
|
|
|
*
|
|
|
|
* An increment is an auxiliary variable that is calculated from regular measurement
|
2017-07-12 20:21:48 +02:00
|
|
|
* variables (that belong to the multisize measurements table). Increments are used to
|
2014-03-18 23:36:32 +01:00
|
|
|
* create a graduation schema for the sewing pattern.
|
|
|
|
*
|
2014-05-01 13:33:40 +02:00
|
|
|
* A pattern piece contains
|
2014-03-18 23:36:32 +01:00
|
|
|
* 1) auxiliary pattern construction elements (calculation),
|
2014-05-01 13:33:40 +02:00
|
|
|
* 2) pattern construction elements (modeling), and
|
2014-03-18 23:36:32 +01:00
|
|
|
* 3) special markers, e.g. seam allowances (details).
|
|
|
|
* Of these, 2) and 3) are visible in the final pattern (draw mode 'Modeling'),
|
|
|
|
* 1) is only displayed when editing (draw mode 'Calculation') the pattern.
|
2013-11-19 21:56:49 +01:00
|
|
|
*/
|
2018-01-26 13:43:52 +01:00
|
|
|
class VDomDocument : public QObject, public QDomDocument
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2018-01-26 13:43:52 +01:00
|
|
|
Q_OBJECT
|
2013-07-13 12:51:31 +02:00
|
|
|
public:
|
2017-04-18 10:46:53 +02:00
|
|
|
static const QString AttrId;
|
2017-08-15 09:44:00 +02:00
|
|
|
static const QString AttrText;
|
|
|
|
static const QString AttrBold;
|
|
|
|
static const QString AttrItalic;
|
|
|
|
static const QString AttrAlignment;
|
2017-08-15 17:10:15 +02:00
|
|
|
static const QString AttrFSIncrement;
|
2017-08-15 09:44:00 +02:00
|
|
|
|
2017-04-18 10:46:53 +02:00
|
|
|
static const QString TagVersion;
|
|
|
|
static const QString TagUnit;
|
2017-08-15 09:44:00 +02:00
|
|
|
static const QString TagLine;
|
2015-03-16 13:23:02 +01:00
|
|
|
|
2018-01-26 13:49:42 +01:00
|
|
|
explicit VDomDocument(QObject *parent = nullptr);
|
2018-06-21 13:46:55 +02:00
|
|
|
virtual ~VDomDocument();
|
2017-08-02 13:40:17 +02:00
|
|
|
QDomElement elementById(quint32 id, const QString &tagName = QString());
|
2015-07-03 16:03:19 +02:00
|
|
|
|
2014-02-25 15:02:09 +01:00
|
|
|
template <typename T>
|
2015-07-03 16:03:19 +02:00
|
|
|
void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const;
|
|
|
|
|
2019-02-11 13:15:59 +01:00
|
|
|
template <typename T>
|
2019-02-14 15:25:39 +01:00
|
|
|
void SetAttributeOrRemoveIf(QDomElement &domElement, const QString &name, const T &value,
|
|
|
|
bool removeCondition) const;
|
2019-02-11 13:15:59 +01:00
|
|
|
|
2017-01-16 10:59:02 +01:00
|
|
|
static quint32 GetParametrUInt(const QDomElement& domElement, const QString &name, const QString &defValue);
|
2019-12-29 16:13:30 +01:00
|
|
|
static int GetParametrInt(const QDomElement& domElement, const QString &name, const QString &defValue);
|
2017-01-16 10:59:02 +01:00
|
|
|
static bool GetParametrBool(const QDomElement& domElement, const QString &name, const QString &defValue);
|
2015-12-26 14:08:41 +01:00
|
|
|
|
2017-01-16 10:59:02 +01:00
|
|
|
static NodeUsage GetParametrUsage(const QDomElement& domElement, const QString &name);
|
|
|
|
static void SetParametrUsage(QDomElement& domElement, const QString &name, const NodeUsage &value);
|
2015-12-26 14:08:41 +01:00
|
|
|
|
2017-01-16 10:59:02 +01:00
|
|
|
static QString GetParametrString(const QDomElement& domElement, const QString &name,
|
|
|
|
const QString &defValue = QString());
|
2017-08-21 12:16:43 +02:00
|
|
|
static QString GetParametrEmptyString(const QDomElement& domElement, const QString &name);
|
2017-01-16 10:59:02 +01:00
|
|
|
static qreal GetParametrDouble(const QDomElement& domElement, const QString &name, const QString &defValue);
|
|
|
|
static quint32 GetParametrId(const QDomElement& domElement);
|
2014-08-08 18:07:44 +02:00
|
|
|
|
2017-04-18 10:46:53 +02:00
|
|
|
Unit MUnit() const;
|
|
|
|
|
2015-12-26 14:08:41 +01:00
|
|
|
virtual void setXMLContent(const QString &fileName);
|
2015-08-25 19:53:03 +02:00
|
|
|
static QString UnitsHelpString();
|
|
|
|
|
2019-07-10 11:42:37 +02:00
|
|
|
QDomElement CreateElementWithText(const QString& tagName, const QString& text);
|
|
|
|
|
2017-05-29 15:33:36 +02:00
|
|
|
virtual bool SaveDocument(const QString &fileName, QString &error);
|
2014-05-16 16:11:31 +02:00
|
|
|
QString Major() const;
|
|
|
|
QString Minor() const;
|
|
|
|
QString Patch() const;
|
2018-11-18 08:52:08 +01:00
|
|
|
QString GetFormatVersionStr() const;
|
|
|
|
static int GetFormatVersion(const QString &version);
|
2016-03-04 17:08:29 +01:00
|
|
|
static void RemoveAllChildren(QDomElement &domElement);
|
2014-11-10 15:49:39 +01:00
|
|
|
|
|
|
|
QDomNode ParentNodeById(const quint32 &nodeId);
|
|
|
|
QDomElement CloneNodeById(const quint32 &nodeId);
|
2019-10-30 14:25:25 +01:00
|
|
|
QDomElement NodeById(const quint32 &nodeId, const QString &tagName = QString());
|
2014-12-16 08:42:24 +01:00
|
|
|
|
2014-12-10 19:33:20 +01:00
|
|
|
static bool SafeCopy(const QString &source, const QString &destination, QString &error);
|
2014-06-13 19:02:41 +02:00
|
|
|
|
2017-08-18 06:59:47 +02:00
|
|
|
QVector<VLabelTemplateLine> GetLabelTemplate(const QDomElement &element) const;
|
|
|
|
void SetLabelTemplate(QDomElement &element, const QVector<VLabelTemplateLine> &lines);
|
|
|
|
|
2018-11-17 13:59:45 +01:00
|
|
|
void TestUniqueId() const;
|
|
|
|
|
2019-10-30 14:25:25 +01:00
|
|
|
void RefreshElementIdCache();
|
|
|
|
|
2020-01-09 17:56:37 +01:00
|
|
|
static bool Compare(const QDomElement &element1, const QDomElement &element2);
|
|
|
|
|
2014-12-10 19:33:20 +01:00
|
|
|
protected:
|
2014-12-16 08:42:24 +01:00
|
|
|
bool setTagText(const QString &tag, const QString &text);
|
2016-04-06 04:34:08 +02:00
|
|
|
bool setTagText(const QDomElement &domElement, const QString &text);
|
2014-08-08 18:07:44 +02:00
|
|
|
QString UniqueTagText(const QString &tagName, const QString &defVal = QString()) const;
|
2014-12-16 08:42:24 +01:00
|
|
|
void CollectId(const QDomElement &node, QVector<quint32> &vector)const;
|
|
|
|
|
2018-11-18 08:52:08 +01:00
|
|
|
static void ValidateVersion(const QString &version);
|
|
|
|
|
2018-05-08 10:32:27 +02:00
|
|
|
private slots:
|
2018-01-26 13:43:52 +01:00
|
|
|
void CacheRefreshed();
|
|
|
|
|
2013-07-13 12:51:31 +02:00
|
|
|
private:
|
2013-09-26 20:50:52 +02:00
|
|
|
Q_DISABLE_COPY(VDomDocument)
|
2014-06-13 19:02:41 +02:00
|
|
|
/** @brief Map used for finding element by id. */
|
2018-01-26 13:43:52 +01:00
|
|
|
QHash<quint32, QDomElement> m_elementIdCache;
|
|
|
|
QFutureWatcher<QHash<quint32, QDomElement>> *m_watcher;
|
2014-02-25 15:02:09 +01:00
|
|
|
|
2018-01-26 13:43:52 +01:00
|
|
|
static bool find(QHash<quint32, QDomElement> &cache, const QDomElement &node, quint32 id);
|
|
|
|
QHash<quint32, QDomElement> RefreshCache(const QDomElement &root) const;
|
2017-04-20 18:01:08 +02:00
|
|
|
|
|
|
|
bool SaveCanonicalXML(QIODevice *file, int indent, QString &error) const;
|
2013-07-13 12:51:31 +02:00
|
|
|
};
|
|
|
|
|
2015-07-03 16:03:19 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
/**
|
|
|
|
* @brief SetAttribute set attribute in pattern file. Replace "," by ".".
|
|
|
|
* @param domElement element in xml tree.
|
|
|
|
* @param name name of attribute.
|
|
|
|
* @param value value of attribute.
|
|
|
|
*/
|
|
|
|
inline void VDomDocument::SetAttribute(QDomElement &domElement, const QString &name, const T &value) const
|
|
|
|
{
|
2016-10-04 15:43:06 +02:00
|
|
|
// See specification for xs:decimal
|
|
|
|
const QLocale locale = QLocale::c();
|
|
|
|
domElement.setAttribute(name, locale.toString(value).remove(locale.groupSeparator()));
|
2015-07-03 16:03:19 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 19:02:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-02-25 15:02:09 +01:00
|
|
|
template <>
|
2014-06-11 16:18:50 +02:00
|
|
|
inline void VDomDocument::SetAttribute<QString>(QDomElement &domElement, const QString &name,
|
|
|
|
const QString &value) const
|
2014-02-25 15:02:09 +01:00
|
|
|
{
|
|
|
|
domElement.setAttribute(name, value);
|
|
|
|
}
|
|
|
|
|
2018-04-12 12:13:19 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
inline void VDomDocument::SetAttribute<QChar>(QDomElement &domElement, const QString &name, const QChar &value) const
|
|
|
|
{
|
|
|
|
domElement.setAttribute(name, value);
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:07:44 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
inline void VDomDocument::SetAttribute<bool>(QDomElement &domElement, const QString &name, const bool &value) const
|
|
|
|
{
|
2016-10-04 15:44:11 +02:00
|
|
|
domElement.setAttribute(name, value ? trueStr : falseStr);
|
2014-08-08 18:07:44 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 19:02:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-03-16 07:32:52 +01:00
|
|
|
template <>
|
2014-06-12 09:22:29 +02:00
|
|
|
inline void VDomDocument::SetAttribute<MeasurementsType>(QDomElement &domElement, const QString &name,
|
2018-04-12 12:13:19 +02:00
|
|
|
const MeasurementsType &value) const
|
2014-03-16 07:32:52 +01:00
|
|
|
{
|
2017-07-12 20:21:48 +02:00
|
|
|
domElement.setAttribute(name, value == MeasurementsType::Multisize ? QStringLiteral("multisize") :
|
2016-07-20 17:59:35 +02:00
|
|
|
QStringLiteral("individual"));
|
2014-03-16 07:32:52 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 13:15:59 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
inline void VDomDocument::SetAttributeOrRemoveIf(QDomElement &domElement, const QString &name, const T &value,
|
2019-02-14 15:25:39 +01:00
|
|
|
bool removeCondition) const
|
2019-02-11 13:15:59 +01:00
|
|
|
{
|
2019-02-14 15:25:39 +01:00
|
|
|
not removeCondition ? SetAttribute(domElement, name, value) : domElement.removeAttribute(name);
|
2019-02-11 13:15:59 +01:00
|
|
|
}
|
|
|
|
|
2016-08-06 20:42:40 +02:00
|
|
|
QT_WARNING_POP
|
2013-08-20 12:26:02 +02:00
|
|
|
|
2013-07-13 12:51:31 +02:00
|
|
|
#endif // VDOMDOCUMENT_H
|