From 9efa001bcfefe1dcd285a5a60dcdbf9ddd79a066 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Fri, 28 Feb 2020 03:20:27 +0100 Subject: Fix high dpi scaling of avatars --- src/ui/Avatar.cpp | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'src/ui/Avatar.cpp') diff --git a/src/ui/Avatar.cpp b/src/ui/Avatar.cpp index e4a90f81..3589fce5 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_) { -- cgit 1.5.1