From 1882198e4b1df455e3e125d4729790a9fd881809 Mon Sep 17 00:00:00 2001 From: redsky17 Date: Fri, 18 Jan 2019 04:09:42 +0000 Subject: Make the author text slightly large. Add author color generated based on user id. --- src/Utils.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/Utils.cpp') diff --git a/src/Utils.cpp b/src/Utils.cpp index 8176cb43..6229d42a 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -382,6 +382,23 @@ utils::linkColor() return QPalette().color(QPalette::Link).name(); } +QString +utils::generateHexColor(const QString &input) +{ + auto hash = 0; + + for (int i = 0; i < input.length(); i++) { + hash = input.at(i).digitValue() + ((hash << 5) - hash); + } + hash *= 13; + QString colour("#"); + for (int i = 0; i < 3; i++) { + int value = (hash >> (i * 8)) & 0xFF; + colour.append(("00" + QString::number(value, 16)).right(2)); + } + return colour; +} + void utils::centerWidget(QWidget *widget, QWidget *parent) { -- cgit 1.5.1 From 50e382f554aee2bd12d8cd457b7e17b917162e6d Mon Sep 17 00:00:00 2001 From: redsky17 Date: Fri, 18 Jan 2019 17:17:25 +0000 Subject: Modified the code that generates user's colors so that it will work regardless of the theme choices the user makes. The code now incorporates the contrast between the background color and the color generated by the user_name when picking colors. It currently has two 'big' issues: 1. Colors are not cached. I am planning on adding a QHash for this a little later. This should improve performance by not calculating the color for the same users over and over and over again. 2. Theme changes do not trigger the colors to get refreshed. Currently, you will have to switch to a different room and back to get the colors to refresh. --- resources/styles/nheko-dark.qss | 4 ++ resources/styles/nheko.qss | 4 ++ resources/styles/system.qss | 4 ++ src/Utils.cpp | 110 ++++++++++++++++++++++++++++++++++++++-- src/Utils.h | 23 ++++++++- src/timeline/TimelineItem.cpp | 8 ++- src/timeline/TimelineItem.h | 7 +++ 7 files changed, 151 insertions(+), 9 deletions(-) (limited to 'src/Utils.cpp') diff --git a/resources/styles/nheko-dark.qss b/resources/styles/nheko-dark.qss index 86056bb2..e81fa0b8 100644 --- a/resources/styles/nheko-dark.qss +++ b/resources/styles/nheko-dark.qss @@ -3,6 +3,10 @@ QLabel { color: #caccd1; } +TimelineItem { + qproperty-backgroundColor: #202228; +} + #chatPage, #chatPage > * { background-color: #202228; diff --git a/resources/styles/nheko.qss b/resources/styles/nheko.qss index ca5a8f0d..468ae0f1 100644 --- a/resources/styles/nheko.qss +++ b/resources/styles/nheko.qss @@ -3,6 +3,10 @@ QLabel { color: #333; } +TimelineItem { + qproperty-backgroundColor: white; +} + #chatPage, #chatPage > * { background-color: white; diff --git a/resources/styles/system.qss b/resources/styles/system.qss index 45263e96..6663ee6b 100644 --- a/resources/styles/system.qss +++ b/resources/styles/system.qss @@ -3,6 +3,10 @@ TypingDisplay { qproperty-backgroundColor: palette(window); } +TimelineItem { + qproperty-backgroundColor: palette(window); +} + TimelineView, TimelineView > * { border: none; diff --git a/src/Utils.cpp b/src/Utils.cpp index 6229d42a..6a5c3491 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -383,20 +383,120 @@ utils::linkColor() } QString -utils::generateHexColor(const QString &input) +utils::generateHexColor(const int hash) +{ + QString colour("#"); + for (int i = 0; i < 3; i++) { + int value = (hash >> (i * 8)) & 0xFF; + colour.append(("00" + QString::number(value, 16)).right(2)); + } + // nhlog::ui()->debug("Hex Generated {} -> {}", QString::number(hash).toStdString(), + // colour.toStdString()); + return colour.toUpper(); +} + +int +utils::hashQString(const QString &input) { auto hash = 0; for (int i = 0; i < input.length(); i++) { hash = input.at(i).digitValue() + ((hash << 5) - hash); } + hash *= 13; - QString colour("#"); + + return hash; +} + +QString +utils::generateContrastingHexColor(const QString &input, const QString &background) +{ + nhlog::ui()->debug("Background hex {}", background.toStdString()); + const QColor backgroundCol(background); + const qreal backgroundLum = luminance(background); + + // Create a color for the input + auto hash = hashQString(input); + auto colorHex = generateHexColor(hash); + + // converting to a QColor makes the luminance calc easier. + QColor inputColor = QColor(colorHex); + + // attempt to score both the luminance and the contrast. + // contrast should have a higher precedence, but luminance + // helps dictate how exciting the colors are. + auto colorLum = luminance(inputColor); + auto contrast = computeContrast(colorLum, backgroundLum); + + // If the contrast or luminance don't meet our criteria, + // try again and again until they do. After 10 tries, + // the best-scoring color will be chosen. + int att = 0; + while ((contrast < 5 || (colorLum < 0.05 || colorLum > 0.95)) && ++att < 10) { + hash = hashQString(input) + ((hash << 2) * 13); + auto newHex = generateHexColor(hash); + inputColor.setNamedColor(newHex); + auto tmpLum = luminance(inputColor); + auto tmpContrast = computeContrast(tmpLum, backgroundLum); + + // Prioritize contrast over luminance + // If both values are better, it's a no brainer. + if (tmpContrast > contrast && (tmpLum > 0.05 && tmpLum < 0.95)) { + contrast = tmpContrast; + colorHex = newHex; + colorLum = tmpLum; + } + // Otherwise, if we still can get a more + // vibrant color and have met our contrast + // threshold, pick the more vibrant color, + // even if contrast will drop somewhat. + // choosing 50% luminance as ideal. + else if ((qAbs(tmpLum - 0.50) < qAbs(colorLum - 0.50)) && tmpContrast >= 5) { + contrast = tmpContrast; + colorHex = newHex; + colorLum = tmpLum; + } + // Otherwise, just take the better contrast. + else if (tmpContrast > contrast) { + contrast = tmpContrast; + colorHex = newHex; + colorLum = tmpLum; + } + } + + nhlog::ui()->debug("Hex Generated for {}: [hex: {}, contrast: {}, luminance: {}]", + input.toStdString(), + colorHex.toStdString(), + QString::number(contrast).toStdString(), + QString::number(colorLum).toStdString()); + return colorHex; +} + +qreal +utils::computeContrast(const qreal &one, const qreal &two) +{ + auto ratio = (one + 0.05) / (two + 0.05); + + if (two > one) { + ratio = 1 / ratio; + } + + return ratio; +} + +qreal +utils::luminance(const QColor &col) +{ + int colRgb[3] = {col.red(), col.green(), col.blue()}; + qreal lumRgb[3]; + for (int i = 0; i < 3; i++) { - int value = (hash >> (i * 8)) & 0xFF; - colour.append(("00" + QString::number(value, 16)).right(2)); + qreal v = colRgb[i] / 255.0; + v <= 0.03928 ? lumRgb[i] = v / 12.92 : lumRgb[i] = qPow((v + 0.055) / 1.055, 2.4); } - return colour; + + return lumRgb[0] * 0.2126 + lumRgb[1] * 0.7152 + lumRgb[2] * 0.0722; } void diff --git a/src/Utils.h b/src/Utils.h index 3ce2d758..8b3392da 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -14,6 +14,8 @@ #include #include +#include + class QComboBox; namespace utils { @@ -227,9 +229,26 @@ markdownToHtml(const QString &text); QString linkColor(); -//! Given an input string, create a color string +//! Given an input integer, create a color string in #RRGGBB format QString -generateHexColor(const QString &string); +generateHexColor(const int hash); + +//! Returns the hash code of the input QString +int +hashQString(const QString &input); + +//! Generate a color (matching #RRGGBB) that has an acceptable contrast to background that is based +//! on the input string. +QString +generateContrastingHexColor(const QString &input, const QString &background); + +//! Given two luminance values, compute the contrast ratio between them. +qreal +computeContrast(const qreal &one, const qreal &two); + +//! Compute the luminance of a single color. Based on https://stackoverflow.com/a/9733420 +qreal +luminance(const QColor &col); //! Center a widget in relation to another widget. void diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp index 3df78ff6..bd3d73bc 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp @@ -622,8 +622,6 @@ TimelineItem::generateUserName(const QString &user_id, const QString &displaynam sender = displayname.split(":")[0].split("@")[1]; } - auto userColor = utils::generateHexColor(user_id); - QFont usernameFont; usernameFont.setPointSizeF(usernameFont.pointSizeF() * 1.1); usernameFont.setWeight(QFont::Medium); @@ -639,6 +637,12 @@ TimelineItem::generateUserName(const QString &user_id, const QString &displaynam userName_->setAlignment(Qt::AlignLeft | Qt::AlignTop); userName_->setFixedWidth(QFontMetrics(userName_->font()).width(userName_->text())); + // TimelineItem isn't displayed. This forces the QSS to get + // loaded. + qApp->style()->polish(this); + // generate user's unique color. + auto backCol = backgroundColor().name(); + auto userColor = utils::generateContrastingHexColor(user_id, backCol); userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); auto filter = new UserProfileFilter(user_id, userName_); diff --git a/src/timeline/TimelineItem.h b/src/timeline/TimelineItem.h index 8159e370..c7f320d5 100644 --- a/src/timeline/TimelineItem.h +++ b/src/timeline/TimelineItem.h @@ -132,6 +132,8 @@ private: class TimelineItem : public QWidget { Q_OBJECT + Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor) + public: TimelineItem(const mtx::events::RoomEvent &e, bool with_sender, @@ -202,6 +204,9 @@ public: const QString &room_id, QWidget *parent); + void setBackgroundColor(const QColor &color) { backgroundColor_ = color; } + QColor backgroundColor() const { return backgroundColor_; } + void setUserAvatar(const QImage &pixmap); DescInfo descriptionMessage() const { return descriptionMsg_; } QString eventId() const { return event_id_; } @@ -282,6 +287,8 @@ private: QLabel *timestamp_; QLabel *userName_; TextLabel *body_; + + QColor backgroundColor_; }; template -- cgit 1.5.1 From b3f7c13e2f885bc804665cfc36011f22539bae53 Mon Sep 17 00:00:00 2001 From: redsky17 Date: Sun, 20 Jan 2019 00:12:57 +0000 Subject: Update user id color generation Update the author color generation. Now, instead of generating an entire hex string based on the user id, the user id instead is used to generate a hue value. After this hue value is created, there is some logic to tweak first the lightness and then saturation values to achieve a readable color (in contrast to the background). This change makes it so that user colors will not vary as wildly between the different themes. The values still are not cached and still do not update initially when the theme is changed. Both of these things will be resolved. --- src/Utils.cpp | 132 ++++++++++++++++++++++++++++++++-------------------------- src/Utils.h | 4 -- 2 files changed, 72 insertions(+), 64 deletions(-) (limited to 'src/Utils.cpp') diff --git a/src/Utils.cpp b/src/Utils.cpp index 6a5c3491..b7980fc3 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -382,19 +382,6 @@ utils::linkColor() return QPalette().color(QPalette::Link).name(); } -QString -utils::generateHexColor(const int hash) -{ - QString colour("#"); - for (int i = 0; i < 3; i++) { - int value = (hash >> (i * 8)) & 0xFF; - colour.append(("00" + QString::number(value, 16)).right(2)); - } - // nhlog::ui()->debug("Hex Generated {} -> {}", QString::number(hash).toStdString(), - // colour.toStdString()); - return colour.toUpper(); -} - int utils::hashQString(const QString &input) { @@ -404,8 +391,6 @@ utils::hashQString(const QString &input) hash = input.at(i).digitValue() + ((hash << 5) - hash); } - hash *= 13; - return hash; } @@ -417,59 +402,84 @@ utils::generateContrastingHexColor(const QString &input, const QString &backgrou const qreal backgroundLum = luminance(background); // Create a color for the input - auto hash = hashQString(input); - auto colorHex = generateHexColor(hash); + auto hash = hashQString(input); + // create a hue value based on the hash of the input. + auto userHue = qAbs(hash % 360); + nhlog::ui()->debug( + "User Hue {} : {}", input.toStdString(), QString::number(userHue).toStdString()); + // start with moderate saturation and lightness values. + auto sat = 220; + auto lightness = 125; // converting to a QColor makes the luminance calc easier. - QColor inputColor = QColor(colorHex); - - // attempt to score both the luminance and the contrast. - // contrast should have a higher precedence, but luminance - // helps dictate how exciting the colors are. - auto colorLum = luminance(inputColor); - auto contrast = computeContrast(colorLum, backgroundLum); - - // If the contrast or luminance don't meet our criteria, - // try again and again until they do. After 10 tries, - // the best-scoring color will be chosen. - int att = 0; - while ((contrast < 5 || (colorLum < 0.05 || colorLum > 0.95)) && ++att < 10) { - hash = hashQString(input) + ((hash << 2) * 13); - auto newHex = generateHexColor(hash); - inputColor.setNamedColor(newHex); - auto tmpLum = luminance(inputColor); - auto tmpContrast = computeContrast(tmpLum, backgroundLum); - - // Prioritize contrast over luminance - // If both values are better, it's a no brainer. - if (tmpContrast > contrast && (tmpLum > 0.05 && tmpLum < 0.95)) { - contrast = tmpContrast; - colorHex = newHex; - colorLum = tmpLum; - } - // Otherwise, if we still can get a more - // vibrant color and have met our contrast - // threshold, pick the more vibrant color, - // even if contrast will drop somewhat. - // choosing 50% luminance as ideal. - else if ((qAbs(tmpLum - 0.50) < qAbs(colorLum - 0.50)) && tmpContrast >= 5) { - contrast = tmpContrast; - colorHex = newHex; - colorLum = tmpLum; - } - // Otherwise, just take the better contrast. - else if (tmpContrast > contrast) { - contrast = tmpContrast; - colorHex = newHex; - colorLum = tmpLum; + QColor inputColor = QColor::fromHsl(userHue, sat, lightness); + + // calculate the initial luminance and contrast of the + // generated color. It's possible that no additional + // work will be necessary. + auto lum = luminance(inputColor); + auto contrast = computeContrast(lum, backgroundLum); + + // If the contrast doesn't meet our criteria, + // try again and again until they do by modifying first + // the lightness and then the saturation of the color. + while (contrast < 5) { + // if our lightness is at it's bounds, try changing + // saturation instead. + if (lightness == 242 || lightness == 13) { + qreal newSat = qBound(26.0, sat * 1.25, 242.0); + nhlog::ui()->info("newSat {}", QString::number(newSat).toStdString()); + + inputColor.setHsl(userHue, qFloor(newSat), lightness); + auto tmpLum = luminance(inputColor); + auto higherContrast = computeContrast(tmpLum, backgroundLum); + if (higherContrast > contrast) { + contrast = higherContrast; + sat = newSat; + } else { + newSat = qBound(26.0, sat / 1.25, 242.0); + inputColor.setHsl(userHue, qFloor(newSat), lightness); + tmpLum = luminance(inputColor); + auto lowerContrast = computeContrast(tmpLum, backgroundLum); + if (lowerContrast > contrast) { + contrast = lowerContrast; + sat = newSat; + } + } + } else { + qreal newLightness = qBound(13.0, lightness * 1.25, 242.0); + + inputColor.setHsl(userHue, sat, qFloor(newLightness)); + + auto tmpLum = luminance(inputColor); + auto higherContrast = computeContrast(tmpLum, backgroundLum); + + // Check to make sure we have actually improved contrast + if (higherContrast > contrast) { + contrast = higherContrast; + lightness = newLightness; + // otherwise, try going the other way instead. + } else { + newLightness = qBound(13.0, lightness / 1.25, 242.0); + inputColor.setHsl(userHue, sat, qFloor(newLightness)); + tmpLum = luminance(inputColor); + auto lowerContrast = computeContrast(tmpLum, backgroundLum); + if (lowerContrast > contrast) { + contrast = lowerContrast; + lightness = newLightness; + } + } } } + // get the hex value of the generated color. + auto colorHex = inputColor.name(); + nhlog::ui()->debug("Hex Generated for {}: [hex: {}, contrast: {}, luminance: {}]", input.toStdString(), colorHex.toStdString(), QString::number(contrast).toStdString(), - QString::number(colorLum).toStdString()); + QString::number(lum).toStdString()); return colorHex; } @@ -496,7 +506,9 @@ utils::luminance(const QColor &col) v <= 0.03928 ? lumRgb[i] = v / 12.92 : lumRgb[i] = qPow((v + 0.055) / 1.055, 2.4); } - return lumRgb[0] * 0.2126 + lumRgb[1] * 0.7152 + lumRgb[2] * 0.0722; + auto lum = lumRgb[0] * 0.2126 + lumRgb[1] * 0.7152 + lumRgb[2] * 0.0722; + + return lum; } void diff --git a/src/Utils.h b/src/Utils.h index 8b3392da..8672e7d4 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -229,10 +229,6 @@ markdownToHtml(const QString &text); QString linkColor(); -//! Given an input integer, create a color string in #RRGGBB format -QString -generateHexColor(const int hash); - //! Returns the hash code of the input QString int hashQString(const QString &input); -- cgit 1.5.1 From 237c7ad114b7c3d6960fdaf712b600e715eff082 Mon Sep 17 00:00:00 2001 From: redsky17 Date: Sun, 20 Jan 2019 04:43:48 +0000 Subject: Author Color Fixes Author color is now cached so that it will not be re-calculated each time a new message is posted. This cache gets cleared when the theme is changed. Additionally, the author color is now automatically refreshed when the theme is changed, fixing the issue where you had to change rooms before the colors would switch. --- src/ChatPage.h | 1 + src/MainWindow.cpp | 6 +++++- src/UserSettingsPage.cpp | 5 ++++- src/UserSettingsPage.h | 1 + src/Utils.cpp | 24 ++++++++++++++++++++++++ src/Utils.h | 12 ++++++++++++ src/timeline/TimelineItem.cpp | 30 +++++++++++++++++++++++++----- src/timeline/TimelineItem.h | 3 +++ 8 files changed, 75 insertions(+), 7 deletions(-) (limited to 'src/Utils.cpp') diff --git a/src/ChatPage.h b/src/ChatPage.h index 2c728c17..492613ec 100644 --- a/src/ChatPage.h +++ b/src/ChatPage.h @@ -148,6 +148,7 @@ signals: const QImage &icon); void updateGroupsInfo(const mtx::responses::JoinedGroups &groups); + void themeChanged(); private slots: void showUnreadMessageNotification(int count); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 55dbba34..450eb71a 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -112,7 +112,11 @@ MainWindow::MainWindow(QWidget *parent) connect( userSettingsPage_, SIGNAL(trayOptionChanged(bool)), trayIcon_, SLOT(setVisible(bool))); - + connect(userSettingsPage_, &UserSettingsPage::themeChanged, this, [this]() { + utils::clearAuthorColors(); + }); + connect( + userSettingsPage_, &UserSettingsPage::themeChanged, chat_page_, &ChatPage::themeChanged); connect(trayIcon_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index fdb67080..e3c0d190 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -379,7 +379,10 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge connect(themeCombo_, static_cast(&QComboBox::activated), - [this](const QString &text) { settings_->setTheme(text.toLower()); }); + [this](const QString &text) { + settings_->setTheme(text.toLower()); + emit themeChanged(); + }); connect(scaleFactorCombo_, static_cast(&QComboBox::activated), [](const QString &factor) { utils::setScaleFactor(factor.toFloat()); }); diff --git a/src/UserSettingsPage.h b/src/UserSettingsPage.h index 94fb816f..900f57e4 100644 --- a/src/UserSettingsPage.h +++ b/src/UserSettingsPage.h @@ -132,6 +132,7 @@ protected: signals: void moveBack(); void trayOptionChanged(bool value); + void themeChanged(); private slots: void importSessionKeys(); diff --git a/src/Utils.cpp b/src/Utils.cpp index b7980fc3..66bc86f6 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -15,6 +15,8 @@ using TimelineEvent = mtx::events::collections::TimelineEvents; +QHash authorColors_; + QString utils::localUser() { @@ -511,6 +513,28 @@ utils::luminance(const QColor &col) return lum; } +void +utils::clearAuthorColors() +{ + authorColors_.clear(); +} + +QString +utils::getAuthorColor(const QString &author) +{ + if (authorColors_.contains(author)) { + return authorColors_[author]; + } else { + return ""; + } +} + +void +utils::addAuthorColor(const QString &author, const QString &color) +{ + authorColors_[author] = color; +} + void utils::centerWidget(QWidget *widget, QWidget *parent) { diff --git a/src/Utils.h b/src/Utils.h index 8672e7d4..b6f89f73 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -246,6 +246,18 @@ computeContrast(const qreal &one, const qreal &two); qreal luminance(const QColor &col); +//! Clear the author color hashmap +void +clearAuthorColors(); + +//! Get the given QString from the authorColors hash +QString +getAuthorColor(const QString &author); + +//! Put the given QString into the authorColor hash +void +addAuthorColor(const QString &author, const QString &color); + //! Center a widget in relation to another widget. void centerWidget(QWidget *widget, QWidget *parent); diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp index bd3d73bc..103bd6d6 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp @@ -192,7 +192,8 @@ TimelineItem::init() emit eventRedacted(event_id_); }); }); - + connect( + ChatPage::instance(), &ChatPage::themeChanged, this, &TimelineItem::refreshAuthorColor); connect(markAsRead_, &QAction::triggered, this, &TimelineItem::sendReadReceipt); connect(viewRawMessage_, &QAction::triggered, this, &TimelineItem::openRawMessageViewer); @@ -603,6 +604,21 @@ TimelineItem::generateBody(const QString &body) }); } +void +TimelineItem::refreshAuthorColor() +{ + if (userName_) { + QString userColor = utils::getAuthorColor(userName_->text()); + if (userColor.isEmpty()) { + qApp->style()->polish(this); + // generate user's unique color. + auto backCol = backgroundColor().name(); + userColor = utils::generateContrastingHexColor(userName_->text(), backCol); + utils::addAuthorColor(userName_->text(), userColor); + } + userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); + } +} // The username/timestamp is displayed along with the message body. void TimelineItem::generateBody(const QString &user_id, const QString &displayname, const QString &body) @@ -639,10 +655,14 @@ TimelineItem::generateUserName(const QString &user_id, const QString &displaynam // TimelineItem isn't displayed. This forces the QSS to get // loaded. - qApp->style()->polish(this); - // generate user's unique color. - auto backCol = backgroundColor().name(); - auto userColor = utils::generateContrastingHexColor(user_id, backCol); + QString userColor = utils::getAuthorColor(user_id); + if (userColor.isEmpty()) { + qApp->style()->polish(this); + // generate user's unique color. + auto backCol = backgroundColor().name(); + userColor = utils::generateContrastingHexColor(user_id, backCol); + utils::addAuthorColor(user_id, userColor); + } userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); auto filter = new UserProfileFilter(user_id, userName_); diff --git a/src/timeline/TimelineItem.h b/src/timeline/TimelineItem.h index c7f320d5..6ed3325f 100644 --- a/src/timeline/TimelineItem.h +++ b/src/timeline/TimelineItem.h @@ -227,6 +227,9 @@ signals: void eventRedacted(const QString &event_id); void redactionFailed(const QString &msg); +public slots: + void refreshAuthorColor(); + protected: void paintEvent(QPaintEvent *event) override; void contextMenuEvent(QContextMenuEvent *event) override; -- cgit 1.5.1 From 2ba51c821e3893499ba08df1d7f442869933a286 Mon Sep 17 00:00:00 2001 From: redsky17 Date: Sat, 26 Jan 2019 02:53:43 +0000 Subject: Update user colors to use Cache vs Utils User colors are now stored in cache. This is consistent with other similar variables. I think there's a bug right now where it doesn't properly refresh colors for the TimeLineItem when the theme is changed. --- src/Cache.cpp | 29 +++++++++++++++++++++++++++++ src/Cache.h | 6 ++++++ src/MainWindow.cpp | 2 +- src/Utils.cpp | 22 ---------------------- src/Utils.h | 12 ------------ src/timeline/TimelineItem.cpp | 12 +++++++----- 6 files changed, 43 insertions(+), 40 deletions(-) (limited to 'src/Utils.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index a9094e2d..d6a7b509 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -2059,6 +2059,7 @@ Cache::roomMembers(const std::string &room_id) QHash Cache::DisplayNames; QHash Cache::AvatarUrls; +QHash Cache::UserColors; QString Cache::displayName(const QString &room_id, const QString &user_id) @@ -2090,6 +2091,16 @@ Cache::avatarUrl(const QString &room_id, const QString &user_id) return QString(); } +QString +Cache::userColor(const QString &user_id) +{ + if (UserColors.contains(user_id)) { + return UserColors[user_id]; + } + + return QString(); +} + void Cache::insertDisplayName(const QString &room_id, const QString &user_id, @@ -2119,3 +2130,21 @@ Cache::removeAvatarUrl(const QString &room_id, const QString &user_id) auto fmt = QString("%1 %2").arg(room_id).arg(user_id); AvatarUrls.remove(fmt); } + +void +Cache::insertUserColor(const QString &user_id, const QString &color_name) +{ + UserColors.insert(user_id, color_name); +} + +void +Cache::removeUserColor(const QString &user_id) +{ + UserColors.remove(user_id); +} + +void +Cache::clearUserColors() +{ + UserColors.clear(); +} \ No newline at end of file diff --git a/src/Cache.h b/src/Cache.h index b730d6fc..1a133618 100644 --- a/src/Cache.h +++ b/src/Cache.h @@ -282,13 +282,16 @@ public: static QHash DisplayNames; static QHash AvatarUrls; + static QHash UserColors; static std::string displayName(const std::string &room_id, const std::string &user_id); static QString displayName(const QString &room_id, const QString &user_id); static QString avatarUrl(const QString &room_id, const QString &user_id); + static QString userColor(const QString &user_id); static void removeDisplayName(const QString &room_id, const QString &user_id); static void removeAvatarUrl(const QString &room_id, const QString &user_id); + static void removeUserColor(const QString &user_id); static void insertDisplayName(const QString &room_id, const QString &user_id, @@ -296,6 +299,9 @@ public: static void insertAvatarUrl(const QString &room_id, const QString &user_id, const QString &avatar_url); + static void insertUserColor(const QString &user_id, const QString &color_name); + + static void clearUserColors(); //! Load saved data for the display names & avatars. void populateMembers(); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 51b23fe2..2e062c0c 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -113,7 +113,7 @@ MainWindow::MainWindow(QWidget *parent) connect( userSettingsPage_, SIGNAL(trayOptionChanged(bool)), trayIcon_, SLOT(setVisible(bool))); connect(userSettingsPage_, &UserSettingsPage::themeChanged, this, []() { - utils::clearAuthorColors(); + Cache::clearUserColors(); }); connect( userSettingsPage_, &UserSettingsPage::themeChanged, chat_page_, &ChatPage::themeChanged); diff --git a/src/Utils.cpp b/src/Utils.cpp index 66bc86f6..1d1dbeb2 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -513,28 +513,6 @@ utils::luminance(const QColor &col) return lum; } -void -utils::clearAuthorColors() -{ - authorColors_.clear(); -} - -QString -utils::getAuthorColor(const QString &author) -{ - if (authorColors_.contains(author)) { - return authorColors_[author]; - } else { - return ""; - } -} - -void -utils::addAuthorColor(const QString &author, const QString &color) -{ - authorColors_[author] = color; -} - void utils::centerWidget(QWidget *widget, QWidget *parent) { diff --git a/src/Utils.h b/src/Utils.h index b6f89f73..8672e7d4 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -246,18 +246,6 @@ computeContrast(const qreal &one, const qreal &two); qreal luminance(const QColor &col); -//! Clear the author color hashmap -void -clearAuthorColors(); - -//! Get the given QString from the authorColors hash -QString -getAuthorColor(const QString &author); - -//! Put the given QString into the authorColor hash -void -addAuthorColor(const QString &author, const QString &color); - //! Center a widget in relation to another widget. void centerWidget(QWidget *widget, QWidget *parent); diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp index 103bd6d6..11341b92 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp @@ -608,15 +608,17 @@ void TimelineItem::refreshAuthorColor() { if (userName_) { - QString userColor = utils::getAuthorColor(userName_->text()); + QString userColor = Cache::userColor(userName_->text()); if (userColor.isEmpty()) { + // This attempts to refresh this item since it's not drawn + // which allows us to get the background color accurately. qApp->style()->polish(this); // generate user's unique color. auto backCol = backgroundColor().name(); userColor = utils::generateContrastingHexColor(userName_->text(), backCol); - utils::addAuthorColor(userName_->text(), userColor); + Cache::insertUserColor(userName_->text(), userColor); + userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); } - userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); } } // The username/timestamp is displayed along with the message body. @@ -655,13 +657,13 @@ TimelineItem::generateUserName(const QString &user_id, const QString &displaynam // TimelineItem isn't displayed. This forces the QSS to get // loaded. - QString userColor = utils::getAuthorColor(user_id); + QString userColor = Cache::userColor(user_id); if (userColor.isEmpty()) { qApp->style()->polish(this); // generate user's unique color. auto backCol = backgroundColor().name(); userColor = utils::generateContrastingHexColor(user_id, backCol); - utils::addAuthorColor(user_id, userColor); + Cache::insertUserColor(user_id, userColor); } userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); -- cgit 1.5.1