1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 09:57:36 +00:00

Maps: Add MapsSettings with multiple tile providers options

This commit is contained in:
Bastiaan van der Plaat 2023-09-16 21:10:23 +02:00 committed by Andrew Kaster
parent 264782557e
commit aed25991e6
14 changed files with 379 additions and 15 deletions

View file

@ -0,0 +1,16 @@
serenity_component(
MapsSettings
RECOMMENDED
TARGETS MapsSettings
)
compile_gml(MapsSettingsWidget.gml MapsSettingsWidgetGML.cpp)
set(SOURCES
main.cpp
MapsSettingsWidgetGML.cpp
MapsSettingsWidget.cpp
)
serenity_app(MapsSettings ICON app-maps)
target_link_libraries(MapsSettings PRIVATE LibConfig LibCore LibGfx LibGUI LibMain)

View file

@ -0,0 +1,17 @@
/*
* Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/StringView.h>
namespace Maps {
static constexpr StringView default_tile_provider_url_format = "https://tile.openstreetmap.org/{}/{}/{}.png"sv;
static constexpr StringView default_tile_provider_attribution_text = "© OpenStreetMap contributors"sv;
static constexpr StringView default_tile_provider_attribution_url = "https://www.openstreetmap.org/copyright"sv;
}

View file

@ -0,0 +1,107 @@
/*
* Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "MapsSettingsWidget.h"
#include "Defaults.h"
#include <LibConfig/Client.h>
#include <LibGUI/ComboBox.h>
#include <LibGUI/JsonArrayModel.h>
#include <LibGUI/TextBox.h>
#include <LibGUI/Widget.h>
namespace MapsSettings {
ErrorOr<NonnullRefPtr<MapsSettingsWidget>> MapsSettingsWidget::create()
{
auto widget = TRY(try_create());
TRY(widget->setup());
return widget;
}
void MapsSettingsWidget::apply_settings()
{
// Tile Provider
if (m_is_custom_tile_provider) {
Config::write_string("Maps"sv, "MapWidget"sv, "TileProviderUrlFormat"sv, m_custom_tile_provider_textbox->text());
Config::remove_key("Maps"sv, "MapWidget"sv, "TileProviderAttributionText"sv);
Config::remove_key("Maps"sv, "MapWidget"sv, "TileProviderAttributionUrl"sv);
} else {
auto tile_provider_url_format = m_tile_provider_combobox->model()->index(m_tile_provider_combobox->selected_index(), 1).data().to_deprecated_string();
Config::write_string("Maps"sv, "MapWidget"sv, "TileProviderUrlFormat"sv, tile_provider_url_format);
auto tile_provider_attribution_text = m_tile_provider_combobox->model()->index(m_tile_provider_combobox->selected_index(), 2).data().to_deprecated_string();
Config::write_string("Maps"sv, "MapWidget"sv, "TileProviderAttributionText"sv, tile_provider_attribution_text);
auto tile_provider_attribution_url = m_tile_provider_combobox->model()->index(m_tile_provider_combobox->selected_index(), 3).data().to_deprecated_string();
Config::write_string("Maps"sv, "MapWidget"sv, "TileProviderAttributionUrl"sv, tile_provider_attribution_url);
}
}
void MapsSettingsWidget::reset_default_values()
{
set_tile_provider(Maps::default_tile_provider_url_format);
}
ErrorOr<void> MapsSettingsWidget::setup()
{
// Tile Provider
Vector<GUI::JsonArrayModel::FieldSpec> tile_provider_fields;
tile_provider_fields.empend("name", "Name"_string, Gfx::TextAlignment::CenterLeft);
tile_provider_fields.empend("url_format", "URL format"_string, Gfx::TextAlignment::CenterLeft);
tile_provider_fields.empend("attribution_text", "Attribution text"_string, Gfx::TextAlignment::CenterLeft);
tile_provider_fields.empend("attribution_url", "Attribution URL"_string, Gfx::TextAlignment::CenterLeft);
auto tile_providers = GUI::JsonArrayModel::create(DeprecatedString::formatted("{}/MapsTileProviders.json", Core::StandardPaths::config_directory()), move(tile_provider_fields));
tile_providers->invalidate();
Vector<JsonValue> custom_tile_provider;
custom_tile_provider.append("Custom...");
custom_tile_provider.append("");
custom_tile_provider.append("");
custom_tile_provider.append("");
TRY(tile_providers->add(move(custom_tile_provider)));
m_tile_provider_combobox = *find_descendant_of_type_named<GUI::ComboBox>("tile_provider_combobox");
m_tile_provider_combobox->set_model(move(tile_providers));
m_tile_provider_combobox->set_only_allow_values_from_model(true);
m_custom_tile_provider_group = *find_descendant_of_type_named<GUI::Widget>("custom_tile_provider_group");
m_custom_tile_provider_textbox = *find_descendant_of_type_named<GUI::TextBox>("custom_tile_provider_textbox");
m_custom_tile_provider_textbox->set_placeholder(Maps::default_tile_provider_url_format);
m_custom_tile_provider_textbox->on_change = [&]() { set_modified(true); };
m_tile_provider_combobox->on_change = [&](DeprecatedString const&, GUI::ModelIndex const& index) {
auto tile_provider_url_format = m_tile_provider_combobox->model()->index(index.row(), 1).data().to_deprecated_string();
m_is_custom_tile_provider = tile_provider_url_format.is_empty();
m_custom_tile_provider_group->set_enabled(m_is_custom_tile_provider);
set_modified(true);
};
set_tile_provider(Config::read_string("Maps"sv, "MapWidget"sv, "TileProviderUrlFormat"sv, Maps::default_tile_provider_url_format));
return {};
}
void MapsSettingsWidget::set_tile_provider(StringView tile_provider_url_format)
{
bool found = false;
for (int index = 0; index < m_tile_provider_combobox->model()->row_count(); index++) {
auto url_format = m_tile_provider_combobox->model()->index(index, 1).data().to_deprecated_string();
if (url_format == tile_provider_url_format) {
m_tile_provider_combobox->set_selected_index(index, GUI::AllowCallback::No);
found = true;
break;
}
}
if (!found) {
m_is_custom_tile_provider = true;
m_custom_tile_provider_textbox->set_text(tile_provider_url_format, GUI::AllowCallback::No);
m_tile_provider_combobox->set_selected_index(m_tile_provider_combobox->model()->row_count() - 1, GUI::AllowCallback::No);
m_custom_tile_provider_group->set_enabled(true);
} else {
m_custom_tile_provider_group->set_enabled(false);
}
}
}

View file

@ -0,0 +1,58 @@
@MapsSettings::MapsSettingsWidget {
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {
margins: [8]
}
@GUI::GroupBox {
title: "Tile Provider"
fixed_height: 104
layout: @GUI::VerticalBoxLayout {
margins: [16, 8, 8]
spacing: 2
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
spacing: 16
}
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
bitmap: "/res/icons/32x32/search-engine.png"
}
@GUI::Label {
text: "Tile Provider:"
text_alignment: "CenterLeft"
fixed_width: 110
}
@GUI::ComboBox {
name: "tile_provider_combobox"
}
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
spacing: 16
}
name: "custom_tile_provider_group"
@GUI::Widget {
fixed_width: 32
}
@GUI::Label {
text: "Enter URL template:"
text_alignment: "CenterLeft"
fixed_width: 110
}
@GUI::TextBox {
name: "custom_tile_provider_textbox"
}
}
}
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGUI/SettingsWindow.h>
namespace MapsSettings {
class MapsSettingsWidget final : public GUI::SettingsWindow::Tab {
C_OBJECT_ABSTRACT(MapsSettingsWidget)
public:
static ErrorOr<NonnullRefPtr<MapsSettingsWidget>> create();
virtual void apply_settings() override;
virtual void reset_default_values() override;
private:
MapsSettingsWidget() = default;
static ErrorOr<NonnullRefPtr<MapsSettingsWidget>> try_create();
ErrorOr<void> setup();
void set_tile_provider(StringView url);
RefPtr<GUI::ComboBox> m_tile_provider_combobox;
RefPtr<GUI::Widget> m_custom_tile_provider_group;
RefPtr<GUI::TextBox> m_custom_tile_provider_textbox;
bool m_is_custom_tile_provider { false };
};
}

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "MapsSettingsWidget.h"
#include <LibConfig/Client.h>
#include <LibCore/System.h>
#include <LibGUI/Application.h>
#include <LibGUI/Icon.h>
#include <LibGUI/SettingsWindow.h>
#include <LibMain/Main.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
auto app = TRY(GUI::Application::create(arguments));
TRY(Core::System::unveil("/res", "r"));
TRY(Core::System::unveil("/home", "r"));
TRY(Core::System::unveil("/tmp/session/%sid/portal/config", "rw"));
TRY(Core::System::unveil(nullptr, nullptr));
auto app_icon = GUI::Icon::default_icon("app-maps"sv);
auto window = TRY(GUI::SettingsWindow::create("Maps Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes));
window->set_icon(app_icon.bitmap_for_size(16));
(void)TRY(window->add_tab(TRY(MapsSettings::MapsSettingsWidget::create()), "Maps"_string, "maps"sv));
window->show();
return app->exec();
}