123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- #include "qtmaterialbadge.h"
- #include "qtmaterialbadge_p.h"
- #include <QPainter>
- #include "lib/qtmaterialstyle.h"
- /*!
- * \class QtMaterialBadgePrivate
- * \internal
- */
- /*!
- * \internal
- */
- QtMaterialBadgePrivate::QtMaterialBadgePrivate(QtMaterialBadge *q)
- : q_ptr(q)
- {
- }
- /*!
- * \internal
- */
- QtMaterialBadgePrivate::~QtMaterialBadgePrivate()
- {
- }
- /*!
- * \internal
- */
- void QtMaterialBadgePrivate::init()
- {
- Q_Q(QtMaterialBadge);
- x = 0;
- y = 0;
- padding = 10;
- useThemeColors = true;
- q->setAttribute(Qt::WA_TransparentForMouseEvents);
- QFont font(q->font());
- font.setPointSizeF(10);
- font.setStyleName("Bold");
- q->setFont(font);
- q->setText("+1");
- }
- /*!
- * \class QtMaterialBadge
- */
- QtMaterialBadge::QtMaterialBadge(QWidget *parent)
- : QtMaterialOverlayWidget(parent),
- d_ptr(new QtMaterialBadgePrivate(this))
- {
- d_func()->init();
- }
- QtMaterialBadge::QtMaterialBadge(const QIcon &icon, QWidget *parent)
- : QtMaterialOverlayWidget(parent),
- d_ptr(new QtMaterialBadgePrivate(this))
- {
- d_func()->init();
- setIcon(icon);
- }
- QtMaterialBadge::QtMaterialBadge(const QString &text, QWidget *parent)
- : QtMaterialOverlayWidget(parent),
- d_ptr(new QtMaterialBadgePrivate(this))
- {
- d_func()->init();
- setText(text);
- }
- QtMaterialBadge::~QtMaterialBadge()
- {
- }
- void QtMaterialBadge::setUseThemeColors(bool value)
- {
- Q_D(QtMaterialBadge);
- if (d->useThemeColors == value) {
- return;
- }
- d->useThemeColors = value;
- update();
- }
- bool QtMaterialBadge::useThemeColors() const
- {
- Q_D(const QtMaterialBadge);
- return d->useThemeColors;
- }
- void QtMaterialBadge::setTextColor(const QColor &color)
- {
- Q_D(QtMaterialBadge);
- d->textColor = color;
- MATERIAL_DISABLE_THEME_COLORS
- update();
- }
- QColor QtMaterialBadge::textColor() const
- {
- Q_D(const QtMaterialBadge);
- if (d->useThemeColors || !d->textColor.isValid()) {
- return QtMaterialStyle::instance().themeColor("canvas");
- } else {
- return d->textColor;
- }
- }
- void QtMaterialBadge::setBackgroundColor(const QColor &color)
- {
- Q_D(QtMaterialBadge);
- d->backgroundColor = color;
- MATERIAL_DISABLE_THEME_COLORS
- update();
- }
- QColor QtMaterialBadge::backgroundColor() const
- {
- Q_D(const QtMaterialBadge);
- if (d->useThemeColors || !d->backgroundColor.isValid()) {
- return QtMaterialStyle::instance().themeColor("accent1");
- } else {
- return d->backgroundColor;
- }
- }
- void QtMaterialBadge::setRelativePosition(const QPointF &pos)
- {
- setRelativePosition(pos.x(), pos.y());
- }
- void QtMaterialBadge::setRelativePosition(qreal x, qreal y)
- {
- Q_D(QtMaterialBadge);
- d->x = x;
- d->y = y;
- update();
- }
- QPointF QtMaterialBadge::relativePosition() const
- {
- Q_D(const QtMaterialBadge);
- return QPointF(d->x, d->y);
- }
- void QtMaterialBadge::setRelativeXPosition(qreal x)
- {
- Q_D(QtMaterialBadge);
- d->x = x;
- update();
- }
- qreal QtMaterialBadge::relativeXPosition() const
- {
- Q_D(const QtMaterialBadge);
- return d->x;
- }
- void QtMaterialBadge::setRelativeYPosition(qreal y)
- {
- Q_D(QtMaterialBadge);
- d->y = y;
- update();
- }
- qreal QtMaterialBadge::relativeYPosition() const
- {
- Q_D(const QtMaterialBadge);
- return d->y;
- }
- /*!
- * \reimp
- */
- QSize QtMaterialBadge::sizeHint() const
- {
- const int s = getDiameter();
- return QSize(s+4, s+4);
- }
- void QtMaterialBadge::setIcon(const QIcon &icon)
- {
- Q_D(QtMaterialBadge);
- d->icon = icon;
- update();
- }
- QIcon QtMaterialBadge::icon() const
- {
- Q_D(const QtMaterialBadge);
- return d->icon;
- }
- void QtMaterialBadge::setText(const QString &text)
- {
- Q_D(QtMaterialBadge);
- d->text = text;
- if (!d->icon.isNull()) {
- d->icon = QIcon();
- }
- d->size = fontMetrics().size(Qt::TextShowMnemonic, text);
- update();
- }
- QString QtMaterialBadge::text() const
- {
- Q_D(const QtMaterialBadge);
- return d->text;
- }
- /*!
- * \reimp
- */
- void QtMaterialBadge::paintEvent(QPaintEvent *event)
- {
- Q_UNUSED(event)
- Q_D(QtMaterialBadge);
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing);
- painter.translate(d->x, d->y);
- QBrush brush;
- brush.setStyle(Qt::SolidPattern);
- brush.setColor(isEnabled() ? backgroundColor()
- : QtMaterialStyle::instance().themeColor("disabled"));
- 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);
- painter.setPen(textColor());
- painter.setBrush(Qt::NoBrush);
- painter.drawText(r.translated(0, -0.5), Qt::AlignCenter, d->text);
- }
- else
- {
- painter.drawEllipse(r);
- QRectF q(0, 0, 16, 16);
- q.moveCenter(r.center());
- QPixmap pixmap = icon().pixmap(16, 16);
- QPainter icon(&pixmap);
- icon.setCompositionMode(QPainter::CompositionMode_SourceIn);
- icon.fillRect(pixmap.rect(), textColor());
- painter.drawPixmap(q.toRect(), pixmap);
- }
- }
- int QtMaterialBadge::getDiameter() const
- {
- Q_D(const QtMaterialBadge);
- if (d->icon.isNull()) {
- return qMax(d->size.width(), d->size.height()) + d->padding;
- } else {
- return 24;
- }
- }
|