From 8c3e0576acf8713f5bfd8dca785025e6cfbda421 Mon Sep 17 00:00:00 2001 From: dmatetelki Date: Tue, 14 Jul 2015 15:39:27 +0200 Subject: [PATCH] GraphWidget::mouseMoveEvent print x,y coordinates of mouse and node in statusbar --- lib/qtgraph/graphwidget.cpp | 40 +++++++++++++++++++++++-------------- lib/qtgraph/graphwidget.hpp | 1 + 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/qtgraph/graphwidget.cpp b/lib/qtgraph/graphwidget.cpp index 1a1f48c..b19d525 100644 --- a/lib/qtgraph/graphwidget.cpp +++ b/lib/qtgraph/graphwidget.cpp @@ -16,6 +16,9 @@ #include +#include +#include + #include #include @@ -39,18 +42,10 @@ namespace std { return dist(a, b); } }; -} +} // namespace std namespace { -// for the map -// bool operator< (const float2& v1, const float2& v2) -// { -// return length(v1) < length(v2); -// } - - - float2 inline float2FromQPointF(const QPointF& p) { return float2(p.x(), p.y()); @@ -61,11 +56,6 @@ QPointF inline QPointFFromfloat2(const float2& f) return QPointF(f.x, f.y); } -// inline float dist(const float2& v1, const float2& v2) -// { -// return sqrt(pow((v2.x - v1.x),2) + pow((v2.y - v1.y),2)); -// } - QList calculateShortestRoute(const QGraphicsScene* scene, const Graph* graph, const Node* source, @@ -270,7 +260,6 @@ void GraphWidget::showLines() d_n = dynamic_cast(gi); } insertEdge(s_n, d_n); - m_graph->addEdge(s, d); } } } @@ -371,6 +360,27 @@ void GraphWidget::scaleView(qreal scaleFactor) scale(scaleFactor, scaleFactor); } +void GraphWidget::mouseMoveEvent(QMouseEvent* e) +{ + const QPoint global_p = QCursor::pos(); + const QPoint widget_p = mapFromGlobal(global_p); + const QPointF scene_p = mapToScene(widget_p); + + const QGraphicsItem* i = scene()->itemAt(scene_p); + const Node* n = dynamic_cast(i); + + QString msg(QString("(x,y) coordinates: (%1,%2)").arg(e->x()).arg(e->y())); + if (n != 0) + msg += QString("(node) coordinates: (%1,%2)").arg(n->pos().x()).arg(n->pos().y()); + + QObject* p = parentWidget(); + QMainWindow* w = dynamic_cast(p); + w->statusBar()->showMessage(msg); + + QGraphicsView::mouseMoveEvent(e); +} + + void GraphWidget::zoomIn() { scaleView(qreal(1.2)); diff --git a/lib/qtgraph/graphwidget.hpp b/lib/qtgraph/graphwidget.hpp index 7822759..7c1c535 100644 --- a/lib/qtgraph/graphwidget.hpp +++ b/lib/qtgraph/graphwidget.hpp @@ -30,6 +30,7 @@ protected: void keyPressEvent(QKeyEvent *event); void wheelEvent(QWheelEvent *event); void drawBackground(QPainter *painter, const QRectF &rect); + void mouseMoveEvent( QMouseEvent* e ); void scaleView(qreal scaleFactor);