From 3f3968a0e4b778e56f02754f3d2043bc5d5d85c7 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 7 Jul 2011 16:03:14 +0200 Subject: [PATCH] move command is undocommand --- include/commands.h | 30 +++++++++++-- include/graphlogic.h | 15 ++++--- include/mainwindow.h | 2 +- include/node.h | 11 +++-- src/commands.cpp | 33 ++++++++++++++ src/graphlogic.cpp | 101 ++++++++++++++++++++----------------------- src/graphwidget.cpp | 1 + src/mainwindow.cpp | 6 ++- src/node.cpp | 10 +++-- 9 files changed, 137 insertions(+), 72 deletions(-) diff --git a/include/commands.h b/include/commands.h index 413773f..c7ec4ba 100644 --- a/include/commands.h +++ b/include/commands.h @@ -15,12 +15,13 @@ struct UndoContext Node *m_activeNode; Node *m_hintNode; QList *m_nodeList; - QPointF m_pos; QColor m_color; Node *m_source; Node *m_destination; bool m_secondary; + qreal m_x; + qreal m_y; UndoContext(GraphLogic *graphLogic = 0, Node *activeNode = 0, @@ -30,7 +31,9 @@ struct UndoContext QColor color = QColor(), Node *source = 0, Node *destination = 0, - bool secondary = false) + bool secondary = false, + qreal x = 0, + qreal y = 0) : m_graphLogic(graphLogic) , m_activeNode(activeNode) , m_hintNode(hintNode) @@ -39,7 +42,10 @@ struct UndoContext , m_color(color) , m_source(source) , m_destination(destination) - , m_secondary(secondary) {}; + , m_secondary(secondary) + , m_x(x) + , m_y(y) + {}; }; @@ -125,4 +131,22 @@ private: Edge *m_edge; }; +class MoveCommand : public QUndoCommand +{ + +public: + + MoveCommand(UndoContext context); + + void undo(); + void redo(); + +private: + + bool m_done; + UndoContext m_context; + + QList m_nodeList; +}; + #endif // COMMANDS_H diff --git a/include/graphlogic.h b/include/graphlogic.h index a19b672..e759c5d 100644 --- a/include/graphlogic.h +++ b/include/graphlogic.h @@ -23,7 +23,6 @@ class GraphLogic : public QObject public: explicit GraphLogic(GraphWidget *parent = 0); - GraphWidget *graphWidget() const; void setUndoStack(QUndoStack *stack); @@ -40,6 +39,10 @@ public: void setHintNode(Node *node); void reShowNumbers(); + void moveNode(qreal x, qreal y); + +// void move(const QPointF &oldpos, const QPointF &newpos); /// @todo Rewrite as an undo action + public slots: // commands from toolbars: @@ -67,12 +70,12 @@ signals: private: - void moveUp(); - void moveDown(); - void moveLeft(); - void moveRight(); + void moveNodeUp(); + void moveNodeDown(); + void moveNodeLeft(); + void moveNodeRight(); + - void move(const int &x, const int &y); /// @todo Rewrite as an undo action void setNodeColor(const QColor &color, const bool &subtree = false); /// @todo Rewrite as an undo action void setNodeTextColor(const QColor &color, const bool &subtree = false); /// @todo Rewrite as an undo action diff --git a/include/mainwindow.h b/include/mainwindow.h index d51b236..330da0b 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -47,7 +47,7 @@ public slots: // toolbars void showMainToolbar(const bool &show = true); void showStatusIconToolbar(const bool &show = true); - void showUdoToolbar(const bool &show = true); + void showUndoToolbar(const bool &show = true); // handle changed content at quit void quit(); diff --git a/include/node.h b/include/node.h index 100b589..ac25b02 100644 --- a/include/node.h +++ b/include/node.h @@ -6,9 +6,11 @@ #include #include "edge.h" -#include "graphwidget.h" +//#include "graphwidget.h" +#include "graphlogic.h" -class GraphWidget; +//class GraphWidget; +class GraphLogic; class Node : public QGraphicsTextItem { @@ -16,7 +18,7 @@ class Node : public QGraphicsTextItem public: - Node(); + Node(GraphLogic *graphLogic); ~Node(); // add/remove edges @@ -94,8 +96,9 @@ private: EdgeElement(Edge *e, bool s) : edge(e), startsFromThisNode(s) {} }; + QList m_edgeList; -// GraphWidget *m_graph; + GraphLogic *m_graphLogic; int m_number; bool m_hasBorder; bool m_numberIsSpecial; diff --git a/src/commands.cpp b/src/commands.cpp index 197bdc6..1cc770b 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -234,3 +234,36 @@ void RemoveEdgeCommand::redo() m_context.m_graphLogic->setActiveNode(m_context.m_activeNode); } + +MoveCommand::MoveCommand(UndoContext context) + : m_context(context) +{ + setText(QObject::tr("Node \"").append( + m_context.m_activeNode == m_context.m_nodeList->first() ? + QObject::tr("Base node") : + m_context.m_activeNode->toPlainText()).append("\" moved")); + + // move just the active Node or it's subtree too? + if (QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier) + { + m_nodeList = m_context.m_activeNode->subtree(); + setText(text().append(QObject::tr(" with subtree"))); + } + else + { + m_nodeList.push_back(m_context.m_activeNode); + } +} + +void MoveCommand::undo() +{ + foreach(Node *node, m_nodeList) + node->moveBy(-m_context.m_x, -m_context.m_y); +} + +void MoveCommand::redo() +{ + foreach(Node *node, m_nodeList) + node->moveBy(m_context.m_x, m_context.m_y); +} diff --git a/src/graphlogic.cpp b/src/graphlogic.cpp index 87fcc7d..1048f3c 100644 --- a/src/graphlogic.cpp +++ b/src/graphlogic.cpp @@ -38,13 +38,13 @@ GraphLogic::GraphLogic(GraphWidget *parent) (Qt::Key_F, &GraphLogic::hintMode)); m_memberMap.insert(std::pair - (Qt::Key_Up, &GraphLogic::moveUp)); + (Qt::Key_Up, &GraphLogic::moveNodeUp)); m_memberMap.insert(std::pair - (Qt::Key_Down, &GraphLogic::moveDown)); + (Qt::Key_Down, &GraphLogic::moveNodeDown)); m_memberMap.insert(std::pair - (Qt::Key_Left, &GraphLogic::moveLeft)); + (Qt::Key_Left, &GraphLogic::moveNodeLeft)); m_memberMap.insert(std::pair - (Qt::Key_Right, &GraphLogic::moveRight)); + (Qt::Key_Right, &GraphLogic::moveNodeRight)); m_memberMap.insert(std::pair (Qt::Key_Backspace, &GraphLogic::delNumber)); @@ -296,10 +296,28 @@ void GraphLogic::writeContentToPngFile(const QString &fileName) emit notification(tr("MindMap exported as ") + fileName); } -void GraphLogic::reShowNumbers() +Node * GraphLogic::nodeFactory() { - if (m_showingNodeNumbers) - showNodeNumbers(); + Node *node = new Node(this); + + connect(node, SIGNAL(nodeChanged()), this, SLOT(nodeChanged())); + connect(node, SIGNAL(nodeSelected()), this, SLOT(nodeSelected())); + connect(node, SIGNAL(nodeEdited()), this, SLOT(nodeEdited())); + connect(node, SIGNAL(nodeMoved(QGraphicsSceneMouseEvent*)), + this, SLOT(nodeMoved(QGraphicsSceneMouseEvent*))); + connect(node, SIGNAL(nodeLostFocus()), this, SLOT(nodeLostFocus())); + + return node; +} + +void GraphLogic::setActiveNode(Node *node) +{ + if (m_activeNode!=0) + m_activeNode->setBorder(false); + + m_activeNode = node; + if (m_activeNode) + m_activeNode->setBorder(); } void GraphLogic::setHintNode(Node *node) @@ -307,6 +325,12 @@ void GraphLogic::setHintNode(Node *node) m_hintNode = node; } +void GraphLogic::reShowNumbers() +{ + if (m_showingNodeNumbers) + showNodeNumbers(); +} + void GraphLogic::insertNode() { // checks @@ -571,39 +595,39 @@ void GraphLogic::nodeLostFocus() } } -void GraphLogic::moveUp() +void GraphLogic::moveNodeUp() { QApplication::keyboardModifiers() & Qt::ControlModifier ? - move(0,-20) : + moveNode(qreal(0),qreal(-20)) : m_graphWidget->verticalScrollBar()->setValue( m_graphWidget->verticalScrollBar()->value()-20); } -void GraphLogic::moveDown() +void GraphLogic::moveNodeDown() { QApplication::keyboardModifiers() & Qt::ControlModifier ? - move(0,20) : + moveNode(qreal(0),qreal(20)) : m_graphWidget->verticalScrollBar()->setValue( m_graphWidget->verticalScrollBar()->value()+20); } -void GraphLogic::moveLeft() +void GraphLogic::moveNodeLeft() { QApplication::keyboardModifiers() & Qt::ControlModifier ? - move(-20,0) : + moveNode(qreal(-20),qreal(0)) : m_graphWidget->horizontalScrollBar()->setValue( m_graphWidget->horizontalScrollBar()->value()-20); } -void GraphLogic::moveRight() +void GraphLogic::moveNodeRight() { QApplication::keyboardModifiers() & Qt::ControlModifier ? - move(20,0) : + moveNode(qreal(20),qreal(0)) : m_graphWidget->horizontalScrollBar()->setValue( m_graphWidget->horizontalScrollBar()->value()+20); } -void GraphLogic::move(const int &x, const int &y) +void GraphLogic::moveNode(qreal x, qreal y) { if (!m_activeNode) { @@ -611,20 +635,15 @@ void GraphLogic::move(const int &x, const int &y) return; } - if (QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier) - { - QList nodeList = m_activeNode->subtree(); - foreach(Node *node, nodeList) - node->moveBy(x, y); + UndoContext context; + context.m_graphLogic = this; + context.m_nodeList = &m_nodeList; + context.m_activeNode = m_activeNode; + context.m_x = x; + context.m_y = y; - emit contentChanged(); - } - else // Move just the active Node. - { - m_activeNode->moveBy(x, y); - emit contentChanged(); - } + QUndoCommand *moveCommand = new MoveCommand(context); + m_undoStack->push(moveCommand); } void GraphLogic::setNodeColor(const QColor &color, const bool &subtree) @@ -686,20 +705,6 @@ void GraphLogic::applyNumber() selectNode(m_hintNode); } -Node * GraphLogic::nodeFactory() -{ - Node *node = new Node(); - - connect(node, SIGNAL(nodeChanged()), this, SLOT(nodeChanged())); - connect(node, SIGNAL(nodeSelected()), this, SLOT(nodeSelected())); - connect(node, SIGNAL(nodeEdited()), this, SLOT(nodeEdited())); - connect(node, SIGNAL(nodeMoved(QGraphicsSceneMouseEvent*)), - this, SLOT(nodeMoved(QGraphicsSceneMouseEvent*))); - connect(node, SIGNAL(nodeLostFocus()), this, SLOT(nodeLostFocus())); - - return node; -} - void GraphLogic::selectNode(Node *node) { // leave hint mode @@ -722,16 +727,6 @@ void GraphLogic::selectNode(Node *node) } } -void GraphLogic::setActiveNode(Node *node) -{ - if (m_activeNode!=0) - m_activeNode->setBorder(false); - - m_activeNode = node; - if (m_activeNode) - m_activeNode->setBorder(); -} - QList GraphLogic::allEdges() const { QList list; diff --git a/src/graphwidget.cpp b/src/graphwidget.cpp index 27550d0..3baf870 100644 --- a/src/graphwidget.cpp +++ b/src/graphwidget.cpp @@ -40,6 +40,7 @@ void GraphWidget::closeScene() { m_graphlogic->removeAllNodes(); this->hide(); + } GraphLogic *GraphWidget::graphLogic() const diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 98ee137..77d09a6 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -223,7 +223,9 @@ bool MainWindow::closeFile() m_contentChanged = false; setTitle(""); m_graphicsView->closeScene(); + m_undoStack->clear(); showMainToolbar(false); + showUndoToolbar(false); return true; } @@ -275,7 +277,7 @@ void MainWindow::showStatusIconToolbar(const bool &show) false); } -void MainWindow::showUdoToolbar(const bool &show) +void MainWindow::showUndoToolbar(const bool &show) { m_ui->undoToolBar->setVisible(show ? !m_ui->undoToolBar->isVisible() : @@ -484,7 +486,7 @@ void MainWindow::setupEditToolbar() m_undoToolbar = new QAction(tr("undo toolbar"), this); m_undoToolbar->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U)); connect(m_undoToolbar, SIGNAL(activated()), - this, SLOT (showUdoToolbar())); + this, SLOT (showUndoToolbar())); m_ui->menuEdit->addAction(m_mainToolbar); m_ui->menuEdit->addAction(m_iconToolbar); diff --git a/src/node.cpp b/src/node.cpp index cde24f4..4b3741c 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -15,8 +15,9 @@ const double Node::m_twoPi = Node::m_pi * 2.0; const QColor Node::m_gold(255,215,0); -Node::Node() - : m_number(-1) +Node::Node(GraphLogic *graphLogic) + : m_graphLogic(graphLogic) + , m_number(-1) , m_hasBorder(false) , m_numberIsSpecial(false) , m_color(m_gold) @@ -454,6 +455,8 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) return newPos; } + /// undo call here + break; } case ItemPositionHasChanged: @@ -492,7 +495,8 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) // notify parent so subtree can be moved too if necessary void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - emit nodeMoved(event); + QPointF diff(event->scenePos() - event->lastScenePos()); + m_graphLogic->moveNode(diff.x(), diff.y()); } QPainterPath Node::shape () const