implement Avatar
This commit is contained in:
parent
d42fa35c42
commit
5bb106c643
|
@ -3,10 +3,13 @@
|
|||
#include <QIcon>
|
||||
#include "avatar.h"
|
||||
#include "avatar_p.h"
|
||||
#include "lib/style.h"
|
||||
|
||||
AvatarPrivate::AvatarPrivate(Avatar *q)
|
||||
: q_ptr(q),
|
||||
size(40)
|
||||
size(40),
|
||||
type(Material::LetterAvatar),
|
||||
useThemeColors(true)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -17,6 +20,10 @@ void AvatarPrivate::init()
|
|||
QFont font(q->font());
|
||||
font.setPointSizeF(16);
|
||||
q->setFont(font);
|
||||
|
||||
QSizePolicy policy(QSizePolicy::MinimumExpanding,
|
||||
QSizePolicy::MinimumExpanding);
|
||||
q->setSizePolicy(policy);
|
||||
}
|
||||
|
||||
Avatar::Avatar(QWidget *parent)
|
||||
|
@ -26,53 +33,201 @@ Avatar::Avatar(QWidget *parent)
|
|||
d_func()->init();
|
||||
}
|
||||
|
||||
Avatar::Avatar(const QIcon &icon, QWidget *parent)
|
||||
: QWidget(parent),
|
||||
d_ptr(new AvatarPrivate(this))
|
||||
{
|
||||
d_func()->init();
|
||||
|
||||
setIcon(icon);
|
||||
}
|
||||
|
||||
Avatar::Avatar(const QChar &letter, QWidget *parent)
|
||||
: QWidget(parent),
|
||||
d_ptr(new AvatarPrivate(this))
|
||||
{
|
||||
d_func()->init();
|
||||
|
||||
setLetter(letter);
|
||||
}
|
||||
|
||||
Avatar::Avatar(const QImage &image, QWidget *parent)
|
||||
: QWidget(parent),
|
||||
d_ptr(new AvatarPrivate(this))
|
||||
{
|
||||
d_func()->init();
|
||||
|
||||
setImage(image);
|
||||
}
|
||||
|
||||
Avatar::~Avatar()
|
||||
{
|
||||
}
|
||||
|
||||
void Avatar::setUseThemeColors(bool value)
|
||||
{
|
||||
Q_D(Avatar);
|
||||
|
||||
d->useThemeColors = value;
|
||||
}
|
||||
|
||||
bool Avatar::useThemeColors() const
|
||||
{
|
||||
Q_D(const Avatar);
|
||||
|
||||
return d->useThemeColors;
|
||||
}
|
||||
|
||||
void Avatar::setTextColor(const QColor &color)
|
||||
{
|
||||
Q_D(Avatar);
|
||||
|
||||
d->textColor = color;
|
||||
setUseThemeColors(false);
|
||||
}
|
||||
|
||||
QColor Avatar::textColor() const
|
||||
{
|
||||
Q_D(const Avatar);
|
||||
|
||||
if (d->useThemeColors || !d->textColor.isValid()) {
|
||||
return Style::instance().themeColor("canvas");
|
||||
} else {
|
||||
return d->textColor;
|
||||
}
|
||||
}
|
||||
|
||||
void Avatar::setBackgroundColor(const QColor &color)
|
||||
{
|
||||
Q_D(Avatar);
|
||||
|
||||
d->backgroundColor = color;
|
||||
setUseThemeColors(false);
|
||||
}
|
||||
|
||||
QColor Avatar::backgroundColor() const
|
||||
{
|
||||
Q_D(const Avatar);
|
||||
|
||||
if (d->useThemeColors || !d->textColor.isValid()) {
|
||||
return Style::instance().themeColor("primary1");
|
||||
} else {
|
||||
return d->backgroundColor;
|
||||
}
|
||||
}
|
||||
|
||||
QSize Avatar::sizeHint() const
|
||||
{
|
||||
Q_D(const Avatar);
|
||||
|
||||
return QSize(d->size+2, d->size+2);
|
||||
}
|
||||
|
||||
void Avatar::setSize(int size)
|
||||
{
|
||||
Q_D(Avatar);
|
||||
|
||||
d->size = size;
|
||||
|
||||
if (!d->image.isNull()) {
|
||||
d->pixmap = QPixmap::fromImage(d->image.scaled(d->size, d->size,
|
||||
Qt::IgnoreAspectRatio,
|
||||
Qt::SmoothTransformation));
|
||||
}
|
||||
|
||||
QFont f(font());
|
||||
f.setPointSizeF(size*16/40);
|
||||
setFont(f);
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
int Avatar::size() const
|
||||
{
|
||||
Q_D(const Avatar);
|
||||
|
||||
return d->size;
|
||||
}
|
||||
|
||||
void Avatar::setLetter(const QChar &letter)
|
||||
{
|
||||
Q_D(Avatar);
|
||||
|
||||
d->letter = letter;
|
||||
d->type = Material::LetterAvatar;
|
||||
}
|
||||
|
||||
void Avatar::setImage(const QImage &image)
|
||||
{
|
||||
Q_D(Avatar);
|
||||
|
||||
d->image = image;
|
||||
d->type = Material::ImageAvatar;
|
||||
|
||||
d->pixmap = QPixmap::fromImage(image.scaled(d->size, d->size,
|
||||
Qt::IgnoreAspectRatio,
|
||||
Qt::SmoothTransformation));
|
||||
}
|
||||
|
||||
void Avatar::setIcon(const QIcon &icon)
|
||||
{
|
||||
Q_D(Avatar);
|
||||
|
||||
d->icon = icon;
|
||||
d->type = Material::IconAvatar;
|
||||
}
|
||||
|
||||
void Avatar::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
QPainter painter(this);
|
||||
Q_D(Avatar);
|
||||
|
||||
painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
||||
QPainter painter(this);
|
||||
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
QBrush brush;
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.setBrush(brush);
|
||||
|
||||
QRect r = rect();
|
||||
painter.drawEllipse(r.center(), 20, 20);
|
||||
const int hs = d->size/2;
|
||||
|
||||
//painter.setPen(textColor());
|
||||
painter.setPen(Qt::white);
|
||||
painter.setBrush(Qt::NoBrush);
|
||||
painter.drawText(r.translated(0, -1), Qt::AlignCenter, "A");
|
||||
if (Material::ImageAvatar != d->type)
|
||||
{
|
||||
QBrush brush;
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
brush.setColor(backgroundColor());
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.setBrush(brush);
|
||||
painter.drawEllipse(r.center(), hs, hs);
|
||||
}
|
||||
|
||||
return;//
|
||||
QIcon icon("../qt-material-widgets/ic_message_white_24px.svg");
|
||||
switch (d->type)
|
||||
{
|
||||
case Material::ImageAvatar:
|
||||
{
|
||||
QPainterPath path;
|
||||
path.addEllipse(width()/2-hs, height()/2-hs, d->size, d->size);
|
||||
painter.setClipPath(path);
|
||||
|
||||
icon.paint(&painter,
|
||||
QRect(width()/2-10, height()/2-10, 20, 20),
|
||||
Qt::AlignCenter,
|
||||
QIcon::Normal);
|
||||
|
||||
//
|
||||
|
||||
QImage img("../qt-material-widgets/uxceo-128.jpg");
|
||||
|
||||
QPixmap pm = QPixmap::fromImage(img.scaled(40,
|
||||
40,
|
||||
Qt::IgnoreAspectRatio,
|
||||
Qt::SmoothTransformation));
|
||||
|
||||
QPainterPath path;
|
||||
path.addEllipse(width()/2-20, height()/2-20, 40, 40);
|
||||
painter.setClipPath(path);
|
||||
|
||||
painter.drawPixmap(QRect(width()/2-20, height()/2-20, 40, 40), pm);
|
||||
painter.drawPixmap(QRect(width()/2-hs, height()/2-hs, d->size, d->size),
|
||||
d->pixmap);
|
||||
break;
|
||||
}
|
||||
case Material::IconAvatar:
|
||||
{
|
||||
d->icon.paint(&painter,
|
||||
QRect((width()-hs)/2, (height()-hs)/2, hs, hs),
|
||||
Qt::AlignCenter,
|
||||
QIcon::Normal);
|
||||
break;
|
||||
}
|
||||
case Material::LetterAvatar:
|
||||
{
|
||||
painter.setPen(textColor());
|
||||
painter.setBrush(Qt::NoBrush);
|
||||
painter.drawText(r.translated(0, -1), Qt::AlignCenter, "A");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,16 +9,34 @@ class Avatar : public QWidget
|
|||
{
|
||||
Q_OBJECT
|
||||
|
||||
enum AvatarType {
|
||||
ImageAvatar,
|
||||
IconAvatar,
|
||||
LetterAvatar
|
||||
};
|
||||
Q_PROPERTY(QColor textColor WRITE setTextColor READ textColor)
|
||||
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor)
|
||||
|
||||
public:
|
||||
explicit Avatar(QWidget *parent = 0);
|
||||
explicit Avatar(const QIcon &icon, QWidget *parent = 0);
|
||||
explicit Avatar(const QChar &letter, QWidget *parent = 0);
|
||||
explicit Avatar(const QImage &image, QWidget *parent = 0);
|
||||
~Avatar();
|
||||
|
||||
void setUseThemeColors(bool value);
|
||||
bool useThemeColors() const;
|
||||
|
||||
void setTextColor(const QColor &color);
|
||||
QColor textColor() const;
|
||||
|
||||
void setBackgroundColor(const QColor &color);
|
||||
QColor backgroundColor() const;
|
||||
|
||||
QSize sizeHint() const Q_DECL_OVERRIDE;
|
||||
|
||||
void setSize(int size);
|
||||
int size() const;
|
||||
|
||||
void setLetter(const QChar &letter);
|
||||
void setImage(const QImage &image);
|
||||
void setIcon(const QIcon &icon);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define AVATAR_P_H
|
||||
|
||||
#include <QObject>
|
||||
#include "lib/theme.h"
|
||||
|
||||
class Avatar;
|
||||
|
||||
|
@ -15,8 +16,16 @@ public:
|
|||
|
||||
void init();
|
||||
|
||||
Avatar *const q_ptr;
|
||||
int size;
|
||||
Avatar *const q_ptr;
|
||||
int size;
|
||||
Material::AvatarType type;
|
||||
QChar letter;
|
||||
QImage image;
|
||||
QIcon icon;
|
||||
QPixmap pixmap;
|
||||
bool useThemeColors;
|
||||
QColor textColor;
|
||||
QColor backgroundColor;
|
||||
};
|
||||
|
||||
#endif // AVATAR_P_H
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
|
||||
BadgePrivate::BadgePrivate(Badge *q)
|
||||
: q_ptr(q),
|
||||
padding(10)
|
||||
padding(10),
|
||||
useThemeColors(true)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <QLayout>
|
||||
#include <QEvent>
|
||||
#include <QIcon>
|
||||
#include "avatarexamples.h"
|
||||
#include "components/avatar.h"
|
||||
#include "exampleview.h"
|
||||
|
@ -13,6 +14,8 @@ AvatarExamples::AvatarExamples(QWidget *parent)
|
|||
{
|
||||
Avatar *avatar = new Avatar;
|
||||
|
||||
avatar->setLetter(QChar('M'));
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(avatar);
|
||||
|
||||
|
@ -23,7 +26,95 @@ AvatarExamples::AvatarExamples(QWidget *parent)
|
|||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
} }
|
||||
}
|
||||
{
|
||||
Avatar *avatar = new Avatar;
|
||||
|
||||
QIcon icon("../qt-material-widgets/ic_message_white_24px.svg");
|
||||
avatar->setIcon(icon);
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(avatar);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
"hello"
|
||||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
Avatar *avatar = new Avatar;
|
||||
|
||||
QImage img("../qt-material-widgets/uxceo-128.jpg");
|
||||
avatar->setImage(img);
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(avatar);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
"hello"
|
||||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
Avatar *avatar = new Avatar;
|
||||
avatar->setSize(80);
|
||||
|
||||
avatar->setLetter(QChar('M'));
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(avatar);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
"hello"
|
||||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
Avatar *avatar = new Avatar;
|
||||
avatar->setSize(60);
|
||||
|
||||
QIcon icon("../qt-material-widgets/ic_message_white_24px.svg");
|
||||
avatar->setIcon(icon);
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(avatar);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
"hello"
|
||||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
Avatar *avatar = new Avatar;
|
||||
avatar->setSize(80);
|
||||
|
||||
QImage img("../qt-material-widgets/uxceo-128.jpg");
|
||||
avatar->setImage(img);
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(avatar);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
"hello"
|
||||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
}
|
||||
|
||||
AvatarExamples::~AvatarExamples()
|
||||
{
|
||||
|
|
|
@ -8,6 +8,12 @@
|
|||
|
||||
namespace Material
|
||||
{
|
||||
enum AvatarType {
|
||||
ImageAvatar,
|
||||
IconAvatar,
|
||||
LetterAvatar
|
||||
};
|
||||
|
||||
enum RippleStyle {
|
||||
CenteredRipple,
|
||||
PositionedRipple,
|
||||
|
|
Loading…
Reference in New Issue