#include <QGraphicsItem>#include "cscenecommand.h"#include "cinterfacetag.h"class CAppendItemCommand : public CSceneCommand{ CInterfaceTag fitemTag; QPointF fpos;public: CAppendItemCommand(QUndoCommand * parent = 0); void setTag(const CInterfaceTag & tag); void setPos(const QPointF & pos); void undo(); void redo();};
#include "cappenditemcommand.h"CAppendItemCommand::CAppendItemCommand(QUndoCommand *parent): CSceneCommand(parent){}void CAppendItemCommand::setTag(const CInterfaceTag &tag){ fitemTag = tag;}void CAppendItemCommand::setPos(const QPointF &pos){ fpos = pos;}void CAppendItemCommand::undo(){ if(scene()) { scene()->remove_selected(items()); }}void CAppendItemCommand::redo(){ if(scene()) { scene()->addRailItem(fitemTag,fpos); }}
void QExGraphicsScene::addRailItem(const CInterfaceTag &itemClass, const QPointF &position){ QRailItemFactory * provider = frailItemProviderHash.value(itemClass,NULL); if(provider) { QRailItem * addedItem = provider->createRailItem(position); CAppendItemCommand * appendCommand = new CAppendItemCommand; appendCommand->setScene(this); appendCommand->setItem(addedItem); appendCommand->setPos(position); appendCommand->setTag(itemClass); fundoStack->push(appendCommand); }}
void QExGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event){fbeginPos = event->scenePos();}void QExGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){ if(fbeginPos!=event->pos()) { CMoveItemCommand * moveCommand = new CMoveItemCommand; moveCommand->setScene(this); moveCommand->setItems(selectedItems()); moveCommand->setBeginPos(fbeginPos); moveCommand->setEndPos(event->pos()); fundoStack->push(moveCommand); }}
void CMoveItemCommand::setBeginPos(const QPointF &pos){ fbeginPos = pos;}void CMoveItemCommand::setEndPos(const QPointF &pos){ fendPos = pos;}void CMoveItemCommand::undo(){ if(scene()) { QPointF delta = fendPos-fbeginPos; foreach (QGraphicsItem * item, items()) { item->moveBy(delta.x(),delta.y()); } }}void CMoveItemCommand::redo(){ if(scene()) { QPointF delta = fbeginPos-fendPos; foreach (QGraphicsItem * item, items()) { item->moveBy(delta.x(),delta.y()); } }}