From 080c5aac6aa8e9425198e118b8294e250dff20b4 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 9 Jun 2011 14:55:16 +0200 Subject: [PATCH] active node can be deleted with DEL. Code is getting messy, time to some refactor --- edge.cpp | 6 ++++++ edge.h | 1 + graphwidget.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++------- node.cpp | 12 +++++++++++ node.h | 2 ++ 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/edge.cpp b/edge.cpp index 4b8535e..69ff306 100644 --- a/edge.cpp +++ b/edge.cpp @@ -22,6 +22,12 @@ Edge::Edge(Node *sourceNode, Node *destNode) // setZValue(1); } +Edge::~Edge() +{ + m_sourceNode->removeEdge(this); + m_destNode->removeEdge(this); +} + Node *Edge::sourceNode() const { return m_sourceNode; diff --git a/edge.h b/edge.h index 3d4c4b0..7e94c8f 100644 --- a/edge.h +++ b/edge.h @@ -10,6 +10,7 @@ class Edge : public QGraphicsItem public: Edge(Node *sourceNode, Node *destNode); + ~Edge(); Node *sourceNode() const; Node *destNode() const; diff --git a/graphwidget.cpp b/graphwidget.cpp index 985f15e..ca3fdc3 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -8,7 +8,9 @@ GraphWidget::GraphWidget(QWidget *parent) : QGraphicsView(parent), - m_showingNodeNumbers(false) + m_activeNode(0), + m_showingNodeNumbers(false), + m_followNode(0) { qDebug() << __PRETTY_FUNCTION__; @@ -124,16 +126,20 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) switch (event->key()) { case Qt::Key_Up: - m_activeNode->moveBy(0, -20); + if (m_activeNode) + m_activeNode->moveBy(0, -20); break; case Qt::Key_Down: - m_activeNode->moveBy(0, 20); + if (m_activeNode) + m_activeNode->moveBy(0, 20); break; case Qt::Key_Left: - m_activeNode->moveBy(-20, 0); + if (m_activeNode) + m_activeNode->moveBy(-20, 0); break; case Qt::Key_Right: - m_activeNode->moveBy(20, 0); + if (m_activeNode) + m_activeNode->moveBy(20, 0); break; case Qt::Key_Plus: scaleView(qreal(1.2)); @@ -147,6 +153,11 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) m_followNumber.clear(); showingAllNodeNumbers(m_showingNodeNumbers); + if (m_showingNodeNumbers) + { + m_nodeList.first()->showNumber(0,true,true); + m_followNode = m_nodeList.first(); + } break; case Qt::Key_Insert: @@ -182,6 +193,8 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) if (m_followNumber.isEmpty()) { showingAllNodeNumbers(true); + m_nodeList.first()->showNumber(0,true,true); + m_followNode = m_nodeList.first(); } else { @@ -193,16 +206,40 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) case Qt::Key_Return: case Qt::Key_Enter: - if (m_followNode) + if (m_followNode && m_showingNodeNumbers) { + qDebug() << m_activeNode; + qDebug() << m_followNode; showingAllNodeNumbers(false); - m_activeNode->setActive(false); + if (m_activeNode) + m_activeNode->setActive(false); m_activeNode = m_followNode; m_activeNode->setActive(); m_showingNodeNumbers = false; } break; + case Qt::Key_Delete: + + if (m_activeNode) + { + if (m_followNode==m_activeNode) + m_followNode=0; + + m_nodeList.removeAll(m_activeNode); + delete m_activeNode; + m_activeNode = 0; + + /// @bug + if (m_showingNodeNumbers) + { + m_showingNodeNumbers = false; + showingAllNodeNumbers(false); + } + } + + break; + default: QGraphicsView::keyPressEvent(event); } @@ -283,7 +320,9 @@ void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number, const boo { qDebug() << "set active"; showingAllNodeNumbers(false); - m_activeNode->setActive(false); + if (m_activeNode) + m_activeNode->setActive(false); + m_activeNode = m_followNode; m_activeNode->setActive(); m_showingNodeNumbers = false; diff --git a/node.cpp b/node.cpp index a121c27..3687acb 100644 --- a/node.cpp +++ b/node.cpp @@ -26,6 +26,12 @@ Node::Node(GraphWidget *parent) : // setTextInteractionFlags(Qt::TextEditorInteraction); } +Node::~Node() +{ + qDebug() << __PRETTY_FUNCTION__; + foreach (Edge *edge, m_edgeList) delete edge; +} + void Node::addEdge(Edge *edge) { qDebug() << __PRETTY_FUNCTION__; @@ -34,6 +40,12 @@ void Node::addEdge(Edge *edge) edge->adjust(); } +void Node::removeEdge(Edge *edge) +{ + qDebug() << __PRETTY_FUNCTION__; + + m_edgeList.removeAll(edge); +} QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) { diff --git a/node.h b/node.h index 6068fad..a6270d1 100644 --- a/node.h +++ b/node.h @@ -13,8 +13,10 @@ class Node : public QGraphicsTextItem public: Node(GraphWidget *graphWidget = 0); + ~Node(); void addEdge(Edge *edge); + void removeEdge(Edge *edge); // QList edges() const; void setActive(const bool &active = true); void showNumber(const int &number, const bool& show = true, const bool &numberIsSpecial = false);