diff --git a/graphwidget.cpp b/graphwidget.cpp index bb1f739..d6f9c4e 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -17,7 +17,8 @@ GraphWidget::GraphWidget(QWidget *parent) : m_showingNodeNumbers(false), m_hintNode(0), m_editingNode(false), - m_edgeAdding(false) + m_edgeAdding(false), + m_edgeDeleting(false) { m_scene = new QGraphicsScene(this); m_scene->setItemIndexMethod(QGraphicsScene::NoIndex); @@ -158,6 +159,13 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) tr("Edge adding cancelled"), 5000); // millisec } + else if (m_edgeDeleting) + { + m_edgeDeleting = false; + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("Edge deleting cancelled"), + 5000); // millisec + } else if(m_showingNodeNumbers) { m_hintNumber.clear(); @@ -284,6 +292,12 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) if (m_edgeAdding) { m_scene->addItem(new Edge(m_activeNode, m_hintNode)); + m_edgeAdding = false; + } + if (m_edgeDeleting) + { + m_activeNode->removeEdge(m_hintNode); + m_edgeDeleting = false; } else // selecting { @@ -344,7 +358,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) if (m_activeNode) { dynamic_cast(m_parent)->getStatusBar()->showMessage( - tr("Add node: select destination node"), + tr("Add edge: select destination node"), 5000); // millisec m_edgeAdding = true; @@ -358,6 +372,26 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) break; + // add edge to active node + case Qt::Key_D: + + if (m_activeNode) + { + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("Delete edge: select destination node"), + 5000); // millisec + + m_edgeDeleting = true; + } + else + { + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("No active node."), + 5000); // millisec + } + + break; + default: QGraphicsView::keyPressEvent(event); } @@ -466,6 +500,11 @@ void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number, m_scene->addItem(new Edge(m_activeNode, m_hintNode)); m_edgeAdding = false; } + if (m_edgeDeleting) + { + m_activeNode->removeEdge(m_hintNode); + m_edgeDeleting = false; + } else // selecting { if (m_activeNode) @@ -502,6 +541,11 @@ void GraphWidget::nodeSelected(Node *node) m_scene->addItem(new Edge(m_activeNode, node)); m_edgeAdding = false; } + if (m_edgeDeleting) + { + m_activeNode->removeEdge(node); + m_edgeDeleting = false; + } else { setActiveNode(node); diff --git a/graphwidget.h b/graphwidget.h index 82339d4..8271480 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -47,6 +47,7 @@ private: Node *m_hintNode; bool m_editingNode; bool m_edgeAdding; + bool m_edgeDeleting; }; #endif // GRAPHWIDGET_H diff --git a/node.cpp b/node.cpp index 8f33242..9f3d7a2 100644 --- a/node.cpp +++ b/node.cpp @@ -48,6 +48,22 @@ void Node::removeEdge(Edge *edge) } } +void Node::removeEdge(Node *otherEnd) +{ + qDebug() << __PRETTY_FUNCTION__; + + for(QList::iterator it = m_edgeList.begin(); + it != m_edgeList.end(); it++) + { + if ((it->edge->sourceNode() == otherEnd && it->edge->destNode() == this) + || (it->edge->sourceNode() == this && it->edge->destNode() == otherEnd)) + { + delete it->edge; + return; + } + } +} + QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) { switch (change) { diff --git a/node.h b/node.h index 451e9af..4bfe1a3 100644 --- a/node.h +++ b/node.h @@ -18,6 +18,7 @@ public: void addEdge(Edge *edge, bool startsFromThisNode); void removeEdge(Edge *edge); + void removeEdge(Node *otherEnd); void setBorder(const bool &hasBorder); void setActive(const bool &active = true); void showNumber(const int &number, const bool& show = true,