Fix high dpi scaling of avatars

This commit is contained in:
Nicolas Werner 2020-02-28 03:20:27 +01:00
parent 11bffd5d90
commit 9efa001bcf
7 changed files with 57 additions and 74 deletions

View file

@ -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<int>(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<int>(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_) {

View file

@ -38,6 +38,7 @@ private:
ui::AvatarType type_;
QString letter_;
QString avatar_url_, room_, user_;
QColor background_color_;
QColor text_color_;
QIcon icon_;