implement basic Avatar

This commit is contained in:
laserpants 2016-06-17 16:09:19 +03:00
parent 0581bb0313
commit d42fa35c42
5 changed files with 87 additions and 16 deletions

View File

@ -1,28 +1,39 @@
#include <QWidget>
#include <QPainter>
#include <QIcon>
#include "avatar.h"
#include "avatar_p.h"
AvatarPrivate::AvatarPrivate(Avatar *q)
: q_ptr(q),
size(40)
{
}
void AvatarPrivate::init()
{
Q_Q(Avatar);
QFont font(q->font());
font.setPointSizeF(16);
q->setFont(font);
}
Avatar::Avatar(QWidget *parent)
: QWidget(parent)
: QWidget(parent),
d_ptr(new AvatarPrivate(this))
{
d_func()->init();
}
Avatar::~Avatar()
{
}
void Avatar::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
}
void Avatar::mouseReleaseEvent(QMouseEvent *event)
{
QWidget::mouseReleaseEvent(event);
}
void Avatar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.drawRect(rect().adjusted(0, 0, -1, -1));
@ -31,12 +42,37 @@ void Avatar::paintEvent(QPaintEvent *event)
QBrush brush;
brush.setStyle(Qt::SolidPattern);
painter.setPen(Qt::NoPen);
painter.setBrush(brush);
QRect r = rect();
painter.drawEllipse(r.center(), 20, 20);
QWidget::paintEvent(event);
//painter.setPen(textColor());
painter.setPen(Qt::white);
painter.setBrush(Qt::NoBrush);
painter.drawText(r.translated(0, -1), Qt::AlignCenter, "A");
return;//
QIcon icon("../qt-material-widgets/ic_message_white_24px.svg");
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);
}

View File

@ -3,18 +3,30 @@
#include <QWidget>
class AvatarPrivate;
class Avatar : public QWidget
{
Q_OBJECT
enum AvatarType {
ImageAvatar,
IconAvatar,
LetterAvatar
};
public:
explicit Avatar(QWidget *parent = 0);
~Avatar();
protected:
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
const QScopedPointer<AvatarPrivate> d_ptr;
private:
Q_DISABLE_COPY(Avatar)
Q_DECLARE_PRIVATE(Avatar)
};
#endif // AVATAR_H

22
components/avatar_p.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef AVATAR_P_H
#define AVATAR_P_H
#include <QObject>
class Avatar;
class AvatarPrivate
{
Q_DISABLE_COPY(AvatarPrivate)
Q_DECLARE_PUBLIC(Avatar)
public:
AvatarPrivate(Avatar *q);
void init();
Avatar *const q_ptr;
int size;
};
#endif // AVATAR_P_H

View File

@ -136,7 +136,8 @@ HEADERS += mainwindow.h \
components/textfield_p.h \
components/textfield_internal.h \
components/badge_p.h \
components/drawer.h
components/drawer.h \
components/avatar_p.h
RESOURCES += \
resources.qrc

BIN
uxceo-128.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.0 KiB