removenode became undo command

master
Denes Matetelki 14 years ago
parent 63f4973962
commit 4504af7468

@ -21,6 +21,13 @@ public:
const char* what() const throw(); const char* what() const throw();
}; };
class CannotDeleteBaseNodeException : public std::exception
{
public:
const char* what() const throw();
};
class InsertNodeCommand : public QUndoCommand class InsertNodeCommand : public QUndoCommand
{ {
@ -35,9 +42,32 @@ public:
private: private:
GraphLogic *m_graphLogic; GraphLogic *m_graphLogic;
Node *m_node; Node *m_node;
QPointF m_pos; QPointF m_pos;
Node *m_activeNode; Node *m_activeNode;
Edge *m_edge;
};
class RemoveNodeCommand : public QUndoCommand
{
public:
RemoveNodeCommand(GraphLogic *graphLogic);
void undo();
void redo();
private:
GraphLogic *m_graphLogic;
Node *m_activeNode;
Node *m_hintNode;
QList <Node *> m_nodeList;
QList <Edge *> m_edgeList;
}; };
#endif // COMMANDS_H #endif // COMMANDS_H

@ -10,7 +10,9 @@
class GraphWidget; class GraphWidget;
class InsertNodeCommand; class InsertNodeCommand;
class RemoveNodeCommand;
class GraphLogic : public QObject class GraphLogic : public QObject
{ {
@ -101,6 +103,7 @@ private:
friend class InsertNodeCommand; friend class InsertNodeCommand;
friend class RemoveNodeCommand;
}; };
#endif // GRAPHLOGIC_H #endif // GRAPHLOGIC_H

@ -22,10 +22,13 @@ public:
// add/remove edges // add/remove edges
void addEdge(Edge *edge, bool startsFromThisNode); void addEdge(Edge *edge, bool startsFromThisNode);
void deleteEdge(Node *otherEnd); void deleteEdge(Node *otherEnd);
void removeEdgeFromList(Edge *edge); void deleteEdges();
void removeEdge(Edge *edge);
void removeEdges(); void removeEdges();
// graph traversal // graph traversal
QList<Edge *> edges() const;
QList<Edge *> edgesFrom(const bool &excludeSecondaries = true) const; QList<Edge *> edgesFrom(const bool &excludeSecondaries = true) const;
QList<Edge *> edgesToThis(const bool &excludeSecondaries = true) const; QList<Edge *> edgesToThis(const bool &excludeSecondaries = true) const;
Edge * edgeTo(const Node* node) const; Edge * edgeTo(const Node* node) const;

