Skip to content

Commit

Permalink
Add fusion style to settings (enables dark mode)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaulat committed Nov 24, 2023
1 parent e69f5c8 commit 6e390a1
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 67 deletions.
1 change: 1 addition & 0 deletions scwx-qt/scwx-qt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ set(SRC_TYPES source/scwx/qt/types/alert_types.cpp
source/scwx/qt/types/imgui_font.cpp
source/scwx/qt/types/layer_types.cpp
source/scwx/qt/types/map_types.cpp
source/scwx/qt/types/qt_types.cpp
source/scwx/qt/types/radar_product_record.cpp
source/scwx/qt/types/text_event_key.cpp
source/scwx/qt/types/text_types.cpp
Expand Down
11 changes: 11 additions & 0 deletions scwx-qt/source/scwx/qt/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <scwx/qt/manager/radar_product_manager.hpp>
#include <scwx/qt/manager/resource_manager.hpp>
#include <scwx/qt/manager/settings_manager.hpp>
#include <scwx/qt/settings/general_settings.hpp>
#include <scwx/qt/types/qt_types.hpp>
#include <scwx/qt/ui/setup/setup_wizard.hpp>
#include <scwx/network/cpr.hpp>
#include <scwx/util/logger.hpp>
Expand Down Expand Up @@ -72,6 +74,15 @@ int main(int argc, char* argv[])
scwx::qt::config::RadarSite::Initialize();
scwx::qt::manager::SettingsManager::Instance().Initialize();

// Theme
auto uiStyle = scwx::qt::types::GetUiStyle(
scwx::qt::settings::GeneralSettings::Instance().theme().GetValue());
if (uiStyle != scwx::qt::types::UiStyle::Default)
{
QApplication::setStyle(
QString::fromStdString(scwx::qt::types::GetUiStyleName(uiStyle)));
}

// Run initial setup if required
if (scwx::qt::ui::setup::SetupWizard::IsSetupRequired())
{
Expand Down
31 changes: 31 additions & 0 deletions scwx-qt/source/scwx/qt/settings/general_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <scwx/qt/settings/settings_container.hpp>
#include <scwx/qt/map/map_provider.hpp>
#include <scwx/qt/types/alert_types.hpp>
#include <scwx/qt/types/qt_types.hpp>

#include <array>

Expand All @@ -25,9 +26,12 @@ class GeneralSettings::Impl
types::GetAlertActionName(types::AlertAction::Go);
std::string defaultMapProviderValue =
map::GetMapProviderName(map::MapProvider::MapTiler);
std::string defaultThemeValue =
types::GetUiStyleName(types::UiStyle::Default);

boost::to_lower(defaultDefaultAlertActionValue);
boost::to_lower(defaultMapProviderValue);
boost::to_lower(defaultThemeValue);

antiAliasingEnabled_.SetDefault(true);
debugEnabled_.SetDefault(false);
Expand All @@ -42,6 +46,7 @@ class GeneralSettings::Impl
mapProvider_.SetDefault(defaultMapProviderValue);
mapboxApiKey_.SetDefault("?");
maptilerApiKey_.SetDefault("?");
theme_.SetDefault(defaultThemeValue);
trackLocation_.SetDefault(false);
updateNotificationsEnabled_.SetDefault(true);

Expand Down Expand Up @@ -102,6 +107,24 @@ class GeneralSettings::Impl
{ return !value.empty(); });
maptilerApiKey_.SetValidator([](const std::string& value)
{ return !value.empty(); });
theme_.SetValidator(
[](const std::string& value)
{
for (types::UiStyle uiStyle : types::UiStyleIterator())
{
// If the value is equal to a lower case UI style name
std::string uiStyleName = types::GetUiStyleName(uiStyle);
boost::to_lower(uiStyleName);
if (value == uiStyleName)
{
// Regard as a match, valid
return true;
}
}

// No match found, invalid
return false;
});
}

~Impl() {}
Expand All @@ -119,6 +142,7 @@ class GeneralSettings::Impl
SettingsVariable<std::string> mapProvider_ {"map_provider"};
SettingsVariable<std::string> mapboxApiKey_ {"mapbox_api_key"};
SettingsVariable<std::string> maptilerApiKey_ {"maptiler_api_key"};
SettingsVariable<std::string> theme_ {"theme"};
SettingsVariable<bool> trackLocation_ {"track_location"};
SettingsVariable<bool> updateNotificationsEnabled_ {"update_notifications"};
};
Expand All @@ -139,6 +163,7 @@ GeneralSettings::GeneralSettings() :
&p->mapProvider_,
&p->mapboxApiKey_,
&p->maptilerApiKey_,
&p->theme_,
&p->trackLocation_,
&p->updateNotificationsEnabled_});
SetDefaults();
Expand Down Expand Up @@ -215,6 +240,11 @@ SettingsVariable<std::string>& GeneralSettings::maptiler_api_key() const
return p->maptilerApiKey_;
}

SettingsVariable<std::string>& GeneralSettings::theme() const
{
return p->theme_;
}

