Allow to choose Qt style

PR #21553.
This commit is contained in:
Vladimir Golovnev 2024-10-11 16:04:57 +03:00 committed by Vladimir Golovnev (Glassez)
parent 93096dba56
commit b579afe1aa
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
8 changed files with 106 additions and 28 deletions

View File

@ -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

View File

@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014-2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2014-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@ -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)
@ -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

View File

@ -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

View File

@ -130,6 +130,8 @@ public:
#ifdef Q_OS_WIN
bool WinStartup() const;
void setWinStartup(bool b);
QString getStyle() const;
void setStyle(const QString &styleName);
#endif
// Downloads

View File

@ -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"
@ -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);
@ -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);
@ -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
@ -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);
}
}
@ -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
{
@ -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)
@ -1746,7 +1784,7 @@ 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
@ -1754,16 +1792,16 @@ void OptionsDialog::setLocale(const QString &localeStr)
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

View File

@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2023-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@ -143,6 +143,7 @@ private:
// General options
void initializeLanguageCombo();
void initializeStyleCombo();
QString getLocale() const;
bool isSplashScreenDisabled() const;
#ifdef Q_OS_WIN

View File

@ -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>
@ -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>
@ -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>
@ -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>
@ -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>

View File

@ -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"
@ -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);