Skip to content

Commit

Permalink
Fix theming with Qt 5.15
Browse files Browse the repository at this point in the history
* Fixes #4765
* Fixes #4766
  • Loading branch information
droidmonkey committed Jun 2, 2020
1 parent b21bee4 commit 4ab956a
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 26 deletions.
45 changes: 25 additions & 20 deletions src/gui/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/gui/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/gui/styles/base/BaseStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<int>(swatch.color(S_base_divider).rgb()) : 0;
return static_cast<int>(swatch.color(S_base_divider).rgb());
}
case SH_MessageBox_TextInteractionFlags:
return Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse;
Expand Down
5 changes: 4 additions & 1 deletion src/gui/styles/dark/DarkStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
#include <QMenuBar>
#include <QToolBar>

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"));
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/gui/styles/dark/DarkStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion src/gui/styles/light/LightStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
#include <QMenuBar>
#include <QToolBar>

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"));
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/gui/styles/light/LightStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/gui/TestGuiBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/gui/TestGuiFdoSecrets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4ab956a

Please sign in to comment.