From: Michael Schuster Date: Mon, 9 Dec 2019 19:34:27 +0000 (+0100) Subject: Implement and move some colour-aware helper methods into the Theme class X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~222^2^2~496 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=be4fc6b8874a6030d1fa456f63fd387504abecc7;p=nextcloud-desktop.git Implement and move some colour-aware helper methods into the Theme class This introduces a new method to change the colours in the links in QLabel's. Utilizes a custom crafted RegEx function to replace already-coloured links. Moved code is based on stuff from the SettingsDialog class. Signed-off-by: Michael Schuster --- diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index 7ae062667..f98acf3c3 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -346,7 +346,7 @@ void SettingsDialog::customizeStyle() _toolBar->setStyleSheet(QString::fromLatin1(TOOLBAR_CSS).arg(background, dark, highlightColor, highlightTextColor)); Q_FOREACH (QAction *a, _actionGroup->actions()) { - QIcon icon = createColorAwareIcon(a->property("iconPath").toString()); + QIcon icon = Theme::createColorAwareIcon(a->property("iconPath").toString(), palette()); a->setIcon(icon); QToolButton *btn = qobject_cast(_toolBar->widgetForAction(a)); if (btn) @@ -354,34 +354,6 @@ void SettingsDialog::customizeStyle() } } -static bool isDarkColor(const QColor &color) -{ - // account for different sensitivity of the human eye to certain colors - double treshold = 1.0 - (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255.0; - return treshold > 0.5; -} - -QIcon SettingsDialog::createColorAwareIcon(const QString &name) -{ - QPalette pal = palette(); - QImage img(name); - QImage inverted(img); - inverted.invertPixels(QImage::InvertRgb); - - QIcon icon; - if (isDarkColor(pal.color(QPalette::Base))) { - icon.addPixmap(QPixmap::fromImage(inverted)); - } else { - icon.addPixmap(QPixmap::fromImage(img)); - } - if (isDarkColor(pal.color(QPalette::HighlightedText))) { - icon.addPixmap(QPixmap::fromImage(img), QIcon::Normal, QIcon::On); - } else { - icon.addPixmap(QPixmap::fromImage(inverted), QIcon::Normal, QIcon::On); - } - return icon; -} - class ToolButtonAction : public QWidgetAction { public: @@ -422,7 +394,7 @@ QAction *SettingsDialog::createActionWithIcon(const QIcon &icon, const QString & QAction *SettingsDialog::createColorAwareAction(const QString &iconPath, const QString &text) { // all buttons must have the same size in order to keep a good layout - QIcon coloredIcon = createColorAwareIcon(iconPath); + QIcon coloredIcon = Theme::createColorAwareIcon(iconPath, palette()); return createActionWithIcon(coloredIcon, text, iconPath); } diff --git a/src/gui/settingsdialog.h b/src/gui/settingsdialog.h index 97d15c03c..869e796a4 100644 --- a/src/gui/settingsdialog.h +++ b/src/gui/settingsdialog.h @@ -76,7 +76,6 @@ private: void customizeStyle(); void activityAdded(AccountState *); - QIcon createColorAwareIcon(const QString &name); QAction *createColorAwareAction(const QString &iconName, const QString &fileName); QAction *createActionWithIcon(const QIcon &icon, const QString &text, const QString &iconPath = QString()); diff --git a/src/libsync/theme.cpp b/src/libsync/theme.cpp index 9d6a2a405..4f9c7e184 100644 --- a/src/libsync/theme.cpp +++ b/src/libsync/theme.cpp @@ -565,4 +565,76 @@ QString Theme::versionSwitchOutput() const return helpText; } +bool Theme::isDarkColor(const QColor &color) +{ + // account for different sensitivity of the human eye to certain colors + double treshold = 1.0 - (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255.0; + return treshold > 0.5; +} + +QColor Theme::getBackgroundAwareLinkColor(const QColor &backgroundColor) +{ + return QColor((isDarkColor(backgroundColor) ? QColor("#aabdff") : QGuiApplication::palette().color(QPalette::Link))); +} + +QColor Theme::getBackgroundAwareLinkColor() +{ + return getBackgroundAwareLinkColor(QGuiApplication::palette().base().color()); +} + +void Theme::replaceLinkColorStringBackgroundAware(QString &linkString, const QColor &backgroundColor) +{ + linkString.replace(QRegularExpression("(