From 7f50f99f68d2badcd6d56a8678493f0cdf411a99 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sat, 30 May 2020 10:28:43 -0400 Subject: [PATCH] Fix theming with Qt 5.15 * Fixes #4765 * Fixes #4766 --- src/gui/Application.cpp | 45 ++++++++++++++++------------- src/gui/Application.h | 2 ++ src/gui/styles/base/BaseStyle.cpp | 5 ++-- src/gui/styles/dark/DarkStyle.cpp | 5 +++- src/gui/styles/dark/DarkStyle.h | 3 +- src/gui/styles/light/LightStyle.cpp | 5 +++- src/gui/styles/light/LightStyle.h | 3 +- src/main.cpp | 1 + tests/gui/TestGui.cpp | 1 + tests/gui/TestGuiBrowser.cpp | 1 + tests/gui/TestGuiFdoSecrets.cpp | 1 + 11 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/gui/Application.cpp b/src/gui/Application.cpp index 4a382567ed..894678a96a 100644 --- a/src/gui/Application.cpp +++ b/src/gui/Application.cpp @@ -68,26 +68,6 @@ Application::Application(int& argc, char** argv) registerUnixSignals(); #endif - QString appTheme = config()->get(Config::GUI_ApplicationTheme).toString(); - if (appTheme == "auto") { - if (osUtils->isDarkMode()) { - setStyle(new DarkStyle); - m_darkTheme = true; - } else { - setStyle(new LightStyle); - } - } else if (appTheme == "light") { - setStyle(new LightStyle); - } else if (appTheme == "dark") { - setStyle(new DarkStyle); - m_darkTheme = true; - } else { - // Classic mode, only check for dark theme when not on Windows -#ifndef Q_OS_WIN - m_darkTheme = osUtils->isDarkMode(); -#endif - } - QString userName = qgetenv("USER"); if (userName.isEmpty()) { userName = qgetenv("USERNAME"); @@ -162,6 +142,31 @@ Application::~Application() } } +void Application::applyTheme() +{ + QString appTheme = config()->get(Config::GUI_ApplicationTheme).toString(); + if (appTheme == "auto") { + if (osUtils->isDarkMode()) { + setStyle(new DarkStyle); + m_darkTheme = true; + } else { + setStyle(new LightStyle); + } + } else if (appTheme == "light") { + setStyle(new LightStyle); + } else if (appTheme == "dark") { + setStyle(new DarkStyle); + m_darkTheme = true; + } else { + // Classic mode, only check for dark theme when not on Windows +#ifndef Q_OS_WIN + m_darkTheme = osUtils->isDarkMode(); +#endif + } + + setPalette(style()->standardPalette()); +} + bool Application::event(QEvent* event) { // Handle Apple QFileOpenEvent from finder (double click on .kdbx file) diff --git a/src/gui/Application.h b/src/gui/Application.h index b39fbe0e97..21dff6affc 100644 --- a/src/gui/Application.h +++ b/src/gui/Application.h @@ -39,6 +39,8 @@ class Application : public QApplication Application(int& argc, char** argv); ~Application() override; + void applyTheme(); + bool event(QEvent* event) override; bool isAlreadyRunning() const; bool isDarkTheme() const; diff --git a/src/gui/styles/base/BaseStyle.cpp b/src/gui/styles/base/BaseStyle.cpp index 3b3c60411e..aac9daf1c7 100644 --- a/src/gui/styles/base/BaseStyle.cpp +++ b/src/gui/styles/base/BaseStyle.cpp @@ -397,7 +397,6 @@ namespace Phantom colors[S_button] = Dc::adjustLightness(colors[S_button], 0.01); colors[S_base] = pal.color(QPalette::Base); colors[S_text] = pal.color(QPalette::Text); - colors[S_text] = pal.color(QPalette::WindowText); colors[S_windowText] = pal.color(QPalette::WindowText); colors[S_highlight] = pal.color(QPalette::Highlight); colors[S_highlightedText] = pal.color(QPalette::HighlightedText); @@ -4622,11 +4621,13 @@ int BaseStyle::styleHint(StyleHint hint, case SH_Table_GridLineColor: { using namespace Phantom::SwatchColors; namespace Ph = Phantom; + if (!option) + return 0; auto ph_swatchPtr = Ph::getCachedSwatchOfQPalette(&d->swatchCache, &d->headSwatchFastKey, option->palette); const Ph::PhSwatch& swatch = *ph_swatchPtr.data(); // Qt code in table views for drawing grid lines is broken. See case for // CE_ItemViewItem painting for more information. - return option ? static_cast(swatch.color(S_base_divider).rgb()) : 0; + return static_cast(swatch.color(S_base_divider).rgb()); } case SH_MessageBox_TextInteractionFlags: return Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse; diff --git a/src/gui/styles/dark/DarkStyle.cpp b/src/gui/styles/dark/DarkStyle.cpp index 2617679c05..4a8dea2563 100644 --- a/src/gui/styles/dark/DarkStyle.cpp +++ b/src/gui/styles/dark/DarkStyle.cpp @@ -23,8 +23,9 @@ #include #include -void DarkStyle::polish(QPalette& palette) +QPalette DarkStyle::standardPalette() const { + auto palette = BaseStyle::standardPalette(); palette.setColor(QPalette::Active, QPalette::Window, QStringLiteral("#3B3B3D")); palette.setColor(QPalette::Inactive, QPalette::Window, QStringLiteral("#404042")); palette.setColor(QPalette::Disabled, QPalette::Window, QStringLiteral("#424242")); @@ -84,6 +85,8 @@ void DarkStyle::polish(QPalette& palette) palette.setColor(QPalette::Disabled, QPalette::Link, QStringLiteral("#74A474")); palette.setColor(QPalette::All, QPalette::LinkVisited, QStringLiteral("#75B875")); palette.setColor(QPalette::Disabled, QPalette::LinkVisited, QStringLiteral("#77A677")); + + return palette; } QString DarkStyle::getAppStyleSheet() const diff --git a/src/gui/styles/dark/DarkStyle.h b/src/gui/styles/dark/DarkStyle.h index aab949c3a2..9b955d3a51 100644 --- a/src/gui/styles/dark/DarkStyle.h +++ b/src/gui/styles/dark/DarkStyle.h @@ -26,8 +26,9 @@ class DarkStyle : public BaseStyle Q_OBJECT public: + QPalette standardPalette() const override; + using BaseStyle::polish; - void polish(QPalette& palette) override; void polish(QWidget* widget) override; protected: diff --git a/src/gui/styles/light/LightStyle.cpp b/src/gui/styles/light/LightStyle.cpp index 40cb583975..ad4c4feeac 100644 --- a/src/gui/styles/light/LightStyle.cpp +++ b/src/gui/styles/light/LightStyle.cpp @@ -24,8 +24,9 @@ #include #include -void LightStyle::polish(QPalette& palette) +QPalette LightStyle::standardPalette() const { + auto palette = BaseStyle::standardPalette(); palette.setColor(QPalette::Active, QPalette::Window, QStringLiteral("#F7F7F7")); palette.setColor(QPalette::Inactive, QPalette::Window, QStringLiteral("#FCFCFC")); palette.setColor(QPalette::Disabled, QPalette::Window, QStringLiteral("#EDEDED")); @@ -85,6 +86,8 @@ void LightStyle::polish(QPalette& palette) palette.setColor(QPalette::Disabled, QPalette::Link, QStringLiteral("#4F6935")); palette.setColor(QPalette::All, QPalette::LinkVisited, QStringLiteral("#507826")); palette.setColor(QPalette::Disabled, QPalette::LinkVisited, QStringLiteral("#506935")); + + return palette; } QString LightStyle::getAppStyleSheet() const diff --git a/src/gui/styles/light/LightStyle.h b/src/gui/styles/light/LightStyle.h index 72153bd15f..d2d4f48a3d 100644 --- a/src/gui/styles/light/LightStyle.h +++ b/src/gui/styles/light/LightStyle.h @@ -26,8 +26,9 @@ class LightStyle : public BaseStyle Q_OBJECT public: + QPalette standardPalette() const override; + using BaseStyle::polish; - void polish(QPalette& palette) override; void polish(QWidget* widget) override; protected: diff --git a/src/main.cpp b/src/main.cpp index 89ea235ad8..9a0ba41725 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,6 +57,7 @@ int main(int argc, char** argv) Application::setApplicationName("KeePassXC"); Application::setApplicationVersion(KEEPASSXC_VERSION); app.setProperty("KPXC_QUALIFIED_APPNAME", "org.keepassxc.KeePassXC"); + app.applyTheme(); #if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0) QGuiApplication::setDesktopFileName(app.property("KPXC_QUALIFIED_APPNAME").toString() + QStringLiteral(".desktop")); #endif diff --git a/tests/gui/TestGui.cpp b/tests/gui/TestGui.cpp index 5cfe323b64..c2ef5e1929 100644 --- a/tests/gui/TestGui.cpp +++ b/tests/gui/TestGui.cpp @@ -95,6 +95,7 @@ int main(int argc, char* argv[]) app.setApplicationVersion(KEEPASSXC_VERSION); app.setQuitOnLastWindowClosed(false); app.setAttribute(Qt::AA_Use96Dpi, true); + app.applyTheme(); QTEST_DISABLE_KEYPAD_NAVIGATION TestGui tc; QTEST_SET_MAIN_SOURCE_PATH diff --git a/tests/gui/TestGuiBrowser.cpp b/tests/gui/TestGuiBrowser.cpp index 6eac62798b..fb981d6df6 100644 --- a/tests/gui/TestGuiBrowser.cpp +++ b/tests/gui/TestGuiBrowser.cpp @@ -59,6 +59,7 @@ int main(int argc, char* argv[]) app.setApplicationVersion(KEEPASSXC_VERSION); app.setQuitOnLastWindowClosed(false); app.setAttribute(Qt::AA_Use96Dpi, true); + app.applyTheme(); QTEST_DISABLE_KEYPAD_NAVIGATION TestGuiBrowser tc; QTEST_SET_MAIN_SOURCE_PATH diff --git a/tests/gui/TestGuiFdoSecrets.cpp b/tests/gui/TestGuiFdoSecrets.cpp index d223972c24..c869aac4fb 100644 --- a/tests/gui/TestGuiFdoSecrets.cpp +++ b/tests/gui/TestGuiFdoSecrets.cpp @@ -63,6 +63,7 @@ int main(int argc, char* argv[]) app.setApplicationVersion(KEEPASSXC_VERSION); app.setQuitOnLastWindowClosed(false); app.setAttribute(Qt::AA_Use96Dpi, true); + app.applyTheme(); QTEST_DISABLE_KEYPAD_NAVIGATION TestGuiFdoSecrets tc; QTEST_SET_MAIN_SOURCE_PATH