From e3cd789ea8b65d0a2df38883d1d03c078aa8acb5 Mon Sep 17 00:00:00 2001 From: Bastiaan van der Plaat Date: Mon, 18 Sep 2023 08:14:37 +0200 Subject: [PATCH] Maps: Add context menu to the map widget --- Userland/Applications/Maps/MapWidget.cpp | 40 ++++++++++++++++++++++++ Userland/Applications/Maps/MapWidget.h | 5 +++ 2 files changed, 45 insertions(+) diff --git a/Userland/Applications/Maps/MapWidget.cpp b/Userland/Applications/Maps/MapWidget.cpp index 3ae2b8911a..cfd9e3d6ac 100644 --- a/Userland/Applications/Maps/MapWidget.cpp +++ b/Userland/Applications/Maps/MapWidget.cpp @@ -8,7 +8,9 @@ #include "MapWidget.h" #include #include +#include #include +#include #include #include @@ -52,6 +54,7 @@ MapWidget::MapWidget(Options const& options) : m_tile_layer_url(options.tile_layer_url) , m_center(options.center) , m_zoom(options.zoom) + , m_context_menu_enabled(options.context_menu_enabled) , m_scale_enabled(options.scale_enabled) , m_scale_max_width(options.scale_max_width) , m_attribution_enabled(options.attribution_enabled) @@ -171,6 +174,43 @@ void MapWidget::mousewheel_event(GUI::MouseEvent& event) set_zoom_for_mouse_event(new_zoom, event); } +void MapWidget::context_menu_event(GUI::ContextMenuEvent& event) +{ + if (!m_context_menu_enabled) + return; + + LatLng latlng = { tile_y_to_latitude(latitude_to_tile_y(m_center.latitude, m_zoom) + static_cast(event.position().y() - height() / 2) / TILE_SIZE, m_zoom), + tile_x_to_longitude(longitude_to_tile_x(m_center.longitude, m_zoom) + static_cast(event.position().x() - width() / 2) / TILE_SIZE, m_zoom) }; + + m_context_menu = GUI::Menu::construct(); + m_context_menu->add_action(GUI::Action::create( + "&Copy Coordinates to Clipboard", MUST(Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"sv)), [latlng](auto&) { + GUI::Clipboard::the().set_plain_text(MUST(String::formatted("{}, {}", latlng.latitude, latlng.longitude)).bytes_as_string_view()); + })); + m_context_menu->add_separator(); + auto link_icon = MUST(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-symlink.png"sv)); + m_context_menu->add_action(GUI::Action::create( + "Open in &OpenStreetMap", link_icon, [this, latlng](auto&) { + Desktop::Launcher::open(URL(MUST(String::formatted("https://www.openstreetmap.org/#map={}/{}/{}", m_zoom, latlng.latitude, latlng.longitude)))); + })); + m_context_menu->add_action(GUI::Action::create( + "Open in &Google Maps", link_icon, [this, latlng](auto&) { + Desktop::Launcher::open(URL(MUST(String::formatted("https://www.google.com/maps/@{},{},{}z", latlng.latitude, latlng.longitude, m_zoom)))); + })); + m_context_menu->add_action(GUI::Action::create( + "Open in &Bing Maps", link_icon, [this, latlng](auto&) { + Desktop::Launcher::open(URL(MUST(String::formatted("https://www.bing.com/maps/?cp={}~{}&lvl={}", latlng.latitude, latlng.longitude, m_zoom)))); + })); + m_context_menu->add_action(GUI::Action::create( + "Open in &DuckDuckGo Maps", link_icon, [latlng](auto&) { + Desktop::Launcher::open(URL(MUST(String::formatted("https://duckduckgo.com/?q={},+{}&ia=web&iaxm=maps", latlng.latitude, latlng.longitude)))); + })); + m_context_menu->add_separator(); + m_context_menu->add_action(GUI::Action::create( + "Center &map here", MUST(Gfx::Bitmap::load_from_file("/res/icons/16x16/scale.png"sv)), [this, latlng](auto&) { set_center(latlng); })); + m_context_menu->popup(event.screen_position()); +} + void MapWidget::set_zoom_for_mouse_event(int zoom, GUI::MouseEvent& event) { if (zoom == m_zoom || zoom < ZOOM_MIN || zoom > ZOOM_MAX) diff --git a/Userland/Applications/Maps/MapWidget.h b/Userland/Applications/Maps/MapWidget.h index 285568fbdd..8da48b7600 100644 --- a/Userland/Applications/Maps/MapWidget.h +++ b/Userland/Applications/Maps/MapWidget.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -28,6 +29,7 @@ public: String tile_layer_url { "https://tile.openstreetmap.org/{}/{}/{}.png"_string }; LatLng center; int zoom; + bool context_menu_enabled { true }; bool scale_enabled { true }; int scale_max_width { 100 }; bool attribution_enabled { true }; @@ -120,6 +122,7 @@ private: virtual void mousedown_event(GUI::MouseEvent&) override; virtual void mouseup_event(GUI::MouseEvent&) override; virtual void mousewheel_event(GUI::MouseEvent&) override; + virtual void context_menu_event(GUI::ContextMenuEvent& event) override; virtual void paint_event(GUI::PaintEvent&) override; void set_zoom_for_mouse_event(int zoom, GUI::MouseEvent&); @@ -154,6 +157,8 @@ private: String m_tile_layer_url; LatLng m_center; int m_zoom {}; + bool m_context_menu_enabled {}; + RefPtr m_context_menu; bool m_scale_enabled {}; int m_scale_max_width {}; bool m_attribution_enabled {};