Commit b29f33a7 authored by Johannes Zarl-Zierl's avatar Johannes Zarl-Zierl

Fix inconsistencies when changing color schemes.

Instead of changing the palette of a widget, it is often possible to
change the foreground and/or background role to use different colors of
the standard palette. This way, changes in the palette can propagete to
the widget without additional work.

When this is not an option, the widget needs to react to palette changes
explicitly and (re-)adjust the palette accordingly.

UI changes: The exif grid now uses regular highlight colors instead of
hardcoded bright red.

A very big thank you to David Redondo for helping me get a better
understanding of the palette system and for pointing out how to avoid
breaking palette propagation!
parent 30629fd4
/* Copyright (C) 2003-2019 The KPhotoAlbum Development Team
/* Copyright (C) 2003-2020 The KPhotoAlbum Development Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
......@@ -389,10 +389,7 @@ void Browser::BrowserWidget::createWidgets()
m_treeView->setAcceptDrops(true);
m_treeView->setDropIndicatorShown(true);
m_treeView->setDefaultDropAction(Qt::MoveAction);
QPalette pal = m_treeView->palette();
pal.setBrush(QPalette::Base, QApplication::palette().color(QPalette::Background));
m_treeView->setPalette(pal);
m_treeView->setBackgroundRole(QPalette::Background);
m_treeView->header()->setStretchLastSection(false);
m_treeView->header()->setSortIndicatorShown(true);
......
/* Copyright (C) 2003-2019 The KPhotoAlbum Development Team
/* Copyright (C) 2003-2020 The KPhotoAlbum Development Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
......@@ -31,10 +31,6 @@ Browser::CenteringIconView::CenteringIconView(QWidget *parent)
: QListView(parent)
, m_viewMode(NormalIconView)
{
QPalette pal = palette();
pal.setBrush(QPalette::Base, QApplication::palette().color(QPalette::Base));
setPalette(pal);
setGridSize(QSize(CELLWIDTH, CELLHEIGHT));
viewport()->setAutoFillBackground(false);
......
......@@ -25,6 +25,7 @@
#include <QGridLayout>
#include <QLabel>
#include <QPainter>
#include <QPalette>
#include <QResizeEvent>
#include <QScrollBar>
#include <QTimer>
......@@ -74,8 +75,8 @@ void Exif::Grid::setupUI(const QString &charset)
int count = 0;
for (const QString &key : sorted) {
const int subrow = (count % perCol);
const QColor color = (subrow & 1) ? palette().base().color() : palette().alternateBase().color();
QPair<QLabel *, QLabel *> pair = infoLabelPair(exifNameNoGroup(key), items[key].join(QLatin1String(", ")), color);
const QPalette::ColorRole role = (subrow & 1) ? QPalette::Base : QPalette::AlternateBase;
QPair<QLabel *, QLabel *> pair = infoLabelPair(exifNameNoGroup(key), items[key].join(QLatin1String(", ")), role);
int col = (count / perCol) * 2;
layout->addWidget(pair.first, row + subrow, col);
......@@ -94,25 +95,21 @@ QLabel *Exif::Grid::headerLabel(const QString &title)
{
QLabel *label = new QLabel(title);
QPalette pal;
pal.setBrush(QPalette::Window, palette().dark());
pal.setBrush(QPalette::WindowText, palette().brightText());
label->setPalette(pal);
label->setBackgroundRole(QPalette::Dark);
label->setForegroundRole(QPalette::BrightText);
label->setAutoFillBackground(true);
label->setAlignment(Qt::AlignCenter);
return label;
}
QPair<QLabel *, QLabel *> Exif::Grid::infoLabelPair(const QString &title, const QString &value, const QColor &color)
QPair<QLabel *, QLabel *> Exif::Grid::infoLabelPair(const QString &title, const QString &value, const QPalette::ColorRole backgroundRole)
{
QLabel *keyLabel = new QLabel(title);
QLabel *valueLabel = new QLabel(value);
QPalette pal;
pal.setBrush(QPalette::Background, color);
keyLabel->setPalette(pal);
valueLabel->setPalette(pal);
keyLabel->setBackgroundRole(backgroundRole);
valueLabel->setBackgroundRole(backgroundRole);
keyLabel->setAutoFillBackground(true);
valueLabel->setAutoFillBackground(true);
m_labels.append(qMakePair(keyLabel, valueLabel));
......@@ -164,10 +161,12 @@ void Exif::Grid::updateSearchString(const QString &search)
{
for (QPair<QLabel *, QLabel *> tuple : m_labels) {
const bool matches = tuple.first->text().contains(search, Qt::CaseInsensitive) && search.length() != 0;
QPalette pal = tuple.first->palette();
pal.setBrush(QPalette::Foreground, matches ? Qt::red : palette().text());
tuple.first->setPalette(pal);
tuple.second->setPalette(pal);
const auto fgRole = matches ? QPalette::HighlightedText : QPalette::Text;
const auto bgRole = matches ? QPalette::Highlight : QPalette::Base;
tuple.first->setForegroundRole(fgRole);
tuple.first->setBackgroundRole(bgRole);
tuple.second->setForegroundRole(fgRole);
tuple.second->setBackgroundRole(bgRole);
QFont fnt = tuple.first->font();
fnt.setBold(matches);
tuple.first->setFont(fnt);
......
/* Copyright (C) 2012-2019 The KPhotoAlbum Development Team
/* Copyright (C) 2012-2020 The KPhotoAlbum Development Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
......@@ -59,7 +59,7 @@ private:
QString exifNameNoGroup(const QString &fullName);
void scroll(int dy);
QLabel *headerLabel(const QString &title);
QPair<QLabel *, QLabel *> infoLabelPair(const QString &title, const QString &value, const QColor &color);
QPair<QLabel *, QLabel *> infoLabelPair(const QString &title, const QString &value, const QPalette::ColorRole role);
private slots:
void updateWidgetSize();
......
......@@ -608,6 +608,13 @@ void HTMLDialog::slotSuggestOutputDir()
}
}
bool HTMLDialog::event(QEvent *event)
{
if (event->type() == QEvent::PaletteChange)
slotUpdateOutputLabel();
return KPageDialog::event(event);
}
int HTMLDialog::exec(const DB::FileNameList &list)
{
if (list.empty()) {
......
/* Copyright (C) 2003-2010 Jesper K. Pedersen <blackie@kde.org>
/* Copyright (C) 2003-2020 Jesper K. Pedersen <blackie@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
......@@ -55,6 +55,7 @@ protected slots:
void slotSuggestOutputDir();
protected:
bool event(QEvent *event) override;
bool checkVars();
Setup setup() const;
QList<ImageSizeCheckBox *> activeResolutions() const;
......
......@@ -47,19 +47,17 @@ ImportMatcher::ImportMatcher(const QString &otherCategory, const QString &myCate
gridLay->setColumnStretch(1, 1);
setWidget(top);
QPalette pal = palette();
pal.setBrush(QPalette::Window, palette().dark());
pal.setBrush(QPalette::WindowText, palette().brightText());
QLabel *label = new QLabel(i18n("Key in file"), grid);
label->setAutoFillBackground(true);
label->setPalette(pal);
label->setForegroundRole(QPalette::Dark);
label->setBackgroundRole(QPalette::BrightText);
label->setAlignment(Qt::AlignCenter);
gridLay->addWidget(label, 0, 0);
label = new QLabel(i18n("Key in your database"), grid);
label->setAutoFillBackground(true);
label->setPalette(pal);
label->setForegroundRole(QPalette::Dark);
label->setBackgroundRole(QPalette::BrightText);
label->setAlignment(Qt::AlignCenter);
gridLay->addWidget(label, 0, 1);
......
......@@ -44,11 +44,6 @@
MainWindow::StatusBar::StatusBar()
: QStatusBar()
{
QPalette pal = palette();
pal.setBrush(QPalette::Base, QApplication::palette().color(QPalette::Background));
pal.setBrush(QPalette::Background, QApplication::palette().color(QPalette::Background));
setPalette(pal);
setupGUI();
m_pendingShowTimer = new QTimer(this);
m_pendingShowTimer->setSingleShot(true);
......
......@@ -141,6 +141,8 @@ MainWindow::Window::Window(QWidget *parent)
, m_positionBrowser(nullptr)
#endif
{
// propagate palette changes to subwindows:
setAttribute(Qt::WA_WindowPropagation);
qCDebug(MainWindowLog) << "Using icon theme: " << QIcon::themeName();
qCDebug(MainWindowLog) << "Icon search paths: " << QIcon::themeSearchPaths();
QElapsedTimer timer;
......@@ -170,11 +172,7 @@ MainWindow::Window::Window(QWidget *parent)
m_dateBarLine->setFrameStyle(QFrame::HLine | QFrame::Plain);
m_dateBarLine->setLineWidth(0);
m_dateBarLine->setMidLineWidth(0);
QPalette pal = m_dateBarLine->palette();
pal.setColor(QPalette::WindowText, palette().window().color());
m_dateBarLine->setPalette(pal);
m_dateBarLine->setForegroundRole(QPalette::Window);
lay->addWidget(m_dateBarLine);
setHistogramVisibilty(Settings::SettingsData::instance()->showHistogram());
......@@ -706,9 +704,8 @@ bool MainWindow::Window::event(QEvent *event)
const QString schemePath = qApp->property("KDE_COLOR_SCHEME_PATH").toString();
qCInfo(MainWindowLog) << "Color Scheme changed to " << (schemePath.isEmpty() ? QString::fromLatin1("system default") : schemePath);
Settings::SettingsData::instance()->setColorScheme(schemePath);
return QWidget::event(event);
}
return QWidget::event(event);
return KXmlGuiWindow::event(event);
}
void MainWindow::Window::closeEvent(QCloseEvent *e)
......
......@@ -264,9 +264,11 @@ QDate Settings::BirthdayPage::parseDate(QString date)
void Settings::BirthdayPage::checkDateInput(QString date)
{
QDate parsedDate = parseDate(date);
if (parsedDate.isValid()) {
if (parsedDate.isValid() || date.isEmpty()) {
m_calendar->setSelectedDate(parsedDate);
m_dateInput->setPalette(palette());
// re-enable palette propagation:
m_dateInput->setAttribute(Qt::WA_SetPalette);
} else {
auto errorPalette = m_dateInput->palette();
KColorScheme::adjustForeground(errorPalette, KColorScheme::ForegroundRole::NegativeText, QPalette::Text);
......
......@@ -26,6 +26,7 @@
#include <ImageManager/ImageRequest.h>
#include <Settings/SettingsData.h>
#include <QEvent>
#include <QTemporaryFile>
namespace Utilities
......@@ -41,12 +42,19 @@ ToolTip::ToolTip(QWidget *parent, Qt::WindowFlags f)
setWindowOpacity(0.8);
setAutoFillBackground(true);
updatePalette();
}
void ToolTip::updatePalette()
{
QPalette p = palette();
QColor bgColor = palette().shadow().color();
bgColor.setAlpha(170);
p.setColor(QPalette::Background, bgColor);
p.setColor(QPalette::WindowText, palette().brightText().color());
setPalette(p);
// re-enable palette-propagation:
setAttribute(Qt::WA_SetPalette);
}
void ToolTip::requestImage(const DB::FileName &fileName)
......@@ -82,6 +90,13 @@ void ToolTip::requestToolTip(const DB::FileName &fileName)
requestImage(fileName);
}
bool ToolTip::event(QEvent *e)
{
if (e->type() == QEvent::PaletteChange)
updatePalette();
return QLabel::event(e);
}
void ToolTip::renderToolTip()
{
const int size = Settings::SettingsData::instance()->previewSize();
......
......@@ -41,8 +41,10 @@ public:
void requestToolTip(const DB::FileName &fileName);
protected:
bool event(QEvent *e) override;
explicit ToolTip(QWidget *parent, Qt::WindowFlags f);
virtual void placeWindow() = 0;
void updatePalette();
private:
void renderToolTip();
......
......@@ -58,16 +58,7 @@ Viewer::InfoBox::InfoBox(Viewer::ViewerWidget *viewer)
setMidLineWidth(0);
setAutoFillBackground(false);
QPalette p = palette();
// we want a transparent background, not the default widget grey
QColor bgColor = palette().shadow().color();
bgColor.setAlpha(170);
p.setColor(QPalette::Base, bgColor);
// adapt other colors as necessary
p.setColor(QPalette::WindowText, palette().brightText().color());
p.setColor(QPalette::Text, palette().brightText().color());
p.setColor(QPalette::Link, p.color(QPalette::Link).lighter());
setPalette(p);
updatePalette();
m_jumpToContext = new QToolButton(this);
m_jumpToContext->setIcon(QIcon::fromTheme(QString::fromUtf8("kphotoalbum")));
......@@ -162,6 +153,13 @@ void Viewer::InfoBox::setSize()
resize(realWidth, qMin((int)document()->size().height(), maxHeight));
}
bool Viewer::InfoBox::event(QEvent *e)
{
if (e->type() == QEvent::PaletteChange)
updatePalette();
return QTextBrowser::event(e);
}
void Viewer::InfoBox::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
......@@ -320,6 +318,22 @@ void Viewer::InfoBox::hackLinkColorForQt52()
}
}
void Viewer::InfoBox::updatePalette()
{
QPalette p = palette();
// we want a transparent background, not the default widget grey
QColor bgColor = palette().shadow().color();
bgColor.setAlpha(170);
p.setColor(QPalette::Base, bgColor);
// adapt other colors as necessary
p.setColor(QPalette::WindowText, palette().brightText().color());
p.setColor(QPalette::Text, palette().brightText().color());
p.setColor(QPalette::Link, p.color(QPalette::Link).lighter());
setPalette(p);
// re-enable palette propagation:
setAttribute(Qt::WA_SetPalette);
}
void Viewer::InfoBox::contextMenuEvent(QContextMenuEvent *event)
{
if (!m_menu) {
......
......@@ -63,6 +63,7 @@ public:
void setSize();
protected:
bool event(QEvent *e) override;
QVariant loadResource(int type, const QUrl &name) override;
void mouseMoveEvent(QMouseEvent *) override;
void mousePressEvent(QMouseEvent *) override;
......@@ -74,6 +75,7 @@ protected:
void showBrowser();
void possiblyStartResize(const QPoint &pos);
void hackLinkColorForQt52();
void updatePalette();
protected slots:
void jumpToContext();
......
......@@ -85,6 +85,8 @@ void Viewer::SpeedDisplay::setAlphaChannel(int backgroundAlpha, int labelAlpha)
fgColor.setAlpha(labelAlpha);
p.setColor(QPalette::WindowText, fgColor);
setPalette(p);
// re-enable palette propagation:
setAttribute(Qt::WA_SetPalette);
}
void Viewer::SpeedDisplay::setAlphaChannel(int alpha)
......
......@@ -46,9 +46,7 @@ Viewer::VideoDisplay::VideoDisplay(QWidget *parent)
, m_zoomType(FullZoom)
, m_zoomFactor(1)
{
QPalette pal = palette();
pal.setColor(QPalette::Window, palette().shadow().color());
setPalette(pal);
setBackgroundRole(QPalette::Shadow);
setAutoFillBackground(true);
m_mediaObject = nullptr;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment