From: Soeren Apel Date: Fri, 26 Oct 2018 20:10:22 +0000 (+0200) Subject: Settings: Allow user to choose the Qt UI style X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=374c697f74ba8abbfe2a014416eb398bb460d1c3;p=pulseview.git Settings: Allow user to choose the Qt UI style --- diff --git a/main.cpp b/main.cpp index 455d7afd..12d3bb6e 100644 --- a/main.cpp +++ b/main.cpp @@ -256,7 +256,7 @@ int main(int argc, char *argv[]) // Prepare the global settings since logging needs them early on pv::GlobalSettings settings; - settings.save_default_palette(); + settings.save_internal_defaults(); settings.set_defaults_where_needed(); settings.apply_theme(); diff --git a/pv/dialogs/settings.cpp b/pv/dialogs/settings.cpp index 491bbfc2..b79772ff 100644 --- a/pv/dialogs/settings.cpp +++ b/pv/dialogs/settings.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -228,6 +229,26 @@ QWidget *Settings::get_general_settings_form(QWidget *parent) const description_1->setAlignment(Qt::AlignRight); general_layout->addRow(description_1); + QComboBox *style_cb = new QComboBox(); + style_cb->addItem(tr("System Default"), ""); + for (QString& s : QStyleFactory::keys()) + style_cb->addItem(s, s); + + const QString current_style = + settings.value(GlobalSettings::Key_General_Style).toString(); + if (current_style.isEmpty()) + style_cb->setCurrentIndex(0); + else + style_cb->setCurrentIndex(style_cb->findText(current_style, 0)); + + connect(style_cb, SIGNAL(currentIndexChanged(int)), + this, SLOT(on_general_style_changed(int))); + general_layout->addRow(tr("Qt widget style"), style_cb); + + QLabel *description_2 = new QLabel(tr("(Dark themes look best with the Fusion style)")); + description_2->setAlignment(Qt::AlignRight); + general_layout->addRow(description_2); + return form; } @@ -553,6 +574,19 @@ void Settings::on_general_theme_changed_changed(int state) settings.apply_theme(); } +void Settings::on_general_style_changed(int state) +{ + GlobalSettings settings; + + if (state == 0) + settings.setValue(GlobalSettings::Key_General_Style, ""); + else + settings.setValue(GlobalSettings::Key_General_Style, + QStyleFactory::keys().at(state - 1)); + + settings.apply_theme(); +} + void Settings::on_view_zoomToFitDuringAcq_changed(int state) { GlobalSettings settings; diff --git a/pv/dialogs/settings.hpp b/pv/dialogs/settings.hpp index 1473aa59..4aa54580 100644 --- a/pv/dialogs/settings.hpp +++ b/pv/dialogs/settings.hpp @@ -59,6 +59,7 @@ public: private Q_SLOTS: void on_page_changed(QListWidgetItem *current, QListWidgetItem *previous); void on_general_theme_changed_changed(int state); + void on_general_style_changed(int state); void on_view_zoomToFitDuringAcq_changed(int state); void on_view_zoomToFitAfterAcq_changed(int state); void on_view_triggerIsZero_changed(int state); diff --git a/pv/globalsettings.cpp b/pv/globalsettings.cpp index af89671c..c15adf74 100644 --- a/pv/globalsettings.cpp +++ b/pv/globalsettings.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include using std::map; @@ -43,6 +43,7 @@ const vector< pair > Themes { }; const QString GlobalSettings::Key_General_Theme = "General_Theme"; +const QString GlobalSettings::Key_General_Style = "General_Style"; const QString GlobalSettings::Key_View_ZoomToFitDuringAcq = "View_ZoomToFitDuringAcq"; const QString GlobalSettings::Key_View_ZoomToFitAfterAcq = "View_ZoomToFitAfterAcq"; const QString GlobalSettings::Key_View_TriggerIsZeroTime = "View_TriggerIsZeroTime"; @@ -65,6 +66,7 @@ const QString GlobalSettings::Key_Log_NotifyOfStacktrace = "Log_NotifyOfStacktra vector GlobalSettings::callbacks_; bool GlobalSettings::tracking_ = false; map GlobalSettings::tracked_changes_; +QString GlobalSettings::default_style_; QPalette GlobalSettings::default_palette_; GlobalSettings::GlobalSettings() : @@ -78,6 +80,8 @@ void GlobalSettings::set_defaults_where_needed() // Use no theme by default if (!contains(Key_General_Theme)) setValue(Key_General_Theme, 0); + if (!contains(Key_General_Style)) + setValue(Key_General_Style, ""); // Enable zoom-to-fit after acquisition by default if (!contains(Key_View_ZoomToFitAfterAcq)) @@ -124,8 +128,12 @@ void GlobalSettings::set_defaults_where_needed() setValue(Key_Log_NotifyOfStacktrace, true); } -void GlobalSettings::save_default_palette() +void GlobalSettings::save_internal_defaults() { + default_style_ = qApp->style()->objectName(); + if (default_style_.isEmpty()) + default_style_ = "fusion"; + default_palette_ = QApplication::palette(); } @@ -143,10 +151,13 @@ void GlobalSettings::apply_theme() qApp->setPalette(default_palette_); + const QString style = value(Key_General_Style).toString(); + if (style.isEmpty()) + qApp->setStyle(default_style_); + else + qApp->setStyle(style); + if (theme_name.compare("QDarkStyleSheet") == 0) { -#ifdef Q_OS_WIN - qApp->setStyle(QStyleFactory::create("Fusion")); -#endif QPalette dark_palette; dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); dark_palette.setColor(QPalette::WindowText, Qt::white); @@ -155,9 +166,6 @@ void GlobalSettings::apply_theme() dark_palette.setColor(QPalette::Highlight, QColor(42, 130, 218)); qApp->setPalette(dark_palette); } else if (theme_name.compare("DarkStyle") == 0) { -#ifdef Q_OS_WIN - qApp->setStyle(QStyleFactory::create("Fusion")); -#endif QPalette dark_palette; dark_palette.setColor(QPalette::Window, QColor(53, 53, 53)); dark_palette.setColor(QPalette::WindowText, Qt::white); diff --git a/pv/globalsettings.hpp b/pv/globalsettings.hpp index d23bf637..d8374a35 100644 --- a/pv/globalsettings.hpp +++ b/pv/globalsettings.hpp @@ -52,6 +52,7 @@ class GlobalSettings : public QSettings public: static const QString Key_General_Theme; + static const QString Key_General_Style; static const QString Key_View_ZoomToFitDuringAcq; static const QString Key_View_ZoomToFitAfterAcq; static const QString Key_View_TriggerIsZeroTime; @@ -81,8 +82,8 @@ public: GlobalSettings(); void set_defaults_where_needed(); + void save_internal_defaults(); - void save_default_palette(); void apply_theme(); static void add_change_handler(GlobalSettingsInterface *cb); @@ -122,6 +123,7 @@ private: static bool tracking_; static map tracked_changes_; + static QString default_style_; static QPalette default_palette_; };