Skip to content

Commit

Permalink
Allow to choose Qt style
Browse files Browse the repository at this point in the history
PR #21553.
  • Loading branch information
glassez committed Oct 11, 2024
1 parent 93096db commit b579afe
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jobs:
- name: Install Qt
uses: jurplel/install-qt-action@v4
with:
version: "6.7.0"
version: "6.7.3"
archives: qtbase qtsvg qttools
cache: true

Expand Down
11 changes: 1 addition & 10 deletions src/app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014-2023 Vladimir Golovnev <[email protected]>
* Copyright (C) 2014-2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2006 Christophe Dumez <[email protected]>
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -58,10 +58,6 @@
#include <QSplashScreen>
#include <QTimer>

#ifdef Q_OS_WIN
#include <QOperatingSystemVersion>
#endif

#ifdef QBT_STATIC_QT
#include <QtPlugin>
Q_IMPORT_PLUGIN(QICOPlugin)
Expand Down Expand Up @@ -189,11 +185,6 @@ int main(int argc, char *argv[])
// We must save it here because QApplication constructor may change it
const bool isOneArg = (argc == 2);

#if !defined(DISABLE_GUI) && defined(Q_OS_WIN)
if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10)
QApplication::setStyle(u"Fusion"_s);
#endif

// `app` must be declared out of try block to allow display message box in case of exception
std::unique_ptr<Application> app;
try
Expand Down
13 changes: 13 additions & 0 deletions src/base/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,19 @@ void Preferences::setWinStartup(const bool b)
settings.remove(profileID);
}
}

QString Preferences::getStyle() const
{
return value<QString>(u"Appearance/Style"_s);
}

void Preferences::setStyle(const QString &styleName)
{
if (styleName == getStyle())
return;

setValue(u"Appearance/Style"_s, styleName);
}
#endif // Q_OS_WIN

// Downloads
Expand Down
2 changes: 2 additions & 0 deletions src/base/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class Preferences final : public QObject
#ifdef Q_OS_WIN
bool WinStartup() const;
void setWinStartup(bool b);
QString getStyle() const;
void setStyle(const QString &styleName);
#endif

// Downloads
Expand Down
52 changes: 45 additions & 7 deletions src/gui/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#include <QSystemTrayIcon>
#include <QTranslator>

#ifdef Q_OS_WIN
#include <QStyleFactory>
#endif

#include "base/bittorrent/session.h"
#include "base/bittorrent/sharelimitaction.h"
#include "base/exceptions.h"
Expand Down Expand Up @@ -236,6 +240,8 @@ void OptionsDialog::loadBehaviorTabOptions()
initializeLanguageCombo();
setLocale(pref->getLocale());

initializeStyleCombo();

m_ui->checkUseCustomTheme->setChecked(Preferences::instance()->useCustomUITheme());
m_ui->customThemeFilePath->setSelectedPath(Preferences::instance()->customUIThemePath());
m_ui->customThemeFilePath->setMode(FileSystemPathEdit::Mode::FileOpen);
Expand Down Expand Up @@ -345,7 +351,11 @@ void OptionsDialog::loadBehaviorTabOptions()

m_ui->checkBoxPerformanceWarning->setChecked(session->isPerformanceWarningEnabled());

connect(m_ui->comboI18n, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton);
connect(m_ui->comboLanguage, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton);

#ifdef Q_OS_WIN
connect(m_ui->comboStyle, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton);
#endif

#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
connect(m_ui->checkUseSystemIcon, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
Expand Down Expand Up @@ -443,6 +453,10 @@ void OptionsDialog::saveBehaviorTabOptions() const
}
pref->setLocale(locale);

#ifdef Q_OS_WIN
pref->setStyle(m_ui->comboStyle->currentText());
#endif

#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
pref->useSystemIcons(m_ui->checkUseSystemIcon->isChecked());
#endif
Expand Down Expand Up @@ -1387,7 +1401,7 @@ void OptionsDialog::initializeLanguageCombo()
for (const QString &langFile : langFiles)
{
const QString langCode = QStringView(langFile).sliced(12).chopped(3).toString(); // remove "qbittorrent_" and ".qm"
m_ui->comboI18n->addItem(Utils::Misc::languageToLocalizedString(langCode), langCode);
m_ui->comboLanguage->addItem(Utils::Misc::languageToLocalizedString(langCode), langCode);
}
}

Expand Down Expand Up @@ -1672,6 +1686,30 @@ bool OptionsDialog::isSplashScreenDisabled() const
return !m_ui->checkShowSplash->isChecked();
}

void OptionsDialog::initializeStyleCombo()
{
#ifdef Q_OS_WIN
const QString prefStyleName = Preferences::instance()->getStyle();
const QString selectedStyleName = prefStyleName.isEmpty() ? QApplication::style()->name() : prefStyleName;
QStringList styleNames = QStyleFactory::keys();
for (qsizetype i = 1, stylesCount = styleNames.size(); i < stylesCount; ++i)
{
if (selectedStyleName.compare(styleNames.at(i), Qt::CaseInsensitive) == 0)
{
styleNames.swapItemsAt(0, i);
break;
}
}
m_ui->comboStyle->addItems(styleNames);
#else
m_ui->labelStyle->hide();
m_ui->comboStyle->hide();
m_ui->UISettingsBoxLayout->removeWidget(m_ui->labelStyle);
m_ui->UISettingsBoxLayout->removeWidget(m_ui->comboStyle);
m_ui->UISettingsBoxLayout->removeItem(m_ui->spacerStyle);
#endif
}

