From 0565dd3c8a667c9ed0d12f70e8faab88eb3122f2 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Mon, 13 Jun 2011 17:29:18 +0200 Subject: [PATCH] edge can be added to active node with pressing 'a' and then selecting the endpoint with mouse click or hint mode. The add can be cancelled with 'esc' --- graphwidget.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++------- graphwidget.h | 2 ++ node.cpp | 2 +- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/graphwidget.cpp b/graphwidget.cpp index 5325b4b..bb1f739 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -16,7 +16,8 @@ GraphWidget::GraphWidget(QWidget *parent) : m_activeNode(0), m_showingNodeNumbers(false), m_hintNode(0), - m_editingNode(false) + m_editingNode(false), + m_edgeAdding(false) { m_scene = new QGraphicsScene(this); m_scene->setItemIndexMethod(QGraphicsScene::NoIndex); @@ -145,7 +146,26 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) } - switch (event->key()) { + switch (event->key()) + { + + case Qt::Key_Escape: + + if (m_edgeAdding) + { + m_edgeAdding = false; + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("Edge adding cancelled"), + 5000); // millisec + } + else if(m_showingNodeNumbers) + { + m_hintNumber.clear(); + showingAllNodeNumbers(false); + m_showingNodeNumbers = false; + } + + break; // move sceve, or move node if modkey is ctrl case Qt::Key_Up: @@ -260,10 +280,18 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) if (m_hintNode && m_showingNodeNumbers) { showingAllNodeNumbers(false); - if (m_activeNode) - m_activeNode->setActive(false); - m_activeNode = m_hintNode; - m_activeNode->setActive(); + + if (m_edgeAdding) + { + m_scene->addItem(new Edge(m_activeNode, m_hintNode)); + } + else // selecting + { + if (m_activeNode) + m_activeNode->setActive(false); + m_activeNode = m_hintNode; + m_activeNode->setActive(); + } m_showingNodeNumbers = false; } @@ -318,6 +346,8 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) dynamic_cast(m_parent)->getStatusBar()->showMessage( tr("Add node: select destination node"), 5000); // millisec + + m_edgeAdding = true; } else { @@ -430,11 +460,20 @@ void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number, if (hit==1) { showingAllNodeNumbers(false); - if (m_activeNode) - m_activeNode->setActive(false); - m_activeNode = m_hintNode; - m_activeNode->setActive(); + if (m_edgeAdding) + { + m_scene->addItem(new Edge(m_activeNode, m_hintNode)); + m_edgeAdding = false; + } + else // selecting + { + if (m_activeNode) + m_activeNode->setActive(false); + + m_activeNode = m_hintNode; + m_activeNode->setActive(); + } m_showingNodeNumbers = false; } else if (hit == 0) @@ -455,3 +494,16 @@ void GraphWidget::setActiveNodeEditable() m_activeNode->setEditable(); m_scene->setFocusItem(m_activeNode); } + +void GraphWidget::nodeSelected(Node *node) +{ + if (m_edgeAdding) + { + m_scene->addItem(new Edge(m_activeNode, node)); + m_edgeAdding = false; + } + else + { + setActiveNode(node); + } +} diff --git a/graphwidget.h b/graphwidget.h index 90cbe92..82339d4 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -20,6 +20,7 @@ public: void setActiveNode(Node *node); void insertNode(); void setActiveNodeEditable(); + void nodeSelected(Node *node); protected: @@ -45,6 +46,7 @@ private: QString m_hintNumber; Node *m_hintNode; bool m_editingNode; + bool m_edgeAdding; }; #endif // GRAPHWIDGET_H diff --git a/node.cpp b/node.cpp index 4f8c0b2..8f33242 100644 --- a/node.cpp +++ b/node.cpp @@ -125,7 +125,7 @@ void Node::setActive(const bool &active) void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) { - m_graph->setActiveNode(this); + m_graph->nodeSelected(this); QGraphicsItem::mousePressEvent(event); }