Skip to content

Commit

Permalink
Add mouse events with coordinate to GLWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
ntadej committed Jun 25, 2024
1 parent 7ad4166 commit 073e922
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/widgets/gl_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
7 changes: 7 additions & 0 deletions src/widgets/gl_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 12 additions & 0 deletions test/widgets/gl_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 073e922

Please sign in to comment.