Add ability to install event handlers from mouse picking

This commit is contained in:
Dan Paulat 2024-01-13 23:16:38 -06:00
parent ff882f5c06
commit b3e035c53d
26 changed files with 222 additions and 76 deletions

View file

@ -81,7 +81,8 @@ bool DrawLayer::RunMousePicking(
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords)
const common::Coordinate& mouseGeoCoords,
std::shared_ptr<types::EventHandler>& eventHandler)
{
bool itemPicked = false;
@ -93,7 +94,8 @@ bool DrawLayer::RunMousePicking(
mouseLocalPos,
mouseGlobalPos,
mouseCoords,
mouseGeoCoords))
mouseGeoCoords,
eventHandler))
{
// If a draw item was picked, don't process additional items
itemPicked = true;

View file

@ -25,10 +25,11 @@ public:
virtual bool
RunMousePicking(const QMapLibreGL::CustomLayerRenderParameters& params,
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords) override;
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords,
std::shared_ptr<types::EventHandler>& eventHandler) override;
protected:
void AddDrawItem(const std::shared_ptr<gl::draw::DrawItem>& drawItem);

View file

@ -31,7 +31,8 @@ bool GenericLayer::RunMousePicking(
const QPointF& /* mouseLocalPos */,
const QPointF& /* mouseGlobalPos */,
const glm::vec2& /* mousePos */,
const common::Coordinate& /* mouseGeoCoords */)
const common::Coordinate& /* mouseGeoCoords */,
std::shared_ptr<types::EventHandler>& /* eventHandler */)
{
// By default, the layer has nothing to pick
return false;

View file

@ -1,6 +1,7 @@
#pragma once
#include <scwx/qt/map/map_context.hpp>
#include <scwx/qt/types/event_types.hpp>
#include <scwx/common/geographic.hpp>
#include <memory>
@ -38,15 +39,17 @@ public:
* @param [in] mouseGlobalPos Mouse cursor screen position
* @param [in] mouseCoords Mouse cursor location in map screen coordinates
* @param [in] mouseGeoCoords Mouse cursor location in geographic coordinates
* @param [out] eventHandler Event handler associated with picked draw item
*
* @return true if a draw item was picked, otherwise false
*/
virtual bool
RunMousePicking(const QMapLibreGL::CustomLayerRenderParameters& params,
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords);
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords,
std::shared_ptr<types::EventHandler>& eventHandler);
signals:
void NeedsRendering();

View file

