base class for undoCommands

master
Denes Matetelki 14 years ago
parent d5ed92497b
commit fc10d59bfa

@ -48,14 +48,28 @@ struct UndoContext
{}; {};
}; };
class BaseUndoClass : public QUndoCommand
/// @todo need a base class...
enum MergeableCommandId
{ {
public:
enum MergeableCommandId
{
MoveCommandId = 0 MoveCommandId = 0
};
BaseUndoClass(UndoContext context);
protected:
bool m_done;
UndoContext m_context;
Node *m_activeNode;
QList <Node *> m_nodeList;
bool m_subtree;
}; };
class InsertNodeCommand : public QUndoCommand
class InsertNodeCommand : public BaseUndoClass
{ {
public: public:
@ -68,15 +82,11 @@ public:
private: private:
bool m_done;
UndoContext m_context;
Node *m_node; Node *m_node;
Node *m_activeNode;
Edge *m_edge; Edge *m_edge;
}; };
class RemoveNodeCommand : public QUndoCommand class RemoveNodeCommand : public BaseUndoClass
{ {
public: public:
@ -88,17 +98,11 @@ public:
private: private:
bool m_done;
UndoContext m_context;
Node *m_activeNode;
Node *m_hintNode; Node *m_hintNode;
QList <Node *> m_nodeList;
QList <Edge *> m_edgeList; QList <Edge *> m_edgeList;
}; };
class AddEdgeCommand : public QUndoCommand class AddEdgeCommand : public BaseUndoClass
{ {
public: public:
@ -111,14 +115,10 @@ public:
private: private:
bool m_done;
UndoContext m_context;
Node *m_activeNode;
Edge *m_edge; Edge *m_edge;
}; };
class RemoveEdgeCommand : public QUndoCommand class RemoveEdgeCommand : public BaseUndoClass
{ {
public: public:
@ -130,14 +130,10 @@ public:
private: private:
bool m_done;
UndoContext m_context;
Node *m_activeNode;
Edge *m_edge; Edge *m_edge;
}; };
class MoveCommand : public QUndoCommand class MoveCommand : public BaseUndoClass
{ {
public: public:
@ -148,14 +144,7 @@ public:
void redo(); void redo();
bool mergeWith(const QUndoCommand *command); bool mergeWith(const QUndoCommand *command);
int id() const { return MoveCommandId; } int id() const;
private:
bool m_done;
UndoContext m_context;
QList <Node *> m_nodeList;
}; };
#endif // COMMANDS_H #endif // COMMANDS_H

@ -39,9 +39,7 @@ public:
void setHintNode(Node *node); void setHintNode(Node *node);
void reShowNumbers(); void reShowNumbers();
void moveNode(qreal x, qreal y); void moveNode(qreal x, qreal y); // undo command
// void move(const QPointF &oldpos, const QPointF &newpos); /// @todo Rewrite as an undo action
public slots: public slots:

