implement Badge
This commit is contained in:
parent
1390661453
commit
58a4ef9fd4
|
@ -1,10 +1,173 @@
|
||||||
#include "badge.h"
|
#include "badge.h"
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QIcon>
|
||||||
|
#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)
|
Badge::Badge(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent),
|
||||||
|
d_ptr(new BadgePrivate(this))
|
||||||
{
|
{
|
||||||
|
d_func()->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
Badge::~Badge()
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,17 +3,42 @@
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
|
class BadgePrivate;
|
||||||
|
|
||||||
class Badge : public QWidget
|
class Badge : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY(QColor textColor WRITE setTextColor READ textColor)
|
||||||
|
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Badge(QWidget *parent = 0);
|
explicit Badge(QWidget *parent = 0);
|
||||||
~Badge();
|
~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<BadgePrivate> d_ptr;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Q_DISABLE_COPY(Badge)
|
Q_DISABLE_COPY(Badge)
|
||||||
//Q_DECLARE_PRIVATE(Badge)
|
Q_DECLARE_PRIVATE(Badge)
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BADGE_H
|
#endif // BADGE_H
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
#ifndef BADGE_P_H
|
||||||
|
#define BADGE_P_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
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
|
|
@ -133,7 +133,8 @@ HEADERS += mainwindow.h \
|
||||||
components/snackbar.h \
|
components/snackbar.h \
|
||||||
components/snackbar_p.h \
|
components/snackbar_p.h \
|
||||||
components/textfield_p.h \
|
components/textfield_p.h \
|
||||||
components/textfield_internal.h
|
components/textfield_internal.h \
|
||||||
|
components/badge_p.h
|
||||||
|
|
||||||
RESOURCES += \
|
RESOURCES += \
|
||||||
resources.qrc
|
resources.qrc
|
||||||
|
|
Loading…
Reference in New Issue