SettingsVariable<bool>& GeneralSettings::track_location() const
{
return p->trackLocation_;
Expand Down Expand Up @@ -259,6 +289,7 @@ bool operator==(const GeneralSettings& lhs, const GeneralSettings& rhs)
lhs.p->mapProvider_ == rhs.p->mapProvider_ &&
lhs.p->mapboxApiKey_ == rhs.p->mapboxApiKey_ &&
lhs.p->maptilerApiKey_ == rhs.p->maptilerApiKey_ &&
lhs.p->theme_ == rhs.p->theme_ &&
lhs.p->trackLocation_ == rhs.p->trackLocation_ &&
lhs.p->updateNotificationsEnabled_ ==
rhs.p->updateNotificationsEnabled_);
Expand Down
1 change: 1 addition & 0 deletions scwx-qt/source/scwx/qt/settings/general_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GeneralSettings : public SettingsCategory
SettingsVariable<std::string>& map_provider() const;
SettingsVariable<std::string>& mapbox_api_key() const;
SettingsVariable<std::string>& maptiler_api_key() const;
SettingsVariable<std::string>& theme() const;
SettingsVariable<bool>& track_location() const;
SettingsVariable<bool>& update_notifications_enabled() const;

Expand Down
42 changes: 42 additions & 0 deletions scwx-qt/source/scwx/qt/types/qt_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <scwx/qt/types/qt_types.hpp>

#include <boost/algorithm/string.hpp>

namespace scwx
{
namespace qt
{
namespace types
{

static const std::unordered_map<UiStyle, std::string> uiStyleName_ {
{UiStyle::Default, "Default"},
{UiStyle::Fusion, "Fusion"},
{UiStyle::Unknown, "?"}};

UiStyle GetUiStyle(const std::string& name)
{
auto result =
std::find_if(uiStyleName_.cbegin(),
uiStyleName_.cend(),
[&](const std::pair<UiStyle, std::string>& pair) -> bool
{ return boost::iequals(pair.second, name); });

if (result != uiStyleName_.cend())
{
return result->first;
}
else
{
return UiStyle::Unknown;
}
}

std::string GetUiStyleName(UiStyle uiStyle)
{
return uiStyleName_.at(uiStyle);
}

} // namespace types
} // namespace qt
} // namespace scwx
16 changes: 16 additions & 0 deletions scwx-qt/source/scwx/qt/types/qt_types.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <scwx/util/iterator.hpp>

#include <string>

#include <Qt>

namespace scwx
Expand All @@ -16,6 +20,18 @@ enum ItemDataRole
RawDataRole
};

enum UiStyle
{
Default,
Fusion,
Unknown
};
typedef scwx::util::Iterator<UiStyle, UiStyle::Default, UiStyle::Fusion>
UiStyleIterator;

UiStyle GetUiStyle(const std::string& name);
std::string GetUiStyleName(UiStyle alertAction);

} // namespace types
} // namespace qt
} // namespace scwx
43 changes: 40 additions & 3 deletions scwx-qt/source/scwx/qt/ui/settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <scwx/qt/settings/text_settings.hpp>
#include <scwx/qt/types/alert_types.hpp>
#include <scwx/qt/types/font_types.hpp>
#include <scwx/qt/types/qt_types.hpp>
#include <scwx/qt/types/text_types.hpp>
#include <scwx/qt/ui/radar_site_dialog.hpp>
#include <scwx/qt/util/color.hpp>
Expand Down Expand Up @@ -98,6 +99,7 @@ class SettingsDialogImpl
&mapProvider_,
&mapboxApiKey_,
&mapTilerApiKey_,
&theme_,
&defaultAlertAction_,
&antiAliasingEnabled_,
&updateNotificationsEnabled_,
Expand Down Expand Up @@ -175,6 +177,7 @@ class SettingsDialogImpl
settings::SettingsInterface<std::string> mapboxApiKey_ {};
settings::SettingsInterface<std::string> mapTilerApiKey_ {};
settings::SettingsInterface<std::string> defaultAlertAction_ {};
settings::SettingsInterface<std::string> theme_ {};
settings::SettingsInterface<bool> antiAliasingEnabled_ {};
settings::SettingsInterface<bool> updateNotificationsEnabled_ {};
settings::SettingsInterface<bool> debugEnabled_ {};
Expand Down Expand Up @@ -366,6 +369,43 @@ void SettingsDialogImpl::ConnectSignals()

void SettingsDialogImpl::SetupGeneralTab()
{
settings::GeneralSettings& generalSettings =
settings::GeneralSettings::Instance();

for (const auto& uiStyle : types::UiStyleIterator())
{
self_->ui->themeComboBox->addItem(
QString::fromStdString(types::GetUiStyleName(uiStyle)));
}

theme_.SetSettingsVariable(generalSettings.theme());
theme_.SetMapFromValueFunction(
[](const std::string& text) -> std::string
{
for (types::UiStyle uiStyle : types::UiStyleIterator())
{
const std::string uiStyleName = types::GetUiStyleName(uiStyle);

if (boost::iequals(text, uiStyleName))
{
// Return UI style label
return uiStyleName;
}
}

// UI style label not found, return unknown
return "?";
});
theme_.SetMapToValueFunction(
[](std::string text) -> std::string
{
// Convert label to lower case and return
boost::to_lower(text);
return text;
});
theme_.SetEditWidget(self_->ui->themeComboBox);
theme_.SetResetButton(self_->ui->resetThemeButton);

auto radarSites = config::RadarSite::GetAll();

// Sort radar sites by ID
Expand All @@ -382,9 +422,6 @@ void SettingsDialogImpl::SetupGeneralTab()
self_->ui->radarSiteComboBox->addItem(text);
}

settings::GeneralSettings& generalSettings =
settings::GeneralSettings::Instance();

defaultRadarSite_.SetSettingsVariable(generalSettings.default_radar_site());
defaultRadarSite_.SetMapFromValueFunction(
[](const std::string& id) -> std::string
Expand Down
Loading

0 comments on commit 6e390a1

Please sign in to comment.