@ -6,17 +6,35 @@
#include <math.h> #include <math.h>
InsertNodeCommand::InsertNodeCommand(UndoContext context) BaseUndoClass::BaseUndoClass(UndoContext context)
: m_done(false) : m_done(false)
, m_context(context) , m_context(context)
, m_activeNode(context.m_activeNode) , 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( setText(QObject::tr("Node added to \"").append(
m_activeNode == m_context.m_nodeList->first() ? m_activeNode == m_context.m_nodeList->first() ?
QObject::tr("Base node") : QObject::tr("Base node") :
m_activeNode->toPlainText()).append("\"")); m_activeNode->toPlainText()).append("\""));
m_context.m_graphLogic->nodeLostFocus(); m_context.m_graphLogic->nodeLostFocus();
// create new node which inherits the color and textColor // create new node which inherits the color and textColor
@ -78,24 +96,13 @@ void InsertNodeCommand::redo()
} }
RemoveNodeCommand::RemoveNodeCommand(UndoContext context) RemoveNodeCommand::RemoveNodeCommand(UndoContext context)
: m_context(context) : BaseUndoClass(context)
, m_activeNode(context.m_activeNode)
, m_hintNode(context.m_hintNode) , m_hintNode(context.m_hintNode)
{ {
setText(QObject::tr("Node deleted \"").append( setText(QObject::tr("Node deleted \"").append(
m_activeNode->toPlainText().append("\""))); m_activeNode->toPlainText().append("\"")).append(
m_subtree ?
// remove just the active Node or it's subtree too? QObject::tr(" with subtree") : QString("")));
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);
}
// collect affected edges // collect affected edges
foreach(Node *node, m_nodeList) foreach(Node *node, m_nodeList)
@ -151,9 +158,7 @@ void RemoveNodeCommand::redo()
} }
AddEdgeCommand::AddEdgeCommand(UndoContext context) AddEdgeCommand::AddEdgeCommand(UndoContext context)
: m_done(false) : BaseUndoClass(context)
, m_context(context)
, m_activeNode(context.m_activeNode)
{ {
setText(QObject::tr("Edge added between \"").append( setText(QObject::tr("Edge added between \"").append(
m_context.m_source == m_context.m_nodeList->first() ? m_context.m_source == m_context.m_nodeList->first() ?
@ -201,8 +206,7 @@ AddEdgeCommand::~AddEdgeCommand()
} }
RemoveEdgeCommand::RemoveEdgeCommand(UndoContext context) RemoveEdgeCommand::RemoveEdgeCommand(UndoContext context)
: m_context(context) : BaseUndoClass(context)
, m_activeNode(context.m_activeNode)
{ {
setText(QObject::tr("Edge deleted between \"").append( setText(QObject::tr("Edge deleted between \"").append(
m_context.m_source == m_context.m_nodeList->first() ? m_context.m_source == m_context.m_nodeList->first() ?
@ -236,25 +240,14 @@ void RemoveEdgeCommand::redo()
} }
MoveCommand::MoveCommand(UndoContext context) MoveCommand::MoveCommand(UndoContext context)
: m_context(context) : BaseUndoClass(context)
{ {
setText(QObject::tr("Node \"").append( setText(QObject::tr("Node \"").append(
m_context.m_activeNode == m_context.m_nodeList->first() ? m_context.m_activeNode == m_context.m_nodeList->first() ?
QObject::tr("Base node") : QObject::tr("Base node") :
m_context.m_activeNode->toPlainText()). m_context.m_activeNode->toPlainText()).
append("\" moved (%1, %2)").arg(m_context.m_x).arg(m_context.m_y)); append("\" moved (%1, %2)").arg(m_context.m_x).arg(m_context.m_y).
append(m_subtree ? QObject::tr(" with subtree") : QString("")));
// 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() void MoveCommand::undo()
@ -279,6 +272,9 @@ bool MoveCommand::mergeWith(const QUndoCommand *command)
if (m_context.m_activeNode != moveCommand->m_context.m_activeNode) if (m_context.m_activeNode != moveCommand->m_context.m_activeNode)
return false; return false;
if (m_subtree != moveCommand->m_subtree)
return false;
m_context.m_x += moveCommand->m_context.m_x; m_context.m_x += moveCommand->m_context.m_x;
m_context.m_y += moveCommand->m_context.m_y; 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() ? m_context.m_activeNode == m_context.m_nodeList->first() ?
QObject::tr("Base node") : QObject::tr("Base node") :
m_context.m_activeNode->toPlainText()). m_context.m_activeNode->toPlainText()).
append("\" moved (%1, %2)").arg(m_context.m_x).arg(m_context.m_y)); append("\" moved (%1, %2)").arg(m_context.m_x).arg(m_context.m_y).
append(m_subtree ? QObject::tr(" with subtree") : QString("")));
if (QApplication::keyboardModifiers() & Qt::ControlModifier &&
QApplication::keyboardModifiers() & Qt::ShiftModifier)
{
setText(text().append(QObject::tr(" with subtree")));
}
return true; return true;
} }
int MoveCommand::id() const
{
return MoveCommandId;
}

Loading…
Cancel
Save