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.
This commit is contained in:
redsky17 2019-01-26 02:53:43 +00:00
parent 4185b8d121
commit 2ba51c821e
6 changed files with 43 additions and 40 deletions

View file

@ -2059,6 +2059,7 @@ Cache::roomMembers(const std::string &room_id)
QHash<QString, QString> Cache::DisplayNames; QHash<QString, QString> Cache::DisplayNames;
QHash<QString, QString> Cache::AvatarUrls; QHash<QString, QString> Cache::AvatarUrls;
QHash<QString, QString> Cache::UserColors;
QString QString
Cache::displayName(const QString &room_id, const QString &user_id) 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(); return QString();
} }
QString
Cache::userColor(const QString &user_id)
{
if (UserColors.contains(user_id)) {
return UserColors[user_id];
}
return QString();
}
void void
Cache::insertDisplayName(const QString &room_id, Cache::insertDisplayName(const QString &room_id,
const QString &user_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); auto fmt = QString("%1 %2").arg(room_id).arg(user_id);
AvatarUrls.remove(fmt); 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();
}

View file

@ -282,13 +282,16 @@ public:
static QHash<QString, QString> DisplayNames; static QHash<QString, QString> DisplayNames;
static QHash<QString, QString> AvatarUrls; static QHash<QString, QString> AvatarUrls;
static QHash<QString, QString> UserColors;
static std::string displayName(const std::string &room_id, const std::string &user_id); 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 displayName(const QString &room_id, const QString &user_id);
static QString avatarUrl(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 removeDisplayName(const QString &room_id, const QString &user_id);
static void removeAvatarUrl(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, static void insertDisplayName(const QString &room_id,
const QString &user_id, const QString &user_id,
@ -296,6 +299,9 @@ public:
static void insertAvatarUrl(const QString &room_id, static void insertAvatarUrl(const QString &room_id,
const QString &user_id, const QString &user_id,
const QString &avatar_url); 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. //! Load saved data for the display names & avatars.
void populateMembers(); void populateMembers();

View file

@ -113,7 +113,7 @@ 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, []() { connect(userSettingsPage_, &UserSettingsPage::themeChanged, this, []() {
utils::clearAuthorColors(); Cache::clearUserColors();
}); });
connect( connect(
userSettingsPage_, &UserSettingsPage::themeChanged, chat_page_, &ChatPage::themeChanged); userSettingsPage_, &UserSettingsPage::themeChanged, chat_page_, &ChatPage::themeChanged);

View file

@ -513,28 +513,6 @@ 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,18 +246,6 @@ 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

@ -608,15 +608,17 @@ void
TimelineItem::refreshAuthorColor() TimelineItem::refreshAuthorColor()
{ {
if (userName_) { if (userName_) {
QString userColor = utils::getAuthorColor(userName_->text()); QString userColor = Cache::userColor(userName_->text());
if (userColor.isEmpty()) { 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); qApp->style()->polish(this);
// generate user's unique color. // generate user's unique color.
auto backCol = backgroundColor().name(); auto backCol = backgroundColor().name();
userColor = utils::generateContrastingHexColor(userName_->text(), backCol); 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. // 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 // TimelineItem isn't displayed. This forces the QSS to get
// loaded. // loaded.
QString userColor = utils::getAuthorColor(user_id); QString userColor = Cache::userColor(user_id);
if (userColor.isEmpty()) { if (userColor.isEmpty()) {
qApp->style()->polish(this); qApp->style()->polish(this);
// generate user's unique color. // generate user's unique color.
auto backCol = backgroundColor().name(); auto backCol = backgroundColor().name();
userColor = utils::generateContrastingHexColor(user_id, backCol); userColor = utils::generateContrastingHexColor(user_id, backCol);
utils::addAuthorColor(user_id, userColor); Cache::insertUserColor(user_id, userColor);
} }
userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); userName_->setStyleSheet("QLabel { color : " + userColor + "; }");