From 421a8df4be800e9299df2a214dab267b32fd864b Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 18 Nov 2021 13:29:36 +0200 Subject: [PATCH] Remove tool from all visibility groups it in. --- ChangeLog.txt | 1 + src/app/valentina/dialogs/vwidgetgroups.cpp | 2 +- src/libs/ifc/xml/vabstractpattern.cpp | 87 +++++++++++--------- src/libs/ifc/xml/vabstractpattern.h | 7 +- src/libs/vtools/undocommands/deltool.cpp | 88 +++++++++++++++++++-- src/libs/vtools/undocommands/deltool.h | 10 ++- 6 files changed, 145 insertions(+), 50 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 6ccc36261..bbab9f1ed 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -20,6 +20,7 @@ - Increased the limit for the Letter field. - Fix double call of a main path context menu. - Improve storing visibility group items. Don't store duplicate information. +- Remove tool from all visibility groups it in. # Valentina 0.7.49 July 1, 2021 - Fix crash. diff --git a/src/app/valentina/dialogs/vwidgetgroups.cpp b/src/app/valentina/dialogs/vwidgetgroups.cpp index 33b9f1874..ab5fb36f5 100644 --- a/src/app/valentina/dialogs/vwidgetgroups.cpp +++ b/src/app/valentina/dialogs/vwidgetgroups.cpp @@ -349,7 +349,7 @@ void VWidgetGroups::FillTable(QMap groups) item->setToolTip(tr("Categories: %1.").arg(data.tags.join(", "))); } - if(doc->GroupIsEmpty(i.key())) + if(data.items.isEmpty()) { QFont font = item->font(); font.setStrikeOut(true); diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index 2b6f700a6..b1d87c194 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -1997,29 +1997,42 @@ QDomElement VAbstractPattern::GetDraw(const QString &name) const } //--------------------------------------------------------------------------------------------------------------------- -QDomElement VAbstractPattern::CreateGroups() +auto VAbstractPattern::CreateGroups(const QString &patternPieceName) -> QDomElement { QDomElement draw; - if (GetActivDrawElement(draw)) + + if (patternPieceName.isEmpty()) { - QDomElement groups = draw.firstChildElement(TagGroups); - - if (groups.isNull()) + if (not GetActivDrawElement(draw)) { - groups = createElement(TagGroups); - draw.appendChild(groups); + return QDomElement(); } - - return groups; } - return QDomElement(); + else + { + draw = GetPPElement(patternPieceName); + if (not draw.isElement()) + { + return QDomElement(); + } + } + + QDomElement groups = draw.firstChildElement(TagGroups); + + if (groups.isNull()) + { + groups = createElement(TagGroups); + draw.appendChild(groups); + } + + return groups; } //--------------------------------------------------------------------------------------------------------------------- -QDomElement VAbstractPattern::CreateGroup(quint32 id, const QString &name, const QStringList &tags, - const QMap &groupData, vidtype tool) +auto VAbstractPattern::CreateGroup(quint32 id, const QString &name, const QStringList &tags, + const QMap &groupData, vidtype tool) -> QDomElement { - if (id == NULL_ID || groupData.isEmpty()) + if (id == NULL_ID) { return QDomElement(); } @@ -2140,22 +2153,22 @@ QStringList VAbstractPattern::GetGroupCategories() const } //--------------------------------------------------------------------------------------------------------------------- -QMap VAbstractPattern::GetGroups() +auto VAbstractPattern::GetGroups(const QString &patternPieceName) -> QMap { QMap data; try { - QDomElement groups = CreateGroups(); + QDomElement groups = CreateGroups(patternPieceName); if (not groups.isNull()) { QDomNode domNode = groups.firstChild(); - while (domNode.isNull() == false) + while (not domNode.isNull()) { if (domNode.isElement()) { const QDomElement group = domNode.toElement(); - if (group.isNull() == false) + if (not group.isNull()) { if (group.tagName() == TagGroup) { @@ -2164,6 +2177,25 @@ QMap VAbstractPattern::GetGroups() groupData.visible = GetParametrBool(group, AttrVisible, trueStr); groupData.name = GetParametrString(group, AttrName, tr("New group")); groupData.tags = FilterGroupTags(GetParametrEmptyString(group, AttrTags)); + groupData.tool = GetParametrUInt(group, AttrTool, NULL_ID_STR); + + QVector> items; + + const QDomNodeList nodeList = group.childNodes(); + const qint32 num = nodeList.size(); + for (qint32 i = 0; i < num; ++i) + { + const QDomElement element = nodeList.at(i).toElement(); + if (not element.isNull() && element.tagName() == TagGroupItem) + { + const quint32 tool = GetParametrUInt(element, AttrTool, NULL_ID_STR); + const quint32 object = GetParametrUInt(element, AttrObject, QString::number(tool)); + + items.append(QPair(object, tool)); + } + } + + groupData.items = items; data.insert(id, groupData); } @@ -2448,27 +2480,6 @@ QDomElement VAbstractPattern::RemoveItemFromGroup(quint32 toolId, quint32 object return QDomElement(); } -//--------------------------------------------------------------------------------------------------------------------- -/** - * @brief Returns true if the given group is empty - * @param id group id - * @return true if the given group is empty - */ -bool VAbstractPattern::GroupIsEmpty(quint32 id) -{ - QDomElement group = elementById(id, TagGroup); - - if (group.isNull() == false) - { - return not group.hasChildNodes(); - } - else - { - qDebug() << "The group of id " << id << " doesn't exist"; - return true; - } -} - //--------------------------------------------------------------------------------------------------------------------- bool VAbstractPattern::GetGroupVisibility(quint32 id) { diff --git a/src/libs/ifc/xml/vabstractpattern.h b/src/libs/ifc/xml/vabstractpattern.h index 3ca01017a..f349ed0f8 100644 --- a/src/libs/ifc/xml/vabstractpattern.h +++ b/src/libs/ifc/xml/vabstractpattern.h @@ -83,6 +83,8 @@ struct VGroupData QString name{}; bool visible{true}; QStringList tags{}; + vidtype tool{NULL_ID}; + QVector> items{}; }; QT_WARNING_POP @@ -212,7 +214,7 @@ public: QDomElement GetDraw(const QString &name) const; void ParseGroups(const QDomElement &domElement); - QDomElement CreateGroups(); + QDomElement CreateGroups(const QString &patternPieceName = QString()); QDomElement CreateGroup(quint32 id, const QString &name, const QStringList &tags, const QMap &groupData, vidtype tool=null_id); vidtype GroupLinkedToTool(vidtype toolId) const; @@ -225,11 +227,10 @@ public: QStringList GetGroupCategories() const; - QMap GetGroups(); + QMap GetGroups(const QString &patternPieceName = QString()); QMap GetGroupsContainingItem(quint32 toolId, quint32 objectId, bool containItem); QDomElement AddItemToGroup(quint32 toolId, quint32 objectId, quint32 groupId); QDomElement RemoveItemFromGroup(quint32 toolId, quint32 objectId, quint32 groupId); - bool GroupIsEmpty(quint32 id); bool GetGroupVisibility(quint32 id); static QStringList FilterGroupTags(const QString &tags); diff --git a/src/libs/vtools/undocommands/deltool.cpp b/src/libs/vtools/undocommands/deltool.cpp index dd2f2229b..c22be3b72 100644 --- a/src/libs/vtools/undocommands/deltool.cpp +++ b/src/libs/vtools/undocommands/deltool.cpp @@ -35,9 +35,25 @@ #include "../vmisc/vabstractvalapplication.h" #include "vundocommand.h" +namespace +{ +//--------------------------------------------------------------------------------------------------------------------- +auto FixGroups(QMap groups, const QMap &fix) -> QMap +{ + auto i = fix.constBegin(); + while (i != fix.constEnd()) + { + groups.insert(i.key(), i.value()); + ++i; + } + + return groups; +} +} // namespace + //--------------------------------------------------------------------------------------------------------------------- DelTool::DelTool(VAbstractPattern *doc, quint32 id, QUndoCommand *parent) - : VUndoCommand(QDomElement(), doc, parent), parentNode(QDomNode()), siblingId(NULL_ID), + : VUndoCommand(QDomElement(), doc, parent), nameActivDraw(doc->GetNameActivPP()) { setText(tr("delete tool")); @@ -45,11 +61,35 @@ DelTool::DelTool(VAbstractPattern *doc, quint32 id, QUndoCommand *parent) siblingId = doc->SiblingNodeId(nodeId); parentNode = doc->ParentNodeById(nodeId); xml = doc->CloneNodeById(nodeId); -} -//--------------------------------------------------------------------------------------------------------------------- -DelTool::~DelTool() -{} + QMap groups = doc->GetGroups(nameActivDraw); + auto i = groups.constBegin(); + while (i != groups.constEnd()) + { + VGroupData groupData = i.value(); + auto *itemRecord = std::find_if(groupData.items.begin(), groupData.items.end(), + [id](const QPair &item) { return item.second == id; }); + + if (itemRecord != groupData.items.end()) + { + m_groupsBefore.insert(i.key(), groupData); + + QVector> cleanItems; + for (auto item : groupData.items) + { + if (item.second != id) + { + cleanItems.append(item); + } + } + + VGroupData cleanGroupData = groupData; + cleanGroupData.items = cleanItems; + m_groupsAfter.insert(i.key(), cleanGroupData); + } + ++i; + } +} //--------------------------------------------------------------------------------------------------------------------- void DelTool::undo() @@ -57,6 +97,12 @@ void DelTool::undo() qCDebug(vUndo, "Undo."); UndoDeleteAfterSibling(parentNode, siblingId); + + if (not m_groupsBefore.isEmpty()) + { + UpdateGroups(FixGroups(doc->GetGroups(nameActivDraw), m_groupsBefore)); + } + emit NeedFullParsing(); if (VAbstractValApplication::VApp()->GetDrawMode() == Draw::Calculation) @@ -76,5 +122,37 @@ void DelTool::redo() } QDomElement domElement = doc->NodeById(nodeId); parentNode.removeChild(domElement); + + if (not m_groupsAfter.isEmpty()) + { + UpdateGroups(FixGroups(doc->GetGroups(nameActivDraw), m_groupsAfter)); + } + emit NeedFullParsing(); } + +//--------------------------------------------------------------------------------------------------------------------- +void DelTool::UpdateGroups(const QMap &groups) const +{ + QDomElement groupsTag = doc->CreateGroups(nameActivDraw); + if (not groupsTag.isNull()) + { + VDomDocument::RemoveAllChildren(groupsTag); + + auto i = groups.constBegin(); + while (i != groups.constEnd()) + { + QMap groupMap; + for (auto record : i.value().items) + { + groupMap.insert(record.first, record.second); + } + + QDomElement group = doc->CreateGroup(i.key(), i.value().name, i.value().tags, groupMap, i.value().tool); + doc->SetAttribute(group, VAbstractPattern::AttrVisible, i.value().visible); + groupsTag.appendChild(group); + + ++i; + } + } +} diff --git a/src/libs/vtools/undocommands/deltool.h b/src/libs/vtools/undocommands/deltool.h index f1dd952af..00fc3005a 100644 --- a/src/libs/vtools/undocommands/deltool.h +++ b/src/libs/vtools/undocommands/deltool.h @@ -43,14 +43,18 @@ class DelTool : public VUndoCommand Q_OBJECT public: DelTool(VAbstractPattern *doc, quint32 id, QUndoCommand *parent = nullptr); - virtual ~DelTool() override; + virtual ~DelTool() override =default; virtual void undo() override; virtual void redo() override; private: Q_DISABLE_COPY(DelTool) - QDomNode parentNode; - quint32 siblingId; + QDomNode parentNode{}; + quint32 siblingId{NULL_ID}; const QString nameActivDraw; + QMap m_groupsBefore{}; + QMap m_groupsAfter{}; + + void UpdateGroups(const QMap &groups) const; }; #endif // DELTOOL_H