Implement and move some colour-aware helper methods into the Theme class
authorMichael Schuster <michael@schuster.ms>
Mon, 9 Dec 2019 19:34:27 +0000 (20:34 +0100)
committerMichael Schuster <48932272+misch7@users.noreply.github.com>
Mon, 9 Dec 2019 20:37:21 +0000 (21:37 +0100)
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 <michael@schuster.ms>
src/gui/settingsdialog.cpp
src/gui/settingsdialog.h
src/libsync/theme.cpp
src/libsync/theme.h

index 7ae0626670725455d6157e49383773ae40aa95c0..f98acf3c38a2f5d7a4dbc5e43ce93a5c9037749b 100644 (file)
@@ -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<QToolButton *>(_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);
 }
 
index 97d15c03c4b49075dfcb1e449efe4ee25f1b3583..869e796a41b8406b2d657b31f66434a17c17514f 100644 (file)
@@ -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());
 
index 9d6a2a405bb7c8eb90cbdfbee7791a335a6573a9..4f9c7e1841ca731369e4014502cb0434d0dfcc06 100644 (file)
@@ -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("(<a href|<a style='color:#([a-zA-Z0-9]{6});' href)"), QString::fromLatin1("<a style='color:%1;' href").arg(getBackgroundAwareLinkColor(backgroundColor).name()));
+}
+
+void Theme::replaceLinkColorStringBackgroundAware(QString &linkString)
+{
+    replaceLinkColorStringBackgroundAware(linkString, QGuiApplication::palette().color(QPalette::Base));
+}
+
+QIcon Theme::createColorAwareIcon(const QString &name, const QPalette &palette)
+{
+    QImage img(name);
+    QImage inverted(img);
+    inverted.invertPixels(QImage::InvertRgb);
+
+    QIcon icon;
+    if (Theme::isDarkColor(palette.color(QPalette::Base))) {
+        icon.addPixmap(QPixmap::fromImage(inverted));
+    } else {
+        icon.addPixmap(QPixmap::fromImage(img));
+    }
+    if (Theme::isDarkColor(palette.color(QPalette::HighlightedText))) {
+        icon.addPixmap(QPixmap::fromImage(img), QIcon::Normal, QIcon::On);
+    } else {
+        icon.addPixmap(QPixmap::fromImage(inverted), QIcon::Normal, QIcon::On);
+    }
+    return icon;
+}
+
+QIcon Theme::createColorAwareIcon(const QString &name)
+{
+    return createColorAwareIcon(name, QGuiApplication::palette());
+}
+
+QPixmap Theme::createColorAwarePixmap(const QString &name, const QPalette &palette)
+{
+    QImage img(name);
+    QImage inverted(img);
+    inverted.invertPixels(QImage::InvertRgb);
+
+    QPixmap pixmap;
+    if (Theme::isDarkColor(palette.color(QPalette::Base))) {
+        pixmap = QPixmap::fromImage(inverted);
+    } else {
+        pixmap = QPixmap::fromImage(img);
+    }
+    return pixmap;
+}
+
+QPixmap Theme::createColorAwarePixmap(const QString &name)
+{
+    return createColorAwarePixmap(name, QGuiApplication::palette());
+}
+
 } // end namespace client
index bccb239d7d88ad17db81286b565e2c0224377b80..7df181ba0701e1541a7584189650caac9b69e326 100644 (file)
@@ -24,6 +24,7 @@ class QObject;
 class QPixmap;
 class QColor;
 class QPaintDevice;
+class QPalette;
 
 namespace OCC {
 
@@ -355,6 +356,87 @@ public:
     * (actually 2019/09/13 only systray theming).
     */
        virtual QIcon uiThemeIcon(const QString &iconName, bool uiHasDarkBg) const;
+    
+    /**
+     * @brief Perform a calculation to check if a colour is dark or light and accounts for different sensitivity of the human eye.
+     *
+     * @return True if the specified colour is dark.
+     *
+     * 2019/12/08: Moved here from SettingsDialog.
+     */
+    static bool isDarkColor(const QColor &color);
+    
+    /**
+     * @brief Return the colour to be used for HTML links (e.g. used in QLabel), based on the current app palette or given colour (Dark-/Light-Mode switching).
+     *
+     * @return Background-aware colour for HTML links, based on the current app palette or given colour.
+     *
+     * 2019/12/08: Implemented for the Dark Mode on macOS, because the app palette can not account for that (Qt 5.12.5).
+     */
+    static QColor getBackgroundAwareLinkColor(const QColor &backgroundColor);
+    
+    /**
+     * @brief Return the colour to be used for HTML links (e.g. used in QLabel), based on the current app palette (Dark-/Light-Mode switching).
+     *
+     * @return Background-aware colour for HTML links, based on the current app palette.
+     *
+     * 2019/12/08: Implemented for the Dark Mode on macOS, because the app palette can not account for that (Qt 5.12.5).
+     */
+    static QColor getBackgroundAwareLinkColor();
+
+    /**
+     * @brief Appends a CSS-style colour value to all HTML link tags in a given string, based on the current app palette or given colour (Dark-/Light-Mode switching).
+     *
+     * 2019/12/08: Implemented for the Dark Mode on macOS, because the app palette can not account for that (Qt 5.12.5).
+     *
+     * This way we also avoid having certain strings re-translated on Transifex.
+     */
+    static void replaceLinkColorStringBackgroundAware(QString &linkString, const QColor &backgroundColor);
+
+    /**
+     * @brief Appends a CSS-style colour value to all HTML link tags in a given string, based on the current app palette (Dark-/Light-Mode switching).
+     *
+     * 2019/12/08: Implemented for the Dark Mode on macOS, because the app palette can not account for that (Qt 5.12.5).
+     *
+     * This way we also avoid having certain strings re-translated on Transifex.
+     */
+    static void replaceLinkColorStringBackgroundAware(QString &linkString);
+
+    /**
+     * @brief Creates a colour-aware icon based on the specified palette's base colour.
+     *
+     * @return QIcon, colour-aware (inverted on dark backgrounds).
+     *
+     * 2019/12/09: Moved here from SettingsDialog.
+     */
+    static QIcon createColorAwareIcon(const QString &name, const QPalette &palette);
+
+    /**
+     * @brief Creates a colour-aware icon based on the app palette's base colour (Dark-/Light-Mode switching).
+     *
+     * @return QIcon, colour-aware (inverted on dark backgrounds).
+     *
+     * 2019/12/09: Moved here from SettingsDialog.
+     */
+    static QIcon createColorAwareIcon(const QString &name);
+
+    /**
+     * @brief Creates a colour-aware pixmap based on the specified palette's base colour.
+     *
+     * @return QPixmap, colour-aware (inverted on dark backgrounds).
+     *
+     * 2019/12/09: Adapted from createColorAwareIcon.
+     */
+    static QPixmap createColorAwarePixmap(const QString &name, const QPalette &palette);
+
+    /**
+     * @brief Creates a colour-aware pixmap based on the app palette's base colour (Dark-/Light-Mode switching).
+     *
+     * @return QPixmap, colour-aware (inverted on dark backgrounds).
+     *
+     * 2019/12/09: Adapted from createColorAwareIcon.
+     */
+    static QPixmap createColorAwarePixmap(const QString &name);
 
 protected:
 #ifndef TOKEN_AUTH_ONLY