Switch to Fusion style when activate custom theme or use default dark theme on Windows.

This commit is contained in:
Roman Telezhynskyi 2023-08-09 15:46:02 +03:00
parent 0f7a8b7e0d
commit 14ec95ee9f
15 changed files with 94 additions and 96 deletions

View File

@ -36,7 +36,6 @@
#include "../vganalytics/def.h" #include "../vganalytics/def.h"
#include "../vganalytics/vganalytics.h" #include "../vganalytics/vganalytics.h"
#include "../vmisc/qt_dispatch/qt_dispatch.h" #include "../vmisc/qt_dispatch/qt_dispatch.h"
#include "../vmisc/theme/vapplicationstyle.h"
#include "../vmisc/theme/vtheme.h" #include "../vmisc/theme/vtheme.h"
#include "../vmisc/vsysexits.h" #include "../vmisc/vsysexits.h"
#include "version.h" #include "version.h"
@ -435,6 +434,7 @@ void VPApplication::InitOptions()
CheckSystemLocale(); CheckSystemLocale();
VTheme::InitApplicationStyle();
VTheme::SetIconTheme(); VTheme::SetIconTheme();
VTheme::InitThemeMode(); VTheme::InitThemeMode();
@ -499,17 +499,6 @@ void VPApplication::ParseCommandLine(const SocketConnection &connection, const Q
qCDebug(pApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName)); qCDebug(pApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName));
StartLocalServer(serverName); StartLocalServer(serverName);
LoadTranslation(PuzzleSettings()->GetLocale()); LoadTranslation(PuzzleSettings()->GetLocale());
QString styleOpt = cmd->OptionStyle();
if (styleOpt != QLatin1String("native"))
{
QStyle *style = QStyleFactory::create(styleOpt);
if (style != nullptr)
{
style = new VApplicationStyle(style);
setStyle(style);
}
}
} }
ProcessArguments(cmd); ProcessArguments(cmd);

View File

@ -67,18 +67,6 @@ void VPCommandLine::ShowHelp(int exitCode)
parser.showHelp(exitCode); parser.showHelp(exitCode);
} }
//----------------------------------------------------------------------------------------------------------------------
auto VPCommandLine::OptionStyle() const -> QString
{
QString value = OptionValue(LONG_OPTION_STYLE);
if (value.isEmpty())
{
return QStringLiteral("native");
}
return value;
}
//---------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------
VPCommandLine::VPCommandLine() VPCommandLine::VPCommandLine()
{ {
@ -120,7 +108,6 @@ void VPCommandLine::InitCommandLineOptions()
translate("VCommandLine", "Disable high dpi scaling. Call this option if has problem with scaling (by default " translate("VCommandLine", "Disable high dpi scaling. Call this option if has problem with scaling (by default "
"scaling enabled). Alternatively you can use the %1 environment variable.") "scaling enabled). Alternatively you can use the %1 environment variable.")
.arg(QStringLiteral("QT_AUTO_SCREEN_SCALE_FACTOR=0"))}, .arg(QStringLiteral("QT_AUTO_SCREEN_SCALE_FACTOR=0"))},
{LONG_OPTION_STYLE, tr("Application style") + QString(" `Fusion`, `Windows`, `native`, ..."), "", "native"},
}); });
} }

View File

@ -65,8 +65,6 @@ public:
Q_NORETURN void ShowHelp(int exitCode = 0); Q_NORETURN void ShowHelp(int exitCode = 0);
auto OptionStyle() const -> QString;
protected: protected:
VPCommandLine(); VPCommandLine();

View File

