173 lines
3.2 KiB
C++
Executable File
173 lines
3.2 KiB
C++
Executable File
|
|
|
|
#include "lts_notification_ctl.h"
|
|
|
|
#include <QToolButton>
|
|
#include <QImage>
|
|
#include <QPainter>
|
|
#include <QIcon>
|
|
#include <QColor>
|
|
#include <QDebug>
|
|
|
|
|
|
namespace LtsNotification
|
|
{
|
|
|
|
LtsNotificationController::LtsNotificationController(const QString &identifier)
|
|
: QObject()
|
|
, m_count(0)
|
|
, m_identifier(QStringLiteral("LtsNotification%1").arg(identifier))
|
|
{}
|
|
|
|
|
|
LtsNotificationController::~LtsNotificationController()
|
|
{
|
|
qDeleteAll(m_notiList);
|
|
}
|
|
|
|
|
|
void LtsNotificationController::setIcon(const QIcon& icon)
|
|
{
|
|
m_icon = icon;
|
|
|
|
setAllIcons();
|
|
}
|
|
|
|
|
|
void LtsNotificationController::setCount(const int count)
|
|
{
|
|
m_count = count;
|
|
|
|
setAllIcons();
|
|
}
|
|
|
|
|
|
|
|
void LtsNotificationController::setBackground(const int r, const int g, const int b)
|
|
{
|
|
|
|
for (QToolButton *tb : qAsConst(m_notiList))
|
|
{
|
|
|
|
QString stylesheet;
|
|
|
|
if(r == -1 && g == -1 && b == -1)
|
|
stylesheet = QStringLiteral("");
|
|
else
|
|
stylesheet = QStringLiteral("QToolButton {background-color: rgb(") +
|
|
QString::number(r) + QStringLiteral(",") +
|
|
QString::number(g) + QStringLiteral(",") +
|
|
QString::number(b) + QStringLiteral(");}");
|
|
|
|
tb->setStyleSheet(stylesheet);
|
|
}
|
|
}
|
|
|
|
void LtsNotificationController::setEnabled(bool enable)
|
|
{
|
|
m_enable = enable;
|
|
|
|
setAllIcons();
|
|
}
|
|
|
|
|
|
void LtsNotificationController::setAction(const std::function<void(void)> &action)
|
|
{
|
|
m_action = action;
|
|
}
|
|
|
|
|
|
void LtsNotificationController::setToolTip(const QString &toolTip)
|
|
{
|
|
m_toolTip = toolTip;
|
|
|
|
for (QToolButton *tb : qAsConst(m_notiList))
|
|
{
|
|
tb->setToolTip(toolTip);
|
|
tb->setIconSize(QSize(30,30));
|
|
}
|
|
}
|
|
|
|
|
|
void LtsNotificationController::setAllIcons()
|
|
{
|
|
if (m_icon.isNull())
|
|
return;
|
|
|
|
|
|
QIcon icon = modifyIcon(m_icon);
|
|
|
|
for (QToolButton *tb : qAsConst(m_notiList))
|
|
{
|
|
tb->setIcon(icon);
|
|
tb->setIconSize(QSize(30,30));
|
|
tb->setEnabled(m_enable);
|
|
}
|
|
}
|
|
|
|
|
|
#define SIZE 40
|
|
|
|
QIcon LtsNotificationController::modifyIcon(const QIcon &icon)
|
|
{
|
|
|
|
|
|
|
|
if (m_count < 1)
|
|
return icon;
|
|
|
|
QPixmap iconImage(icon.pixmap(SIZE * 8 / 10, SIZE * 8 / 10));
|
|
QPainter painter(&iconImage);
|
|
|
|
painter.save();
|
|
painter.setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform);
|
|
|
|
QFont f;
|
|
f.setFamily(QStringLiteral("Monospace"));
|
|
f.setPointSize(16);
|
|
f.setBold(true);
|
|
painter.setFont(f);
|
|
|
|
QColor color(Qt::red);
|
|
painter.setBrush(color);
|
|
painter.setPen(QPen(color, 3.1));
|
|
painter.drawText(QRect(0, 0, 30, 30), QString::number(m_count));
|
|
painter.restore();
|
|
|
|
return iconImage;
|
|
}
|
|
|
|
|
|
QToolButton *LtsNotificationController::createWidget()
|
|
{
|
|
QToolButton *b = new QToolButton();
|
|
b->setToolButtonStyle(Qt::ToolButtonIconOnly);
|
|
b->setObjectName(m_identifier);
|
|
|
|
|
|
|
|
b->setMinimumSize(SIZE, SIZE);
|
|
b->setMaximumSize(SIZE, SIZE);
|
|
|
|
connect(b, &QToolButton::destroyed,
|
|
this, &LtsNotificationController::slotNotiWidgetDestroyed);
|
|
connect(b, &QToolButton::clicked,
|
|
this, [this](){ if (m_action) m_action(); });
|
|
|
|
if (!m_icon.isNull())
|
|
b->setIcon(modifyIcon(m_icon));
|
|
|
|
b->setToolTip(m_toolTip);
|
|
|
|
m_notiList.append(b);
|
|
return b;
|
|
}
|
|
|
|
|
|
void LtsNotificationController::slotNotiWidgetDestroyed(QObject *obj)
|
|
{
|
|
m_notiList.removeAll(static_cast<QToolButton*>(obj));
|
|
}
|
|
|
|
}
|