diff --git a/components/badge.cpp b/components/badge.cpp index cd8e875..2d4c4a7 100644 --- a/components/badge.cpp +++ b/components/badge.cpp @@ -1,10 +1,173 @@ #include "badge.h" +#include +#include +#include "badge_p.h" +#include "lib/style.h" + +BadgePrivate::BadgePrivate(Badge *q) + : q_ptr(q), + padding(10) +{ +} + +BadgePrivate::~BadgePrivate() +{ +} + +void BadgePrivate::init() +{ + Q_Q(Badge); + + q->setAttribute(Qt::WA_TransparentForMouseEvents); + + QFont font(q->font()); + font.setPointSizeF(10); + font.setStyleName("Medium"); + q->setFont(font); + + q->setText("+1"); +} Badge::Badge(QWidget *parent) - : QWidget(parent) + : QWidget(parent), + d_ptr(new BadgePrivate(this)) { + d_func()->init(); } Badge::~Badge() { } + +void Badge::setUseThemeColors(bool value) +{ + Q_D(Badge); + + d->useThemeColors = value; +} + +bool Badge::useThemeColors() const +{ + Q_D(const Badge); + + return d->useThemeColors; +} + +void Badge::setTextColor(const QColor &color) +{ + Q_D(Badge); + + d->textColor = color; + setUseThemeColors(false); +} + +QColor Badge::textColor() const +{ + Q_D(const Badge); + + if (d->useThemeColors || !d->textColor.isValid()) { + return Style::instance().themeColor("canvas"); + } else { + return d->textColor; + } +} + +void Badge::setBackgroundColor(const QColor &color) +{ + Q_D(Badge); + + d->backgroundColor = color; + setUseThemeColors(false); +} + +QColor Badge::backgroundColor() const +{ + Q_D(const Badge); + + if (d->useThemeColors || !d->textColor.isValid()) { + return Style::instance().themeColor("accent1"); + } else { + return d->backgroundColor; + } +} + +QSize Badge::sizeHint() const +{ + Q_D(const Badge); + + const int s = getDiameter(); + return QSize(s, s); +} + +void Badge::setIcon(const QIcon &icon) +{ + Q_D(Badge); + + d->icon = icon; + update(); +} + +void Badge::setText(const QString &text) +{ + Q_D(Badge); + + d->text = text; + + if (!d->icon.isNull()) { + d->icon = QIcon(); + } + + QFontMetrics fm = fontMetrics(); + d->size = fm.size(Qt::TextShowMnemonic, text); + + update(); +} + +void Badge::paintEvent(QPaintEvent *event) +{ + Q_UNUSED(event) + + Q_D(Badge); + + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + + QBrush brush; + brush.setStyle(Qt::SolidPattern); + brush.setColor(backgroundColor()); + painter.setBrush(brush); + painter.setPen(Qt::NoPen); + + const int s = getDiameter(); + + QRectF r(0, 0, s, s); + r.translate(QPointF((width()-s), (height()-s))/2); + + if (d->icon.isNull()) + { + painter.drawEllipse(r.translated(0, 0.5)); + + painter.setPen(textColor()); + painter.setBrush(Qt::NoBrush); + painter.drawText(r, Qt::AlignCenter, d->text); + } + else + { + painter.drawEllipse(r); + + QRectF q(0, 0, 16, 16); + q.moveCenter(r.center()); + d->icon.paint(&painter, q.toRect(), Qt::AlignCenter); + } +} + +int Badge::getDiameter() const +{ + Q_D(const Badge); + + if (d->icon.isNull()) { + return qMax(d->size.width(), d->size.height()) + d->padding; + } else { + return 24; + } +} diff --git a/components/badge.h b/components/badge.h index bce54b1..2223147 100644 --- a/components/badge.h +++ b/components/badge.h @@ -3,17 +3,42 @@ #include +class BadgePrivate; + class Badge : public QWidget { Q_OBJECT + Q_PROPERTY(QColor textColor WRITE setTextColor READ textColor) + Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor) + public: explicit Badge(QWidget *parent = 0); ~Badge(); + 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 setIcon(const QIcon &icon); + void setText(const QString &text); + +protected: + void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + int getDiameter() const; + + const QScopedPointer d_ptr; + private: Q_DISABLE_COPY(Badge) - //Q_DECLARE_PRIVATE(Badge) + Q_DECLARE_PRIVATE(Badge) }; #endif // BADGE_H diff --git a/components/badge_p.h b/components/badge_p.h new file mode 100644 index 0000000..266f83f --- /dev/null +++ b/components/badge_p.h @@ -0,0 +1,29 @@ +#ifndef BADGE_P_H +#define BADGE_P_H + +#include + +class Badge; + +class BadgePrivate +{ + Q_DISABLE_COPY(BadgePrivate) + Q_DECLARE_PUBLIC(Badge) + +public: + BadgePrivate(Badge *q); + ~BadgePrivate(); + + void init(); + + Badge *const q_ptr; + QSize size; + int padding; + QString text; + QIcon icon; + bool useThemeColors; + QColor textColor; + QColor backgroundColor; +}; + +#endif // BADGE_P_H diff --git a/qt-material-widgets.pro b/qt-material-widgets.pro index 3dbedcd..605532c 100644 --- a/qt-material-widgets.pro +++ b/qt-material-widgets.pro @@ -133,7 +133,8 @@ HEADERS += mainwindow.h \ components/snackbar.h \ components/snackbar_p.h \ components/textfield_p.h \ - components/textfield_internal.h + components/textfield_internal.h \ + components/badge_p.h RESOURCES += \ resources.qrc