@ -1,6 +1,7 @@
#include "include/commands.h" #include "include/commands.h"
#include <QDebug> #include <QDebug>
#include <QApplication>
#include <math.h> #include <math.h>
@ -17,16 +18,22 @@ const char* CannotPlaceNewNodeException::what() const throw()
toStdString().c_str(); toStdString().c_str();
} }
const char* CannotDeleteBaseNodeException::what() const throw()
{
return QObject::tr("Base node cannot be deleted.").toStdString().c_str();
}
InsertNodeCommand::InsertNodeCommand(GraphLogic *graphLogic) InsertNodeCommand::InsertNodeCommand(GraphLogic *graphLogic)
: m_graphLogic(graphLogic) : m_graphLogic(graphLogic)
, m_node(0) , m_node(0)
, m_activeNode(m_graphLogic->m_activeNode) , m_activeNode(m_graphLogic->m_activeNode)
, m_edge(0)
{ {
if (!m_activeNode) if (!m_activeNode)
throw NoActiveNodeException(); throw NoActiveNodeException();
setText(QObject::tr("New node added to ").append( setText(QObject::tr("Node added to ").append(
m_activeNode == m_graphLogic->m_nodeList.first() ? m_activeNode == m_graphLogic->m_nodeList.first() ?
QObject::tr("Base node") : QObject::tr("Base node") :
QString("\"").append(m_activeNode->toPlainText().append("\"")))); QString("\"").append(m_activeNode->toPlainText().append("\""))));
@ -55,6 +62,11 @@ InsertNodeCommand::InsertNodeCommand(GraphLogic *graphLogic)
m_node->setColor(m_activeNode->color()); m_node->setColor(m_activeNode->color());
m_node->setTextColor(m_activeNode->textColor()); m_node->setTextColor(m_activeNode->textColor());
m_node->setHtml(QString("")); m_node->setHtml(QString(""));
m_edge = new Edge(m_activeNode, m_node);
m_edge->setColor(m_node->color());
m_edge->setWidth(m_node->scale()*2 + 1);
m_edge->setSecondary(false);
} }
void InsertNodeCommand::undo() void InsertNodeCommand::undo()
@ -62,7 +74,10 @@ void InsertNodeCommand::undo()
m_graphLogic->m_nodeList.removeAll(m_node); m_graphLogic->m_nodeList.removeAll(m_node);
m_graphLogic->m_graphWidget->scene()->removeItem(m_node); m_graphLogic->m_graphWidget->scene()->removeItem(m_node);
m_node->removeEdges(); // m_node->deleteEdges();
m_edge->sourceNode()->removeEdge(m_edge);
m_edge->destNode()->removeEdge(m_edge);
m_graphLogic->m_graphWidget->scene()->removeItem(m_edge);
m_graphLogic->setActiveNode(m_activeNode); m_graphLogic->setActiveNode(m_activeNode);
@ -76,7 +91,10 @@ void InsertNodeCommand::redo()
m_node->setPos(m_pos); m_node->setPos(m_pos);
m_graphLogic->addEdge(m_activeNode, m_node); // m_graphLogic->addEdge(m_activeNode, m_node);
m_edge->sourceNode()->addEdge(m_edge,true);
m_edge->destNode()->addEdge(m_edge,false);
m_graphLogic->m_graphWidget->scene()->addItem(m_edge);
m_graphLogic->setActiveNode(m_node); m_graphLogic->setActiveNode(m_node);
@ -89,3 +107,87 @@ void InsertNodeCommand::redo()
if (m_graphLogic->m_showingNodeNumbers) if (m_graphLogic->m_showingNodeNumbers)
m_graphLogic->showNodeNumbers(); m_graphLogic->showNodeNumbers();
} }
RemoveNodeCommand::RemoveNodeCommand(GraphLogic *graphLogic)
: m_graphLogic(graphLogic)
, m_activeNode(m_graphLogic->m_activeNode)
, m_hintNode(m_graphLogic->m_hintNode)
{
if (!m_activeNode)
throw NoActiveNodeException();
if (m_activeNode == m_graphLogic->m_nodeList.first())
throw CannotDeleteBaseNodeException();
setText(QObject::tr("Node removed \"").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);
}
foreach(Node *node, m_nodeList)
foreach(Edge *edge, node->edges())
if (m_edgeList.indexOf(edge) == -1)
m_edgeList.push_back(edge);
}
void RemoveNodeCommand::undo()
{
foreach (Node *node, m_nodeList)
{
m_graphLogic->m_graphWidget->scene()->addItem(node);
m_graphLogic->m_nodeList.append(node);
}
foreach (Edge *edge, m_edgeList)
{
edge->sourceNode()->addEdge(edge,true);
edge->destNode()->addEdge(edge,false);
m_graphLogic->m_graphWidget->scene()->addItem(edge);
}
emit m_graphLogic->contentChanged(false);
m_graphLogic->m_activeNode = m_activeNode;
m_graphLogic->m_hintNode = m_hintNode;
// it we are in hint mode, the numbers shall be re-calculated
if (m_graphLogic->m_showingNodeNumbers)
m_graphLogic->showNodeNumbers();
}
void RemoveNodeCommand::redo()
{
foreach(Node *node, m_nodeList)
{
if (m_graphLogic->m_hintNode==node)
m_graphLogic->m_hintNode=0;
m_graphLogic->m_nodeList.removeAll(node);
m_graphLogic->m_graphWidget->scene()->removeItem(node);
}
foreach(Edge *edge, m_edgeList)
{
edge->sourceNode()->removeEdge(edge);
edge->destNode()->removeEdge(edge);
m_graphLogic->m_graphWidget->scene()->removeItem(edge);
}
m_graphLogic->m_activeNode = 0;
emit m_graphLogic->contentChanged();
// it we are in hint mode, the numbers shall be re-calculated
if (m_graphLogic->m_showingNodeNumbers)
m_graphLogic->showNodeNumbers();
}

