diff --git a/include/commands.h b/include/commands.h index 04f66de..154c26c 100644 --- a/include/commands.h +++ b/include/commands.h @@ -48,14 +48,28 @@ struct UndoContext {}; }; - -/// @todo need a base class... -enum MergeableCommandId +class BaseUndoClass : public QUndoCommand { - MoveCommandId = 0 +public: + + enum MergeableCommandId + { + MoveCommandId = 0 + }; + + BaseUndoClass(UndoContext context); + +protected: + + bool m_done; + UndoContext m_context; + Node *m_activeNode; + QList m_nodeList; + bool m_subtree; }; -class InsertNodeCommand : public QUndoCommand + +class InsertNodeCommand : public BaseUndoClass { public: @@ -68,15 +82,11 @@ public: private: - bool m_done; - UndoContext m_context; - Node *m_node; - Node *m_activeNode; Edge *m_edge; }; -class RemoveNodeCommand : public QUndoCommand +class RemoveNodeCommand : public BaseUndoClass { public: @@ -88,17 +98,11 @@ public: private: - bool m_done; - UndoContext m_context; - - Node *m_activeNode; Node *m_hintNode; - - QList m_nodeList; QList m_edgeList; }; -class AddEdgeCommand : public QUndoCommand +class AddEdgeCommand : public BaseUndoClass { public: @@ -111,14 +115,10 @@ public: private: - bool m_done; - UndoContext m_context; - - Node *m_activeNode; Edge *m_edge; }; -class RemoveEdgeCommand : public QUndoCommand +class RemoveEdgeCommand : public BaseUndoClass { public: @@ -130,14 +130,10 @@ public: private: - bool m_done; - UndoContext m_context; - - Node *m_activeNode; Edge *m_edge; }; -class MoveCommand : public QUndoCommand +class MoveCommand : public BaseUndoClass { public: @@ -148,14 +144,7 @@ public: void redo(); bool mergeWith(const QUndoCommand *command); - int id() const { return MoveCommandId; } - -private: - - bool m_done; - UndoContext m_context; - - QList m_nodeList; + int id() const; }; #endif // COMMANDS_H diff --git a/include/graphlogic.h b/include/graphlogic.h index e759c5d..3ee3802 100644 --- a/include/graphlogic.h +++ b/include/graphlogic.h @@ -39,9 +39,7 @@ 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 + void moveNode(qreal x, qreal y); // undo command public slots: diff --git a/src/commands.cpp b/src/commands.cpp index 3e65ccd..b41632c 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -6,16 +6,34 @@ #include -InsertNodeCommand::InsertNodeCommand(UndoContext context) +BaseUndoClass::BaseUndoClass(UndoContext context) : m_done(false) , m_context(context) , m_activeNode(context.m_activeNode) + , m_subtree(false) +{ + // remove just the active Node or it's subtree too? + if (QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier) + { + m_nodeList = m_activeNode->subtree(); + m_subtree = true; + } + else + { + m_nodeList.push_back(m_activeNode); + } +} + + + +InsertNodeCommand::InsertNodeCommand(UndoContext context) + : BaseUndoClass(context) { setText(QObject::tr("Node added to \"").append( m_activeNode == m_context.m_nodeList->first() ? - QObject::tr("Base node") : - m_activeNode->toPlainText()).append("\"")); - + QObject::tr("Base node") : + m_activeNode->toPlainText()).append("\"")); m_context.m_graphLogic->nodeLostFocus(); @@ -78,24 +96,13 @@ void InsertNodeCommand::redo() } RemoveNodeCommand::RemoveNodeCommand(UndoContext context) - : m_context(context) - , m_activeNode(context.m_activeNode) + : BaseUndoClass(context) , m_hintNode(context.m_hintNode) { setText(QObject::tr("Node deleted \"").append( - m_activeNode->toPlainText().append("\""))); - - // remove just the active Node or it's subtree too? - if (QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier) - { - m_nodeList = m_activeNode->subtree(); - setText(text().append(QObject::tr(" with subtree"))); - } - else - { - m_nodeList.push_back(m_activeNode); - } + m_activeNode->toPlainText().append("\"")).append( + m_subtree ? + QObject::tr(" with subtree") : QString(""))); // collect affected edges foreach(Node *node, m_nodeList) @@ -151,9 +158,7 @@ void RemoveNodeCommand::redo() } AddEdgeCommand::AddEdgeCommand(UndoContext context) - : m_done(false) - , m_context(context) - , m_activeNode(context.m_activeNode) + : BaseUndoClass(context) { setText(QObject::tr("Edge added between \"").append( m_context.m_source == m_context.m_nodeList->first() ? @@ -201,8 +206,7 @@ AddEdgeCommand::~AddEdgeCommand() } RemoveEdgeCommand::RemoveEdgeCommand(UndoContext context) - : m_context(context) - , m_activeNode(context.m_activeNode) + : BaseUndoClass(context) { setText(QObject::tr("Edge deleted between \"").append( m_context.m_source == m_context.m_nodeList->first() ? @@ -236,25 +240,14 @@ void RemoveEdgeCommand::redo() } MoveCommand::MoveCommand(UndoContext context) - : m_context(context) + : BaseUndoClass(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 (%1, %2)").arg(m_context.m_x).arg(m_context.m_y)); - - // 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); - } + append("\" moved (%1, %2)").arg(m_context.m_x).arg(m_context.m_y). + append(m_subtree ? QObject::tr(" with subtree") : QString(""))); } void MoveCommand::undo() @@ -279,6 +272,9 @@ bool MoveCommand::mergeWith(const QUndoCommand *command) if (m_context.m_activeNode != moveCommand->m_context.m_activeNode) return false; + if (m_subtree != moveCommand->m_subtree) + return false; + m_context.m_x += moveCommand->m_context.m_x; m_context.m_y += moveCommand->m_context.m_y; @@ -286,13 +282,13 @@ bool MoveCommand::mergeWith(const QUndoCommand *command) m_context.m_activeNode == m_context.m_nodeList->first() ? QObject::tr("Base node") : m_context.m_activeNode->toPlainText()). - append("\" moved (%1, %2)").arg(m_context.m_x).arg(m_context.m_y)); - - if (QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier) - { - setText(text().append(QObject::tr(" with subtree"))); - } + append("\" moved (%1, %2)").arg(m_context.m_x).arg(m_context.m_y). + append(m_subtree ? QObject::tr(" with subtree") : QString(""))); return true; } + +int MoveCommand::id() const +{ + return MoveCommandId; +}