Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mouse events with coordinate to GLWidget #141

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading