color/textcolor commands are undo commands

master
Denes Matetelki 14 years ago
parent fc10d59bfa
commit 24b4d965e0

@ -22,6 +22,7 @@ struct UndoContext
bool m_secondary; bool m_secondary;
qreal m_x; qreal m_x;
qreal m_y; qreal m_y;
bool m_subtree;
UndoContext(GraphLogic *graphLogic = 0, UndoContext(GraphLogic *graphLogic = 0,
Node *activeNode = 0, Node *activeNode = 0,
@ -33,7 +34,8 @@ struct UndoContext
Node *destination = 0, Node *destination = 0,
bool secondary = false, bool secondary = false,
qreal x = 0, qreal x = 0,
qreal y = 0) qreal y = 0,
bool subtree = false)
: m_graphLogic(graphLogic) : m_graphLogic(graphLogic)
, m_activeNode(activeNode) , m_activeNode(activeNode)
, m_hintNode(hintNode) , m_hintNode(hintNode)
@ -45,6 +47,7 @@ struct UndoContext
, m_secondary(secondary) , m_secondary(secondary)
, m_x(x) , m_x(x)
, m_y(y) , m_y(y)
, m_subtree(subtree)
{}; {};
}; };
@ -147,4 +150,35 @@ public:
int id() const; int id() const;
}; };
class NodeColorCommand : public BaseUndoClass
{
public:
NodeColorCommand(UndoContext context);
void undo();
void redo();
private:
QMap<Node*, QColor> m_colorMap;
};
class NodeTextColorCommand : public BaseUndoClass
{
public:
NodeTextColorCommand(UndoContext context);
void undo();
void redo();
private:
QMap<Node*, QColor> m_colorMap;
};
#endif // COMMANDS_H #endif // COMMANDS_H

@ -49,7 +49,7 @@ public slots:
void nodeEdited(); /// @todo Rewrite as an undo action void nodeEdited(); /// @todo Rewrite as an undo action
void scaleUp(); /// @todo Rewrite as an undo action void scaleUp(); /// @todo Rewrite as an undo action
void scaleDown(); /// @todo Rewrite as an undo action void scaleDown(); /// @todo Rewrite as an undo action
void nodeColor(); void nodeColor(); // undo command
void nodeTextColor(); void nodeTextColor();
void addEdge(); void addEdge();
void removeEdge(); void removeEdge();
@ -73,10 +73,6 @@ private:
void moveNodeLeft(); void moveNodeLeft();
void moveNodeRight(); void moveNodeRight();
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
// hint mode // hint mode
void appendNumber(const int &unm); void appendNumber(const int &unm);
void delNumber(); void delNumber();
@ -108,12 +104,6 @@ private:
std::map<int, void(GraphLogic::*)(void)> m_memberMap; std::map<int, void(GraphLogic::*)(void)> m_memberMap;
QUndoStack *m_undoStack; QUndoStack *m_undoStack;
// friend class InsertNodeCommand;
// friend class RemoveNodeCommand;
// friend class AddEdgeCommand;
// friend class RemoveEdgeCommand;
}; };
#endif // GRAPHLOGIC_H #endif // GRAPHLOGIC_H

