PowerDesignerX/source/operationCommand.cpp

65 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "operationCommand.h"
#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)
{
QGraphicsItemGroup* group = dynamic_cast<QGraphicsItemGroup*>(item->parentItem());
if(!group)
{
m_pGraphicsScene->addItem(item);
}
}
m_pGraphicsScene->update();
}
void DeleteItemCommand::redo()
{
foreach(QGraphicsItem* item, m_listItem)
{
QGraphicsItemGroup* group = dynamic_cast<QGraphicsItemGroup*>(item->parentItem());
if(!group)
{
m_pGraphicsScene->removeItem(item); //remove即可不要delete因为会影响撤回undo操作
}
}
m_pGraphicsScene->update();
}