diff --git a/components/avatar.cpp b/components/avatar.cpp index 495bcf4..92f6240 100644 --- a/components/avatar.cpp +++ b/components/avatar.cpp @@ -1,28 +1,39 @@ #include #include +#include #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); } diff --git a/components/avatar.h b/components/avatar.h index 1ce6fbb..82b8f09 100644 --- a/components/avatar.h +++ b/components/avatar.h @@ -3,18 +3,30 @@ #include +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 d_ptr; + +private: + Q_DISABLE_COPY(Avatar) + Q_DECLARE_PRIVATE(Avatar) }; #endif // AVATAR_H diff --git a/components/avatar_p.h b/components/avatar_p.h new file mode 100644 index 0000000..7b5102a --- /dev/null +++ b/components/avatar_p.h @@ -0,0 +1,22 @@ +#ifndef AVATAR_P_H +#define AVATAR_P_H + +#include + +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 diff --git a/qt-material-widgets.pro b/qt-material-widgets.pro index 8ab6c08..d9dd055 100644 --- a/qt-material-widgets.pro +++ b/qt-material-widgets.pro @@ -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 diff --git a/uxceo-128.jpg b/uxceo-128.jpg new file mode 100644 index 0000000..fdf9625 Binary files /dev/null and b/uxceo-128.jpg differ