diff --git a/graphwidget.cpp b/graphwidget.cpp index 879b3b3..2b17f2d 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -220,15 +220,12 @@ void GraphWidget::insertPicture(const QString &picture) void GraphWidget::keyPressEvent(QKeyEvent *event) { - // esc leaves node editing mode - if (event->key() == Qt::Key_Escape && m_editingNode) + if (event->key() == Qt::Key_Escape) { - m_activeNode->setEditable(false); - m_editingNode = false; + nodeLostFocus(); return; } - // in node editing mode forward every key (except esc) to node - else if (m_editingNode) + if (m_editingNode) { m_activeNode->keyPressEvent(event); return; @@ -257,28 +254,6 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) switch (event->key()) { - - case Qt::Key_Escape: - - if (m_edgeAdding) - { - m_edgeAdding = false; - m_parent->statusBarMsg(tr("Edge adding cancelled.")); - } - else if (m_edgeDeleting) - { - m_edgeDeleting = false; - m_parent->statusBarMsg(tr("Edge deleting cancelled.")); - } - 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: case Qt::Key_Down: @@ -703,6 +678,32 @@ void GraphWidget::nodeMoved(QGraphicsSceneMouseEvent *event) node->setPos(node->pos() + event->scenePos() - event->lastScenePos()); } +void GraphWidget::nodeLostFocus() +{ + if (m_editingNode) + { + m_editingNode = false; + m_activeNode->setEditable(false); + m_activeNode->update(); + } + else if (m_edgeAdding) + { + m_edgeAdding = false; + m_parent->statusBarMsg(tr("Edge adding cancelled.")); + } + else if (m_edgeDeleting) + { + m_edgeDeleting = false; + m_parent->statusBarMsg(tr("Edge deleting cancelled.")); + } + else if(m_showingNodeNumbers) + { + m_hintNumber.clear(); + showingAllNodeNumbers(false); + m_showingNodeNumbers = false; + } +} + void GraphWidget::addEdge(Node *source, Node *destination) { if (destination == m_nodeList.first()) diff --git a/graphwidget.h b/graphwidget.h index a814e81..f8cf443 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -22,6 +22,7 @@ public: void setActiveNodeEditable(); void nodeSelected(Node *node); void nodeMoved(QGraphicsSceneMouseEvent *event); + void nodeLostFocus(); QList edges() const; void contentChanged(const bool &changed = true); diff --git a/node.cpp b/node.cpp index abccdc1..8f9c98e 100644 --- a/node.cpp +++ b/node.cpp @@ -476,6 +476,12 @@ QPainterPath Node::shape () const return path; } +void Node::focusOutEvent(QFocusEvent *event) +{ + Q_UNUSED(event); + m_graph->nodeLostFocus(); +} + double Node::doubleModulo(const double &devided, const double &devisor) { return devided - static_cast(devisor * static_cast(devided diff --git a/node.h b/node.h index 1375a58..41e6860 100644 --- a/node.h +++ b/node.h @@ -56,6 +56,7 @@ protected: void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); QPainterPath shape () const; + void focusOutEvent(QFocusEvent *event); private: