1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include <QPainter>
#include <QPainterPath>
#include <QSettings>
#include "AvatarProvider.h"
#include "Utils.h"
#include "ui/Avatar.h"
Avatar::Avatar(QWidget *parent, int size)
: QWidget(parent)
, size_(size)
{
type_ = ui::AvatarType::Letter;
letter_ = "A";
QFont _font(font());
_font.setPointSizeF(ui::FontSize);
setFont(_font);
QSizePolicy policy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
setSizePolicy(policy);
}
QColor
Avatar::textColor() const
{
if (!text_color_.isValid())
return QColor("black");
return text_color_;
}
QColor
Avatar::backgroundColor() const
{
if (!text_color_.isValid())
return QColor("white");
return background_color_;
}
QSize
Avatar::sizeHint() const
{
return QSize(size_ + 2, size_ + 2);
}
void
Avatar::setTextColor(const QColor &color)
{
text_color_ = color;
}
void
Avatar::setBackgroundColor(const QColor &color)
{
background_color_ = color;
}
void
Avatar::setLetter(const QString &letter)
{
letter_ = letter;
type_ = ui::AvatarType::Letter;
update();
}
void
Avatar::setImage(const QString &avatar_url)
{
avatar_url_ = avatar_url;
AvatarProvider::resolve(avatar_url,
static_cast<int>(size_ * pixmap_.devicePixelRatio()),
this,
[this, requestedRatio = pixmap_.devicePixelRatio()](QPixmap pm) {
if (pm.isNull())
return;
type_ = ui::AvatarType::Image;
pixmap_ = pm;
pixmap_.setDevicePixelRatio(requestedRatio);
update();
});
}
void
Avatar::setImage(const QString &room, const QString &user)
{
room_ = room;
user_ = user;
AvatarProvider::resolve(room,
user,
static_cast<int>(size_ * pixmap_.devicePixelRatio()),
this,
[this, requestedRatio = pixmap_.devicePixelRatio()](QPixmap pm) {
if (pm.isNull())
return;
type_ = ui::AvatarType::Image;
pixmap_ = pm;
pixmap_.setDevicePixelRatio(requestedRatio);
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
Avatar::paintEvent(QPaintEvent *)
{
bool rounded = QSettings().value(QStringLiteral("user/avatar_circles"), true).toBool();
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform |
QPainter::TextAntialiasing);
QRectF 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);
rounded ? painter.drawEllipse(r) : painter.drawRoundedRect(r, 3, 3);
} else if (painter.isActive()) {
setDevicePixelRatio(painter.device()->devicePixelRatioF());
}
switch (type_) {
case ui::AvatarType::Image: {
QPainterPath ppath;
rounded ? ppath.addEllipse(width() / 2 - hs, height() / 2 - hs, size_, size_)
: ppath.addRoundedRect(r, 3, 3);
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;
}
}
|