@ -31,7 +31,6 @@
const QString LONG_OPTION_RAW_LAYOUT = QStringLiteral("rawLayout"); // NOLINT const QString LONG_OPTION_RAW_LAYOUT = QStringLiteral("rawLayout"); // NOLINT
const QString SINGLE_OPTION_RAW_LAYOUT = QStringLiteral("r"); // NOLINT const QString SINGLE_OPTION_RAW_LAYOUT = QStringLiteral("r"); // NOLINT
const QString LONG_OPTION_STYLE = QStringLiteral("style"); // NOLINT
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
/** /**
@ -40,5 +39,5 @@ const QString LONG_OPTION_STYLE = QStringLiteral("style"); // NOLINT
*/ */
auto AllKeys() -> QStringList auto AllKeys() -> QStringList
{ {
return {LONG_OPTION_RAW_LAYOUT, SINGLE_OPTION_RAW_LAYOUT, LONG_OPTION_STYLE}; return {LONG_OPTION_RAW_LAYOUT, SINGLE_OPTION_RAW_LAYOUT};
} }

View File

@ -33,9 +33,6 @@
extern const QString LONG_OPTION_RAW_LAYOUT; extern const QString LONG_OPTION_RAW_LAYOUT;
extern const QString SINGLE_OPTION_RAW_LAYOUT; extern const QString SINGLE_OPTION_RAW_LAYOUT;
extern const QString LONG_OPTION_STYLE;
extern const QString LONG_OPTION_STYLESHEET;
auto AllKeys() -> QStringList; auto AllKeys() -> QStringList;
#endif // VPCOMMANDS_H #endif // VPCOMMANDS_H

View File

@ -101,8 +101,6 @@ Q_GLOBAL_STATIC_WITH_ARGS(const QString, SINGLE_OPTION_UNITS, (QChar('u')))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, LONG_OPTION_TEST, (QLatin1String("test"))) // NOLINT Q_GLOBAL_STATIC_WITH_ARGS(const QString, LONG_OPTION_TEST, (QLatin1String("test"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, LONG_OPTION_STYLE, (QLatin1String("style"))) // NOLINT
QT_WARNING_POP QT_WARNING_POP
} // namespace } // namespace
@ -480,6 +478,7 @@ void MApplication::InitOptions()
CheckSystemLocale(); CheckSystemLocale();
VTheme::InitApplicationStyle();
VTheme::SetIconTheme(); VTheme::SetIconTheme();
VTheme::InitThemeMode(); VTheme::InitThemeMode();
@ -694,22 +693,6 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
qCDebug(mApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName)); qCDebug(mApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName));
StartLocalServer(serverName); StartLocalServer(serverName);
LoadTranslation(TapeSettings()->GetLocale()); LoadTranslation(TapeSettings()->GetLocale());
QString styleOpt = parser.value(*LONG_OPTION_STYLE);
if (styleOpt.isEmpty())
{
styleOpt = QLatin1String("native");
}
if (styleOpt != QLatin1String("native"))
{
QStyle *style = QStyleFactory::create(styleOpt);
if (style != nullptr)
{
style = new VApplicationStyle(style);
setStyle(style);
}
}
} }
const QStringList args = parser.positionalArguments(); const QStringList args = parser.positionalArguments();
@ -822,7 +805,6 @@ void MApplication::InitParserOptions(QCommandLineParser &parser)
tr("Disable high dpi scaling. Call this option if has problem with scaling (by default scaling enabled). " tr("Disable high dpi scaling. Call this option if has problem with scaling (by default scaling enabled). "
"Alternatively you can use the %1 environment variable.") "Alternatively you can use the %1 environment variable.")
.arg("QT_AUTO_SCREEN_SCALE_FACTOR=0")}, .arg("QT_AUTO_SCREEN_SCALE_FACTOR=0")},
{*LONG_OPTION_STYLE, tr("Application style") + QString(" `Fusion`, `Windows`, `native`, ..."), "", "native"},
}); });
} }

View File

