matrixion/src/TrayIcon.cpp

128 lines
3.1 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Nheko Contributors
2021-03-05 02:35:15 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-05-21 16:36:06 +03:00
2020-01-31 08:12:02 +03:00
#include <QAction>
#include <QApplication>
#include <QList>
2017-10-28 15:46:39 +03:00
#include <QMenu>
2020-01-31 08:12:02 +03:00
#include <QPainter>
2017-10-01 18:15:23 +03:00
#include <QTimer>
2022-01-12 21:09:46 +03:00
#include <QWindow>
2017-05-21 16:36:06 +03:00
#include "TrayIcon.h"
MsgCountComposedIcon::MsgCountComposedIcon(const QString &filename)
2017-08-20 13:47:22 +03:00
: QIconEngine()
, icon_{QIcon{filename}}
2017-05-21 16:36:06 +03:00
{
}
2017-08-20 13:47:22 +03:00
void
2017-09-10 12:59:21 +03:00
MsgCountComposedIcon::paint(QPainter *painter,
const QRect &rect,
QIcon::Mode mode,
QIcon::State state)
2017-05-21 16:36:06 +03:00
{
2021-09-18 01:22:33 +03:00
painter->setRenderHint(QPainter::TextAntialiasing);
painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->setRenderHint(QPainter::Antialiasing);
icon_.paint(painter, rect, Qt::AlignCenter, mode, state);
if (msgCount <= 0)
return;
QColor backgroundColor("red");
QColor textColor("white");
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(backgroundColor);
QFont f;
f.setPointSizeF(8);
f.setWeight(QFont::Thin);
painter->setBrush(brush);
painter->setPen(Qt::NoPen);
painter->setFont(f);
QRectF bubble(rect.width() - BubbleDiameter,
rect.height() - BubbleDiameter,
BubbleDiameter,
BubbleDiameter);
painter->drawEllipse(bubble);
painter->setPen(QPen(textColor));
painter->setBrush(Qt::NoBrush);
painter->drawText(bubble, Qt::AlignCenter, QString::number(msgCount));
2017-05-21 16:36:06 +03:00
}
2017-08-20 13:47:22 +03:00
QIconEngine *
MsgCountComposedIcon::clone() const
2017-05-21 16:36:06 +03:00
{
2021-09-18 01:22:33 +03:00
return new MsgCountComposedIcon(*this);
2017-05-21 16:36:06 +03:00
}
QList<QSize>
2021-12-28 22:09:08 +03:00
MsgCountComposedIcon::availableSizes(QIcon::Mode mode, QIcon::State state)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const
#endif
{
2021-09-18 01:22:33 +03:00
Q_UNUSED(mode);
Q_UNUSED(state);
QList<QSize> sizes;
sizes.append(QSize(24, 24));
sizes.append(QSize(32, 32));
sizes.append(QSize(48, 48));
sizes.append(QSize(64, 64));
sizes.append(QSize(128, 128));
sizes.append(QSize(256, 256));
return sizes;
}
QPixmap
2017-10-01 18:15:23 +03:00
MsgCountComposedIcon::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
{
2021-09-18 01:22:33 +03:00
QImage img(size, QImage::Format_ARGB32);
img.fill(qRgba(0, 0, 0, 0));
QPixmap result = QPixmap::fromImage(img, Qt::NoFormatConversion);
{
QPainter painter(&result);
paint(&painter, QRect(QPoint(0, 0), size), mode, state);
}
return result;
}
2022-01-12 21:09:46 +03:00
TrayIcon::TrayIcon(const QString &filename, QWindow *parent)
2017-08-20 13:47:22 +03:00
: QSystemTrayIcon(parent)
2017-05-21 16:36:06 +03:00
{
2024-01-09 05:17:28 +03:00
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
2021-09-18 01:22:33 +03:00
setIcon(QIcon(filename));
#else
2021-09-18 01:22:33 +03:00
icon_ = new MsgCountComposedIcon(filename);
setIcon(QIcon(icon_));
#endif
2017-05-21 16:36:06 +03:00
2022-01-12 21:09:46 +03:00
QMenu *menu = new QMenu();
2021-09-18 01:22:33 +03:00
setContextMenu(menu);
2021-04-08 13:26:15 +03:00
2021-09-18 01:22:33 +03:00
viewAction_ = new QAction(tr("Show"), this);
quitAction_ = new QAction(tr("Quit"), this);
2017-05-21 16:36:06 +03:00
2022-01-12 21:09:46 +03:00
connect(viewAction_, &QAction::triggered, parent, &QWindow::show);
2021-09-18 01:22:33 +03:00
connect(quitAction_, &QAction::triggered, this, QApplication::quit);
2017-05-21 16:36:06 +03:00
2021-09-18 01:22:33 +03:00
menu->addAction(viewAction_);
menu->addAction(quitAction_);
2017-05-21 16:36:06 +03:00
}
2017-08-20 13:47:22 +03:00
void
TrayIcon::setUnreadCount(int count)
2017-05-21 16:36:06 +03:00
{
2023-06-04 04:51:58 +03:00
qGuiApp->setBadgeNumber(count);
2017-05-21 16:36:06 +03:00
}
#include "moc_TrayIcon.cpp"