From 41a1ede6fc71079349dd94df722d6be33a695777 Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 23 Mar 2023 23:46:11 +0100 Subject: [PATCH] Improve handling style changes in custom dialog style * Avoid using global palette when computing stylesheet * Re-compute stylesheet on palette changes --- aboutdialog/aboutdialog.cpp | 14 ++++- aboutdialog/aboutdialog.h | 3 + enterpassworddialog/enterpassworddialog.cpp | 5 +- misc/dialogutils.cpp | 63 ++++++++++++++------- misc/dialogutils.h | 12 ++-- settingsdialog/settingsdialog.cpp | 15 ++++- settingsdialog/settingsdialog.h | 1 + 7 files changed, 87 insertions(+), 26 deletions(-) diff --git a/aboutdialog/aboutdialog.cpp b/aboutdialog/aboutdialog.cpp index 8477e2e..e373b10 100644 --- a/aboutdialog/aboutdialog.cpp +++ b/aboutdialog/aboutdialog.cpp @@ -44,7 +44,7 @@ AboutDialog::AboutDialog(QWidget *parent, const QString &applicationName, const { m_ui->setupUi(this); makeHeading(m_ui->productNameLabel); - setStyleSheet(dialogStyle()); + setStyleSheet(dialogStyleForPalette(palette())); setWindowFlags((windowFlags()) & ~(Qt::WindowMinMaxButtonsHint | Qt::WindowContextHelpButtonHint | Qt::WindowFullscreenButtonHint)); if (!applicationName.isEmpty()) { m_ui->productNameLabel->setText(applicationName); @@ -118,6 +118,18 @@ AboutDialog::~AboutDialog() { } +bool AboutDialog::event(QEvent *event) +{ + const auto res = QDialog::event(event); + switch (event->type()) { + case QEvent::PaletteChange: + setStyleSheet(dialogStyleForPalette(palette())); + break; + default:; + } + return res; +} + void AboutDialog::linkActivated(const QString &link) { if (link == QLatin1String("qtversion")) { diff --git a/aboutdialog/aboutdialog.h b/aboutdialog/aboutdialog.h index d29d53d..17d2a03 100644 --- a/aboutdialog/aboutdialog.h +++ b/aboutdialog/aboutdialog.h @@ -27,6 +27,9 @@ public: explicit AboutDialog(QWidget *parent, const QString &website = QString(), const QString &description = QString(), const QImage &image = QImage()); ~AboutDialog() override; +protected: + bool event(QEvent *event) override; + private Q_SLOTS: void linkActivated(const QString &link); diff --git a/enterpassworddialog/enterpassworddialog.cpp b/enterpassworddialog/enterpassworddialog.cpp index 8a78d2c..8850628 100644 --- a/enterpassworddialog/enterpassworddialog.cpp +++ b/enterpassworddialog/enterpassworddialog.cpp @@ -43,7 +43,7 @@ EnterPasswordDialog::EnterPasswordDialog(QWidget *parent) // setup ui m_ui->setupUi(this); makeHeading(m_ui->instructionLabel); - setStyleSheet(dialogStyle()); + setStyleSheet(dialogStyleForPalette(palette())); setDescription(); setPromptForUserName(false); setVerificationRequired(false); @@ -212,6 +212,9 @@ void EnterPasswordDialog::setInstruction(const QString &value) bool EnterPasswordDialog::event(QEvent *event) { switch (event->type()) { + case QEvent::PaletteChange: + setStyleSheet(dialogStyleForPalette(palette())); + break; case QEvent::KeyPress: { QKeyEvent *keyEvent = static_cast(event); if (keyEvent->key() == Qt::Key_CapsLock) { diff --git a/misc/dialogutils.cpp b/misc/dialogutils.cpp index d583b3c..d78a0b7 100644 --- a/misc/dialogutils.cpp +++ b/misc/dialogutils.cpp @@ -1,5 +1,7 @@ #include "./dialogutils.h" +#include "../misc/desktoputils.h" + #include #include #include @@ -56,13 +58,28 @@ QString generateWindowTitle(DocumentStatus documentStatus, const QString &docume #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK) #ifdef Q_OS_WIN32 +/*! + * \brief Returns the color used to draw frames. + */ +QColor windowFrameColorForPalette(const QPalette &palette) +{ + return palette.window().color().darker(108); +} /*! * \brief Returns the color used to draw frames. */ QColor windowFrameColor() { - return QGuiApplication::palette().window().color().darker(108); + return windowFrameColorForPalette(QGuiApplication::palette()); +} + +/*! + * \brief Returns the color used to draw instructions. + */ +QColor instructionTextColorForPalette(const QPalette &palette) +{ + return isPaletteDark(palette) ? palette.text().color() : QColor(0x00, 0x33, 0x99); } /*! @@ -70,33 +87,41 @@ QColor windowFrameColor() */ QColor instructionTextColor() { - const auto baseColor = QGuiApplication::palette().base().color(); - return (baseColor.value() > 204 && baseColor.saturation() < 63) ? QColor(0x00, 0x33, 0x99) : QGuiApplication::palette().text().color(); + return instructionTextColorForPalette(QGuiApplication::palette()); } - #endif +/*! + * \brief Returns the stylesheet for dialogs and other windows used in my + * applications. + */ +const QString &dialogStyleForPalette(const QPalette &palette) +{ +#ifdef Q_OS_WINDOWS + return QStringLiteral("#mainWidget { color: palette(text); background-color: " + "palette(base); border: none; }" + "#bottomWidget { background-color: palette(window); " + "color: palette(window-text); border-top: 1px solid %1; }" + "QMessageBox QLabel, QInputDialog QLabel, " + "*[classNames~=\"heading\"] { font-size: 12pt; color: %2; " + "}" + "*[classNames~=\"input-invalid\"] { color: red; }") + .arg(windowFrameColorForPalette(palette).name(), instructionTextColorForPalette(palette).name()); +#else + Q_UNUSED(palette) + static const auto style = QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }" + "*[classNames~=\"input-invalid\"] { color: red; }"); + return style; +#endif +} + /*! * \brief Returns the stylesheet for dialogs and other windows used in my * applications. */ const QString &dialogStyle() { -#ifdef Q_OS_WIN32 - static const auto style = QStringLiteral("#mainWidget { color: palette(text); background-color: " - "palette(base); border: none; }" - "#bottomWidget { background-color: palette(window); " - "color: palette(window-text); border-top: 1px solid %1; }" - "QMessageBox QLabel, QInputDialog QLabel, " - "*[classNames~=\"heading\"] { font-size: 12pt; color: %2; " - "}" - "*[classNames~=\"input-invalid\"] { color: red; }") - .arg(windowFrameColor().name(), instructionTextColor().name()); -#else - static const auto style = QStringLiteral("*[classNames~=\"heading\"] { font-weight: bold; }" - "*[classNames~=\"input-invalid\"] { color: red; }"); -#endif - return style; + return dialogStyleForPalette(QGuiApplication::palette()); } #ifdef QT_UTILITIES_GUI_QTWIDGETS diff --git a/misc/dialogutils.h b/misc/dialogutils.h index 0b9aca1..1007ac1 100644 --- a/misc/dialogutils.h +++ b/misc/dialogutils.h @@ -8,6 +8,7 @@ QT_FORWARD_DECLARE_CLASS(QString) QT_FORWARD_DECLARE_CLASS(QWidget) QT_FORWARD_DECLARE_CLASS(QColor) +QT_FORWARD_DECLARE_CLASS(QPalette) QT_FORWARD_DECLARE_CLASS(QPoint) QT_FORWARD_DECLARE_CLASS(QRect) @@ -29,11 +30,14 @@ enum class DocumentStatus { QT_UTILITIES_EXPORT QString generateWindowTitle(DocumentStatus documentStatus, const QString &documentPath); #if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK) -#ifdef Q_OS_WIN32 -QT_UTILITIES_EXPORT QColor windowFrameColor(); -QT_UTILITIES_EXPORT QColor instructionTextColor(); +#ifdef Q_OS_WINDOWS +[[deprecated]] QT_UTILITIES_EXPORT QColor windowFrameColor(); +QT_UTILITIES_EXPORT QColor windowFrameColorForPalette(const QPalette &palette); +[[deprecated]] QT_UTILITIES_EXPORT QColor instructionTextColor(); +QT_UTILITIES_EXPORT QColor instructionTextColorForPalette(const QPalette &palette); #endif -QT_UTILITIES_EXPORT const QString &dialogStyle(); +[[deprecated]] QT_UTILITIES_EXPORT const QString &dialogStyle(); +QT_UTILITIES_EXPORT const QString &dialogStyleForPalette(const QPalette &palette); #ifdef QT_UTILITIES_GUI_QTWIDGETS QT_UTILITIES_EXPORT QRect availableScreenGeometryAtPoint(const QPoint &point); QT_UTILITIES_EXPORT void centerWidget(QWidget *widget, const QWidget *parent = nullptr, const QPoint *position = nullptr); diff --git a/settingsdialog/settingsdialog.cpp b/settingsdialog/settingsdialog.cpp index 0542fbf..ce5f8e5 100644 --- a/settingsdialog/settingsdialog.cpp +++ b/settingsdialog/settingsdialog.cpp @@ -37,7 +37,7 @@ SettingsDialog::SettingsDialog(QWidget *parent) { m_ui->setupUi(this); makeHeading(m_ui->headingLabel); - setStyleSheet(dialogStyle()); + setStyleSheet(dialogStyleForPalette(palette())); // setup models m_categoryFilterModel->setSourceModel(m_categoryModel); @@ -329,4 +329,17 @@ void SettingsDialog::reset() } emit resetted(); } + +bool SettingsDialog::event(QEvent *event) +{ + const auto res = QDialog::event(event); + switch (event->type()) { + case QEvent::PaletteChange: + setStyleSheet(dialogStyleForPalette(palette())); + break; + default:; + } + return res; +} + } // namespace QtUtilities diff --git a/settingsdialog/settingsdialog.h b/settingsdialog/settingsdialog.h index 567dbd2..08bca21 100644 --- a/settingsdialog/settingsdialog.h +++ b/settingsdialog/settingsdialog.h @@ -46,6 +46,7 @@ Q_SIGNALS: void resetted(); protected: + bool event(QEvent *event) override; void showEvent(QShowEvent *event) override; private Q_SLOTS: