Skip to content

Commit

Permalink
Add an option to let the user choose the qt style used by the app (#2663
Browse files Browse the repository at this point in the history
)

* 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 <alexey+github@asokolov.org>
  • Loading branch information
ctrlaltca and DarthGandalf committed Jul 15, 2024
1 parent e7f1aff commit f200592
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/kvirc/kernel/KviApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));

Expand Down
3 changes: 2 additions & 1 deletion src/kvirc/kernel/KviOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
3 changes: 2 additions & 1 deletion src/kvirc/kernel/KviOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion src/modules/options/OptionsWidget_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
#include "KviTalToolTip.h"

#include <QLayout>
#include <QStyleFactory>

#define DEFAULT_QT_STYLE "Default"
OptionsWidget_theme::OptionsWidget_theme(QWidget * parent)
: KviOptionsWidget(parent)
{
Expand All @@ -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)
{
Expand Down
8 changes: 8 additions & 0 deletions src/modules/options/OptionsWidget_theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "KviOptionsWidget.h"
#include "KviSelectors.h"

#include <QComboBox>

#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")
Expand All @@ -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
Expand Down

0 comments on commit f200592

Please sign in to comment.