qt-material-widgets/components/qtmaterialbadge.cpp

300 lines
4.7 KiB
C++
Raw Normal View History

2017-09-28 22:01:01 +00:00
#include "qtmaterialbadge.h"
#include "qtmaterialbadge_p.h"
#include <QPainter>
#include "lib/qtmaterialstyle.h"
2022-02-17 02:06:23 +00:00
namespace md
{
2017-09-28 22:01:01 +00:00
/*!
* \class QtMaterialBadgePrivate
* \internal
*/
/*!
* \internal
*/
2022-02-17 02:06:23 +00:00
BadgePrivate::BadgePrivate(Badge *q)
2017-09-28 22:01:01 +00:00
: q_ptr(q)
{
}
/*!
* \internal
*/
2022-02-17 02:06:23 +00:00
BadgePrivate::~BadgePrivate()
2017-09-28 22:01:01 +00:00
{
}
/*!
* \internal
*/
2022-02-17 02:06:23 +00:00
void BadgePrivate::init()
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_Q(Badge);
2017-09-28 22:01:01 +00:00
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
*/
2022-02-17 02:06:23 +00:00
Badge::Badge(QWidget *parent)
: OverlayWidget(parent),
d_ptr(new BadgePrivate(this))
2017-09-28 22:01:01 +00:00
{
d_func()->init();
}
2022-02-17 02:06:23 +00:00
Badge::Badge(const QIcon &icon, QWidget *parent)
: OverlayWidget(parent),
d_ptr(new BadgePrivate(this))
2017-09-28 22:01:01 +00:00
{
d_func()->init();
setIcon(icon);
}
2022-02-17 02:06:23 +00:00
Badge::Badge(const QString &text, QWidget *parent)
: OverlayWidget(parent),
d_ptr(new BadgePrivate(this))
2017-09-28 22:01:01 +00:00
{
d_func()->init();
setText(text);
}
2022-02-17 02:06:23 +00:00
Badge::~Badge()
2017-09-28 22:01:01 +00:00
{
}
2022-02-17 02:06:23 +00:00
void Badge::setUseThemeColors(bool value)
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(Badge);
2017-09-28 22:01:01 +00:00
if (d->useThemeColors == value) {
return;
}
d->useThemeColors = value;
update();
}
2022-02-17 02:06:23 +00:00
bool Badge::useThemeColors() const
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(const Badge);
2017-09-28 22:01:01 +00:00
return d->useThemeColors;
}
2022-02-17 02:06:23 +00:00
void Badge::setTextColor(const QColor &color)
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(Badge);
2017-09-28 22:01:01 +00:00
d->textColor = color;
MATERIAL_DISABLE_THEME_COLORS
2017-09-28 22:10:11 +00:00
update();
2017-09-28 22:01:01 +00:00
}
2022-02-17 02:06:23 +00:00
QColor Badge::textColor() const
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(const Badge);
2017-09-28 22:01:01 +00:00
if (d->useThemeColors || !d->textColor.isValid()) {
2022-02-17 02:06:23 +00:00
return Style::instance().themeColor("canvas");
2017-09-28 22:01:01 +00:00
} else {
return d->textColor;
}
}
2022-02-17 02:06:23 +00:00
void Badge::setBackgroundColor(const QColor &color)
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(Badge);
2017-09-28 22:01:01 +00:00
d->backgroundColor = color;
MATERIAL_DISABLE_THEME_COLORS
2017-09-28 22:10:11 +00:00
update();
2017-09-28 22:01:01 +00:00
}
2022-02-17 02:06:23 +00:00
QColor Badge::backgroundColor() const
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(const Badge);
2017-09-28 22:01:01 +00:00
if (d->useThemeColors || !d->backgroundColor.isValid()) {
2022-02-17 02:06:23 +00:00
return Style::instance().themeColor("accent1");
2017-09-28 22:01:01 +00:00
} else {
return d->backgroundColor;
}
}
2022-02-17 02:06:23 +00:00
void Badge::setRelativePosition(const QPointF &pos)
2017-09-28 22:01:01 +00:00
{
setRelativePosition(pos.x(), pos.y());
}
2022-02-17 02:06:23 +00:00
void Badge::setRelativePosition(qreal x, qreal y)
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(Badge);
2017-09-28 22:01:01 +00:00
d->x = x;
d->y = y;
update();
}
2022-02-17 02:06:23 +00:00
QPointF Badge::relativePosition() const
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(const Badge);
2017-09-28 22:01:01 +00:00
return QPointF(d->x, d->y);
}
2022-02-17 02:06:23 +00:00
void Badge::setRelativeXPosition(qreal x)
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(Badge);
2017-09-28 22:01:01 +00:00
d->x = x;
update();
}
2022-02-17 02:06:23 +00:00
qreal Badge::relativeXPosition() const
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(const Badge);
2017-09-28 22:01:01 +00:00
return d->x;
}
2022-02-17 02:06:23 +00:00
void Badge::setRelativeYPosition(qreal y)
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(Badge);
2017-09-28 22:01:01 +00:00
d->y = y;
update();
}
2022-02-17 02:06:23 +00:00
qreal Badge::relativeYPosition() const
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(const Badge);
2017-09-28 22:01:01 +00:00
return d->y;
}
/*!
* \reimp
*/
2022-02-17 02:06:23 +00:00
QSize Badge::sizeHint() const
2017-09-28 22:01:01 +00:00
{
const int s = getDiameter();
return QSize(s+4, s+4);
}
2022-02-17 02:06:23 +00:00
void Badge::setIcon(const QIcon &icon)
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(Badge);
2017-09-28 22:01:01 +00:00
d->icon = icon;
update();
}
2022-02-17 02:06:23 +00:00
QIcon Badge::icon() const
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(const Badge);
2017-09-28 22:01:01 +00:00
return d->icon;
}
2022-02-17 02:06:23 +00:00
void Badge::setText(const QString &text)
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(Badge);
2017-09-28 22:01:01 +00:00
d->text = text;
if (!d->icon.isNull()) {
d->icon = QIcon();
}
d->size = fontMetrics().size(Qt::TextShowMnemonic, text);
update();
}
2022-02-17 02:06:23 +00:00
QString Badge::text() const
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(const Badge);
2017-09-28 22:01:01 +00:00
return d->text;
}
/*!
* \reimp
*/
2022-02-17 02:06:23 +00:00
void Badge::paintEvent(QPaintEvent *event)
2017-09-28 22:01:01 +00:00
{
Q_UNUSED(event)
2022-02-17 02:06:23 +00:00
Q_D(Badge);
2017-09-28 22:01:01 +00:00
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(d->x, d->y);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(isEnabled() ? backgroundColor()
2022-02-17 02:06:23 +00:00
: Style::instance().themeColor("disabled"));
2017-09-28 22:01:01 +00:00
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);
}
}
2022-02-17 02:06:23 +00:00
int Badge::getDiameter() const
2017-09-28 22:01:01 +00:00
{
2022-02-17 02:06:23 +00:00
Q_D(const Badge);
2017-09-28 22:01:01 +00:00
if (d->icon.isNull()) {
return qMax(d->size.width(), d->size.height()) + d->padding;
} else {
return 24;
}
}
2022-02-17 02:06:23 +00:00
}