From f2005924313b18a2869f63e6298f715ce4f13aa4 Mon Sep 17 00:00:00 2001 From: ctrlaltca Date: Mon, 15 Jul 2024 21:32:05 +0200 Subject: [PATCH] Add an option to let the user choose the qt style used by the app (#2663) * Remove hardcoded usage of Fusion style on Gtk; add an option to use a custom Qt Style (like Fusion on Windows, supporting dark mode) * Update src/modules/options/OptionsWidget_theme.cpp Co-authored-by: Alexey Sokolov --- src/kvirc/kernel/KviApplication.cpp | 20 +++++++------- src/kvirc/kernel/KviOptions.cpp | 3 ++- src/kvirc/kernel/KviOptions.h | 3 ++- src/modules/options/OptionsWidget_theme.cpp | 30 ++++++++++++++++++++- src/modules/options/OptionsWidget_theme.h | 8 ++++++ 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/kvirc/kernel/KviApplication.cpp b/src/kvirc/kernel/KviApplication.cpp index b29f85f519..da2ab31cf0 100644 --- a/src/kvirc/kernel/KviApplication.cpp +++ b/src/kvirc/kernel/KviApplication.cpp @@ -228,16 +228,6 @@ KviApplication::KviApplication(int & argc, char ** argv) // don't let qt quit the application by itself setQuitOnLastWindowClosed(false); -//note: the early qApp->style() call leads to a crash on osx -#if !defined(COMPILE_ENABLE_GTKSTYLE) && !defined(COMPILE_ON_MAC) - // workaround for gtk+ style forcing a crappy white background (ticket #777, #964, #1009, ..) - if(QString("QGtkStyle").compare(qApp->style()->metaObject()->className()) == 0) - { - setStyle(QStyleFactory::create("Fusion")); - setPalette(style()->standardPalette()); - } -#endif - // Restore Qt5-like rounding to fix HiDPI support on QWebEngine QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round); } @@ -343,6 +333,16 @@ void KviApplication::setup() updatePseudoTransparency(); #endif + { + // set global Qt style if needed + QString requestedQtStyle = KVI_OPTION_STRING(KviOption_stringQtStyle); + + if(!requestedQtStyle.isEmpty() && QStyleFactory::keys().contains(requestedQtStyle)) { + setStyle(QStyleFactory::create(requestedQtStyle)); + setPalette(style()->standardPalette()); + } + } + // enforce our "icon in popups" option - this is done also in each updateGui() call setAttribute(Qt::AA_DontShowIconsInMenus, !KVI_OPTION_BOOL(KviOption_boolShowIconsInPopupMenus)); diff --git a/src/kvirc/kernel/KviOptions.cpp b/src/kvirc/kernel/KviOptions.cpp index 0391ae1eea..5e77e04002 100644 --- a/src/kvirc/kernel/KviOptions.cpp +++ b/src/kvirc/kernel/KviOptions.cpp @@ -412,7 +412,8 @@ KviStringOption g_stringOptionsTable[KVI_NUM_STRING_OPTIONS] = { STRING_OPTION("DefaultSrvEncoding", "", KviOption_sectFlagFrame), STRING_OPTION("LogsPath", "", KviOption_sectFlagUser | KviOption_encodePath), STRING_OPTION("LogsDynamicPath", "", KviOption_sectFlagUser | KviOption_encodePath), - STRING_OPTION("LogsExportPath", "", KviOption_sectFlagUser | KviOption_encodePath) + STRING_OPTION("LogsExportPath", "", KviOption_sectFlagUser | KviOption_encodePath), + STRING_OPTION("QtStyle", "", KviOption_sectFlagIrcView | KviOption_resetUpdateGui | KviOption_groupTheme | KviOption_resetReloadImages) }; #define STRINGLIST_OPTION(_txt, _flags) \ diff --git a/src/kvirc/kernel/KviOptions.h b/src/kvirc/kernel/KviOptions.h index ffe0775e0b..317c5eec65 100644 --- a/src/kvirc/kernel/KviOptions.h +++ b/src/kvirc/kernel/KviOptions.h @@ -423,8 +423,9 @@ DECLARE_OPTION_STRUCT(KviStringListOption, QStringList) #define KviOption_stringLogsPath 57 /* logfolder */ #define KviOption_stringLogsDynamicPath 58 /* logfolder */ #define KviOption_stringLogsExportPath 59 /* logview module log export */ +#define KviOption_stringQtStyle 60 /* themes::general */ -#define KVI_NUM_STRING_OPTIONS 60 +#define KVI_NUM_STRING_OPTIONS 61 #define KVI_STRINGLIST_OPTIONS_PREFIX "stringlist" #define KVI_STRINGLIST_OPTIONS_PREFIX_LEN 10 diff --git a/src/modules/options/OptionsWidget_theme.cpp b/src/modules/options/OptionsWidget_theme.cpp index d2637267ec..f4d3cc8ebb 100644 --- a/src/modules/options/OptionsWidget_theme.cpp +++ b/src/modules/options/OptionsWidget_theme.cpp @@ -31,7 +31,9 @@ #include "KviTalToolTip.h" #include +#include +#define DEFAULT_QT_STYLE "Default" OptionsWidget_theme::OptionsWidget_theme(QWidget * parent) : KviOptionsWidget(parent) { @@ -44,12 +46,38 @@ OptionsWidget_theme::OptionsWidget_theme(QWidget * parent) KVI_OPTION_BOOL(KviOption_boolUseGlobalApplicationFont)); connect(b, SIGNAL(toggled(bool)), f, SLOT(setEnabled(bool))); - addRowSpacer(0, 3, 1, 3); + addLabel(0, 3, 0, 3, __tr2qs_ctx("Qt Style:", "options")); + m_pQtStyle = new QComboBox(this); + addWidgetToLayout(m_pQtStyle, 1, 3, 1, 3); + m_pQtStyle->addItem(DEFAULT_QT_STYLE); + for (const QString& key : QStyleFactory::keys()) { + m_pQtStyle->addItem(key); + } + if(KVI_OPTION_STRING(KviOption_stringQtStyle).isEmpty()) { + m_pQtStyle->setCurrentText(DEFAULT_QT_STYLE); + } else { + m_pQtStyle->setCurrentText(KVI_OPTION_STRING(KviOption_stringQtStyle)); + } + + addRowSpacer(0, 4, 1, 4); } OptionsWidget_theme::~OptionsWidget_theme() = default; +void OptionsWidget_theme::commit() +{ + KviOptionsWidget::commit(); + + if(m_pQtStyle->currentText() == DEFAULT_QT_STYLE) { + KVI_OPTION_STRING(KviOption_stringQtStyle) = ""; + } else { + KVI_OPTION_STRING(KviOption_stringQtStyle) = m_pQtStyle->currentText(); + QApplication::setStyle(QStyleFactory::create(KVI_OPTION_STRING(KviOption_stringQtStyle))); + QApplication::setPalette(style()->standardPalette()); + } +} + OptionsWidget_themeTransparency::OptionsWidget_themeTransparency(QWidget * parent) : KviOptionsWidget(parent) { diff --git a/src/modules/options/OptionsWidget_theme.h b/src/modules/options/OptionsWidget_theme.h index ab3546f7f4..a4f01f689c 100644 --- a/src/modules/options/OptionsWidget_theme.h +++ b/src/modules/options/OptionsWidget_theme.h @@ -28,6 +28,8 @@ #include "KviOptionsWidget.h" #include "KviSelectors.h" +#include + #define KVI_OPTIONS_WIDGET_ICON_OptionsWidget_theme KviIconManager::Gui #define KVI_OPTIONS_WIDGET_NAME_OptionsWidget_theme __tr2qs_no_lookup("General") #define KVI_OPTIONS_WIDGET_KEYWORDS_OptionsWidget_theme __tr2qs_no_lookup("theme") @@ -39,6 +41,12 @@ class OptionsWidget_theme : public KviOptionsWidget public: OptionsWidget_theme(QWidget * parent); ~OptionsWidget_theme(); + +protected: + void commit() override; + +private: + QComboBox * m_pQtStyle; }; #define KVI_OPTIONS_WIDGET_ICON_OptionsWidget_themeTransparency KviIconManager::Transparent