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.
2 files changed, 13 insertions, 2 deletions
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<mtx::events::msg::Notice> &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<class Widget>
|