matrixion/src/ui/Avatar.cpp

166 lines
3.7 KiB
C++
Raw Normal View History

2017-04-06 02:06:42 +03:00
#include <QPainter>
#include "Utils.h"
2018-07-17 16:37:25 +03:00
#include "ui/Avatar.h"
2017-04-06 02:06:42 +03:00
#define AVATAR_RECT_ROUND 5
2017-04-06 02:06:42 +03:00
Avatar::Avatar(QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
2017-04-06 02:06:42 +03:00
{
size_ = ui::AvatarSize;
type_ = ui::AvatarType::Letter;
letter_ = "A";
rounded_ = true;
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
int
Avatar::size() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return size_;
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::setSize(int size)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
size_ = size;
2017-04-06 02:06:42 +03:00
if (!image_.isNull())
pixmap_ = utils::scaleImageToPixmap(image_, size_);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
QFont _font(font());
_font.setPointSizeF(size_ * (ui::FontSize) / 40);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
setFont(_font);
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
2018-01-12 11:21:53 +03:00
Avatar::setLetter(const QString &letter)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
letter_ = letter;
type_ = ui::AvatarType::Letter;
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Avatar::setImage(const QImage &image)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
image_ = image;
type_ = ui::AvatarType::Image;
pixmap_ = utils::scaleImageToPixmap(image_, size_);
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::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
}
2017-08-20 13:47:22 +03:00
void
Avatar::rounded(bool setting)
{
rounded_ = setting;
}
2017-08-20 13:47:22 +03:00
Avatar::paintEvent(QPaintEvent *)
2017-04-06 02:06:42 +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);
rounded_ ?
painter.drawEllipse(r.center(), hs, hs) :
painter.drawRoundedRect( r,
AVATAR_RECT_ROUND,
AVATAR_RECT_ROUND);
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;
rounded_ ?
ppath.addEllipse(width() / 2 - hs, height() / 2 - hs, size_, size_) :
ppath.addRoundedRect( r,
AVATAR_RECT_ROUND,
AVATAR_RECT_ROUND);
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
}