From 237c7ad114b7c3d6960fdaf712b600e715eff082 Mon Sep 17 00:00:00 2001 From: redsky17 Date: Sun, 20 Jan 2019 04:43:48 +0000 Subject: [PATCH] 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(-) 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;