@ -39,7 +39,6 @@
#include "../vganalytics/def.h" #include "../vganalytics/def.h"
#include "../vganalytics/vganalytics.h" #include "../vganalytics/vganalytics.h"
#include "../vmisc/qt_dispatch/qt_dispatch.h" #include "../vmisc/qt_dispatch/qt_dispatch.h"
#include "../vmisc/theme/vapplicationstyle.h"
#include "../vmisc/theme/vtheme.h" #include "../vmisc/theme/vtheme.h"
#include "../vmisc/vsysexits.h" #include "../vmisc/vsysexits.h"
#include "../vmisc/vvalentinasettings.h" #include "../vmisc/vvalentinasettings.h"
@ -690,19 +689,9 @@ void VApplication::InitOptions()
{ {
LoadTranslation(ValentinaSettings()->GetLocale()); LoadTranslation(ValentinaSettings()->GetLocale());
VTheme::InitApplicationStyle();
VTheme::SetIconTheme(); VTheme::SetIconTheme();
VTheme::InitThemeMode(); VTheme::InitThemeMode();
QString styleOpt = VApplication::CommandLine()->OptStyle();
if (styleOpt != QLatin1String("native"))
{
QStyle *style = QStyleFactory::create(styleOpt);
if (style != nullptr)
{
style = new VApplicationStyle(style);
setStyle(style);
}
}
} }
auto *statistic = VGAnalytics::Instance(); auto *statistic = VGAnalytics::Instance();

View File

@ -595,18 +595,6 @@ auto VCommandLine::OptTiledPageOrientation() const -> PageOrientation
return static_cast<PageOrientation>(not IsOptionSet(LONG_OPTION_TILED_PDF_LANDSCAPE)); return static_cast<PageOrientation>(not IsOptionSet(LONG_OPTION_TILED_PDF_LANDSCAPE));
} }
//---------------------------------------------------------------------------------------------------------------------
auto VCommandLine::OptStyle() const -> QString
{
QString style = OptionValue(LONG_OPTION_STYLE);
if (style.isEmpty())
{
return QStringLiteral("native");
}
return style;
}
//---------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------
void VCommandLine::InitCommandLineOptions() void VCommandLine::InitCommandLineOptions()
{ {
@ -812,9 +800,6 @@ void VCommandLine::InitCommandLineOptions()
{LONG_OPTION_TILED_PDF_LANDSCAPE, {LONG_OPTION_TILED_PDF_LANDSCAPE,
translate("VCommandLine", "Set tiled page orienatation to landscape (export mode). Default value if not set " translate("VCommandLine", "Set tiled page orienatation to landscape (export mode). Default value if not set "
"portrait.")}, "portrait.")},
//==============================================================================================================
{LONG_OPTION_STYLE,
translate("VCommandLine", "Application style") + QString(" `Fusion`, `Windows`, `native`, ..."), "", "native"},
}); });
} }

View File

@ -127,8 +127,6 @@ public:
auto OptTiledPaperSize() const -> VAbstractLayoutDialog::PaperSizeTemplate; auto OptTiledPaperSize() const -> VAbstractLayoutDialog::PaperSizeTemplate;
auto OptTiledPageOrientation() const -> PageOrientation; auto OptTiledPageOrientation() const -> PageOrientation;
auto OptStyle() const -> QString;
protected: protected:
VCommandLine(); VCommandLine();

View File