@ -98,7 +98,6 @@ public:
// Initialize ImGui Qt backend
ImGui_ImplQt_Init();
ImGui_ImplQt_RegisterWidget(widget_);
// Set Map Provider Details
mapProvider_ = GetMapProvider(generalSettings.map_provider().GetValue());
@ -204,6 +203,8 @@ public:
const MapStyle* currentStyle_;
std::string initialStyleName_ {};
std::shared_ptr<types::EventHandler> pickedEventHandler_ {nullptr};
uint64_t frameDraws_;
double prevLatitude_;
@ -979,6 +980,17 @@ void MapWidgetImpl::AddLayer(const std::string& id,
}
}
bool MapWidget::event(QEvent* e)
{
if (p->pickedEventHandler_ != nullptr &&
p->pickedEventHandler_->event_ != nullptr)
{
p->pickedEventHandler_->event_(e);
}
return QOpenGLWidget::event(e);
}
void MapWidget::enterEvent(QEnterEvent* /* ev */)
{
p->hasMouse_ = true;
@ -1083,6 +1095,7 @@ void MapWidget::initializeGL()
// Initialize ImGui OpenGL3 backend
ImGui::SetCurrentContext(p->imGuiContext_);
ImGui_ImplQt_RegisterWidget(this);
ImGui_ImplOpenGL3_Init();
p->imGuiFontsBuildCount_ =
manager::FontManager::Instance().imgui_fonts_build_count();
@ -1209,7 +1222,8 @@ void MapWidgetImpl::RunMousePicking()
util::maplibre::LatLongToScreenCoordinate(coordinate);
// For each layer in reverse
bool itemPicked = false;
bool itemPicked = false;
std::shared_ptr<types::EventHandler> eventHandler = nullptr;
for (auto it = genericLayers_.rbegin(); it != genericLayers_.rend(); ++it)
{
// Run mouse picking for each layer
@ -1217,7 +1231,8 @@ void MapWidgetImpl::RunMousePicking()
lastPos_,
lastGlobalPos_,
mouseScreenCoordinate,
{coordinate.first, coordinate.second}))
{coordinate.first, coordinate.second},
eventHandler))
{
// If a draw item was picked, don't process additional layers
itemPicked = true;
@ -1229,6 +1244,43 @@ void MapWidgetImpl::RunMousePicking()
if (!itemPicked)
{
util::tooltip::Hide();
if (pickedEventHandler_ != nullptr)
{
// Send leave event to picked event handler
if (pickedEventHandler_->event_ != nullptr)
{
QEvent event(QEvent::Type::Leave);
pickedEventHandler_->event_(&event);
}
// Reset picked event handler
pickedEventHandler_ = nullptr;
}
}
else if (eventHandler != nullptr)
{
// If the event handler changed
if (pickedEventHandler_ != eventHandler)
{
// Send leave event to old event handler
if (pickedEventHandler_ != nullptr &&
pickedEventHandler_->event_ != nullptr)
{
QEvent event(QEvent::Type::Leave);
pickedEventHandler_->event_(&event);
}
// Send enter event to new event handler
if (eventHandler->event_ != nullptr)
{
QEvent event(QEvent::Type::Enter);
eventHandler->event_(&event);
}
// Store picked event handler
pickedEventHandler_ = eventHandler;
}
}
Q_EMIT widget_->MouseCoordinateChanged(

View file

@ -122,6 +122,7 @@ private:
qreal pixelRatio();
// QWidget implementation.
bool event(QEvent* e) override;
void enterEvent(QEnterEvent* ev) override final;
void keyPressEvent(QKeyEvent* ev) override final;
void leaveEvent(QEvent* ev) override final;

View file

@ -274,14 +274,25 @@ void OverlayLayer::Deinitialize()
}
bool OverlayLayer::RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& /* params */,
const QPointF& /* mouseLocalPos */,
const QPointF& /* mouseGlobalPos */,
const glm::vec2& /* mouseCoords */,
const common::Coordinate& /* mouseGeoCoords */)
const QMapLibreGL::CustomLayerRenderParameters& params,
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords,
std::shared_ptr<types::EventHandler>& eventHandler)
{
// If sweep time was picked, don't process additional items
return p->sweepTimePicked_;
if (p->sweepTimePicked_)
{
return true;
}
return DrawLayer::RunMousePicking(params,
mouseLocalPos,
mouseGlobalPos,
mouseCoords,
mouseGeoCoords,
eventHandler);
}
void OverlayLayer::UpdateSweepTimeNextFrame()

View file

@ -21,12 +21,13 @@ public:
void Render(const QMapLibreGL::CustomLayerRenderParameters&) override final;
void Deinitialize() override final;
bool
RunMousePicking(const QMapLibreGL::CustomLayerRenderParameters& params,
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords) override final;
bool RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& params,
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords,
std::shared_ptr<types::EventHandler>& eventHandler) override final;
public slots:
void UpdateSweepTimeNextFrame();

View file

@ -332,7 +332,8 @@ bool RadarProductLayer::RunMousePicking(
const QPointF& /* mouseLocalPos */,
const QPointF& mouseGlobalPos,
const glm::vec2& /* mouseCoords */,
const common::Coordinate& mouseGeoCoords)
const common::Coordinate& mouseGeoCoords,
std::shared_ptr<types::EventHandler>& /* eventHandler */)
{
bool itemPicked = false;

View file

@ -23,10 +23,11 @@ public:
virtual bool
RunMousePicking(const QMapLibreGL::CustomLayerRenderParameters& params,
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords) override;
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords,
std::shared_ptr<types::EventHandler>& eventHandler) override;
private:
void UpdateColorTable();

View file

@ -158,7 +158,8 @@ bool RadarSiteLayer::RunMousePicking(
const QPointF& /* mouseLocalPos */,
const QPointF& mouseGlobalPos,
const glm::vec2& /* mouseCoords */,
const common::Coordinate& /* mouseGeoCoords */)
const common::Coordinate& /* mouseGeoCoords */,
std::shared_ptr<types::EventHandler>& /* eventHandler */)
{
if (!p->hoverText_.empty())
{

View file

@ -22,12 +22,13 @@ public:
void Render(const QMapLibreGL::CustomLayerRenderParameters&) override final;
void Deinitialize() override final;
bool
RunMousePicking(const QMapLibreGL::CustomLayerRenderParameters& params,
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords) override final;
bool RunMousePicking(
const QMapLibreGL::CustomLayerRenderParameters& params,
const QPointF& mouseLocalPos,
const QPointF& mouseGlobalPos,
const glm::vec2& mouseCoords,
const common::Coordinate& mouseGeoCoords,
std::shared_ptr<types::EventHandler>& eventHandler) override final;
signals:
void RadarSiteSelected(const std::string& id);