Try to reduce memory usage by reusing avatar pixmaps
This commit is contained in:
parent
89015b9f18
commit
52056a79fa
27 changed files with 149 additions and 208 deletions
|
|
@ -1,12 +1,13 @@
|
|||
#include <QPainter>
|
||||
|
||||
#include "AvatarProvider.h"
|
||||
#include "Utils.h"
|
||||
#include "ui/Avatar.h"
|
||||
|
||||
Avatar::Avatar(QWidget *parent)
|
||||
Avatar::Avatar(QWidget *parent, int size)
|
||||
: QWidget(parent)
|
||||
, size_(size)
|
||||
{
|
||||
size_ = ui::AvatarSize;
|
||||
type_ = ui::AvatarType::Letter;
|
||||
letter_ = "A";
|
||||
|
||||
|
|
@ -60,21 +61,6 @@ Avatar::setBackgroundColor(const QColor &color)
|
|||
background_color_ = color;
|
||||
}
|
||||
|
||||
void
|
||||
Avatar::setSize(int size)
|
||||
{
|
||||
size_ = size;
|
||||
|
||||
if (!image_.isNull())
|
||||
pixmap_ = utils::scaleImageToPixmap(image_, size_);
|
||||
|
||||
QFont _font(font());
|
||||
_font.setPointSizeF(size_ * (ui::FontSize) / 40);
|
||||
|
||||
setFont(_font);
|
||||
update();
|
||||
}
|
||||
|
||||
void
|
||||
Avatar::setLetter(const QString &letter)
|
||||
{
|
||||
|
|
@ -84,12 +70,23 @@ Avatar::setLetter(const QString &letter)
|
|||
}
|
||||
|
||||
void
|
||||
Avatar::setImage(const QImage &image)
|
||||
Avatar::setImage(const QString &avatar_url)
|
||||
{
|
||||
image_ = image;
|
||||
type_ = ui::AvatarType::Image;
|
||||
pixmap_ = utils::scaleImageToPixmap(image_, size_);
|
||||
update();
|
||||
AvatarProvider::resolve(avatar_url, size_, 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();
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
|||
|
|
@ -15,13 +15,14 @@ class Avatar : public QWidget
|
|||
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor)
|
||||
|
||||
public:
|
||||
explicit Avatar(QWidget *parent = 0);
|
||||
explicit Avatar(QWidget *parent = 0, int size = ui::AvatarSize);
|
||||
|
||||
void setBackgroundColor(const QColor &color);
|
||||
void setIcon(const QIcon &icon);
|
||||
void setImage(const QImage &image);
|
||||
void setImage(const QString &avatar_url);
|
||||
void setImage(const QString &room, const QString &user);
|
||||
void setLetter(const QString &letter);
|
||||
void setSize(int size);
|
||||
// void setSize(int size);
|
||||
void setTextColor(const QColor &color);
|
||||
|
||||
QColor backgroundColor() const;
|
||||
|
|
@ -41,7 +42,8 @@ private:
|
|||
QColor background_color_;
|
||||
QColor text_color_;
|
||||
QIcon icon_;
|
||||
QImage image_;
|
||||
QPixmap pixmap_;
|
||||
const std::string room;
|
||||
const std::string user;
|
||||
int size_;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue