From 08d4c7b385dc85f739436336da2bbb0680c3bd5a Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Mon, 10 Aug 2020 21:37:04 +1000 Subject: [PATCH] config: save style --- app/config/config.cpp | 2 + app/core.cpp | 3 +- .../tabs/preferencesappearancetab.cpp | 18 ++--- .../tabs/preferencesappearancetab.h | 9 +-- app/ui/style/style.cpp | 67 ++++++------------- app/ui/style/style.h | 29 +++----- 6 files changed, 43 insertions(+), 85 deletions(-) diff --git a/app/config/config.cpp b/app/config/config.cpp index 24bce9245..dce3449e2 100644 --- a/app/config/config.cpp +++ b/app/config/config.cpp @@ -31,6 +31,7 @@ #include "common/filefunctions.h" #include "common/xmlutils.h" #include "core.h" +#include "ui/style/style.h" #include "window/mainwindow/mainwindow.h" OLIVE_NAMESPACE_ENTER @@ -60,6 +61,7 @@ Config &Config::Current() void Config::SetDefaults() { config_map_.clear(); + SetEntryInternal(QStringLiteral("Style"), NodeParam::kString, StyleManager::kDefaultStyle); SetEntryInternal(QStringLiteral("TimecodeDisplay"), NodeParam::kInt, Timecode::kTimecodeDropFrame); SetEntryInternal(QStringLiteral("DefaultStillLength"), NodeParam::kRational, QVariant::fromValue(rational(2))); SetEntryInternal(QStringLiteral("HoverFocus"), NodeParam::kBoolean, false); diff --git a/app/core.cpp b/app/core.cpp index 1a5d14fbc..945bd3f2b 100644 --- a/app/core.cpp +++ b/app/core.cpp @@ -669,8 +669,7 @@ void Core::OpenStartupProject() void Core::StartGUI(bool full_screen) { // Set UI style - qApp->setStyle(QStyleFactory::create("Fusion")); - StyleManager::SetStyle(StyleManager::DefaultStyle()); + StyleManager::Init(); // Set up shared menus MenuShared::CreateInstance(); diff --git a/app/dialog/preferences/tabs/preferencesappearancetab.cpp b/app/dialog/preferences/tabs/preferencesappearancetab.cpp index 4078c1f0f..1326edcd3 100644 --- a/app/dialog/preferences/tabs/preferencesappearancetab.cpp +++ b/app/dialog/preferences/tabs/preferencesappearancetab.cpp @@ -44,19 +44,19 @@ PreferencesAppearanceTab::PreferencesAppearanceTab() // Appearance -> Theme appearance_layout->addWidget(new QLabel(tr("Theme")), row, 0); - style_ = new QComboBox(); + style_combobox_ = new QComboBox(); - style_list_ = StyleManager::ListInternal(); + const QMap& themes = StyleManager::available_themes(); + QMap::const_iterator i; + for (i=themes.cbegin(); i!=themes.cend(); i++) { + style_combobox_->addItem(i.value(), i.key()); - foreach (const StyleDescriptor& s, style_list_) { - style_->addItem(s.name(), s.path()); - - if (s.path() == StyleManager::GetStyle()) { - style_->setCurrentIndex(style_->count()-1); + if (StyleManager::GetStyle() == i.key()) { + style_combobox_->setCurrentIndex(style_combobox_->count()-1); } } - appearance_layout->addWidget(style_, row, 1); + appearance_layout->addWidget(style_combobox_, row, 1); row++; @@ -89,7 +89,7 @@ PreferencesAppearanceTab::PreferencesAppearanceTab() void PreferencesAppearanceTab::Accept() { - QString style_path = style_->currentData().toString(); + QString style_path = style_combobox_->currentData().toString(); if (style_path != StyleManager::GetStyle()) { StyleManager::SetStyle(style_path); diff --git a/app/dialog/preferences/tabs/preferencesappearancetab.h b/app/dialog/preferences/tabs/preferencesappearancetab.h index 1ab267eb5..35dc1ffac 100644 --- a/app/dialog/preferences/tabs/preferencesappearancetab.h +++ b/app/dialog/preferences/tabs/preferencesappearancetab.h @@ -49,14 +49,7 @@ private: /** * @brief UI widget for selecting the current UI style */ - QComboBox* style_; - - /** - * @brief List of internal styles - */ - QList style_list_; - - QString custom_style_path_; + QComboBox* style_combobox_; QList colors_; diff --git a/app/ui/style/style.cpp b/app/ui/style/style.cpp index 06753432e..9de184cb1 100644 --- a/app/ui/style/style.cpp +++ b/app/ui/style/style.cpp @@ -33,17 +33,9 @@ OLIVE_NAMESPACE_ENTER +const char* StyleManager::kDefaultStyle = "olive-dark"; QString StyleManager::current_style_; - -QList StyleManager::ListInternal() -{ - QList style_list; - - style_list.append(StyleDescriptor(tr("Olive Dark"), ":/style/olive-dark")); - style_list.append(StyleDescriptor(tr("Olive Light"), ":/style/olive-light")); - - return style_list; -} +QMap StyleManager::available_themes_; void StyleManager::UseOSNativeStyling(QWidget *widget) { @@ -145,9 +137,20 @@ void StyleManager::ParsePaletteColor(QSettings *ini, QPalette *palette, QPalette palette->setColor(group, role, QColor(ini->value(role_name).toString())); } -StyleDescriptor StyleManager::DefaultStyle() +void StyleManager::Init() { - return ListInternal().first(); + qApp->setStyle(QStyleFactory::create("Fusion")); + + available_themes_.insert(QStringLiteral("olive-dark"), QStringLiteral("Olive Dark")); + available_themes_.insert(QStringLiteral("olive-light"), QStringLiteral("Olive Light")); + + QString config_style = Config::Current()["Style"].toString(); + + if (config_style.isEmpty() || !available_themes_.contains(config_style)) { + SetStyle(kDefaultStyle); + } else { + SetStyle(config_style); + } } const QString &StyleManager::GetStyle() @@ -155,31 +158,17 @@ const QString &StyleManager::GetStyle() return current_style_; } -void StyleManager::SetStyleFromConfig() -{ - QString config_style = Config::Current()["Style"].toString(); - - if (config_style.isEmpty()) { - SetStyle(DefaultStyle()); - } else { - SetStyle(config_style); - } -} - -void StyleManager::SetStyle(const StyleDescriptor &style) -{ - SetStyle(style.path()); -} - void StyleManager::SetStyle(const QString &style_path) { current_style_ = style_path; + QString abs_style_path = QStringLiteral(":/style/%1").arg(style_path); + // Load all icons for this style (icons must be loaded first because the style change below triggers the icon change) - icon::LoadAll(style_path); + icon::LoadAll(abs_style_path); // Set palette for this - QString palette_file = QStringLiteral("%1/palette.ini").arg(style_path); + QString palette_file = QStringLiteral("%1/palette.ini").arg(abs_style_path); if (QFileInfo::exists(palette_file)) { qApp->setPalette(ParsePalette(palette_file)); } else { @@ -187,7 +176,7 @@ void StyleManager::SetStyle(const QString &style_path) } // Set CSS style for this - QFile css_file(QStringLiteral("%1/style.css").arg(style_path)); + QFile css_file(QStringLiteral("%1/style.css").arg(abs_style_path)); if (css_file.exists() && css_file.open(QFile::ReadOnly | QFile::Text)) { // Read in entire CSS from file and set as the application stylesheet @@ -201,20 +190,4 @@ void StyleManager::SetStyle(const QString &style_path) } } -StyleDescriptor::StyleDescriptor(const QString &name, const QString &path) : - name_(name), - path_(path) -{ -} - -const QString &StyleDescriptor::name() const -{ - return name_; -} - -const QString &StyleDescriptor::path() const -{ - return path_; -} - OLIVE_NAMESPACE_EXIT diff --git a/app/ui/style/style.h b/app/ui/style/style.h index 23dbc4e9c..dbf385db0 100644 --- a/app/ui/style/style.h +++ b/app/ui/style/style.h @@ -28,34 +28,23 @@ OLIVE_NAMESPACE_ENTER -class StyleDescriptor { -public: - StyleDescriptor(const QString& name, const QString& path); - - const QString& name() const; - const QString& path() const; - -private: - QString name_; - QString path_; -}; - class StyleManager : public QObject { public: - static StyleDescriptor DefaultStyle(); + static void Init(); static const QString& GetStyle(); - static void SetStyleFromConfig(); - - static void SetStyle(const StyleDescriptor& style); - static void SetStyle(const QString& style_path); - static QList ListInternal(); - static void UseOSNativeStyling(QWidget* widget); + static const char* kDefaultStyle; + + static const QMap& available_themes() + { + return available_themes_; + } + private: static QPalette ParsePalette(const QString& ini_path); @@ -65,6 +54,8 @@ private: static QString current_style_; + static QMap available_themes_; + }; OLIVE_NAMESPACE_EXIT