add remove item from group and add item to group in tool context menu

--HG--
branch : issue826
This commit is contained in:
Ronan Le Tiec 2018-03-24 23:42:17 +01:00
parent 30fbe09b10
commit be44126c94
3 changed files with 205 additions and 1 deletions

View File

@ -2376,6 +2376,157 @@ QMap<quint32, QPair<QString, bool> > VAbstractPattern::GetGroups()
return data; return data;
} }
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Returns the groups that contain or do not contain the item identified by the toolid and the objectid
* @param id
* @param containItem | true if the groups have to contain the given item, false if they musn't contain the item
* @return
*/
QMap<quint32, QString> VAbstractPattern::GetGroupsContainingItem(quint32 toolId, quint32 objectId, bool containItem)
{
QMap<quint32, QString> data;
if(objectId == 0)
{
objectId = toolId;
}
// TODO : order in alphabetical order
QDomElement groups = CreateGroups();
if (not groups.isNull())
{
QDomNode domNode = groups.firstChild();
while (domNode.isNull() == false) // iterate through the groups
{
if (domNode.isElement())
{
const QDomElement group = domNode.toElement();
if (group.isNull() == false)
{
if (group.tagName() == TagGroup && group.hasChildNodes())
{
bool groupHasItem = GroupHasItem(group, toolId, objectId);
if((containItem && groupHasItem) || (not containItem && not groupHasItem))
{
const quint32 groupId = GetParametrUInt(group, AttrId, "0");
const QString name = GetParametrString(group, AttrName, tr("New group"));
data.insert(groupId, name);
}
}
}
}
domNode = domNode.nextSibling();
}
}
else
{
qDebug("Can't get tag Groups.");
}
return data;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Checks if the given group has the item with the given toolId and objectId
* @param domElement
* @param toolId
* @param objectId
* @return
*/
bool VAbstractPattern::GroupHasItem(const QDomElement &groupDomElement, quint32 toolId, quint32 objectId)
{
bool result = false;
QDomNode itemNode = groupDomElement.firstChild();
while (itemNode.isNull() == false) // iterate through the items of the group
{
if (itemNode.isElement())
{
const QDomElement item = itemNode.toElement();
if (item.isNull() == false)
{
quint32 toolIdIterate= GetParametrUInt(item, AttrTool, "0");
quint32 objectIdIterate= GetParametrUInt(item, AttrObject, "0");
if(toolIdIterate == toolId && objectIdIterate == objectId)
{
result = true;
break;
}
}
}
itemNode = itemNode.nextSibling();
}
return result;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Adds an item to the given group with the given toolId and objectId
* @param toolId
* @param objectId
* @param groupId
*/
void VAbstractPattern::AddItemToGroup(quint32 toolId, quint32 objectId, quint32 groupId)
{
QDomElement group = elementById(groupId, TagGroup);
if(objectId == 0)
{
objectId = toolId;
}
QDomElement item = createElement(TagGroupItem);
item.setAttribute(AttrTool, toolId);
item.setAttribute(AttrObject, objectId);
group.appendChild(item);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Removes the item of given toolId and objectId from the group of given groupId
* @param toolId
* @param objectId
* @param groupId
*/
void VAbstractPattern::RemoveItemFromGroup(quint32 toolId, quint32 objectId, quint32 groupId)
{
QDomElement group = elementById(groupId, TagGroup);
if(objectId == 0)
{
objectId = toolId;
}
QDomNode itemNode = group.firstChild();
while (itemNode.isNull() == false) // iterate through the items of the group
{
if (itemNode.isElement())
{
const QDomElement item = itemNode.toElement();
if (item.isNull() == false)
{
quint32 toolIdIterate= GetParametrUInt(item, AttrTool, "0");
quint32 objectIdIterate= GetParametrUInt(item, AttrObject, "0");
if(toolIdIterate == toolId && objectIdIterate == objectId)
{
group.removeChild(itemNode);
break;
}
}
}
itemNode = itemNode.nextSibling();
}
// Handling when removing the last child:
// - automatically removes the group?
// - or allow a group to be empty?
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VAbstractPattern::GetGroupVisivility(quint32 id) bool VAbstractPattern::GetGroupVisivility(quint32 id)
{ {

View File

@ -195,6 +195,9 @@ public:
QString GetGroupName(quint32 id); QString GetGroupName(quint32 id);
void SetGroupName(quint32 id, const QString &name); void SetGroupName(quint32 id, const QString &name);
QMap<quint32, QPair<QString, bool> > GetGroups(); QMap<quint32, QPair<QString, bool> > GetGroups();
QMap<quint32, QString> GetGroupsContainingItem(quint32 toolId, quint32 objectId, bool containItem);
void AddItemToGroup(quint32 toolId, quint32 objectId, quint32 groupId);
void RemoveItemFromGroup(quint32 toolId, quint32 objectId, quint32 groupId);
bool GetGroupVisivility(quint32 id); bool GetGroupVisivility(quint32 id);
void SetGroupVisivility(quint32 id, bool visible); void SetGroupVisivility(quint32 id, bool visible);
@ -435,6 +438,8 @@ protected:
bool GetActivDrawElement(QDomElement &element) const; bool GetActivDrawElement(QDomElement &element) const;
QVector<VToolRecord> getLocalHistory(const QString &draw) const; QVector<VToolRecord> getLocalHistory(const QString &draw) const;
bool GroupHasItem(const QDomElement &domElement, quint32 toolId, quint32 objectId);
private: private:
Q_DISABLE_COPY(VAbstractPattern) Q_DISABLE_COPY(VAbstractPattern)

View File

@ -150,6 +150,39 @@ void VDrawTool::ContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 itemI
QMenu menu; QMenu menu;
QAction *actionOption = menu.addAction(QIcon::fromTheme("preferences-other"), tr("Options")); QAction *actionOption = menu.addAction(QIcon::fromTheme("preferences-other"), tr("Options"));
// add the menu "add to group" to the context menu
QMap<quint32,QString> groupsNotContainingItem = doc->GetGroupsContainingItem(this->getId(), itemId, false);
QActionGroup* actionsAddToGroup = new QActionGroup(this);
if(not groupsNotContainingItem.empty())
{
QMenu *menuAddToGroup = menu.addMenu(QIcon::fromTheme("list-add"), tr("Add to group"));
QMap<quint32,QString>::iterator i;
for(i = groupsNotContainingItem.begin();i != groupsNotContainingItem.end(); ++i)
{
QAction *actionAddToGroup = menuAddToGroup->addAction(i.value());
actionsAddToGroup->addAction(actionAddToGroup);
actionAddToGroup->setData(i.key());
}
}
// add the menu "remove from group" to the context menu
QMap<quint32,QString> groupsContainingItem = doc->GetGroupsContainingItem(this->getId(), itemId, true);
QActionGroup* actionsRemoveFromGroup = new QActionGroup(this);
if(not groupsContainingItem.empty())
{
QMenu *menuRemoveFromGroup = menu.addMenu(QIcon::fromTheme("list-remove"), tr("Remove from group"));
QMap<quint32,QString>::iterator i;
for(i = groupsContainingItem.begin();i != groupsContainingItem.end(); ++i)
{
QAction *actionRemoveFromGroup = menuRemoveFromGroup->addAction(i.value());
actionsRemoveFromGroup->addAction(actionRemoveFromGroup);
actionRemoveFromGroup->setData(i.key());
}
}
QAction *actionShowLabel = menu.addAction(tr("Show label")); QAction *actionShowLabel = menu.addAction(tr("Show label"));
actionShowLabel->setCheckable(true); actionShowLabel->setCheckable(true);
if (itemId != NULL_ID) if (itemId != NULL_ID)
@ -190,7 +223,12 @@ void VDrawTool::ContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 itemI
} }
QAction *selectedAction = menu.exec(event->screenPos()); QAction *selectedAction = menu.exec(event->screenPos());
if (selectedAction == actionOption)
if(selectedAction == nullptr)
{
return;
}
else if (selectedAction == actionOption)
{ {
qCDebug(vTool, "Show options."); qCDebug(vTool, "Show options.");
emit qApp->getSceneView()->itemClicked(nullptr); emit qApp->getSceneView()->itemClicked(nullptr);
@ -214,6 +252,16 @@ void VDrawTool::ContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 itemI
{ {
ChangeLabelVisibility(itemId, selectedAction->isChecked()); ChangeLabelVisibility(itemId, selectedAction->isChecked());
} }
else if (selectedAction->actionGroup() == actionsAddToGroup)
{
quint32 groupId = selectedAction->data().toUInt();
doc->AddItemToGroup(this->getId(), itemId, groupId);
}
else if (selectedAction->actionGroup() == actionsRemoveFromGroup)
{
quint32 groupId = selectedAction->data().toUInt();
doc->RemoveItemFromGroup(this->getId(), itemId, groupId);
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------