ladybird/Userland/Applications/MapsSettings/MapsSettingsWidget.cpp
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

108 lines
4.9 KiB
C++

/*
* 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_byte_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_byte_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_byte_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(ByteString::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 = [&](ByteString const&, GUI::ModelIndex const& index) {
auto tile_provider_url_format = m_tile_provider_combobox->model()->index(index.row(), 1).data().to_byte_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_byte_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);
}
}
}