原工程:tools/undoframework.pro
//==========================mainwindow.h
//add the slot into mainwindow.h
private slots:
//Jingz添加 void hideItem(); // void deleteItem();//add the action into mainwindow.h
private:
void createActions(); void createMenus(); void createUndoView(); //Jingz添加 QAction *hideAction; ////==========================mainwindow.cpp
//! [2] initialize the action void MainWindow::createActions() { deleteAction = new QAction(tr("&Delete Item"), this); deleteAction->setShortcut(tr("Del")); connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem())); //! [2] //! [3] -------------------------------------------------------------------------- Jingz hideAction = new QAction(tr("&Hide Item"),this); hideAction->setShortcut(tr("Ctrl+H")); connect(hideAction,SIGNAL(triggered()),this,SLOT(hideItem())); //! [3] //! [4]
.............
undoAction = undoStack->createUndoAction(this, tr("&Undo"));
undoAction->setShortcuts(QKeySequence::Undo); redoAction = undoStack->createRedoAction(this, tr("&Redo")); redoAction->setShortcuts(QKeySequence::Redo); ...............
void MainWindow::createMenus()
{ //! [6] fileMenu = menuBar()->addMenu(tr("&File")); fileMenu->addAction(exitAction); //! [7] editMenu = menuBar()->addMenu(tr("&Edit")); editMenu->addAction(undoAction); editMenu->addAction(redoAction); editMenu->addSeparator(); editMenu->addAction(deleteAction); editMenu->addAction(hideAction);//---------------------------------Jingz add the action into menu..............................
//! [16] ================Jingz realize hidItem void MainWindow::hideItem() { if (diagramScene->selectedItems().isEmpty()) return; QUndoCommand *hideCommand = new HideCommand(diagramScene);
//operation to change the item status and keep previous diagramScene in the undoStack
undoStack->push(hideCommand);//把压制的代码解决就行啦。 } //! [16]
//! [3] ================================================commands.h Jingz class HideCommand : public QUndoCommand { public: HideCommand(QGraphicsScene *graphicsScene, QUndoCommand *parent = 0); ~HideCommand(); void undo(); void redo(); private: DiagramItem *myDiagramItem; QGraphicsScene *myGraphicsScene; //QList <int> hideGraphics; //int undoIndex; }; //! [3]
//! [10]=================================================commands.cpp Jingz HideCommand::HideCommand(QGraphicsScene *scene,QUndoCommand *parent) : QUndoCommand(parent) { myGraphicsScene = scene; QList<QGraphicsItem *> list = myGraphicsScene->selectedItems(); myGraphicsScene->selectedItems().first()->setVisible(false); list.first()->setSelected(false); myDiagramItem = static_cast<DiagramItem *>(list.first()); //QList<QGraphicsItem *> list = myGraphicsScene->selectedItems(); //list.first()->setSelected(false); //myDiagramItem = static_cast<DiagramItem *>(list.first()); //setText(QObject::tr("Delete %1") // .arg(createCommandString(myDiagramItem, myDiagramItem->pos()))); //if (hideGraphics.count()>undoIndex)//当出现undo后做新修改,覆盖后面的redo记录 //{ // hideGraphics.pop_back(); //} //for (int i = 0;i < myGraphicsScene->items().count();i++ ) //{ // myDiagramItem = static_cast<DiagramItem *>(myGraphicsScene->items().value(i)); // if (!myGraphicsScene->items().value(i)->isVisible()) // { // myGraphicsScene->items().value(i)->show(); // setText(QObject::tr("Show %1").arg(createCommandString(myDiagramItem, myDiagramItem->pos()))); // } // //} // QList<QGraphicsItem *> list = myGraphicsScene->selectedItems(); /*for (int i = 0;i < myGraphicsScene->selectedItems().count();i++) { myGraphicsScene->selectedItems().value(i)->hide(); myDiagramItem = static_cast<DiagramItem *>(myGraphicsScene->selectedItems().value(i)); myGraphicsScene->selectedItems().value(i)->setSelected(false); hideGraphics.append(i); undoIndex = hideGraphics.count(); setText(QObject::tr("Hide %1").arg(createCommandString(myDiagramItem, myDiagramItem->pos()))); }*/ // myGraphicsScene->update(); //myGraphicsScene->selectedItems().first()->setSelected(false); setText(QObject::tr("Hide %1").arg(createCommandString(myDiagramItem, myDiagramItem->pos()))); } //! [10] HideCommand::~HideCommand() { /*if (!myDiagramItem->scene()) delete myDiagramItem;*/ } //! [11] void HideCommand::undo() { /*int i = hideGraphics.at(--undoIndex);*///撤销本次操作 //if (myGraphicsScene->items().at(i)->isVisible()) //{ // myGraphicsScene->items().at(i)->hide(); //} //else //{ // myGraphicsScene->items().at(i)->show(); //}// //i = hideGraphics.at(--undoIndex);//恢复现场 //if (myGraphicsScene->items().at(i)->isVisible()) //{ // myGraphicsScene->items().at(i)->hide(); //} //else //{ // myGraphicsScene->items().at(i)->show(); //} // myGraphicsScene->items().at(i)->setVisible(false); myDiagramItem->setVisible(true); myGraphicsScene->update(); } //! [11] //! [12] void HideCommand::redo() { /*int i = hideGraphics.at(++undoIndex);*/ /*if (myGraphicsScene->items().at(i)->isVisible()) { myGraphicsScene->items().at(i)->hide(); } else { myGraphicsScene->items().at(i)->show(); } i = hideGraphics.at(++undoIndex); if (myGraphicsScene->items().at(i)->isVisible()) { myGraphicsScene->items().at(i)->hide(); } else { myGraphicsScene->items().at(i)->show(); }*/ /*myGraphicsScene->items().at(i)->setVisible(true);*/ myDiagramItem->setVisible(false); myGraphicsScene->update(); } //! [12] QString createCommandString(DiagramItem *item, const QPointF &pos) { return QObject::tr("%1 at (%2, %3)") .arg(item->diagramType() == DiagramItem::Box ? "Box" : "Triangle") .arg(pos.x()).arg(pos.y()); }