2017-04-06 02:06:42 +03:00
|
|
|
#include <QPainter>
|
2019-09-02 00:41:23 +03:00
|
|
|
#include <QSettings>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2019-08-26 02:24:56 +03:00
|
|
|
#include "AvatarProvider.h"
|
2018-05-11 16:00:14 +03:00
|
|
|
#include "Utils.h"
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "ui/Avatar.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2019-08-26 02:24:56 +03:00
|
|
|
Avatar::Avatar(QWidget *parent, int size)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QWidget(parent)
|
2019-08-26 02:24:56 +03:00
|
|
|
, size_(size)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
type_ = ui::AvatarType::Letter;
|
2018-01-12 11:21:53 +03:00
|
|
|
letter_ = "A";
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
QFont _font(font());
|
|
|
|
_font.setPointSizeF(ui::FontSize);
|
|
|
|
setFont(_font);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
QSizePolicy policy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
|
|
|
|
setSizePolicy(policy);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QColor
|
|
|
|
Avatar::textColor() const
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!text_color_.isValid())
|
|
|
|
return QColor("black");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
return text_color_;
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QColor
|
|
|
|
Avatar::backgroundColor() const
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!text_color_.isValid())
|
|
|
|
return QColor("white");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
return background_color_;
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
QSize
|
|
|
|
Avatar::sizeHint() const
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
return QSize(size_ + 2, size_ + 2);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
Avatar::setTextColor(const QColor &color)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
text_color_ = color;
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
Avatar::setBackgroundColor(const QColor &color)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
background_color_ = color;
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2019-08-26 02:24:56 +03:00
|
|
|
Avatar::setLetter(const QString &letter)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2019-08-26 02:24:56 +03:00
|
|
|
letter_ = letter;
|
|
|
|
type_ = ui::AvatarType::Letter;
|
2017-09-10 12:59:21 +03:00
|
|
|
update();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2019-08-26 02:24:56 +03:00
|
|
|
Avatar::setImage(const QString &avatar_url)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2020-02-28 05:20:27 +03:00
|
|
|
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();
|
|
|
|
});
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2019-08-26 02:24:56 +03:00
|
|
|
Avatar::setImage(const QString &room, const QString &user)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2020-02-28 05:20:27 +03:00
|
|
|
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();
|
|
|
|
});
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
Avatar::setIcon(const QIcon &icon)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
icon_ = icon;
|
|
|
|
type_ = ui::AvatarType::Icon;
|
|
|
|
update();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2019-08-29 07:36:28 +03:00
|
|
|
void
|
2017-08-20 13:47:22 +03:00
|
|
|
Avatar::paintEvent(QPaintEvent *)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2019-09-07 23:22:07 +03:00
|
|
|
bool rounded = QSettings().value("user/avatar_circles", true).toBool();
|
2019-09-02 00:41:23 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
QRect r = rect();
|
|
|
|
const int hs = size_ / 2;
|
|
|
|
|
|
|
|
if (type_ != ui::AvatarType::Image) {
|
|
|
|
QBrush brush;
|
|
|
|
brush.setStyle(Qt::SolidPattern);
|
|
|
|
brush.setColor(backgroundColor());
|
|
|
|
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
painter.setBrush(brush);
|
2019-09-03 09:30:59 +03:00
|
|
|
rounded ? painter.drawEllipse(r.center(), hs, hs)
|
|
|
|
: painter.drawRoundedRect(r, 3, 3);
|
2020-02-28 05:20:27 +03:00
|
|
|
} 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_);
|
2017-09-10 12:59:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type_) {
|
|
|
|
case ui::AvatarType::Icon: {
|
|
|
|
icon_.paint(&painter,
|
|
|
|
QRect((width() - hs) / 2, (height() - hs) / 2, hs, hs),
|
|
|
|
Qt::AlignCenter,
|
|
|
|
QIcon::Normal);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ui::AvatarType::Image: {
|
|
|
|
QPainterPath ppath;
|
2019-08-28 09:31:04 +03:00
|
|
|
|
2019-09-03 09:30:59 +03:00
|
|
|
rounded ? ppath.addEllipse(width() / 2 - hs, height() / 2 - hs, size_, size_)
|
|
|
|
: ppath.addRoundedRect(r, 3, 3);
|
2019-08-28 09:31:04 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
painter.setClipPath(ppath);
|
|
|
|
painter.drawPixmap(QRect(width() / 2 - hs, height() / 2 - hs, size_, size_),
|
|
|
|
pixmap_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ui::AvatarType::Letter: {
|
|
|
|
painter.setPen(textColor());
|
|
|
|
painter.setBrush(Qt::NoBrush);
|
|
|
|
painter.drawText(r.translated(0, -1), Qt::AlignCenter, letter_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|