From 9efa001bcfefe1dcd285a5a60dcdbf9ddd79a066 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Fri, 28 Feb 2020 03:20:27 +0100 Subject: [PATCH] Fix high dpi scaling of avatars --- resources/styles/nheko-dark.qss | 8 ++++-- resources/styles/nheko.qss | 8 ++++-- resources/styles/system.qss | 9 ++++-- src/RoomInfoListItem.cpp | 51 ++++++--------------------------- src/RoomInfoListItem.h | 14 ++------- src/ui/Avatar.cpp | 40 +++++++++++++++++++------- src/ui/Avatar.h | 1 + 7 files changed, 57 insertions(+), 74 deletions(-) diff --git a/resources/styles/nheko-dark.qss b/resources/styles/nheko-dark.qss index 37c524c..4efb6f3 100644 --- a/resources/styles/nheko-dark.qss +++ b/resources/styles/nheko-dark.qss @@ -145,13 +145,15 @@ UserMentionsWidget { qproperty-highlightedTimestampColor: #e7e7e9; qproperty-hoverTimestampColor: #f4f5f8; - qproperty-avatarBgColor: #202228; - qproperty-avatarFgColor: white; - qproperty-bubbleFgColor: white; qproperty-bubbleBgColor: #4d84c7; } +RoomInfoListItem > Avatar { + qproperty-backgroundColor: #202228; + qproperty-textColor: white; +} + CommunitiesListItem { qproperty-highlightedBackgroundColor: #4d84c7; qproperty-hoverBackgroundColor: rgba(230, 230, 230, 30); diff --git a/resources/styles/nheko.qss b/resources/styles/nheko.qss index 26482db..4c59bad 100644 --- a/resources/styles/nheko.qss +++ b/resources/styles/nheko.qss @@ -111,13 +111,15 @@ RoomInfoListItem { qproperty-highlightedTimestampColor: #f4f4f5; qproperty-hoverTimestampColor: white; - qproperty-avatarBgColor: #eee; - qproperty-avatarFgColor: black; - qproperty-bubbleFgColor: white; qproperty-bubbleBgColor: #38A3D8; } +RoomInfoListItem > Avatar { + qproperty-backgroundColor: #eee; + qproperty-textColor: black; +} + CommunitiesListItem { qproperty-highlightedBackgroundColor: #38A3D8; qproperty-hoverBackgroundColor: rgba(200, 200, 200, 40); diff --git a/resources/styles/system.qss b/resources/styles/system.qss index db8f4b4..3ae3147 100644 --- a/resources/styles/system.qss +++ b/resources/styles/system.qss @@ -108,13 +108,16 @@ UserMentionsWidget { qproperty-highlightedTimestampColor: palette(highlightedtext); qproperty-hoverTimestampColor: palette(highlightedtext); - qproperty-avatarBgColor: palette(base); - qproperty-avatarFgColor: palette(text); - qproperty-bubbleBgColor: palette(base); qproperty-bubbleFgColor: palette(text); } +RoomInfoListItem > Avatar { + qproperty-backgroundColor: palette(base); + qproperty-textColor: palette(text); +} + + CommunitiesListItem { qproperty-highlightedBackgroundColor: palette(highlight); qproperty-hoverBackgroundColor: palette(light); diff --git a/src/RoomInfoListItem.cpp b/src/RoomInfoListItem.cpp index 822a7a5..f9fad1d 100644 --- a/src/RoomInfoListItem.cpp +++ b/src/RoomInfoListItem.cpp @@ -33,8 +33,7 @@ constexpr int MaxUnreadCountDisplayed = 99; -constexpr int IconSize = 44; -// constexpr int MaxHeight = IconSize + 2 * Padding; +constexpr int IconSize = 48; struct WidgetMetrics { @@ -77,7 +76,8 @@ RoomInfoListItem::init(QWidget *parent) setMouseTracking(true); setAttribute(Qt::WA_Hover); - setFixedHeight(getMetrics(QFont{}).maxHeight); + auto wm = getMetrics(QFont{}); + setFixedHeight(wm.maxHeight); QPainterPath path; path.addRect(0, 0, parent->width(), height()); @@ -86,6 +86,10 @@ RoomInfoListItem::init(QWidget *parent) ripple_overlay_->setClipPath(path); ripple_overlay_->setClipping(true); + avatar_ = new Avatar(this, wm.iconSize); + avatar_->setLetter(utils::firstChar(roomName_)); + avatar_->move(wm.padding, wm.padding); + unreadCountFont_.setPointSizeF(unreadCountFont_.pointSizeF() * 0.8); unreadCountFont_.setBold(true); @@ -130,8 +134,6 @@ RoomInfoListItem::resizeEvent(QResizeEvent *) void RoomInfoListItem::paintEvent(QPaintEvent *event) { - bool rounded = QSettings().value("user/avatar_circles", true).toBool(); - Q_UNUSED(event); QPainter p(this); @@ -160,8 +162,6 @@ RoomInfoListItem::paintEvent(QPaintEvent *event) subtitlePen.setColor(subtitleColor_); } - QRect avatarRegion(wm.padding, wm.padding, wm.iconSize, wm.iconSize); - // Description line with the default font. int bottom_y = wm.maxHeight - wm.padding - metrics.ascent() / 2; @@ -252,38 +252,6 @@ RoomInfoListItem::paintEvent(QPaintEvent *event) p.setPen(Qt::NoPen); - // We using the first letter of room's name. - if (roomAvatar_.isNull()) { - QBrush brush; - brush.setStyle(Qt::SolidPattern); - brush.setColor(avatarBgColor()); - - p.setPen(Qt::NoPen); - p.setBrush(brush); - - rounded ? p.drawEllipse(avatarRegion.center(), wm.iconSize / 2, wm.iconSize / 2) - : p.drawRoundedRect(avatarRegion, 3, 3); - - QFont bubbleFont; - bubbleFont.setPointSizeF(bubbleFont.pointSizeF() * 1.4); - p.setFont(bubbleFont); - p.setPen(avatarFgColor()); - p.setBrush(Qt::NoBrush); - p.drawText( - avatarRegion.translated(0, -1), Qt::AlignCenter, utils::firstChar(roomName())); - } else { - p.save(); - - QPainterPath path; - rounded ? path.addEllipse(wm.padding, wm.padding, wm.iconSize, wm.iconSize) - : path.addRoundedRect(avatarRegion, 3, 3); - - p.setClipPath(path); - - p.drawPixmap(avatarRegion, roomAvatar_); - p.restore(); - } - if (unreadMsgCount_ > 0) { QBrush brush; brush.setStyle(Qt::SolidPattern); @@ -413,10 +381,7 @@ RoomInfoListItem::mousePressEvent(QMouseEvent *event) void RoomInfoListItem::setAvatar(const QString &avatar_url) { - AvatarProvider::resolve(avatar_url, IconSize, this, [this](const QPixmap &img) { - roomAvatar_ = img; - update(); - }); + avatar_->setImage(avatar_url); } void diff --git a/src/RoomInfoListItem.h b/src/RoomInfoListItem.h index 5cb9e83..c1ee533 100644 --- a/src/RoomInfoListItem.h +++ b/src/RoomInfoListItem.h @@ -25,6 +25,7 @@ #include #include "CacheStructs.h" +#include "ui/Avatar.h" class Menu; class RippleOverlay; @@ -38,9 +39,6 @@ class RoomInfoListItem : public QWidget QColor hoverBackgroundColor READ hoverBackgroundColor WRITE setHoverBackgroundColor) Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor) - Q_PROPERTY(QColor avatarBgColor READ avatarBgColor WRITE setAvatarBgColor) - Q_PROPERTY(QColor avatarFgColor READ avatarFgColor WRITE setAvatarFgColor) - Q_PROPERTY(QColor bubbleBgColor READ bubbleBgColor WRITE setBubbleBgColor) Q_PROPERTY(QColor bubbleFgColor READ bubbleFgColor WRITE setBubbleFgColor) @@ -84,8 +82,6 @@ public: QColor hoverSubtitleColor() const { return hoverSubtitleColor_; } QColor hoverTimestampColor() const { return hoverTimestampColor_; } QColor backgroundColor() const { return backgroundColor_; } - QColor avatarBgColor() const { return avatarBgColor_; } - QColor avatarFgColor() const { return avatarFgColor_; } QColor highlightedTitleColor() const { return highlightedTitleColor_; } QColor highlightedSubtitleColor() const { return highlightedSubtitleColor_; } @@ -108,8 +104,6 @@ public: void setHoverTimestampColor(QColor &color) { hoverTimestampColor_ = color; } void setBackgroundColor(QColor &color) { backgroundColor_ = color; } void setTimestampColor(QColor &color) { timestampColor_ = color; } - void setAvatarFgColor(QColor &color) { avatarFgColor_ = color; } - void setAvatarBgColor(QColor &color) { avatarBgColor_ = color; } void setHighlightedTitleColor(QColor &color) { highlightedTitleColor_ = color; } void setHighlightedSubtitleColor(QColor &color) { highlightedSubtitleColor_ = color; } @@ -163,6 +157,7 @@ private: QString roomName() { return roomName_; } RippleOverlay *ripple_overlay_; + Avatar *avatar_; enum class RoomType { @@ -180,8 +175,6 @@ private: DescInfo lastMsgInfo_; - QPixmap roomAvatar_; - Menu *menu_; QAction *leaveRoom_; @@ -219,9 +212,6 @@ private: QColor highlightedTimestampColor_; QColor hoverTimestampColor_; - QColor avatarBgColor_; - QColor avatarFgColor_; - QColor bubbleBgColor_; QColor bubbleFgColor_; }; diff --git a/src/ui/Avatar.cpp b/src/ui/Avatar.cpp index e4a90f8..3589fce 100644 --- a/src/ui/Avatar.cpp +++ b/src/ui/Avatar.cpp @@ -73,21 +73,31 @@ Avatar::setLetter(const QString &letter) void Avatar::setImage(const QString &avatar_url) { - AvatarProvider::resolve(avatar_url, size_, this, [this](QPixmap pm) { - type_ = ui::AvatarType::Image; - pixmap_ = pm; - update(); - }); + avatar_url_ = avatar_url; + AvatarProvider::resolve(avatar_url, + static_cast(size_ * pixmap_.devicePixelRatio()), + this, + [this](QPixmap pm) { + type_ = ui::AvatarType::Image; + pixmap_ = pm; + update(); + }); } void Avatar::setImage(const QString &room, const QString &user) { - AvatarProvider::resolve(room, user, size_, this, [this](QPixmap pm) { - type_ = ui::AvatarType::Image; - pixmap_ = pm; - update(); - }); + room_ = room; + user_ = user; + AvatarProvider::resolve(room, + user, + static_cast(size_ * pixmap_.devicePixelRatio()), + this, + [this](QPixmap pm) { + type_ = ui::AvatarType::Image; + pixmap_ = pm; + update(); + }); } void @@ -118,6 +128,16 @@ Avatar::paintEvent(QPaintEvent *) painter.setBrush(brush); rounded ? painter.drawEllipse(r.center(), hs, hs) : painter.drawRoundedRect(r, 3, 3); + } else if (painter.isActive() && + abs(pixmap_.devicePixelRatio() - painter.device()->devicePixelRatioF()) > 0.01) { + pixmap_ = + pixmap_.scaled(QSize(size_, size_) * painter.device()->devicePixelRatioF()); + pixmap_.setDevicePixelRatio(painter.device()->devicePixelRatioF()); + + if (!avatar_url_.isEmpty()) + setImage(avatar_url_); + else + setImage(room_, user_); } switch (type_) { diff --git a/src/ui/Avatar.h b/src/ui/Avatar.h index d6d0b5c..aea7d3e 100644 --- a/src/ui/Avatar.h +++ b/src/ui/Avatar.h @@ -38,6 +38,7 @@ private: ui::AvatarType type_; QString letter_; + QString avatar_url_, room_, user_; QColor background_color_; QColor text_color_; QIcon icon_;