Fix high CPU usage on high dpi screens

Fixes #180
This commit is contained in:
Nicolas Werner 2020-04-29 20:23:00 +02:00
parent 17c657a170
commit 27350cf51e
3 changed files with 25 additions and 16 deletions

View File

@ -151,7 +151,8 @@ RoomInfoListItem::paintEvent(QPaintEvent *event)
auto wm = getMetrics(QFont{}); auto wm = getMetrics(QFont{});
QPixmap pixmap(avatar_->size()); QPixmap pixmap(avatar_->size() * p.device()->devicePixelRatioF());
pixmap.setDevicePixelRatio(p.device()->devicePixelRatioF());
if (isPressed_) { if (isPressed_) {
p.fillRect(rect(), highlightedBackgroundColor_); p.fillRect(rect(), highlightedBackgroundColor_);
titlePen.setColor(highlightedTitleColor_); titlePen.setColor(highlightedTitleColor_);

View File

@ -71,11 +71,12 @@ Avatar::setImage(const QString &avatar_url)
AvatarProvider::resolve(avatar_url, AvatarProvider::resolve(avatar_url,
static_cast<int>(size_ * pixmap_.devicePixelRatio()), static_cast<int>(size_ * pixmap_.devicePixelRatio()),
this, this,
[this](QPixmap pm) { [this, requestedRatio = pixmap_.devicePixelRatio()](QPixmap pm) {
if (pm.isNull()) if (pm.isNull())
return; return;
type_ = ui::AvatarType::Image; type_ = ui::AvatarType::Image;
pixmap_ = pm; pixmap_ = pm;
pixmap_.setDevicePixelRatio(requestedRatio);
update(); update();
}); });
} }
@ -89,15 +90,30 @@ Avatar::setImage(const QString &room, const QString &user)
user, user,
static_cast<int>(size_ * pixmap_.devicePixelRatio()), static_cast<int>(size_ * pixmap_.devicePixelRatio()),
this, this,
[this](QPixmap pm) { [this, requestedRatio = pixmap_.devicePixelRatio()](QPixmap pm) {
if (pm.isNull()) if (pm.isNull())
return; return;
type_ = ui::AvatarType::Image; type_ = ui::AvatarType::Image;
pixmap_ = pm; pixmap_ = pm;
pixmap_.setDevicePixelRatio(requestedRatio);
update(); update();
}); });
} }
void
Avatar::setDevicePixelRatio(double ratio)
{
if (type_ == ui::AvatarType::Image && abs(pixmap_.devicePixelRatio() - ratio) > 0.01) {
pixmap_ = pixmap_.scaled(QSize(size_, size_) * ratio);
pixmap_.setDevicePixelRatio(ratio);
if (!avatar_url_.isEmpty())
setImage(avatar_url_);
else
setImage(room_, user_);
}
}
void void
Avatar::paintEvent(QPaintEvent *) Avatar::paintEvent(QPaintEvent *)
{ {
@ -106,7 +122,7 @@ Avatar::paintEvent(QPaintEvent *)
QPainter painter(this); QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing);
QRect r = rect(); QRectF r = rect();
const int hs = size_ / 2; const int hs = size_ / 2;
if (type_ != ui::AvatarType::Image) { if (type_ != ui::AvatarType::Image) {
@ -116,18 +132,9 @@ Avatar::paintEvent(QPaintEvent *)
painter.setPen(Qt::NoPen); painter.setPen(Qt::NoPen);
painter.setBrush(brush); painter.setBrush(brush);
rounded ? painter.drawEllipse(r.center(), hs, hs) rounded ? painter.drawEllipse(r) : painter.drawRoundedRect(r, 3, 3);
: painter.drawRoundedRect(r, 3, 3); } else if (painter.isActive()) {
} else if (painter.isActive() && setDevicePixelRatio(painter.device()->devicePixelRatioF());
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_) { switch (type_) {

View File

@ -21,6 +21,7 @@ public:
void setImage(const QString &room, const QString &user); void setImage(const QString &room, const QString &user);
void setLetter(const QString &letter); void setLetter(const QString &letter);
void setTextColor(const QColor &color); void setTextColor(const QColor &color);
void setDevicePixelRatio(double ratio);
QColor backgroundColor() const; QColor backgroundColor() const;
QColor textColor() const; QColor textColor() const;