2024-08-16 11:39:30 +08:00
|
|
|
|
#include "operationCommand.h"
|
2024-09-02 17:56:02 +08:00
|
|
|
|
#include "graphicsItem/graphicsItemGroup.h"
|
2024-08-16 11:39:30 +08:00
|
|
|
|
#include <QGraphicsItem>
|
|
|
|
|
|
|
|
|
|
|
|
AddItemCommand::AddItemCommand(QGraphicsItem* item, QGraphicsScene* scene, QUndoCommand* parent)
|
|
|
|
|
|
: QUndoCommand(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pItem = item;
|
|
|
|
|
|
m_itemPos = item->pos();
|
|
|
|
|
|
m_pGraphicsScene = scene;
|
|
|
|
|
|
}
|
|
|
|
|
|
AddItemCommand::~AddItemCommand()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
void AddItemCommand::undo()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pGraphicsScene->removeItem(m_pItem);
|
|
|
|
|
|
m_pGraphicsScene->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
void AddItemCommand::redo()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(m_pItem->scene()) //因为添加图元后同步创建一条该指令,平且在push进入stack的时候redo会被触发一次,因此这里加判断,防止重复操作
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
m_pGraphicsScene->addItem(m_pItem);
|
|
|
|
|
|
m_pItem->setPos(m_itemPos);
|
|
|
|
|
|
m_pGraphicsScene->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeleteItemCommand::DeleteItemCommand(QGraphicsScene* scene, QUndoCommand* parent)
|
|
|
|
|
|
: QUndoCommand(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pGraphicsScene = scene;
|
|
|
|
|
|
m_listItem = scene->selectedItems();
|
|
|
|
|
|
}
|
|
|
|
|
|
DeleteItemCommand::~DeleteItemCommand()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
void DeleteItemCommand::undo()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach(QGraphicsItem* item, m_listItem)
|
|
|
|
|
|
{
|
2024-09-02 17:56:02 +08:00
|
|
|
|
// QGraphicsItemGroup* group = dynamic_cast<QGraphicsItemGroup*>(item->parentItem());
|
|
|
|
|
|
// if(!group)
|
2024-08-16 11:39:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
m_pGraphicsScene->addItem(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_pGraphicsScene->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
void DeleteItemCommand::redo()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach(QGraphicsItem* item, m_listItem)
|
|
|
|
|
|
{
|
2024-09-02 17:56:02 +08:00
|
|
|
|
// QGraphicsItemGroup* group = dynamic_cast<QGraphicsItemGroup*>(item->parentItem());
|
|
|
|
|
|
// if(!group)
|
2024-08-16 11:39:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
m_pGraphicsScene->removeItem(item); //remove即可,不要delete,因为会影响撤回(undo)操作
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_pGraphicsScene->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-02 17:56:02 +08:00
|
|
|
|
CreateItemGoupCommand::CreateItemGoupCommand(GraphicsItemGroup* group, QGraphicsScene* scene, QUndoCommand* parent)
|
|
|
|
|
|
: QUndoCommand(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pGroup = group;
|
|
|
|
|
|
m_pGraphicsScene = scene;
|
|
|
|
|
|
m_listItem = group->getItems();
|
|
|
|
|
|
}
|
|
|
|
|
|
CreateItemGoupCommand::~CreateItemGoupCommand()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
void CreateItemGoupCommand::undo()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pGroup->setSelected(false);
|
|
|
|
|
|
foreach (QGraphicsItem *item, m_listItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
//item->setSelected(true);
|
|
|
|
|
|
m_pGroup->removeFromGroup(item);
|
|
|
|
|
|
AbstractShape *ab = qgraphicsitem_cast<AbstractShape*>(item);
|
|
|
|
|
|
ab->updateCoordinate();
|
|
|
|
|
|
ab->setSelected(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
m_pGraphicsScene->removeItem(m_pGroup);
|
|
|
|
|
|
m_pGraphicsScene->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
void CreateItemGoupCommand::redo()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!m_pGroup->scene()) //因为添加图元后同步创建一条该指令,平且在push进入stack的时候redo会被触发一次,因此这里加判断,防止重复操作
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (QGraphicsItem *item, m_listItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
item->setSelected(false);
|
|
|
|
|
|
m_pGroup->addToGroup(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
m_pGraphicsScene->addItem(m_pGroup);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_pGroup->setSelected(true);
|
|
|
|
|
|
m_pGraphicsScene->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DestroyItemGoupCommand::DestroyItemGoupCommand(GraphicsItemGroup* group, QGraphicsScene* scene, QUndoCommand* parent)
|
|
|
|
|
|
: QUndoCommand(parent)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pGroup = group;
|
|
|
|
|
|
m_pGraphicsScene = scene;
|
|
|
|
|
|
m_listItem = group->getItems();
|
|
|
|
|
|
}
|
|
|
|
|
|
DestroyItemGoupCommand::~DestroyItemGoupCommand()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
void DestroyItemGoupCommand::undo()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (QGraphicsItem *item, m_listItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
item->setSelected(false);
|
|
|
|
|
|
m_pGroup->addToGroup(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
m_pGroup->setSelected(true);
|
|
|
|
|
|
m_pGraphicsScene->addItem(m_pGroup);
|
|
|
|
|
|
m_pGraphicsScene->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
void DestroyItemGoupCommand::redo()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pGroup->setSelected(false);
|
|
|
|
|
|
foreach (QGraphicsItem *item, m_listItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
//item->setSelected(true);
|
|
|
|
|
|
m_pGroup->removeFromGroup(item);
|
|
|
|
|
|
AbstractShape *ab = qgraphicsitem_cast<AbstractShape*>(item);
|
|
|
|
|
|
ab->updateCoordinate();
|
|
|
|
|
|
ab->setSelected(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
m_pGraphicsScene->removeItem(m_pGroup);
|
|
|
|
|
|
m_pGraphicsScene->update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|