From a97821edce273b075b1e260d06a65f76920d6363 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 30 Aug 2017 08:55:07 +0300 Subject: [PATCH] Read and write pattern materials. --HG-- branch : feature --- .../dialogs/dialogpatternproperties.cpp | 29 +- .../dialogs/dialogpatternproperties.h | 3 + src/app/valentina/xml/vpattern.cpp | 5 +- src/libs/ifc/schema.qrc | 1 + src/libs/ifc/schema/pattern/v0.6.1.xsd | 949 ++++++++++++++++++ src/libs/ifc/xml/vabstractpattern.cpp | 86 +- src/libs/ifc/xml/vabstractpattern.h | 11 + src/libs/ifc/xml/vpatternconverter.cpp | 22 +- src/libs/ifc/xml/vpatternconverter.h | 3 +- 9 files changed, 1094 insertions(+), 15 deletions(-) create mode 100644 src/libs/ifc/schema/pattern/v0.6.1.xsd diff --git a/src/app/valentina/dialogs/dialogpatternproperties.cpp b/src/app/valentina/dialogs/dialogpatternproperties.cpp index 716059d51..3b7683924 100644 --- a/src/app/valentina/dialogs/dialogpatternproperties.cpp +++ b/src/app/valentina/dialogs/dialogpatternproperties.cpp @@ -65,11 +65,13 @@ DialogPatternProperties::DialogPatternProperties(VPattern *doc, VContainer *pat labelDataChanged(false), askSaveLabelData(false), templateDataChanged(false), + patternMaterialsChanged(false), deleteAction(nullptr), changeImageAction(nullptr), saveImageAction(nullptr), showImageAction(nullptr), - templateLines() + templateLines(), + patternMaterials() { ui->setupUi(this); @@ -78,6 +80,8 @@ DialogPatternProperties::DialogPatternProperties(VPattern *doc, VContainer *pat VSettings *settings = qApp->ValentinaSettings(); settings->GetOsSeparator() ? setLocale(QLocale()) : setLocale(QLocale::c()); + patternMaterials = doc->GetPatternMaterials(); + if (qApp->GetPPath().isEmpty()) { ui->lineEditPathToFile->setText(tr("")); @@ -223,6 +227,7 @@ void DialogPatternProperties::Apply() case 3: SaveLabelData(); SaveTemplateData(); + SaveMaterialData(); emit doc->UpdatePatternLabel(); break; default: @@ -239,6 +244,7 @@ void DialogPatternProperties::Ok() SaveReadOnlyState(); SaveLabelData(); SaveTemplateData(); + SaveMaterialData(); emit doc->UpdatePatternLabel(); @@ -552,7 +558,6 @@ void DialogPatternProperties::SaveDescription() doc->SetDescription(ui->plainTextEditDescription->document()->toPlainText()); descriptionChanged = false; - emit doc->patternChanged(false); } } @@ -565,7 +570,6 @@ void DialogPatternProperties::SaveGradation() doc->SetGradationSizes(sizes); emit UpdateGradation(); gradationChanged = false; - emit doc->patternChanged(false); } } @@ -603,8 +607,6 @@ void DialogPatternProperties::SaveLabelData() labelDataChanged = false; askSaveLabelData = false; - emit doc->patternChanged(false); - emit doc->UpdatePatternLabel(); } } @@ -615,8 +617,16 @@ void DialogPatternProperties::SaveTemplateData() { doc->SetPatternLabelTemplate(templateLines); templateDataChanged = false; - emit doc->patternChanged(false); - emit doc->UpdatePatternLabel(); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void DialogPatternProperties::SaveMaterialData() +{ + if (patternMaterialsChanged) + { + doc->SetPatternMaterials(patternMaterials); + patternMaterialsChanged = false; } } @@ -910,10 +920,13 @@ void DialogPatternProperties::ManagePatternMaterials() { VSettings *settings = qApp->ValentinaSettings(); - DialogPatternMaterials editor(QMap(), settings->IsRememberPatternMaterials()); + DialogPatternMaterials editor(patternMaterials, settings->IsRememberPatternMaterials()); if (QDialog::Accepted == editor.exec()) { + patternMaterials = editor.GetPatternMaterials(); + patternMaterialsChanged = true; + if (settings->IsRememberPatternMaterials()) { settings->SetKnownMaterials(editor.GetKnownMaterials()); diff --git a/src/app/valentina/dialogs/dialogpatternproperties.h b/src/app/valentina/dialogs/dialogpatternproperties.h index fc2c4cdfb..31977884f 100644 --- a/src/app/valentina/dialogs/dialogpatternproperties.h +++ b/src/app/valentina/dialogs/dialogpatternproperties.h @@ -80,12 +80,14 @@ private: bool labelDataChanged; bool askSaveLabelData; bool templateDataChanged; + bool patternMaterialsChanged; QAction *deleteAction; QAction *changeImageAction; QAction *saveImageAction; QAction *showImageAction; QVector templateLines; + QMap patternMaterials; void SetHeightsChecked(bool enabled); void SetSizesChecked(bool enabled); @@ -104,6 +106,7 @@ private: void SaveDefValues(); void SaveLabelData(); void SaveTemplateData(); + void SaveMaterialData(); void SaveReadOnlyState(); void SetDefaultHeight(const QString &def); diff --git a/src/app/valentina/xml/vpattern.cpp b/src/app/valentina/xml/vpattern.cpp index 502b46ca7..b6e19da18 100644 --- a/src/app/valentina/xml/vpattern.cpp +++ b/src/app/valentina/xml/vpattern.cpp @@ -151,7 +151,7 @@ void VPattern::Parse(const Document &parse) QStringList tags = QStringList() << TagDraw << TagIncrements << TagDescription << TagNotes << TagMeasurements << TagVersion << TagGradation << TagImage << TagUnit << TagPatternName << TagPatternNum << TagCompanyName << TagCustomerName - << TagPatternLabel; + << TagPatternLabel << TagPatternMaterials; PrepareForParse(parse); QDomNode domNode = documentElement().firstChild(); while (domNode.isNull() == false) @@ -223,6 +223,9 @@ void VPattern::Parse(const Document &parse) case 13: // TagPatternLabel qCDebug(vXML, "Pattern label."); break; + case 14: // TagPatternMaterials + qCDebug(vXML, "Pattern materials."); + break; default: qCDebug(vXML, "Wrong tag name %s", qUtf8Printable(domElement.tagName())); break; diff --git a/src/libs/ifc/schema.qrc b/src/libs/ifc/schema.qrc index 78bfb02d4..bfc5bf737 100644 --- a/src/libs/ifc/schema.qrc +++ b/src/libs/ifc/schema.qrc @@ -35,6 +35,7 @@ schema/pattern/v0.5.0.xsd schema/pattern/v0.5.1.xsd schema/pattern/v0.6.0.xsd + schema/pattern/v0.6.1.xsd schema/standard_measurements/v0.3.0.xsd schema/standard_measurements/v0.4.0.xsd schema/standard_measurements/v0.4.1.xsd diff --git a/src/libs/ifc/schema/pattern/v0.6.1.xsd b/src/libs/ifc/schema/pattern/v0.6.1.xsd new file mode 100644 index 000000000..bc1808a3e --- /dev/null +++ b/src/libs/ifc/schema/pattern/v0.6.1.xsd @@ -0,0 +1,949 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index 9042c2acc..c8626c8d0 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -86,6 +86,8 @@ const QString VAbstractPattern::TagPatternNum = QStringLiteral("patternNum const QString VAbstractPattern::TagCustomerName = QStringLiteral("customer"); const QString VAbstractPattern::TagCompanyName = QStringLiteral("company"); const QString VAbstractPattern::TagPatternLabel = QStringLiteral("patternLabel"); +const QString VAbstractPattern::TagPatternMaterials = QStringLiteral("patternMaterials"); +const QString VAbstractPattern::TagMaterial = QStringLiteral("material"); const QString VAbstractPattern::TagGrainline = QStringLiteral("grainline"); const QString VAbstractPattern::TagPath = QStringLiteral("path"); const QString VAbstractPattern::TagNodes = QStringLiteral("nodes"); @@ -121,6 +123,7 @@ const QString VAbstractPattern::AttrEnd = QStringLiteral("end"); const QString VAbstractPattern::AttrIncludeAs = QStringLiteral("includeAs"); const QString VAbstractPattern::AttrWidth = QStringLiteral("width"); const QString VAbstractPattern::AttrRotation = QStringLiteral("rotation"); +const QString VAbstractPattern::AttrNumber = QStringLiteral("number"); const QString VAbstractPattern::AttrAll = QStringLiteral("all"); @@ -195,6 +198,7 @@ const QString VAbstractPattern::NodeSplinePath = QStringLiteral("NodeSplinePath" QHash VAbstractPattern::tools = QHash(); QVector VAbstractPattern::patternLabelLines = QVector(); +QMap VAbstractPattern::patternMaterials = QMap(); bool VAbstractPattern::patternLabelWasChanged = false; namespace @@ -1404,7 +1408,7 @@ QVector VAbstractPattern::GetPatternLabelTemplate() const if (patternLabelLines.isEmpty()) { const QDomNodeList list = elementsByTagName(TagPatternLabel); - if (list.isEmpty()) + if (list.isEmpty() || list.at(0).childNodes().count() == 0) { return QVector(); } @@ -1415,6 +1419,35 @@ QVector VAbstractPattern::GetPatternLabelTemplate() const return patternLabelLines; } +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractPattern::SetPatternMaterials(const QMap &materials) +{ + QDomElement tag = CheckTagExists(TagPatternMaterials); + RemoveAllChildren(tag); + SetMaterials(tag, materials); + patternMaterials = materials; + patternLabelWasChanged = true; + modified = true; + emit patternChanged(false); +} + +//--------------------------------------------------------------------------------------------------------------------- +QMap VAbstractPattern::GetPatternMaterials() const +{ + if (patternMaterials.isEmpty()) + { + const QDomNodeList list = elementsByTagName(TagPatternMaterials); + if (list.isEmpty() || list.at(0).childNodes().count() == 0) + { + return QMap(); + } + + patternMaterials = GetMaterials(list.at(0).toElement()); + } + + return patternMaterials; +} + //--------------------------------------------------------------------------------------------------------------------- void VAbstractPattern::SetPatternWasChanged(bool changed) { @@ -1569,7 +1602,7 @@ QDomElement VAbstractPattern::CheckTagExists(const QString &tag) { const QStringList tags = QStringList() << TagUnit << TagImage << TagDescription << TagNotes << TagGradation << TagPatternName << TagPatternNum << TagCompanyName - << TagCustomerName << TagPatternLabel; + << TagCustomerName << TagPatternLabel << TagPatternMaterials; switch (tags.indexOf(tag)) { case 1: //TagImage @@ -1609,6 +1642,9 @@ QDomElement VAbstractPattern::CheckTagExists(const QString &tag) case 9: // TagPatternLabel element = createElement(TagPatternLabel); break; + case 10: // TagPatternMaterials + element = createElement(TagPatternMaterials); + break; case 0: //TagUnit (Mandatory tag) default: return QDomElement(); @@ -2018,6 +2054,52 @@ QPair > VAbstractPattern::ParseItemElement(const QD } } +//--------------------------------------------------------------------------------------------------------------------- +QMap VAbstractPattern::GetMaterials(const QDomElement &element) const +{ + QMap materials; + + if (not element.isNull()) + { + QDomElement tagMaterial = element.firstChildElement(); + while (tagMaterial.isNull() == false) + { + if (tagMaterial.tagName() == TagMaterial) + { + const int number = GetParametrUInt(tagMaterial, AttrNumber, "0"); + const QString name = GetParametrEmptyString(tagMaterial, AttrName); + + if (number > 0 && number <= userMaterialPlaceholdersQuantity) + { + materials.insert(number, name); + } + } + tagMaterial = tagMaterial.nextSiblingElement(TagMaterial); + } + } + + return materials; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractPattern::SetMaterials(QDomElement &element, const QMap &materials) +{ + if (not element.isNull()) + { + QMap::const_iterator i = materials.constBegin(); + while (i != materials.constEnd()) + { + QDomElement tagMaterial = createElement(TagMaterial); + + SetAttribute(tagMaterial, AttrNumber, i.key()); + SetAttribute(tagMaterial, AttrName, i.value()); + + element.appendChild(tagMaterial); + ++i; + } + } +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief IsModified state of the document for cases that do not cover QUndoStack. diff --git a/src/libs/ifc/xml/vabstractpattern.h b/src/libs/ifc/xml/vabstractpattern.h index c70afa71e..f337e2d75 100644 --- a/src/libs/ifc/xml/vabstractpattern.h +++ b/src/libs/ifc/xml/vabstractpattern.h @@ -160,6 +160,9 @@ public: void SetPatternLabelTemplate(const QVector &lines); QVector GetPatternLabelTemplate() const; + void SetPatternMaterials(const QMap &materials); + QMap GetPatternMaterials() const; + void SetPatternWasChanged(bool changed); bool GetPatternWasChanged() const; @@ -216,6 +219,8 @@ public: static const QString TagCompanyName; static const QString TagCustomerName; static const QString TagPatternLabel; + static const QString TagPatternMaterials; + static const QString TagMaterial; static const QString TagGrainline; static const QString TagPath; static const QString TagNodes; @@ -251,6 +256,7 @@ public: static const QString AttrIncludeAs; static const QString AttrWidth; static const QString AttrRotation; + static const QString AttrNumber; static const QString AttrAll; @@ -395,6 +401,8 @@ protected: static QHash tools; /** @brief patternLabelLines list to speed up reading a template by many pieces. */ static QVector patternLabelLines; + /** @brief patternMaterials list to speed up reading materials by many pieces. */ + static QMap patternMaterials; static bool patternLabelWasChanged; static void ToolExists(const quint32 &id); @@ -431,6 +439,9 @@ private: QPair > ParseItemElement(const QDomElement &domElement); + QMap GetMaterials(const QDomElement &element) const; + void SetMaterials(QDomElement &element, const QMap &materials); + }; #endif // VABSTRACTPATTERN_H diff --git a/src/libs/ifc/xml/vpatternconverter.cpp b/src/libs/ifc/xml/vpatternconverter.cpp index 1b8cc5f24..e948d8de7 100644 --- a/src/libs/ifc/xml/vpatternconverter.cpp +++ b/src/libs/ifc/xml/vpatternconverter.cpp @@ -58,8 +58,8 @@ class QDomElement; */ const QString VPatternConverter::PatternMinVerStr = QStringLiteral("0.1.0"); -const QString VPatternConverter::PatternMaxVerStr = QStringLiteral("0.6.0"); -const QString VPatternConverter::CurrentSchema = QStringLiteral("://schema/pattern/v0.6.0.xsd"); +const QString VPatternConverter::PatternMaxVerStr = QStringLiteral("0.6.1"); +const QString VPatternConverter::CurrentSchema = QStringLiteral("://schema/pattern/v0.6.1.xsd"); //VPatternConverter::PatternMinVer; // <== DON'T FORGET TO UPDATE TOO!!!! //VPatternConverter::PatternMaxVer; // <== DON'T FORGET TO UPDATE TOO!!!! @@ -241,6 +241,8 @@ QString VPatternConverter::XSDSchema(int ver) const case (0x000501): return QStringLiteral("://schema/pattern/v0.5.1.xsd"); case (0x000600): + return QStringLiteral("://schema/pattern/v0.6.0.xsd"); + case (0x000601): return CurrentSchema; default: InvalidVersion(ver); @@ -391,6 +393,10 @@ void VPatternConverter::ApplyPatches() ValidateXML(XSDSchema(0x000600), m_convertedFileName); V_FALLTHROUGH case (0x000600): + ToV0_6_1(); + ValidateXML(XSDSchema(0x000601), m_convertedFileName); + V_FALLTHROUGH + case (0x000601): break; default: InvalidVersion(m_ver); @@ -409,7 +415,7 @@ void VPatternConverter::DowngradeToCurrentMaxVersion() bool VPatternConverter::IsReadOnly() const { // Check if attribute readOnly was not changed in file format - Q_STATIC_ASSERT_X(VPatternConverter::PatternMaxVer == CONVERTER_VERSION_CHECK(0, 6, 0), + Q_STATIC_ASSERT_X(VPatternConverter::PatternMaxVer == CONVERTER_VERSION_CHECK(0, 6, 1), "Check attribute readOnly."); // Possibly in future attribute readOnly will change position etc. @@ -812,6 +818,16 @@ void VPatternConverter::ToV0_6_0() Save(); } +//--------------------------------------------------------------------------------------------------------------------- +void VPatternConverter::ToV0_6_1() +{ + // TODO. Delete if minimal supported version is 0.6.1 + Q_STATIC_ASSERT_X(VPatternConverter::PatternMinVer < CONVERTER_VERSION_CHECK(0, 6, 1), + "Time to refactor the code."); + SetVersion(QStringLiteral("0.6.1")); + Save(); +} + //--------------------------------------------------------------------------------------------------------------------- void VPatternConverter::TagUnitToV0_2_0() { diff --git a/src/libs/ifc/xml/vpatternconverter.h b/src/libs/ifc/xml/vpatternconverter.h index 0cc38b39f..63d5c99c4 100644 --- a/src/libs/ifc/xml/vpatternconverter.h +++ b/src/libs/ifc/xml/vpatternconverter.h @@ -53,7 +53,7 @@ public: static const QString PatternMaxVerStr; static const QString CurrentSchema; static Q_DECL_CONSTEXPR const int PatternMinVer = CONVERTER_VERSION_CHECK(0, 1, 0); - static Q_DECL_CONSTEXPR const int PatternMaxVer = CONVERTER_VERSION_CHECK(0, 6, 0); + static Q_DECL_CONSTEXPR const int PatternMaxVer = CONVERTER_VERSION_CHECK(0, 6, 1); protected: virtual int MinVer() const Q_DECL_OVERRIDE; @@ -106,6 +106,7 @@ private: void ToV0_5_0(); void ToV0_5_1(); void ToV0_6_0(); + void ToV0_6_1(); void TagUnitToV0_2_0(); void TagIncrementToV0_2_0();