From 073e922c24b8d357bc20605338cbcc6614c974e6 Mon Sep 17 00:00:00 2001 From: Tadej Novak Date: Tue, 25 Jun 2024 17:27:06 +0200 Subject: [PATCH] Add mouse events with coordinate to GLWidget --- src/widgets/gl_widget.cpp | 26 ++++++++++++++++++++++++++ src/widgets/gl_widget.hpp | 7 +++++++ test/widgets/gl_tester.cpp | 12 ++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/widgets/gl_widget.cpp b/src/widgets/gl_widget.cpp index d3a22b3..652b4d9 100644 --- a/src/widgets/gl_widget.cpp +++ b/src/widgets/gl_widget.cpp @@ -25,10 +25,36 @@ Map *GLWidget::map() { } void GLWidget::mousePressEvent(QMouseEvent *event) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + const QPointF &position = event->position(); +#else + const QPointF &position = event->localPos(); +#endif + emit onMousePressEvent(d_ptr->m_map->coordinateForPixel(position)); + if (event->type() == QEvent::MouseButtonDblClick) { + emit onMouseDoubleClickEvent(d_ptr->m_map->coordinateForPixel(position)); + } + d_ptr->handleMousePressEvent(event); } +void GLWidget::mouseReleaseEvent(QMouseEvent *event) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + const QPointF &position = event->position(); +#else + const QPointF &position = event->localPos(); +#endif + emit onMouseReleaseEvent(d_ptr->m_map->coordinateForPixel(position)); +} + void GLWidget::mouseMoveEvent(QMouseEvent *event) { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + const QPointF &position = event->position(); +#else + const QPointF &position = event->localPos(); +#endif + emit onMouseMoveEvent(d_ptr->m_map->coordinateForPixel(position)); + d_ptr->handleMouseMoveEvent(event); } diff --git a/src/widgets/gl_widget.hpp b/src/widgets/gl_widget.hpp index b2630da..ba67ac6 100644 --- a/src/widgets/gl_widget.hpp +++ b/src/widgets/gl_widget.hpp @@ -35,9 +35,16 @@ class Q_MAPLIBRE_WIDGETS_EXPORT GLWidget : public QOpenGLWidget { Map *map(); +signals: + void onMouseDoubleClickEvent(QMapLibre::Coordinate coordinate); + void onMousePressEvent(QMapLibre::Coordinate coordinate); + void onMouseReleaseEvent(QMapLibre::Coordinate coordinate); + void onMouseMoveEvent(QMapLibre::Coordinate coordinate); + protected: // QWidget implementation. void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void wheelEvent(QWheelEvent *event) override; diff --git a/test/widgets/gl_tester.cpp b/test/widgets/gl_tester.cpp index fc077a1..1739d5c 100644 --- a/test/widgets/gl_tester.cpp +++ b/test/widgets/gl_tester.cpp @@ -21,6 +21,18 @@ void GLTester::initializeAnimation() { connect(m_zoomAnimation.get(), &QPropertyAnimation::finished, this, &GLTester::animationFinished); connect(m_zoomAnimation.get(), &QPropertyAnimation::valueChanged, this, &GLTester::animationValueChanged); + + connect(this, &GLTester::onMousePressEvent, [](Coordinate coordinate) { + qDebug() << "onMousePressEvent" << coordinate; + }); + connect(this, &GLTester::onMouseReleaseEvent, [](Coordinate coordinate) { + qDebug() << "onMouseReleaseEvent" << coordinate; + }); + connect(this, &GLTester::onMouseDoubleClickEvent, [](Coordinate coordinate) { + qDebug() << "onMouseDoubleClickEvent" << coordinate; + }); + connect( + this, &GLTester::onMouseMoveEvent, [](Coordinate coordinate) { qDebug() << "onMouseMoveEvent" << coordinate; }); } int GLTester::selfTest() {