matrixion/src/ui/Avatar.cpp

162 lines
4.6 KiB
C++
Raw Normal View History

2017-04-06 02:06:42 +03:00
#include <QPainter>
#include <QSettings>
2017-04-06 02:06:42 +03:00
#include "AvatarProvider.h"
#include "Utils.h"
2018-07-17 16:37:25 +03:00
#include "ui/Avatar.h"
2017-04-06 02:06:42 +03:00
Avatar::Avatar(QWidget *parent, int size)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
, 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
Avatar::setLetter(const QString &letter)
2017-04-06 02:06:42 +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
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, requestedRatio = pixmap_.devicePixelRatio()](QPixmap pm) {
2020-03-22 20:03:08 +03:00
if (pm.isNull())
return;
2020-02-28 05:20:27 +03:00
type_ = ui::AvatarType::Image;
pixmap_ = pm;
pixmap_.setDevicePixelRatio(requestedRatio);
2020-02-28 05:20:27 +03:00
update();
});
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
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, requestedRatio = pixmap_.devicePixelRatio()](QPixmap pm) {
2020-03-22 20:03:08 +03:00
if (pm.isNull())
return;
2020-02-28 05:20:27 +03:00
type_ = ui::AvatarType::Image;
pixmap_ = pm;
pixmap_.setDevicePixelRatio(requestedRatio);
2020-02-28 05:20:27 +03:00
update();
});
2017-04-06 02:06:42 +03:00
}
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_);
}
}
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();
2017-09-10 12:59:21 +03:00
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QRectF r = rect();
2017-09-10 12:59:21 +03:00
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);
rounded ? painter.drawEllipse(r) : painter.drawRoundedRect(r, 3, 3);
} else if (painter.isActive()) {
setDevicePixelRatio(painter.device()->devicePixelRatioF());
2017-09-10 12:59:21 +03:00
}
switch (type_) {
case ui::AvatarType::Image: {
QPainterPath ppath;
rounded ? ppath.addEllipse(width() / 2 - hs, height() / 2 - hs, size_, size_)
: ppath.addRoundedRect(r, 3, 3);
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
}