From 2c65f4ca90d3b9ff4386845f85d4063778729697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20G=C3=A9czi?= Date: Tue, 17 Dec 2024 00:00:00 +0000 Subject: [PATCH] [qt] Add Night Mode to preferences dialog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ferenc Géczi --- indexer/map_style.cpp | 38 ++++++++++++++++++++++++++++++++++++++ indexer/map_style.hpp | 2 ++ platform/settings.cpp | 1 + platform/settings.hpp | 1 + platform/style_utils.hpp | 11 +++++++++++ qt/preferences_dialog.cpp | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+) create mode 100644 platform/style_utils.hpp diff --git a/indexer/map_style.cpp b/indexer/map_style.cpp index f44dd87807..7ec8b4b9b8 100644 --- a/indexer/map_style.cpp +++ b/indexer/map_style.cpp @@ -64,3 +64,41 @@ bool MapStyleIsDark(MapStyle mapStyle) } return false; } + +MapStyle GetDarkMapStyleVariant(MapStyle mapStyle) +{ + if (MapStyleIsDark(mapStyle) || mapStyle == MapStyleMerged) + return mapStyle; + + switch (mapStyle) + { + case MapStyleDefaultLight: + return MapStyleDefaultDark; + case MapStyleVehicleLight: + return MapStyleVehicleDark; + case MapStyleOutdoorsLight: + return MapStyleOutdoorsDark; + default: + ASSERT(false, ()); + return MapStyleDefaultDark; + } +} + +MapStyle GetLightMapStyleVariant(MapStyle mapStyle) +{ + if (!MapStyleIsDark(mapStyle)) + return mapStyle; + + switch (mapStyle) + { + case MapStyleDefaultDark: + return MapStyleDefaultLight; + case MapStyleVehicleDark: + return MapStyleVehicleLight; + case MapStyleOutdoorsDark: + return MapStyleOutdoorsLight; + default: + ASSERT(false, ()); + return MapStyleDefaultLight; + } +} diff --git a/indexer/map_style.hpp b/indexer/map_style.hpp index 9717367645..7971a2e8d0 100644 --- a/indexer/map_style.hpp +++ b/indexer/map_style.hpp @@ -23,3 +23,5 @@ extern MapStyle MapStyleFromSettings(std::string const & str); extern std::string MapStyleToString(MapStyle mapStyle); extern std::string DebugPrint(MapStyle mapStyle); extern bool MapStyleIsDark(MapStyle mapStyle); +extern MapStyle GetDarkMapStyleVariant(MapStyle mapStyle); +extern MapStyle GetLightMapStyleVariant(MapStyle mapStyle); diff --git a/platform/settings.cpp b/platform/settings.cpp index 371b49bcba..0036adc124 100644 --- a/platform/settings.cpp +++ b/platform/settings.cpp @@ -24,6 +24,7 @@ std::string_view kMeasurementUnits = "Units"; std::string_view kMapLanguageCode = "MapLanguageCode"; std::string_view kDeveloperMode = "DeveloperMode"; std::string_view kDonateUrl = "DonateUrl"; +std::string_view kNightMode = "NightMode"; StringStorage::StringStorage() : StringStorageBase(GetPlatform().SettingsPathForFile(SETTINGS_FILE_NAME)) {} diff --git a/platform/settings.hpp b/platform/settings.hpp index 9a0e0457ac..41f0a357b3 100644 --- a/platform/settings.hpp +++ b/platform/settings.hpp @@ -13,6 +13,7 @@ extern std::string_view kMeasurementUnits; extern std::string_view kDeveloperMode; extern std::string_view kMapLanguageCode; extern std::string_view kDonateUrl; +extern std::string_view kNightMode; template bool FromString(std::string const & str, T & outValue); diff --git a/platform/style_utils.hpp b/platform/style_utils.hpp new file mode 100644 index 0000000000..5e845e50a2 --- /dev/null +++ b/platform/style_utils.hpp @@ -0,0 +1,11 @@ +#pragma once + +namespace style_utils +{ +enum class NightMode : uint8_t +{ + Off = 0, + On = 1, +}; + +} // namespace style_utils diff --git a/qt/preferences_dialog.cpp b/qt/preferences_dialog.cpp index c52c224086..acc0fa8276 100644 --- a/qt/preferences_dialog.cpp +++ b/qt/preferences_dialog.cpp @@ -1,11 +1,13 @@ #include "qt/preferences_dialog.hpp" +#include "indexer/map_style.hpp" #include "coding/string_utf8_multilang.hpp" #include "map/framework.hpp" #include "platform/measurement_utils.hpp" #include "platform/preferred_languages.hpp" #include "platform/settings.hpp" +#include "platform/style_utils.hpp" #include #include @@ -134,6 +136,38 @@ namespace qt }); } + QButtonGroup * nightModeGroup = new QButtonGroup(this); + QGroupBox * nightModeRadioBox = new QGroupBox("Night Mode"); + { + using namespace style_utils; + QHBoxLayout * layout = new QHBoxLayout(); + + QRadioButton * radioButton = new QRadioButton("Off"); + layout->addWidget(radioButton); + nightModeGroup->addButton(radioButton, static_cast(NightMode::Off)); + + radioButton = new QRadioButton("On"); + layout->addWidget(radioButton); + nightModeGroup->addButton(radioButton, static_cast(NightMode::On)); + + nightModeRadioBox->setLayout(layout); + + int i; + if (!settings::Get(settings::kNightMode, i)) + { + i = static_cast(MapStyleIsDark(framework.GetMapStyle()) ? NightMode::On : NightMode::Off); + settings::Set(settings::kNightMode, i); + } + nightModeGroup->button(i)->setChecked(true); + + void (QButtonGroup::* buttonClicked)(int) = &QButtonGroup::idClicked; + connect(nightModeGroup, buttonClicked, [&framework](int i) + { + NightMode nightMode = static_cast(i); + settings::Set(settings::kNightMode, i); + framework.SetMapStyle((nightMode == NightMode::Off) ? GetLightMapStyleVariant(framework.GetMapStyle()) : GetDarkMapStyleVariant(framework.GetMapStyle())); + }); + } #ifdef BUILD_DESIGNER QCheckBox * indexRegenCheckBox = new QCheckBox("Enable auto regeneration of geometry index"); @@ -168,6 +202,7 @@ namespace qt finalLayout->addWidget(developerModeCheckBox); finalLayout->addWidget(mapLanguageLabel); finalLayout->addWidget(mapLanguageComboBox); + finalLayout->addWidget(nightModeRadioBox); #ifdef BUILD_DESIGNER finalLayout->addWidget(indexRegenCheckBox); #endif