Improve handling style changes in custom dialog style

* Avoid using global palette when computing stylesheet
* Re-compute stylesheet on palette changes
This commit is contained in:
Martchus 2023-03-23 23:46:11 +01:00
parent bbb65ae0dd
commit 41a1ede6fc
7 changed files with 87 additions and 26 deletions

View File

@ -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")) {

View File

@ -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);

View File

@ -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<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_CapsLock) {

View File

@ -1,5 +1,7 @@
#include "./dialogutils.h"
#include "../misc/desktoputils.h"
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
@ -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

View File

@ -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);

View File

@ -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

View File

@ -46,6 +46,7 @@ Q_SIGNALS:
void resetted();
protected:
bool event(QEvent *event) override;
void showEvent(QShowEvent *event) override;
private Q_SLOTS: