Remove tool from all visibility groups it in.

This commit is contained in:
Roman Telezhynskyi 2021-11-18 13:29:36 +02:00
parent a915ef5a0d
commit 421a8df4be
6 changed files with 145 additions and 50 deletions

View File

@ -20,6 +20,7 @@
- Increased the limit for the Letter field. - Increased the limit for the Letter field.
- Fix double call of a main path context menu. - Fix double call of a main path context menu.
- Improve storing visibility group items. Don't store duplicate information. - 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 # Valentina 0.7.49 July 1, 2021
- Fix crash. - Fix crash.

View File

@ -349,7 +349,7 @@ void VWidgetGroups::FillTable(QMap<quint32, VGroupData> groups)
item->setToolTip(tr("Categories: %1.").arg(data.tags.join(", "))); item->setToolTip(tr("Categories: %1.").arg(data.tags.join(", ")));
} }
if(doc->GroupIsEmpty(i.key())) if(data.items.isEmpty())
{ {
QFont font = item->font(); QFont font = item->font();
font.setStrikeOut(true); font.setStrikeOut(true);

View File

@ -1997,11 +1997,26 @@ QDomElement VAbstractPattern::GetDraw(const QString &name) const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QDomElement VAbstractPattern::CreateGroups() auto VAbstractPattern::CreateGroups(const QString &patternPieceName) -> QDomElement
{ {
QDomElement draw; QDomElement draw;
if (GetActivDrawElement(draw))
if (patternPieceName.isEmpty())
{ {
if (not GetActivDrawElement(draw))
{
return QDomElement();
}
}
else
{
draw = GetPPElement(patternPieceName);
if (not draw.isElement())
{
return QDomElement();
}
}
QDomElement groups = draw.firstChildElement(TagGroups); QDomElement groups = draw.firstChildElement(TagGroups);
if (groups.isNull()) if (groups.isNull())
@ -2012,14 +2027,12 @@ QDomElement VAbstractPattern::CreateGroups()
return groups; return groups;
} }
return QDomElement();
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QDomElement VAbstractPattern::CreateGroup(quint32 id, const QString &name, const QStringList &tags, auto VAbstractPattern::CreateGroup(quint32 id, const QString &name, const QStringList &tags,
const QMap<quint32, quint32> &groupData, vidtype tool) const QMap<quint32, quint32> &groupData, vidtype tool) -> QDomElement
{ {
if (id == NULL_ID || groupData.isEmpty()) if (id == NULL_ID)
{ {
return QDomElement(); return QDomElement();
} }
@ -2140,22 +2153,22 @@ QStringList VAbstractPattern::GetGroupCategories() const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QMap<quint32, VGroupData> VAbstractPattern::GetGroups() auto VAbstractPattern::GetGroups(const QString &patternPieceName) -> QMap<quint32, VGroupData>
{ {
QMap<quint32, VGroupData> data; QMap<quint32, VGroupData> data;
try try
{ {
QDomElement groups = CreateGroups(); QDomElement groups = CreateGroups(patternPieceName);
if (not groups.isNull()) if (not groups.isNull())
{ {
QDomNode domNode = groups.firstChild(); QDomNode domNode = groups.firstChild();
while (domNode.isNull() == false) while (not domNode.isNull())
{ {
if (domNode.isElement()) if (domNode.isElement())
{ {
const QDomElement group = domNode.toElement(); const QDomElement group = domNode.toElement();
if (group.isNull() == false) if (not group.isNull())
{ {
if (group.tagName() == TagGroup) if (group.tagName() == TagGroup)
{ {
@ -2164,6 +2177,25 @@ QMap<quint32, VGroupData> VAbstractPattern::GetGroups()
groupData.visible = GetParametrBool(group, AttrVisible, trueStr); groupData.visible = GetParametrBool(group, AttrVisible, trueStr);
groupData.name = GetParametrString(group, AttrName, tr("New group")); groupData.name = GetParametrString(group, AttrName, tr("New group"));
groupData.tags = FilterGroupTags(GetParametrEmptyString(group, AttrTags)); groupData.tags = FilterGroupTags(GetParametrEmptyString(group, AttrTags));
groupData.tool = GetParametrUInt(group, AttrTool, NULL_ID_STR);
QVector<QPair<quint32, quint32>> 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<quint32, quint32>(object, tool));
}
}
groupData.items = items;
data.insert(id, groupData); data.insert(id, groupData);
} }
@ -2448,27 +2480,6 @@ QDomElement VAbstractPattern::RemoveItemFromGroup(quint32 toolId, quint32 object
return QDomElement(); 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) bool VAbstractPattern::GetGroupVisibility(quint32 id)
{ {

View File

@ -83,6 +83,8 @@ struct VGroupData
QString name{}; QString name{};
bool visible{true}; bool visible{true};
QStringList tags{}; QStringList tags{};
vidtype tool{NULL_ID};
QVector<QPair<vidtype, vidtype>> items{};
}; };
QT_WARNING_POP QT_WARNING_POP
@ -212,7 +214,7 @@ public:
QDomElement GetDraw(const QString &name) const; QDomElement GetDraw(const QString &name) const;
void ParseGroups(const QDomElement &domElement); void ParseGroups(const QDomElement &domElement);
QDomElement CreateGroups(); QDomElement CreateGroups(const QString &patternPieceName = QString());
QDomElement CreateGroup(quint32 id, const QString &name, const QStringList &tags, QDomElement CreateGroup(quint32 id, const QString &name, const QStringList &tags,
const QMap<quint32, quint32> &groupData, vidtype tool=null_id); const QMap<quint32, quint32> &groupData, vidtype tool=null_id);
vidtype GroupLinkedToTool(vidtype toolId) const; vidtype GroupLinkedToTool(vidtype toolId) const;
@ -225,11 +227,10 @@ public:
QStringList GetGroupCategories() const; QStringList GetGroupCategories() const;
QMap<quint32, VGroupData> GetGroups(); QMap<quint32, VGroupData> GetGroups(const QString &patternPieceName = QString());
QMap<quint32, QString> GetGroupsContainingItem(quint32 toolId, quint32 objectId, bool containItem); QMap<quint32, QString> GetGroupsContainingItem(quint32 toolId, quint32 objectId, bool containItem);
QDomElement AddItemToGroup(quint32 toolId, quint32 objectId, quint32 groupId); QDomElement AddItemToGroup(quint32 toolId, quint32 objectId, quint32 groupId);
QDomElement RemoveItemFromGroup(quint32 toolId, quint32 objectId, quint32 groupId); QDomElement RemoveItemFromGroup(quint32 toolId, quint32 objectId, quint32 groupId);
bool GroupIsEmpty(quint32 id);
bool GetGroupVisibility(quint32 id); bool GetGroupVisibility(quint32 id);
static QStringList FilterGroupTags(const QString &tags); static QStringList FilterGroupTags(const QString &tags);

View File

@ -35,9 +35,25 @@
#include "../vmisc/vabstractvalapplication.h" #include "../vmisc/vabstractvalapplication.h"
#include "vundocommand.h" #include "vundocommand.h"
namespace
{
//---------------------------------------------------------------------------------------------------------------------
auto FixGroups(QMap<quint32, VGroupData> groups, const QMap<quint32, VGroupData> &fix) -> QMap<quint32, VGroupData>
{
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) DelTool::DelTool(VAbstractPattern *doc, quint32 id, QUndoCommand *parent)
: VUndoCommand(QDomElement(), doc, parent), parentNode(QDomNode()), siblingId(NULL_ID), : VUndoCommand(QDomElement(), doc, parent),
nameActivDraw(doc->GetNameActivPP()) nameActivDraw(doc->GetNameActivPP())
{ {
setText(tr("delete tool")); setText(tr("delete tool"));
@ -45,11 +61,35 @@ DelTool::DelTool(VAbstractPattern *doc, quint32 id, QUndoCommand *parent)
siblingId = doc->SiblingNodeId(nodeId); siblingId = doc->SiblingNodeId(nodeId);
parentNode = doc->ParentNodeById(nodeId); parentNode = doc->ParentNodeById(nodeId);
xml = doc->CloneNodeById(nodeId); xml = doc->CloneNodeById(nodeId);
QMap<quint32, VGroupData> 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<vidtype, vidtype> &item) { return item.second == id; });
if (itemRecord != groupData.items.end())
{
m_groupsBefore.insert(i.key(), groupData);
QVector<QPair<vidtype, vidtype>> cleanItems;
for (auto item : groupData.items)
{
if (item.second != id)
{
cleanItems.append(item);
}
} }
//--------------------------------------------------------------------------------------------------------------------- VGroupData cleanGroupData = groupData;
DelTool::~DelTool() cleanGroupData.items = cleanItems;
{} m_groupsAfter.insert(i.key(), cleanGroupData);
}
++i;
}
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void DelTool::undo() void DelTool::undo()
@ -57,6 +97,12 @@ void DelTool::undo()
qCDebug(vUndo, "Undo."); qCDebug(vUndo, "Undo.");
UndoDeleteAfterSibling(parentNode, siblingId); UndoDeleteAfterSibling(parentNode, siblingId);
if (not m_groupsBefore.isEmpty())
{
UpdateGroups(FixGroups(doc->GetGroups(nameActivDraw), m_groupsBefore));
}
emit NeedFullParsing(); emit NeedFullParsing();
if (VAbstractValApplication::VApp()->GetDrawMode() == Draw::Calculation) if (VAbstractValApplication::VApp()->GetDrawMode() == Draw::Calculation)
@ -76,5 +122,37 @@ void DelTool::redo()
} }
QDomElement domElement = doc->NodeById(nodeId); QDomElement domElement = doc->NodeById(nodeId);
parentNode.removeChild(domElement); parentNode.removeChild(domElement);
if (not m_groupsAfter.isEmpty())
{
UpdateGroups(FixGroups(doc->GetGroups(nameActivDraw), m_groupsAfter));
}
emit NeedFullParsing(); emit NeedFullParsing();
} }
//---------------------------------------------------------------------------------------------------------------------
void DelTool::UpdateGroups(const QMap<quint32, VGroupData> &groups) const
{
QDomElement groupsTag = doc->CreateGroups(nameActivDraw);
if (not groupsTag.isNull())
{
VDomDocument::RemoveAllChildren(groupsTag);
auto i = groups.constBegin();
while (i != groups.constEnd())
{
QMap<vidtype, vidtype> 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;
}
}
}

View File

@ -43,14 +43,18 @@ class DelTool : public VUndoCommand
Q_OBJECT Q_OBJECT
public: public:
DelTool(VAbstractPattern *doc, quint32 id, QUndoCommand *parent = nullptr); DelTool(VAbstractPattern *doc, quint32 id, QUndoCommand *parent = nullptr);
virtual ~DelTool() override; virtual ~DelTool() override =default;
virtual void undo() override; virtual void undo() override;
virtual void redo() override; virtual void redo() override;
private: private:
Q_DISABLE_COPY(DelTool) Q_DISABLE_COPY(DelTool)
QDomNode parentNode; QDomNode parentNode{};
quint32 siblingId; quint32 siblingId{NULL_ID};
const QString nameActivDraw; const QString nameActivDraw;
QMap<quint32, VGroupData> m_groupsBefore{};
QMap<quint32, VGroupData> m_groupsAfter{};
void UpdateGroups(const QMap<quint32, VGroupData> &groups) const;
}; };
#endif // DELTOOL_H #endif // DELTOOL_H