@ -308,45 +308,16 @@ void GraphLogic::insertNode()
void GraphLogic::removeNode() void GraphLogic::removeNode()
{ {
if (!m_activeNode) try
{ {
emit notification(tr("No active node.")); QUndoCommand *removeNodeCommand = new RemoveNodeCommand(this);
return; m_undoStack->push(removeNodeCommand);
} }
catch (std::exception &e)
if (m_activeNode == m_nodeList.first())
{ {
emit notification(tr("Base node cannot be deleted.")); emit notification(e.what());
return; return;
} }
// remove just the active Node or it's subtree too?
QList <Node *> nodeList;
if (QApplication::keyboardModifiers() & Qt::ControlModifier &&
QApplication::keyboardModifiers() & Qt::ShiftModifier)
{
nodeList = m_activeNode->subtree();
}
else
{
nodeList.push_back(m_activeNode);
}
foreach(Node *node, nodeList)
{
if (m_hintNode==node)
m_hintNode=0;
m_nodeList.removeAll(node);
delete node;
}
m_activeNode = 0;
emit contentChanged();
// it we are in hint mode, the numbers shall be re-calculated
if (m_showingNodeNumbers)
showNodeNumbers();
} }
void GraphLogic::nodeEdited() void GraphLogic::nodeEdited()

@ -35,7 +35,7 @@ Node::Node()
Node::~Node() Node::~Node()
{ {
removeEdges(); deleteEdges();
} }
void Node::addEdge(Edge *edge, bool startsFromThisNode) void Node::addEdge(Edge *edge, bool startsFromThisNode)
@ -55,15 +55,28 @@ void Node::deleteEdge(Node *otherEnd)
it->edge->destNode() == otherEnd)) it->edge->destNode() == otherEnd))
{ {
Edge *tmp = it->edge; Edge *tmp = it->edge;
tmp->sourceNode()->removeEdgeFromList(tmp); tmp->sourceNode()->removeEdge(tmp);
tmp->destNode()->removeEdgeFromList(tmp); tmp->destNode()->removeEdge(tmp);
delete tmp; delete tmp;
return; return;
} }
} }
} }
void Node::removeEdgeFromList(Edge *edge) void Node::deleteEdges()
{
foreach (EdgeElement element, m_edgeList)
{
Edge *tmp = element.edge;
tmp->sourceNode()->removeEdge(tmp);
tmp->destNode()->removeEdge(tmp);
/// @bug crashes sometimes
delete tmp;
}
}
void Node::removeEdge(Edge *edge)
{ {
for(QList<EdgeElement>::iterator it = m_edgeList.begin(); for(QList<EdgeElement>::iterator it = m_edgeList.begin();
it != m_edgeList.end(); it++) it != m_edgeList.end(); it++)
@ -78,17 +91,23 @@ void Node::removeEdgeFromList(Edge *edge)
void Node::removeEdges() void Node::removeEdges()
{ {
foreach (EdgeElement element, m_edgeList) for(QList<EdgeElement>::iterator it = m_edgeList.begin();
it != m_edgeList.end(); it++)
{ {
Edge *tmp = element.edge; m_edgeList.erase(it);
tmp->sourceNode()->removeEdgeFromList(tmp);
tmp->destNode()->removeEdgeFromList(tmp);
/// @bug crashes sometimes
delete tmp;
} }
} }
QList<Edge *> Node::edges() const
{
QList<Edge *> list;
foreach(EdgeElement element, m_edgeList)
list.push_back(element.edge);
return list;
}
// edges from this Node. Exclude secondaries if needed (calc subtree) // edges from this Node. Exclude secondaries if needed (calc subtree)
QList<Edge *> Node::edgesFrom(const bool &excludeSecondaries) const QList<Edge *> Node::edgesFrom(const bool &excludeSecondaries) const
{ {

Loading…
Cancel
Save