diff --git a/lib/qtgraph/graphwidget.cpp b/lib/qtgraph/graphwidget.cpp index 6582f08..3a4b3c1 100644 --- a/lib/qtgraph/graphwidget.cpp +++ b/lib/qtgraph/graphwidget.cpp @@ -192,34 +192,40 @@ void GraphWidget::keyPressEvent(QKeyEvent *e) break; } case Qt::Key_Space: { - for (QList::iterator it = m_route.begin(); it != m_route.end(); ++it) - (*it)->setIsRoute(false); + modifyRoute(); + } + default: + QGraphicsView::keyPressEvent(e); + } +} - QList selectedItems = scene()->selectedItems(); - if (selectedItems.isEmpty()) - break; +void GraphWidget::modifyRoute() +{ + for (QList::iterator it = m_route.begin(); it != m_route.end(); ++it) + (*it)->setIsRoute(false); - QGraphicsItem* selectedItem = selectedItems.first(); - Node* selectedNode = dynamic_cast(selectedItem); + QList selectedItems = scene()->selectedItems(); + if (selectedItems.isEmpty()) + break; - const QPoint global_p = QCursor::pos(); - const QPoint widget_p = mapFromGlobal(global_p); - const QPointF scene_p = mapToScene(widget_p); + QGraphicsItem* selectedItem = selectedItems.first(); + Node* selectedNode = dynamic_cast(selectedItem); - QGraphicsItem* item_under_mouse = scene()->itemAt(scene_p); - Node* nodeUnderMouse = dynamic_cast(item_under_mouse); + const QPoint global_p = QCursor::pos(); + const QPoint widget_p = mapFromGlobal(global_p); + const QPointF scene_p = mapToScene(widget_p); - if (nodeUnderMouse != 0 && nodeUnderMouse != selectedNode) { - m_route = calculateShortestRoute(scene(), m_graph, selectedNode, nodeUnderMouse); - for (QList::iterator it = m_route.begin(); it != m_route.end(); ++it) - (*it)->setIsRoute(true); - } - } - default: - QGraphicsView::keyPressEvent(e); - } + QGraphicsItem* item_under_mouse = scene()->itemAt(scene_p); + Node* nodeUnderMouse = dynamic_cast(item_under_mouse); + + if (nodeUnderMouse != 0 && nodeUnderMouse != selectedNode) { + m_route = calculateShortestRoute(scene(), m_graph, selectedNode, nodeUnderMouse); + for (QList::iterator it = m_route.begin(); it != m_route.end(); ++it) + (*it)->setIsRoute(true); + } } + void GraphWidget::removeEdge(Node* selectedNode, Node* nodeUnderMouse) { const float2 source_pos = float2FromQPointF(selectedNode->pos()); diff --git a/lib/qtgraph/graphwidget.hpp b/lib/qtgraph/graphwidget.hpp index 3e25d50..2d8d0eb 100644 --- a/lib/qtgraph/graphwidget.hpp +++ b/lib/qtgraph/graphwidget.hpp @@ -38,6 +38,7 @@ private: void removeNode(Node* selectedNode); void insertEdge(Node* selectedNode, Node* nodeUnderMouse); void removeEdge(Node* selectedNode, Node* nodeUnderMouse); + void modifyRoute(); Graph* m_graph; QPixmap* m_background;