@ -134,9 +134,6 @@ const QString LONG_OPTION_LANDSCAPE_ORIENTATION = QStringLiteral("landscapeOrien
const QString LONG_OPTION_NEST_QUANTITY = QStringLiteral("nestQuantity"); const QString LONG_OPTION_NEST_QUANTITY = QStringLiteral("nestQuantity");
const QString LONG_OPTION_PREFER_ONE_SHEET_SOLUTION = QStringLiteral("preferOneSheetSolution"); const QString LONG_OPTION_PREFER_ONE_SHEET_SOLUTION = QStringLiteral("preferOneSheetSolution");
const QString LONG_OPTION_STYLE = QStringLiteral("style");
const QString LONG_OPTION_STYLESHEET = QStringLiteral("stylesheet");
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
/** /**
* @brief AllKeys return list with all command line keys (short and long forms). Used for testing on conflicts. * @brief AllKeys return list with all command line keys (short and long forms). Used for testing on conflicts.
@ -215,7 +212,5 @@ auto AllKeys() -> QStringList
LONG_OPTION_MANUAL_PRIORITY, LONG_OPTION_MANUAL_PRIORITY,
LONG_OPTION_LANDSCAPE_ORIENTATION, LONG_OPTION_LANDSCAPE_ORIENTATION,
LONG_OPTION_NEST_QUANTITY, LONG_OPTION_NEST_QUANTITY,
LONG_OPTION_PREFER_ONE_SHEET_SOLUTION, LONG_OPTION_PREFER_ONE_SHEET_SOLUTION};
LONG_OPTION_STYLE,
LONG_OPTION_STYLESHEET};
} }

View File

@ -130,8 +130,6 @@ extern const QString LONG_OPTION_MANUAL_PRIORITY;
extern const QString LONG_OPTION_LANDSCAPE_ORIENTATION; extern const QString LONG_OPTION_LANDSCAPE_ORIENTATION;
extern const QString LONG_OPTION_NEST_QUANTITY; extern const QString LONG_OPTION_NEST_QUANTITY;
extern const QString LONG_OPTION_PREFER_ONE_SHEET_SOLUTION; extern const QString LONG_OPTION_PREFER_ONE_SHEET_SOLUTION;
extern const QString LONG_OPTION_STYLE;
extern const QString LONG_OPTION_STYLESHEET;
auto AllKeys() -> QStringList; auto AllKeys() -> QStringList;

View File

@ -131,16 +131,19 @@ auto StandardIconPaths() -> QHash<QStyle::StandardPixmap, QString>
} // namespace } // namespace
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VApplicationStyle::VApplicationStyle(const QStyle *style) VApplicationStyle::VApplicationStyle(QStyle *style)
: m_style(style) : m_style(style)
{ {
m_style->setParent(this);
} }
#if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VApplicationStyle::~VApplicationStyle() auto VApplicationStyle::name() const -> QString
{ {
delete m_style; return m_style->name();
} }
#endif
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VApplicationStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const void VApplicationStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const

View File

@ -40,8 +40,12 @@ class VApplicationStyle : public QStyle
Q_OBJECT // NOLINT Q_OBJECT // NOLINT
public: public:
VApplicationStyle(const QStyle *style); explicit VApplicationStyle(QStyle *style);
~VApplicationStyle() override; ~VApplicationStyle() override = default;
#if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0)
auto name() const -> QString;
#endif
void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const override; void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const override;
void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const override; void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const override;
@ -71,7 +75,7 @@ public:
private: private:
Q_DISABLE_COPY_MOVE(VApplicationStyle) // NOLINT Q_DISABLE_COPY_MOVE(VApplicationStyle) // NOLINT
const QStyle *m_style; QStyle *m_style;
auto StyleIcon(StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const -> QIcon; auto StyleIcon(StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const -> QIcon;
auto StylesheetIcon(StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const -> QIcon; auto StylesheetIcon(StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const -> QIcon;

View File

@ -32,9 +32,12 @@
#include <QPainter> #include <QPainter>
#include <QPalette> #include <QPalette>
#include <QPixmap> #include <QPixmap>
#include <QStyle>
#include <QStyleFactory>
#include <QTextStream> #include <QTextStream>
#include <QtDebug> #include <QtDebug>
#include <QtGlobal> #include <QtGlobal>
#include <QtSvg/QSvgRenderer>
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)
#include <QOperatingSystemVersion> #include <QOperatingSystemVersion>
@ -66,10 +69,9 @@ using namespace bpstd::literals::chrono_literals;
#include "../defglobal.h" #include "../defglobal.h"
#include "../vabstractapplication.h" #include "../vabstractapplication.h"
#include "vapplicationstyle.h"
#include "vscenestylesheet.h" #include "vscenestylesheet.h"
#include <QtSvg/QSvgRenderer>
namespace namespace
{ {
#if QT_VERSION < QT_VERSION_CHECK(5, 9, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 9, 0)
@ -350,6 +352,46 @@ auto VTheme::DefaultThemeName() -> QString
#endif #endif
} }
//---------------------------------------------------------------------------------------------------------------------
void VTheme::InitApplicationStyle()
{
VThemeMode themeMode = VAbstractApplication::VApp()->Settings()->GetThemeMode();
if (themeMode == VThemeMode::Light || themeMode == VThemeMode::Dark)
{
QStyle *style = QStyleFactory::create(QStringLiteral("fusion"));
if (style != nullptr)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
Instance()->SetDefaultApplicationStyle(qApp->style());
style = new VApplicationStyle(style);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
qApp->setStyle(style);
}
return;
}
#if defined(Q_OS_WIN)
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
if (NativeDarkThemeAvailable())
{
if (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark)
{
QStyle *style = QStyleFactory::create(QStringLiteral("fusion"));
if (style != nullptr)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
Instance()->SetDefaultApplicationStyle(qApp->style());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast)
qApp->setStyle(style);
}
}
}
#endif
#endif
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTheme::SetIconTheme() void VTheme::SetIconTheme()
{ {
@ -505,7 +547,9 @@ auto VTheme::ThemeStylesheet() -> QString
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTheme::ResetThemeSettings() const void VTheme::ResetThemeSettings() const
{ {
qApp->setStyle(Instance()->GetDefaultApplicationStyle());
SetToAutoTheme(); SetToAutoTheme();
InitApplicationStyle();
SetIconTheme(); SetIconTheme();
InitThemeMode(); InitThemeMode();
VSceneStylesheet::ResetStyles(); VSceneStylesheet::ResetStyles();
@ -583,3 +627,26 @@ VTheme::VTheme(QObject *parent)
} }
#endif #endif
} }
//---------------------------------------------------------------------------------------------------------------------
auto VTheme::GetDefaultApplicationStyle() const -> QStyle *
{
return m_defaultApplicationStyle;
}
//---------------------------------------------------------------------------------------------------------------------
void VTheme::SetDefaultApplicationStyle(QStyle *defaultApplicationStyle)
{
QStyle *old = m_defaultApplicationStyle;
m_defaultApplicationStyle = defaultApplicationStyle;
if (m_defaultApplicationStyle)
{
m_defaultApplicationStyle->setParent(this);
}
if (old && old->parent() == this)
{
delete old;
}
}

View File

@ -42,6 +42,8 @@ enum class VColorSheme
Dark Dark
}; };
class QStyle;
class VTheme : public QObject class VTheme : public QObject
{ {
Q_OBJECT // NOLINT Q_OBJECT // NOLINT
@ -66,6 +68,7 @@ public:
static auto ShouldApplyDarkTheme() -> bool; static auto ShouldApplyDarkTheme() -> bool;
static auto ColorSheme() -> VColorSheme; static auto ColorSheme() -> VColorSheme;
static auto DefaultThemeName() -> QString; static auto DefaultThemeName() -> QString;
static void InitApplicationStyle();
static void SetIconTheme(); static void SetIconTheme();
static void InitThemeMode(); static void InitThemeMode();
static auto ThemeStylesheet() -> QString; static auto ThemeStylesheet() -> QString;
@ -79,10 +82,14 @@ private:
explicit VTheme(QObject *parent = nullptr); explicit VTheme(QObject *parent = nullptr);
QString m_defaultThemeName{}; QString m_defaultThemeName{};
QStyle *m_defaultApplicationStyle{nullptr};
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
QTimer *m_themeTimer{nullptr}; QTimer *m_themeTimer{nullptr};
bool m_darkTheme{false}; bool m_darkTheme{false};
#endif #endif
auto GetDefaultApplicationStyle() const -> QStyle *;
void SetDefaultApplicationStyle(QStyle *defaultApplicationStyle);
}; };
#endif // VTHEME_H #endif // VTHEME_H