From b0e5c3001b5422e84177ff65caff8a51baffadf3 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sat, 19 Oct 2024 19:53:36 +0200 Subject: [PATCH] Issue #200: Icons under dark theme on Arch/KDE is broken --- Pdf4QtEditor/main.cpp | 15 +++++++++++++++ Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp | 11 ++--------- Pdf4QtLibWidgets/sources/pdfwidgetutils.h | 2 +- Pdf4QtViewer/main.cpp | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Pdf4QtEditor/main.cpp b/Pdf4QtEditor/main.cpp index 55f2c40..9825250 100644 --- a/Pdf4QtEditor/main.cpp +++ b/Pdf4QtEditor/main.cpp @@ -18,6 +18,7 @@ #include "pdfeditormainwindow.h" #include "pdfconstants.h" #include "pdfsecurityhandler.h" +#include "pdfwidgetutils.h" #include #include @@ -39,10 +40,14 @@ int main(int argc, char *argv[]) QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Editor")); QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents."); + QCommandLineOption lightGui("theme-light", "Use a light theme for the GUI."); + QCommandLineOption darkGui("theme-dark", "Use a dark theme for the GUI."); QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::applicationName()); parser.addOption(noDrm); + parser.addOption(lightGui); + parser.addOption(darkGui); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("file", "The PDF file to open."); @@ -53,6 +58,16 @@ int main(int argc, char *argv[]) pdf::PDFSecurityHandler::setNoDRMMode(); } + if (parser.isSet(lightGui)) + { + pdf::PDFWidgetUtils::setDarkTheme(false); + } + + if (parser.isSet(darkGui)) + { + pdf::PDFWidgetUtils::setDarkTheme(true); + } + QIcon appIcon(":/app-icon.svg"); QApplication::setWindowIcon(appIcon); diff --git a/Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp b/Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp index dc934e5..67f15d7 100644 --- a/Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp +++ b/Pdf4QtLibWidgets/sources/pdfwidgetutils.cpp @@ -46,8 +46,6 @@ int qt_default_dpi_y() { return 96; } namespace pdf { -std::optional s_isDarkThemeOverride; - static constexpr bool isScalingNeeded() { return QT_VERSION_MAJOR < 6; @@ -176,18 +174,13 @@ void PDFWidgetUtils::style(QWidget* widget) } } -void PDFWidgetUtils::overrideDarkTheme(bool isDarkTheme) +void PDFWidgetUtils::setDarkTheme(bool isDarkTheme) { - s_isDarkThemeOverride = isDarkTheme; + QApplication::styleHints()->setColorScheme(isDarkTheme ? Qt::ColorScheme::Dark : Qt::ColorScheme::Light); } bool PDFWidgetUtils::isDarkTheme() { - if (s_isDarkThemeOverride.has_value()) - { - return s_isDarkThemeOverride.value(); - } - Qt::ColorScheme colorScheme = QApplication::styleHints()->colorScheme(); return colorScheme == Qt::ColorScheme::Dark; } diff --git a/Pdf4QtLibWidgets/sources/pdfwidgetutils.h b/Pdf4QtLibWidgets/sources/pdfwidgetutils.h index b539b6f..bfa296e 100644 --- a/Pdf4QtLibWidgets/sources/pdfwidgetutils.h +++ b/Pdf4QtLibWidgets/sources/pdfwidgetutils.h @@ -61,7 +61,7 @@ public: static void style(QWidget* widget); /// Overrides automatically detected dark theme / light theme settings - static void overrideDarkTheme(bool isDarkTheme); + static void setDarkTheme(bool isDarkTheme); /// Returns true if the dark theme is currently set for the application. static bool isDarkTheme(); diff --git a/Pdf4QtViewer/main.cpp b/Pdf4QtViewer/main.cpp index 62a7cbe..a22ef72 100644 --- a/Pdf4QtViewer/main.cpp +++ b/Pdf4QtViewer/main.cpp @@ -18,6 +18,7 @@ #include "pdfviewermainwindow.h" #include "pdfconstants.h" #include "pdfsecurityhandler.h" +#include "pdfwidgetutils.h" #include #include @@ -33,10 +34,14 @@ int main(int argc, char *argv[]) QApplication::setApplicationDisplayName(QApplication::translate("Application", "PDF4QT Viewer")); QCommandLineOption noDrm("no-drm", "Disable DRM settings of documents."); + QCommandLineOption lightGui("theme-light", "Use a light theme for the GUI."); + QCommandLineOption darkGui("theme-dark", "Use a dark theme for the GUI."); QCommandLineParser parser; parser.setApplicationDescription(QCoreApplication::applicationName()); parser.addOption(noDrm); + parser.addOption(lightGui); + parser.addOption(darkGui); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("file", "The PDF file to open."); @@ -47,6 +52,16 @@ int main(int argc, char *argv[]) pdf::PDFSecurityHandler::setNoDRMMode(); } + if (parser.isSet(lightGui)) + { + pdf::PDFWidgetUtils::setDarkTheme(false); + } + + if (parser.isSet(darkGui)) + { + pdf::PDFWidgetUtils::setDarkTheme(true); + } + QIcon appIcon(":/app-icon.svg"); QApplication::setWindowIcon(appIcon);