1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:27:35 +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)