From c708c33a92153e820cd37f266bf8995682148404 Mon Sep 17 00:00:00 2001 From: blue Date: Fri, 21 Jan 2022 22:02:50 +0300 Subject: [PATCH] basic theme changing --- core/main.cpp | 11 ++++++- shared/global.cpp | 1 + shared/global.h | 5 +++- ui/widgets/settings/pageappearance.cpp | 31 +++++++++++++++++++- ui/widgets/settings/pageappearance.h | 11 +++++++ ui/widgets/settings/pagegeneral.h | 4 +++ ui/widgets/settings/settings.cpp | 40 +++++++++++++++++++++++++- ui/widgets/settings/settings.h | 9 ++++++ 8 files changed, 108 insertions(+), 4 deletions(-) diff --git a/core/main.cpp b/core/main.cpp index 7c94a12..03827d3 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) QApplication::setApplicationName("squawk"); QApplication::setApplicationDisplayName("Squawk"); QApplication::setApplicationVersion("0.2.1"); - + QTranslator qtTranslator; qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); app.installTranslator(&qtTranslator); @@ -88,6 +88,15 @@ int main(int argc, char *argv[]) QApplication::setWindowIcon(icon); new Shared::Global(); //translates enums + + QSettings settings; + QVariant vtheme = settings.value("theme"); + if (vtheme.isValid()) { + QString theme = vtheme.toString().toLower(); + if (theme != "system") { + QApplication::setStyle(theme); + } + } Squawk w; w.show(); diff --git a/shared/global.cpp b/shared/global.cpp index 5f3b4ed..7ee9599 100644 --- a/shared/global.cpp +++ b/shared/global.cpp @@ -84,6 +84,7 @@ Shared::Global::Global(): tr("Squawk is going to query you for the password on every start of the program", "AccountPasswordDescription"), tr("Your password is going to be stored in KDE wallet storage (KWallet). You're going to be queried for permissions", "AccountPasswordDescription") }), + defaultSystemStyle(QApplication::style()->objectName()), pluginSupport({ {"KWallet", false}, {"openFileManagerWindowJob", false} diff --git a/shared/global.h b/shared/global.h index b1ae59c..a87b9bc 100644 --- a/shared/global.h +++ b/shared/global.h @@ -27,7 +27,8 @@ #include #include -#include +#include +#include #include #include #include @@ -84,6 +85,8 @@ namespace Shared { const std::deque accountPassword; const std::deque accountPasswordDescription; + + const QString defaultSystemStyle; static bool supported(const QString& pluginName); static void setSupported(const QString& pluginName, bool support); diff --git a/ui/widgets/settings/pageappearance.cpp b/ui/widgets/settings/pageappearance.cpp index 725f452..2fb8dc8 100644 --- a/ui/widgets/settings/pageappearance.cpp +++ b/ui/widgets/settings/pageappearance.cpp @@ -1,13 +1,42 @@ #include "pageappearance.h" #include "ui_pageappearance.h" +#include + PageAppearance::PageAppearance(QWidget* parent): QWidget(parent), - m_ui(new Ui::PageAppearance()) + m_ui(new Ui::PageAppearance()), + styles() { m_ui->setupUi(this); + + m_ui->themeInput->addItem(tr("System")); + styles.push_back("system"); + QStringList themes = QStyleFactory::keys(); + for (const QString& key : themes) { + m_ui->themeInput->addItem(key); + styles.push_back(key); + } + + QSettings settings; + QVariant vtheme = settings.value("theme"); + if (vtheme.isValid()) { + QString theme = vtheme.toString(); + m_ui->themeInput->setCurrentText(theme); + } else { + m_ui->themeInput->setCurrentText("System"); + } + + connect(m_ui->themeInput, qOverload(&QComboBox::currentIndexChanged), this, &PageAppearance::onThemeChanged); } PageAppearance::~PageAppearance() { } + +void PageAppearance::onThemeChanged(int index) +{ + if (index >= 0) { + emit variableModified("theme", styles[index]); + } +} diff --git a/ui/widgets/settings/pageappearance.h b/ui/widgets/settings/pageappearance.h index 85d45a1..9cb1830 100644 --- a/ui/widgets/settings/pageappearance.h +++ b/ui/widgets/settings/pageappearance.h @@ -3,6 +3,10 @@ #include #include +#include +#include +#include +#include namespace Ui { @@ -19,8 +23,15 @@ public: PageAppearance(QWidget* parent = nullptr); ~PageAppearance(); +signals: + void variableModified(const QString& key, const QVariant& value); + +protected slots: + void onThemeChanged(int index); + private: QScopedPointer m_ui; + std::vector styles; }; #endif // PAGEAPPEARANCE_H diff --git a/ui/widgets/settings/pagegeneral.h b/ui/widgets/settings/pagegeneral.h index 77c0c3a..b8c30c5 100644 --- a/ui/widgets/settings/pagegeneral.h +++ b/ui/widgets/settings/pagegeneral.h @@ -3,6 +3,7 @@ #include #include +#include namespace Ui { @@ -19,6 +20,9 @@ public: PageGeneral(QWidget* parent = nullptr); ~PageGeneral(); +signals: + void variableModified(const QString& key, const QVariant& value); + private: QScopedPointer m_ui; }; diff --git a/ui/widgets/settings/settings.cpp b/ui/widgets/settings/settings.cpp index cdcf0cc..75a7780 100644 --- a/ui/widgets/settings/settings.cpp +++ b/ui/widgets/settings/settings.cpp @@ -3,11 +3,19 @@ Settings::Settings(QWidget* parent): QWidget(parent), - m_ui(new Ui::Settings()) + m_ui(new Ui::Settings()), + modifiedSettings() { m_ui->setupUi(this); connect(m_ui->list, &QListWidget::currentItemChanged, this, &Settings::onCurrentPageChanged); + + connect(m_ui->General, &PageGeneral::variableModified, this, &Settings::onVariableModified); + connect(m_ui->Appearance, &PageAppearance::variableModified, this, &Settings::onVariableModified); + + connect(m_ui->applyButton, &QPushButton::clicked, this, &Settings::apply); + connect(m_ui->okButton, &QPushButton::clicked, this, &Settings::confirm); + connect(m_ui->cancelButton, &QPushButton::clicked, this, &Settings::close); } Settings::~Settings() @@ -22,3 +30,33 @@ void Settings::onCurrentPageChanged(QListWidgetItem* current) m_ui->content->setCurrentIndex(m_ui->list->currentRow()); } } + +void Settings::onVariableModified(const QString& key, const QVariant& value) +{ + modifiedSettings[key] = value; +} + +void Settings::apply() +{ + QSettings settings; + for (const std::pair& pair: modifiedSettings) { + if (pair.first == "theme") { + QString theme = pair.second.toString(); + if (theme.toLower() == "system") { + QApplication::setStyle(Shared::Global::getInstance()->defaultSystemStyle); + } else { + QApplication::setStyle(theme); + } + } + + settings.setValue(pair.first, pair.second); + } + + modifiedSettings.clear(); +} + +void Settings::confirm() +{ + apply(); + close(); +} diff --git a/ui/widgets/settings/settings.h b/ui/widgets/settings/settings.h index f961e08..5031139 100644 --- a/ui/widgets/settings/settings.h +++ b/ui/widgets/settings/settings.h @@ -4,6 +4,9 @@ #include #include #include +#include + +#include "shared/global.h" namespace Ui { @@ -20,11 +23,17 @@ public: Settings(QWidget* parent = nullptr); ~Settings(); +public slots: + void apply(); + void confirm(); + protected slots: void onCurrentPageChanged(QListWidgetItem* current); + void onVariableModified(const QString& key, const QVariant& value); private: QScopedPointer m_ui; + std::map modifiedSettings; }; #endif // SETTINGS_H