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.
This commit is contained in:
redsky17 2019-01-20 04:43:48 +00:00
parent b3f7c13e2f
commit 237c7ad114
8 changed files with 75 additions and 7 deletions

View file

@ -148,6 +148,7 @@ signals:
const QImage &icon); const QImage &icon);
void updateGroupsInfo(const mtx::responses::JoinedGroups &groups); void updateGroupsInfo(const mtx::responses::JoinedGroups &groups);
void themeChanged();
private slots: private slots:
void showUnreadMessageNotification(int count); void showUnreadMessageNotification(int count);

View file

@ -112,7 +112,11 @@ MainWindow::MainWindow(QWidget *parent)
connect( connect(
userSettingsPage_, SIGNAL(trayOptionChanged(bool)), trayIcon_, SLOT(setVisible(bool))); 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_, connect(trayIcon_,
SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, this,

View file

@ -379,7 +379,10 @@ UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidge
connect(themeCombo_, connect(themeCombo_,
static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
[this](const QString &text) { settings_->setTheme(text.toLower()); }); [this](const QString &text) {
settings_->setTheme(text.toLower());
emit themeChanged();
});
connect(scaleFactorCombo_, connect(scaleFactorCombo_,
static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
[](const QString &factor) { utils::setScaleFactor(factor.toFloat()); }); [](const QString &factor) { utils::setScaleFactor(factor.toFloat()); });

View file

@ -132,6 +132,7 @@ protected:
signals: signals:
void moveBack(); void moveBack();
void trayOptionChanged(bool value); void trayOptionChanged(bool value);
void themeChanged();
private slots: private slots:
void importSessionKeys(); void importSessionKeys();

View file

@ -15,6 +15,8 @@
using TimelineEvent = mtx::events::collections::TimelineEvents; using TimelineEvent = mtx::events::collections::TimelineEvents;
QHash<QString, QString> authorColors_;
QString QString
utils::localUser() utils::localUser()
{ {
@ -511,6 +513,28 @@ utils::luminance(const QColor &col)
return lum; 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 void
utils::centerWidget(QWidget *widget, QWidget *parent) utils::centerWidget(QWidget *widget, QWidget *parent)
{ {

View file

@ -246,6 +246,18 @@ computeContrast(const qreal &one, const qreal &two);
qreal qreal
luminance(const QColor &col); 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. //! Center a widget in relation to another widget.
void void
centerWidget(QWidget *widget, QWidget *parent); centerWidget(QWidget *widget, QWidget *parent);

View file

@ -192,7 +192,8 @@ TimelineItem::init()
emit eventRedacted(event_id_); emit eventRedacted(event_id_);
}); });
}); });
connect(
ChatPage::instance(), &ChatPage::themeChanged, this, &TimelineItem::refreshAuthorColor);
connect(markAsRead_, &QAction::triggered, this, &TimelineItem::sendReadReceipt); connect(markAsRead_, &QAction::triggered, this, &TimelineItem::sendReadReceipt);
connect(viewRawMessage_, &QAction::triggered, this, &TimelineItem::openRawMessageViewer); 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. // The username/timestamp is displayed along with the message body.
void void
TimelineItem::generateBody(const QString &user_id, const QString &displayname, const QString &body) 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 // TimelineItem isn't displayed. This forces the QSS to get
// loaded. // loaded.
qApp->style()->polish(this); QString userColor = utils::getAuthorColor(user_id);
// generate user's unique color. if (userColor.isEmpty()) {
auto backCol = backgroundColor().name(); qApp->style()->polish(this);
auto userColor = utils::generateContrastingHexColor(user_id, backCol); // 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 + "; }"); userName_->setStyleSheet("QLabel { color : " + userColor + "; }");
auto filter = new UserProfileFilter(user_id, userName_); auto filter = new UserProfileFilter(user_id, userName_);

View file

@ -227,6 +227,9 @@ signals:
void eventRedacted(const QString &event_id); void eventRedacted(const QString &event_id);
void redactionFailed(const QString &msg); void redactionFailed(const QString &msg);
public slots:
void refreshAuthorColor();
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override; void contextMenuEvent(QContextMenuEvent *event) override;