Skip to content

Commit

Permalink
Add Fusion Light and Fusion Dark styles
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaulat committed Oct 17, 2024
1 parent e9ee5c6 commit 9ee6e2e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
36 changes: 24 additions & 12 deletions scwx-qt/source/scwx/qt/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
#include <fmt/format.h>
#include <QApplication>
#include <QStandardPaths>
#include <QStyleHints>
#include <QTranslator>

static const std::string logPrefix_ = "scwx::main";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);

static void ConfigureTheme(const std::vector<std::string>& args);
static void OverrideDefaultStyle(const std::vector<std::string>& args);

int main(int argc, char* argv[])
Expand Down Expand Up @@ -103,18 +105,7 @@ int main(int argc, char* argv[])
scwx::qt::manager::ResourceManager::Initialize();

// Theme
auto uiStyle = scwx::qt::types::GetUiStyle(
scwx::qt::settings::GeneralSettings::Instance().theme().GetValue());

if (uiStyle == scwx::qt::types::UiStyle::Default)
{
OverrideDefaultStyle(args);
}
else
{
QApplication::setStyle(
QString::fromStdString(scwx::qt::types::GetUiStyleName(uiStyle)));
}
ConfigureTheme(args);

// Run initial setup if required
if (scwx::qt::ui::setup::SetupWizard::IsSetupRequired())
Expand Down Expand Up @@ -152,6 +143,27 @@ int main(int argc, char* argv[])
return result;
}

static void ConfigureTheme(const std::vector<std::string>& args)
{
auto& generalSettings = scwx::qt::settings::GeneralSettings::Instance();

auto uiStyle =
scwx::qt::types::GetUiStyle(generalSettings.theme().GetValue());
auto qtColorScheme = scwx::qt::types::GetQtColorScheme(uiStyle);

if (uiStyle == scwx::qt::types::UiStyle::Default)
{
OverrideDefaultStyle(args);
}
else
{
QApplication::setStyle(
QString::fromStdString(scwx::qt::types::GetQtStyleName(uiStyle)));
}

QGuiApplication::styleHints()->setColorScheme(qtColorScheme);
}

static void
OverrideDefaultStyle([[maybe_unused]] const std::vector<std::string>& args)
{
Expand Down
41 changes: 26 additions & 15 deletions scwx-qt/source/scwx/qt/types/qt_types.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <scwx/qt/types/qt_types.hpp>
#include <scwx/util/enum.hpp>

#include <boost/algorithm/string.hpp>

Expand All @@ -9,27 +10,37 @@ namespace qt
namespace types
{

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

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

UiStyle GetUiStyle(const std::string& name)
static const std::unordered_map<UiStyle, Qt::ColorScheme> qtColorSchemeMap_ {
{UiStyle::Default, Qt::ColorScheme::Unknown},
{UiStyle::Fusion, Qt::ColorScheme::Unknown},
{UiStyle::FusionLight, Qt::ColorScheme::Light},
{UiStyle::FusionDark, Qt::ColorScheme::Dark},
{UiStyle::Unknown, Qt::ColorScheme::Unknown}};

SCWX_GET_ENUM(UiStyle, GetUiStyle, uiStyleName_)

Qt::ColorScheme GetQtColorScheme(UiStyle uiStyle)
{
return qtColorSchemeMap_.at(uiStyle);
}

std::string GetQtStyleName(UiStyle uiStyle)
{
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;
}
return qtStyleName_.at(uiStyle);
}

std::string GetUiStyleName(UiStyle uiStyle)
Expand Down
11 changes: 8 additions & 3 deletions scwx-qt/source/scwx/qt/types/qt_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ enum ItemDataRole
RawDataRole
};

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

Qt::ColorScheme GetQtColorScheme(UiStyle uiStyle);
std::string GetQtStyleName(UiStyle uiStyle);

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

} // namespace types
} // namespace qt
Expand Down

0 comments on commit 9ee6e2e

Please sign in to comment.