1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:27:44 +00:00

Maps: Add context menu to the map widget

This commit is contained in:
Bastiaan van der Plaat 2023-09-18 08:14:37 +02:00 committed by Andrew Kaster
parent ece9908070
commit e3cd789ea8
2 changed files with 45 additions and 0 deletions

View file

@ -8,7 +8,9 @@
#include "MapWidget.h"
#include <AK/URL.h>
#include <LibDesktop/Launcher.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
#include <LibGfx/ImageFormats/ImageDecoder.h>
#include <LibProtocol/Request.h>
@ -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<double>(event.position().y() - height() / 2) / TILE_SIZE, m_zoom),
tile_x_to_longitude(longitude_to_tile_x(m_center.longitude, m_zoom) + static_cast<double>(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)

View file

@ -9,6 +9,7 @@
#include <AK/Queue.h>
#include <LibGUI/Frame.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
#include <LibProtocol/Request.h>
#include <LibProtocol/RequestClient.h>
@ -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<GUI::Menu> m_context_menu;
bool m_scale_enabled {};
int m_scale_max_width {};
bool m_attribution_enabled {};