matrixion/src/ui/Avatar.cc

154 lines
2.6 KiB
C++
Raw Normal View History

2017-04-06 02:06:42 +03:00
#include <QIcon>
#include <QPainter>
#include <QWidget>
#include "Avatar.h"
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_ = QChar('A');
QFont _font(font());
_font.setPointSizeF(ui::FontSize);
setFont(_font);
QSizePolicy policy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
setSizePolicy(policy);
}
Avatar::~Avatar()
{
}
2017-08-20 13:47:22 +03:00
QColor
Avatar::textColor() const
2017-04-06 02:06:42 +03:00
{
if (!text_color_.isValid())
return QColor("black");
return text_color_;
}
2017-08-20 13:47:22 +03:00
QColor
Avatar::backgroundColor() const
2017-04-06 02:06:42 +03:00
{
if (!text_color_.isValid())
return QColor("white");
return background_color_;
}
2017-08-20 13:47:22 +03:00
int
Avatar::size() const
2017-04-06 02:06:42 +03:00
{
return size_;
}
2017-08-20 13:47:22 +03:00
QSize
Avatar::sizeHint() const
2017-04-06 02:06:42 +03:00
{
return QSize(size_ + 2, size_ + 2);
}
2017-08-20 13:47:22 +03:00
void
Avatar::setTextColor(const QColor &color)
2017-04-06 02:06:42 +03:00
{
text_color_ = color;
}
2017-08-20 13:47:22 +03:00
void
Avatar::setBackgroundColor(const QColor &color)
2017-04-06 02:06:42 +03:00
{
background_color_ = color;
}
2017-08-20 13:47:22 +03:00
void
Avatar::setSize(int size)
2017-04-06 02:06:42 +03:00
{
size_ = size;
if (!image_.isNull()) {
2017-08-20 13:47:22 +03:00
pixmap_ =
QPixmap::fromImage(image_.scaled(size_, size_, Qt::KeepAspectRatio, Qt::SmoothTransformation));
2017-04-06 02:06:42 +03:00
}
QFont _font(font());
_font.setPointSizeF(size_ * (ui::FontSize) / 40);
setFont(_font);
update();
}
2017-08-20 13:47:22 +03:00
void
Avatar::setLetter(const QChar &letter)
2017-04-06 02:06:42 +03:00
{
letter_ = letter;
type_ = ui::AvatarType::Letter;
update();
}
2017-08-20 13:47:22 +03:00
void
Avatar::setImage(const QImage &image)
2017-04-06 02:06:42 +03:00
{
image_ = image;
type_ = ui::AvatarType::Image;
2017-08-20 13:47:22 +03:00
pixmap_ = QPixmap::fromImage(image_.scaled(size_, size_, Qt::KeepAspectRatio, Qt::SmoothTransformation));
2017-04-06 02:06:42 +03:00
update();
}
2017-08-20 13:47:22 +03:00
void
Avatar::setIcon(const QIcon &icon)
2017-04-06 02:06:42 +03:00
{
icon_ = icon;
type_ = ui::AvatarType::Icon;
update();
}
2017-08-20 13:47:22 +03:00
void
Avatar::paintEvent(QPaintEvent *)
2017-04-06 02:06:42 +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);
painter.drawEllipse(r.center(), hs, hs);
}
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;
ppath.addEllipse(width() / 2 - hs, height() / 2 - hs, size_, size_);
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;
}
}