matrixion/src/ui/Badge.cpp

223 lines
3.9 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-04-06 02:06:42 +03:00
#include <QPainter>
#include "Badge.h"
Badge::Badge(QWidget *parent)
2017-08-20 13:47:22 +03:00
: OverlayWidget(parent)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
init();
2017-04-06 02:06:42 +03:00
}
Badge::Badge(const QIcon &icon, QWidget *parent)
2017-08-20 13:47:22 +03:00
: OverlayWidget(parent)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
init();
setIcon(icon);
2017-04-06 02:06:42 +03:00
}
Badge::Badge(const QString &text, QWidget *parent)
2017-08-20 13:47:22 +03:00
: OverlayWidget(parent)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
init();
setText(text);
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::init()
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
x_ = 0;
y_ = 0;
// TODO: Make padding configurable.
padding_ = 5;
diameter_ = 24;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
setAttribute(Qt::WA_TransparentForMouseEvents);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
QFont _font(font());
_font.setPointSizeF(7.5);
_font.setStyleName("Bold");
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
setFont(_font);
setText("");
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
QString
Badge::text() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return text_;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
QIcon
Badge::icon() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return icon_;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
QSize
Badge::sizeHint() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
const int d = diameter();
return QSize(d + 4, d + 4);
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
qreal
Badge::relativeYPosition() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return y_;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
qreal
Badge::relativeXPosition() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return x_;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
QPointF
Badge::relativePosition() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return QPointF(x_, y_);
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
QColor
Badge::backgroundColor() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
if (!background_color_.isValid())
return QColor("black");
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
return background_color_;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
QColor
Badge::textColor() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
if (!text_color_.isValid())
return QColor("white");
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
return text_color_;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::setTextColor(const QColor &color)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
text_color_ = color;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::setBackgroundColor(const QColor &color)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
background_color_ = color;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::setRelativePosition(const QPointF &pos)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
setRelativePosition(pos.x(), pos.y());
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::setRelativePosition(qreal x, qreal y)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
x_ = x;
y_ = y;
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::setRelativeXPosition(qreal x)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
x_ = x;
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::setRelativeYPosition(qreal y)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
y_ = y;
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::setIcon(const QIcon &icon)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
icon_ = icon;
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::setText(const QString &text)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
text_ = text;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
if (!icon_.isNull())
icon_ = QIcon();
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
size_ = fontMetrics().size(Qt::TextShowMnemonic, text);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
Badge::setDiameter(int diameter)
{
2017-09-10 12:59:21 +03:00
if (diameter > 0) {
diameter_ = diameter;
update();
}
}
2017-08-20 13:47:22 +03:00
void
Badge::paintEvent(QPaintEvent *)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(x_, y_);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
QBrush brush;
brush.setStyle(Qt::SolidPattern);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
const int d = diameter();
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
QRectF r(0, 0, d, d);
r.translate(QPointF((width() - d), (height() - d)) / 2);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
if (icon_.isNull()) {
QPen pen;
// TODO: Make badge width configurable.
pen.setWidth(1);
pen.setColor(textColor());
2017-09-10 12:59:21 +03:00
painter.setPen(pen);
painter.drawEllipse(r);
2017-09-10 12:59:21 +03:00
painter.setPen(textColor());
painter.setBrush(Qt::NoBrush);
painter.drawText(r.translated(0, -0.5), Qt::AlignCenter, 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);
}
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
int
Badge::diameter() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
if (icon_.isNull()) {
return qMax(size_.width(), size_.height()) + padding_;
}
2017-09-10 12:59:21 +03:00
return diameter_;
2017-04-06 02:06:42 +03:00
}