From 12d401fe2949ca2c823ea38181361650eaaeae66 Mon Sep 17 00:00:00 2001 From: Shane Synan Date: Fri, 2 Dec 2016 16:36:36 -0600 Subject: [PATCH] Switch sender colors to use color palette Add "sender-color-[hash]" to the list of valid palette color roles. Switch QtUiStyle::generateSettingsQss() to make use of the color palette, rather than redundantly specifying the color. This cleans up the output of settings.qss, and (more importantly) allows custom themes to do the same. There's trade-offs involved with using the color palette directly (similar to MarkerLine). It would make the GUI capable of toggling sender coloring in themes, which would be nice. However, themes would no longer be able to set sender colors independently for different message types. Though untested, there might be performance trade-offs with querying the settings provider on each render, too. This can be revisited in the future. --- src/qtui/qtuistyle.cpp | 52 ++++++++++++++++++++++++++++--------- src/qtui/qtuistyle.h | 17 +++++++++--- src/uisupport/qssparser.cpp | 18 +++++++++++++ src/uisupport/uistyle.h | 27 ++++++++++++++++++- 4 files changed, 98 insertions(+), 16 deletions(-) diff --git a/src/qtui/qtuistyle.cpp b/src/qtui/qtuistyle.cpp index 1e8cc354..a81f9295 100644 --- a/src/qtui/qtuistyle.cpp +++ b/src/qtui/qtuistyle.cpp @@ -127,23 +127,30 @@ void QtUiStyle::generateSettingsQss() const } if (s.value("UseSenderColors", true).toBool()) { - out << "\n// Sender Colors\n" - << "ChatLine::sender#plain[sender=\"self\"] { foreground: " << color("SenderSelf", s, defaultSenderColorSelf) << "; }\n\n"; + out << "\n// Sender Colors\n"; + // Generate a color palette for easy reuse elsewhere + // NOTE: A color palette is not a complete replacement for specifying the colors below, as + // specifying the colors one-by-one instead of with QtUi::style()->brush(...) makes it easy + // to toggle the specific coloring of sender/nick at the cost of regenerating this file. + // See UiStyle::ColorRole + out << senderPaletteQss(s); + + out << "ChatLine::sender#plain[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n"; // Matches qssparser.cpp for UiStyle::PlainMsg for (int i = 0; i < defaultSenderColors.count(); i++) - out << senderQss(i, s, "plain"); + out << senderQss(i, "plain"); // Only color the nicks in CTCP ACTIONs if sender colors are enabled if (s.value("UseSenderActionColors", true).toBool()) { // For action messages, color the 'sender' column -and- the nick itself out << "\n// Sender Nickname Colors for action messages\n" - << "ChatLine::sender#action[sender=\"self\"] { foreground: " << color("SenderSelf", s, defaultSenderColorSelf) << "; }\n" - << "ChatLine::nick#action[sender=\"self\"] { foreground: " << color("SenderSelf", s, defaultSenderColorSelf) << "; }\n\n"; + << "ChatLine::sender#action[sender=\"self\"] { foreground: palette(sender-color-self); }\n" + << "ChatLine::nick#action[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n"; // Matches qssparser.cpp for UiStyle::ActionMsg for (int i = 0; i < defaultSenderColors.count(); i++) - out << senderQss(i, s, "action", true); + out << senderQss(i, "action", true); } } @@ -206,19 +213,40 @@ QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, UiSett } -QString QtUiStyle::senderQss(int i, UiSettings &settings, const QString &messageType, bool includeNick) const +QString QtUiStyle::senderPaletteQss(UiSettings &settings) const +{ + QString result; + result += "Palette {\n"; + + // Generate entries for sender-color-self + result += QString(" sender-color-self: %1;\n") + .arg(color("SenderSelf", settings, defaultSenderColorSelf)); + + // Generate entries for sender-color-HASH + for (int i = 0; i < defaultSenderColors.count(); i++) { + QString dez = QString::number(i); + if (dez.length() == 1) dez.prepend('0'); + result += QString(" sender-color-0%1: %2;\n") + .arg(QString::number(i, 16), color("Sender"+dez, settings, defaultSenderColors[i])); + } + result += "}\n\n"; + return result; +} + + +QString QtUiStyle::senderQss(int i, const QString &messageType, bool includeNick) const { QString dez = QString::number(i); if (dez.length() == 1) dez.prepend('0'); if (includeNick) { // Include the nickname in the color rules - return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: %3; }\n" - "ChatLine::nick#%1[sender=\"0%2\"] { foreground: %3; }\n") - .arg(messageType, QString::number(i, 16), color("Sender"+dez, settings, defaultSenderColors[i])); + return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n" + "ChatLine::nick#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n") + .arg(messageType, QString::number(i, 16)); } else { - return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: %3; }\n") - .arg(messageType, QString::number(i, 16), color("Sender"+dez, settings, defaultSenderColors[i])); + return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n") + .arg(messageType, QString::number(i, 16)); } } diff --git a/src/qtui/qtuistyle.h b/src/qtui/qtuistyle.h index 2aaceb66..9d1d5fa4 100644 --- a/src/qtui/qtuistyle.h +++ b/src/qtui/qtuistyle.h @@ -79,17 +79,28 @@ private: QString msgTypeQss(const QString &msgType, const QString &key, UiSettings &settings) const; + /** + * Generates the Qss color palette stylesheet for sender colors + * + * This must be called before generating any Qss dependent on these colors. + * + * @see QtUiStyle::senderQss() + * @param[in] settings Reference to current UI settings, used for loading color values + * @return Quassel-theme-compatible Qss color palette + */ + QString senderPaletteQss(UiSettings &settings) const; + /** * Generate a snippet of Qss stylesheet for a given sender-hash index * + * This depends on the color palette generated by QtUiStyle::senderPaletteQss() + * * @param[in] i Sender hash index from 0 - 15 - * @param[in] settings Reference to current UI settings, used for loading color values * @param[in] messageType Type of message to filter, e.g. 'plain' or 'action' * @param[in] includeNick Also apply foreground color to Nick * @return Snippet of Quassel-theme-compatible Qss stylesheet */ - QString senderQss(int i, UiSettings &settings, const QString &messageType, - bool includeNick = false) const; + QString senderQss(int i, const QString &messageType, bool includeNick = false) const; QString chatListItemQss(const QString &state, const QString &key, UiSettings &settings) const; }; diff --git a/src/uisupport/qssparser.cpp b/src/uisupport/qssparser.cpp index 30a8fdd9..50c8c321 100644 --- a/src/uisupport/qssparser.cpp +++ b/src/uisupport/qssparser.cpp @@ -52,6 +52,24 @@ QssParser::QssParser() _uiStylePalette = QVector(UiStyle::NumRoles, QBrush()); _uiStyleColorRoles["marker-line"] = UiStyle::MarkerLine; + // Sender colors + _uiStyleColorRoles["sender-color-self"] = UiStyle::SenderColorSelf; + _uiStyleColorRoles["sender-color-00"] = UiStyle::SenderColor00; + _uiStyleColorRoles["sender-color-01"] = UiStyle::SenderColor01; + _uiStyleColorRoles["sender-color-02"] = UiStyle::SenderColor02; + _uiStyleColorRoles["sender-color-03"] = UiStyle::SenderColor03; + _uiStyleColorRoles["sender-color-04"] = UiStyle::SenderColor04; + _uiStyleColorRoles["sender-color-05"] = UiStyle::SenderColor05; + _uiStyleColorRoles["sender-color-06"] = UiStyle::SenderColor06; + _uiStyleColorRoles["sender-color-07"] = UiStyle::SenderColor07; + _uiStyleColorRoles["sender-color-08"] = UiStyle::SenderColor08; + _uiStyleColorRoles["sender-color-09"] = UiStyle::SenderColor09; + _uiStyleColorRoles["sender-color-0a"] = UiStyle::SenderColor0a; + _uiStyleColorRoles["sender-color-0b"] = UiStyle::SenderColor0b; + _uiStyleColorRoles["sender-color-0c"] = UiStyle::SenderColor0c; + _uiStyleColorRoles["sender-color-0d"] = UiStyle::SenderColor0d; + _uiStyleColorRoles["sender-color-0e"] = UiStyle::SenderColor0e; + _uiStyleColorRoles["sender-color-0f"] = UiStyle::SenderColor0f; } diff --git a/src/uisupport/uistyle.h b/src/uisupport/uistyle.h index 0b4589a1..ee78c9b5 100644 --- a/src/uisupport/uistyle.h +++ b/src/uisupport/uistyle.h @@ -124,6 +124,30 @@ public: enum ColorRole { MarkerLine, + // Sender colors (16 + self) + // These aren't used directly to avoid having storing all of the sender color options in the + // rendering routine of each item. Also, I couldn't figure out how to do that. + // It would be nice to have the UseSenderColors preference also toggle sender colors set by + // themes, so hopefully this can be extended in the future. + // Furthermore, using this palette directly would mean separate sets of colors couldn't be + // used for different message types. + SenderColorSelf, + SenderColor00, + SenderColor01, + SenderColor02, + SenderColor03, + SenderColor04, + SenderColor05, + SenderColor06, + SenderColor07, + SenderColor08, + SenderColor09, + SenderColor0a, + SenderColor0b, + SenderColor0c, + SenderColor0d, + SenderColor0e, + SenderColor0f, NumRoles // must be last! }; @@ -138,9 +162,10 @@ public: * List of default sender colors * * In order from 1 - 16, matching the Sender## format in the settings file. - * Don't change the length or values of the colors without updating the UI, too. + * Don't change the length or values of the colors without updating the UI and color roles, too. * * @see ../qtui/settingspages/chatviewsettingspage.ui + * @see UiStyle::ColorRole */ const QList defaultSenderColors = QList { QColor(233, 13, 127), /// Sender00 -- 2.20.1