Skip to content

Commit

Permalink
Allow to use Qt's default QStyle
Browse files Browse the repository at this point in the history
Relevant prior PR qbittorrent#21553
  • Loading branch information
sledgehammer999 committed Oct 14, 2024
1 parent fb40275 commit fd0e50c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 71 deletions.
18 changes: 16 additions & 2 deletions src/gui/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ void OptionsDialog::loadBehaviorTabOptions()

initializeStyleCombo();

#ifdef Q_OS_WIN
m_ui->labelStyleHint->setText(tr("%1 is recommended for best compatibility with Windows dark mode", "Fusion is recommended for best compatibility with Windows dark mode").arg(u"Fusion"_s));
#endif

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 @@ -456,7 +460,10 @@ void OptionsDialog::saveBehaviorTabOptions() const
pref->setLocale(locale);

#ifdef Q_OS_WIN
pref->setStyle(m_ui->comboStyle->currentText());
if (const QVariant systemStyle = m_ui->comboStyle->currentData(); systemStyle.isValid())
pref->setStyle(systemStyle.toString());
else
pref->setStyle(m_ui->comboStyle->currentText());
#endif

#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
Expand Down Expand Up @@ -1689,17 +1696,24 @@ bool OptionsDialog::isSplashScreenDisabled() const
void OptionsDialog::initializeStyleCombo()
{
#ifdef Q_OS_WIN
m_ui->comboStyle->addItem(tr("System", "System default Qt style"), u"system"_s);
m_ui->comboStyle->setItemData(0, tr("Let Qt decide the style for this system"), Qt::ToolTipRole);

QStringList styleNames = QStyleFactory::keys();
std::sort(styleNames.begin(), styleNames.end(), Utils::Compare::NaturalLessThan<Qt::CaseInsensitive>());
m_ui->comboStyle->addItems(styleNames);
const QString prefStyleName = Preferences::instance()->getStyle();
const QString selectedStyleName = prefStyleName.isEmpty() ? QApplication::style()->name() : prefStyleName;
m_ui->comboStyle->setCurrentText(selectedStyleName);

if (selectedStyleName.compare(u"system"_s, Qt::CaseInsensitive) != 0)
m_ui->comboStyle->setCurrentText(selectedStyleName);
#else
m_ui->labelStyle->hide();
m_ui->comboStyle->hide();
m_ui->labelStyleHint->hide();
m_ui->UISettingsBoxLayout->removeWidget(m_ui->labelStyle);
m_ui->UISettingsBoxLayout->removeWidget(m_ui->comboStyle);
m_ui->UISettingsBoxLayout->removeWidget(m_ui->labelStyleHint);
m_ui->UISettingsBoxLayout->removeItem(m_ui->spacerStyle);
#endif
}
Expand Down
131 changes: 69 additions & 62 deletions src/gui/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<x>0</x>
<y>0</y>
<width>504</width>
<height>1064</height>
<height>1147</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_9">
Expand All @@ -133,17 +133,8 @@
<string>Interface</string>
</property>
<layout class="QGridLayout" name="UISettingsBoxLayout">
<item row="0" column="0" colspan="3">
<widget class="QLabel" name="labelRestartRequired">
<property name="font">
<font>
<italic>true</italic>
</font>
</property>
<property name="text">
<string>Changing Interface settings requires application restart</string>
</property>
</widget>
<item row="2" column="1">
<widget class="QComboBox" name="comboStyle"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelLanguage">
Expand All @@ -152,10 +143,36 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboLanguage"/>
<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="1" column="2">
<item row="3" column="0" colspan="4">
<widget class="QGroupBox" name="checkUseCustomTheme">
<property name="title">
<string>Use custom UI Theme</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_18">
<item>
<widget class="QLabel" name="label_16">
<property name="text">
<string>UI Theme file:</string>
</property>
</widget>
</item>
<item>
<widget class="FileSystemPathLineEdit" name="customThemeFilePath" native="true"/>
</item>
</layout>
</widget>
</item>
<item row="1" column="3">
<spacer name="spacerLanguage">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
Expand All @@ -175,10 +192,29 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="comboStyle"/>
<item row="0" column="0" colspan="4">
<widget class="QLabel" name="labelRestartRequired">
<property name="font">
<font>
<italic>true</italic>
</font>
</property>
<property name="text">
<string>Changing Interface settings requires application restart</string>
</property>
</widget>
</item>
<item row="2" column="2">
<item row="5" column="0" colspan="2">
<widget class="QPushButton" name="buttonCustomizeUITheme">
<property name="text">
<string>Customize UI Theme...</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboLanguage"/>
</item>
<item row="2" column="3">
<spacer name="spacerStyle">
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
Expand All @@ -191,39 +227,10 @@
</property>
</spacer>
</item>
<item row="3" column="0" colspan="3">
<widget class="QGroupBox" name="checkUseCustomTheme">
<property name="title">
<string>Use custom UI Theme</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_18">
<item>
<widget class="QLabel" name="label_16">
<property name="text">
<string>UI Theme file:</string>
</property>
</widget>
</item>
<item>
<widget class="FileSystemPathLineEdit" name="customThemeFilePath" native="true"/>
</item>
</layout>
</widget>
</item>
<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="5" column="0" colspan="2">
<widget class="QPushButton" name="buttonCustomizeUITheme">
<item row="2" column="2">
<widget class="QLabel" name="labelStyleHint">
<property name="text">
<string>Customize UI Theme...</string>
<string notr="true">labelStyleHint</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -845,8 +852,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>539</width>
<height>1457</height>
<width>538</width>
<height>1588</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -1681,8 +1688,8 @@ readme[0-9].txt: filter 'readme1.txt', 'readme2.txt' but not 'readme10.txt'.</st
<rect>
<x>0</x>
<y>0</y>
<width>504</width>
<height>848</height>
<width>378</width>
<height>839</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_20">
Expand Down Expand Up @@ -2257,8 +2264,8 @@ readme[0-9].txt: filter 'readme1.txt', 'readme2.txt' but not 'readme10.txt'.</st
<rect>
<x>0</x>
<y>0</y>
<width>302</width>
<height>408</height>
<width>303</width>
<height>402</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
Expand Down Expand Up @@ -2606,8 +2613,8 @@ readme[0-9].txt: filter 'readme1.txt', 'readme2.txt' but not 'readme10.txt'.</st
<rect>
<x>0</x>
<y>0</y>
<width>413</width>
<height>693</height>
<width>458</width>
<height>709</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_7">
Expand Down Expand Up @@ -3155,8 +3162,8 @@ Disable encryption: Only connect to peers without protocol encryption</string>
<rect>
<x>0</x>
<y>0</y>
<width>336</width>
<height>391</height>
<width>360</width>
<height>416</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_27">
Expand Down Expand Up @@ -3345,8 +3352,8 @@ Disable encryption: Only connect to peers without protocol encryption</string>
<rect>
<x>0</x>
<y>0</y>
<width>382</width>
<height>1045</height>
<width>434</width>
<height>1102</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_23">
Expand Down
12 changes: 5 additions & 7 deletions src/gui/uithememanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
#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 @@ -85,9 +81,11 @@ UIThemeManager::UIThemeManager()
#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);
if (const QString styleName = Preferences::instance()->getStyle(); styleName.compare(u"system", Qt::CaseInsensitive) != 0)
{
if (!QApplication::setStyle(styleName.isEmpty() ? u"Fusion"_s : 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
Expand Down

0 comments on commit fd0e50c

Please sign in to comment.