1 Star 0 Fork 0

tongy boys/material-pri

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
qtmaterialbadge.cpp 4.85 KB
一键复制 编辑 原始数据 按行查看 历史
#include "qtmaterialbadge.h"
#include "lib/qtmaterialstyle.h"
#include "qtmaterialbadge_p.h"
#include <QPainter>
/*!
* \class QtMaterialBadgePrivate
* \internal
*/
/*!
* \internal
*/
QtMaterialBadgePrivate::QtMaterialBadgePrivate(QtMaterialBadge *q)
: q_ptr(q)
{
}
/*!
* \internal
*/
QtMaterialBadgePrivate::~QtMaterialBadgePrivate()
{
}
/*!
* \internal
*/
void QtMaterialBadgePrivate::init()
{
Q_Q(QtMaterialBadge);
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;
d->useThemeColors = false;
update();
}
QColor QtMaterialBadge::textColor() const
{
Q_D(const QtMaterialBadge);
if (d->useThemeColors || !d->textColor.isValid())
{
return QtMaterialStyle::instance().themeColor(Material::ColorThemeCanvas);
}
return d->textColor;
}
void QtMaterialBadge::setBackgroundColor(const QColor &color)
{
Q_D(QtMaterialBadge);
d->backgroundColor = color;
d->useThemeColors = false;
update();
}
QColor QtMaterialBadge::backgroundColor() const
{
Q_D(const QtMaterialBadge);
if (d->useThemeColors || !d->backgroundColor.isValid())
{
return QtMaterialStyle::instance().themeColor(Material::ColorThemeSecondaryMain);
}
return d->backgroundColor;
}
void QtMaterialBadge::setRelativePosition(const QPointF &pos)
{
Q_D(QtMaterialBadge);
d->relativePosition = pos;
update();
}
void QtMaterialBadge::setRelativePosition(qreal x, qreal y)
{
setRelativePosition(QPointF(x, y));
}
const QPointF &QtMaterialBadge::relativePosition() const
{
Q_D(const QtMaterialBadge);
return d->relativePosition;
}
/*!
* \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();
}
const 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();
}
const 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->relativePosition);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(isEnabled() ? backgroundColor() : QtMaterialStyle::instance().themeColor(Material::ColorThemeDisabled1));
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;
}
return 24;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/tongynj/material-pri.git
git@gitee.com:tongynj/material-pri.git
tongynj
material-pri
material-pri
master

搜索帮助