#ifdef Q_OS_WIN
bool OptionsDialog::WinStartup() const
{
Expand Down Expand Up @@ -1721,7 +1759,7 @@ QString OptionsDialog::getProxyPassword() const
// Locale Settings
QString OptionsDialog::getLocale() const
{
return m_ui->comboI18n->itemData(m_ui->comboI18n->currentIndex(), Qt::UserRole).toString();
return m_ui->comboLanguage->itemData(m_ui->comboLanguage->currentIndex(), Qt::UserRole).toString();
}

void OptionsDialog::setLocale(const QString &localeStr)
Expand All @@ -1746,24 +1784,24 @@ void OptionsDialog::setLocale(const QString &localeStr)
name = locale.name();
}
// Attempt to find exact match
int index = m_ui->comboI18n->findData(name, Qt::UserRole);
int index = m_ui->comboLanguage->findData(name, Qt::UserRole);
if (index < 0)
{
//Attempt to find a language match without a country
int pos = name.indexOf(u'_');
if (pos > -1)
{
QString lang = name.left(pos);
index = m_ui->comboI18n->findData(lang, Qt::UserRole);
index = m_ui->comboLanguage->findData(lang, Qt::UserRole);
}
}
if (index < 0)
{
// Unrecognized, use US English
index = m_ui->comboI18n->findData(u"en"_s, Qt::UserRole);
index = m_ui->comboLanguage->findData(u"en"_s, Qt::UserRole);
Q_ASSERT(index >= 0);
}
m_ui->comboI18n->setCurrentIndex(index);
m_ui->comboLanguage->setCurrentIndex(index);
}

Path OptionsDialog::getTorrentExportDir() const
Expand Down
3 changes: 2 additions & 1 deletion src/gui/optionsdialog.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <[email protected]>
* Copyright (C) 2023-2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2006 Christophe Dumez <[email protected]>
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -143,6 +143,7 @@ private slots:

// General options
void initializeLanguageCombo();
void initializeStyleCombo();
QString getLocale() const;
bool isSplashScreenDisabled() const;
#ifdef Q_OS_WIN
Expand Down
41 changes: 32 additions & 9 deletions src/gui/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@
<property name="title">
<string>Interface</string>
</property>
<layout class="QGridLayout" name="gridLayout_81">
<layout class="QGridLayout" name="UISettingsBoxLayout">
<item row="0" column="0" colspan="3">
<widget class="QLabel" name="label_15">
<widget class="QLabel" name="labelRestartRequired">
<property name="font">
<font>
<italic>true</italic>
Expand All @@ -146,17 +146,17 @@
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_9">
<widget class="QLabel" name="labelLanguage">
<property name="text">
<string>Language:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboI18n"/>
<widget class="QComboBox" name="comboLanguage"/>
</item>
<item row="1" column="2">
<spacer name="horizontalSpacer_111">
<spacer name="spacerLanguage">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
Expand All @@ -168,7 +168,30 @@
</property>
</spacer>
</item>
<item row="2" column="0" colspan="3">
<item row="2" column="0">
<widget class="QLabel" name="labelStyle">
<property name="text">
<string>Style:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="comboStyle"/>
</item>
<item row="2" column="2">
<spacer name="spacerStyle">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="0" colspan="3">
<widget class="QGroupBox" name="checkUseCustomTheme">
<property name="title">
<string>Use custom UI Theme</string>
Expand All @@ -190,14 +213,14 @@
</layout>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="checkUseSystemIcon">
<property name="text">
<string>Use icons from system theme</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<item row="5" column="0" colspan="2">
<widget class="QPushButton" name="buttonCustomizeUITheme">
<property name="text">
<string>Customize UI Theme...</string>
Expand Down Expand Up @@ -3881,7 +3904,7 @@ Use ';' to split multiple entries. Can use wildcard '*'.</string>
</customwidgets>
<tabstops>
<tabstop>tabOption</tabstop>
<tabstop>comboI18n</tabstop>
<tabstop>comboLanguage</tabstop>
<tabstop>checkUseCustomTheme</tabstop>
<tabstop>customThemeFilePath</tabstop>
<tabstop>checkAddStopped</tabstop>
Expand Down
10 changes: 10 additions & 0 deletions src/gui/uithememanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include <QStyle>
#include <QStyleHints>

#ifdef Q_OS_WIN
#include <QOperatingSystemVersion>
#endif

#include "base/global.h"
#include "base/logger.h"
#include "base/path.h"
Expand Down Expand Up @@ -80,6 +84,12 @@ UIThemeManager::UIThemeManager()
, m_useSystemIcons {Preferences::instance()->useSystemIcons()}
#endif
{
#ifdef Q_OS_WIN
const QString defaultStyle = (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10) ? u"Fusion"_s : QString();
if (const QString styleName = Preferences::instance()->getStyle(); !QApplication::setStyle(styleName.isEmpty() ? defaultStyle : styleName))
LogMsg(tr("Set app style failed. Unknown style: \"%1\"").arg(styleName), Log::WARNING);
#endif

// NOTE: Qt::QueuedConnection can be omitted as soon as support for Qt 6.5 is dropped
connect(QApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, &UIThemeManager::onColorSchemeChanged, Qt::QueuedConnection);

Expand Down

0 comments on commit b579afe

Please sign in to comment.