@ -13,8 +13,9 @@ BaseUndoClass::BaseUndoClass(UndoContext context)
, m_subtree(false) , m_subtree(false)
{ {
// remove just the active Node or it's subtree too? // remove just the active Node or it's subtree too?
if (QApplication::keyboardModifiers() & Qt::ControlModifier && if (m_context.m_subtree ||
QApplication::keyboardModifiers() & Qt::ShiftModifier) (QApplication::keyboardModifiers() & Qt::ControlModifier &&
QApplication::keyboardModifiers() & Qt::ShiftModifier))
{ {
m_nodeList = m_activeNode->subtree(); m_nodeList = m_activeNode->subtree();
m_subtree = true; m_subtree = true;
@ -254,12 +255,16 @@ void MoveCommand::undo()
{ {
foreach(Node *node, m_nodeList) foreach(Node *node, m_nodeList)
node->moveBy(-m_context.m_x, -m_context.m_y); node->moveBy(-m_context.m_x, -m_context.m_y);
m_context.m_graphLogic->setActiveNode(m_activeNode);
} }
void MoveCommand::redo() void MoveCommand::redo()
{ {
foreach(Node *node, m_nodeList) foreach(Node *node, m_nodeList)
node->moveBy(m_context.m_x, m_context.m_y); node->moveBy(m_context.m_x, m_context.m_y);
m_context.m_graphLogic->setActiveNode(m_activeNode);
} }
bool MoveCommand::mergeWith(const QUndoCommand *command) bool MoveCommand::mergeWith(const QUndoCommand *command)
@ -292,3 +297,69 @@ int MoveCommand::id() const
{ {
return MoveCommandId; return MoveCommandId;
} }
NodeColorCommand::NodeColorCommand(UndoContext context)
: BaseUndoClass(context)
{
setText(QObject::tr("Changing color of node: \"").append(
m_context.m_activeNode == m_context.m_nodeList->first() ?
QObject::tr("Base node") :
m_context.m_activeNode->toPlainText()).append("\"").
append(m_subtree ? QObject::tr(" with subtree") : QString("")));
foreach(Node *node, m_nodeList)
m_colorMap[node] = node->color();
}
void NodeColorCommand::undo()
{
foreach(Node *node, m_nodeList)
{
node->setColor(m_colorMap[node]);
foreach (Edge * edge, node->edgesToThis(false))
edge->setColor(m_colorMap[node]);
}
m_context.m_graphLogic->setActiveNode(m_activeNode);
}
void NodeColorCommand::redo()
{
foreach(Node *node, m_nodeList)
{
node->setColor(m_context.m_color);
foreach (Edge * edge, node->edgesToThis(false))
edge->setColor(m_context.m_color);
}
m_context.m_graphLogic->setActiveNode(m_activeNode);
}
NodeTextColorCommand::NodeTextColorCommand(UndoContext context)
: BaseUndoClass(context)
{
setText(QObject::tr("Changing textcolor of node: \"").append(
m_context.m_activeNode == m_context.m_nodeList->first() ?
QObject::tr("Base node") :
m_context.m_activeNode->toPlainText()).append("\"").
append(m_subtree ? QObject::tr(" with subtree") : QString("")));
foreach(Node *node, m_nodeList)
m_colorMap[node] = node->textColor();
}
void NodeTextColorCommand::undo()
{
foreach(Node *node, m_nodeList)
node->setTextColor(m_colorMap[node]);
m_context.m_graphLogic->setActiveNode(m_activeNode);
}
void NodeTextColorCommand::redo()
{
foreach(Node *node, m_nodeList)
node->setTextColor(m_context.m_color);
m_context.m_graphLogic->setActiveNode(m_activeNode);
}

@ -468,7 +468,15 @@ void GraphLogic::nodeColor()
if (!dialog.exec()) if (!dialog.exec())
return; return;
setNodeColor(dialog.selectedColor(), subtree); UndoContext context;
context.m_graphLogic = this;
context.m_nodeList = &m_nodeList;
context.m_activeNode = m_activeNode;
context.m_color = dialog.selectedColor();
context.m_subtree = subtree;
QUndoCommand *nodeColorCommand = new NodeColorCommand(context);
m_undoStack->push(nodeColorCommand);
} }
void GraphLogic::nodeTextColor() void GraphLogic::nodeTextColor()
@ -489,7 +497,15 @@ void GraphLogic::nodeTextColor()
if (!dialog.exec()) if (!dialog.exec())
return; return;
setNodeTextColor(dialog.selectedColor(), subtree); UndoContext context;
context.m_graphLogic = this;
context.m_nodeList = &m_nodeList;
context.m_activeNode = m_activeNode;
context.m_color = dialog.selectedColor();
context.m_subtree = subtree;
QUndoCommand *nodeTextColorCommand = new NodeTextColorCommand(context);
m_undoStack->push(nodeTextColorCommand);
} }
void GraphLogic::addEdge() void GraphLogic::addEdge()
@ -646,42 +662,6 @@ void GraphLogic::moveNode(qreal x, qreal y)
m_undoStack->push(moveCommand); m_undoStack->push(moveCommand);
} }
void GraphLogic::setNodeColor(const QColor &color, const bool &subtree)
{
QList <Node *> nodeList;
if (subtree)
{
nodeList = m_activeNode->subtree();
}
else
{
nodeList.push_back(m_activeNode);
}
foreach(Node *node, nodeList)
{
node->setColor(color);
foreach (Edge * edge, node->edgesToThis(false))
edge->setColor(color);
}
}
void GraphLogic::setNodeTextColor(const QColor &color, const bool &subtree)
{
QList <Node *> nodeList;
if (subtree)
{
nodeList = m_activeNode->subtree();
}
else
{
nodeList.push_back(m_activeNode);
}
foreach(Node *node, nodeList)
node->setTextColor(color);
}
void GraphLogic::appendNumber(const int &num) void GraphLogic::appendNumber(const int &num)
{ {
m_hintNumber.append(QString::number(num)); m_hintNumber.append(QString::number(num));

Loading…
Cancel
Save