nheko/src/TrayIcon.cc

117 lines
3.1 KiB
C++
Raw Normal View History

2017-05-21 15:36:06 +02:00
/*
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QApplication>
2017-05-21 15:36:06 +02:00
#include <QTimer>
#include "TrayIcon.h"
2017-07-01 15:34:36 +02:00
#if defined(Q_OS_MAC)
#include <QtMacExtras>
#endif
2017-05-21 15:36:06 +02:00
MsgCountComposedIcon::MsgCountComposedIcon(const QString &filename)
2017-08-20 12:47:22 +02:00
: QIconEngine()
2017-05-21 15:36:06 +02:00
{
icon_ = QIcon(filename);
}
2017-08-20 12:47:22 +02:00
void
MsgCountComposedIcon::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
2017-05-21 15:36:06 +02:00
{
2017-05-22 16:35:17 +02:00
painter->setRenderHint(QPainter::TextAntialiasing);
painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->setRenderHint(QPainter::Antialiasing);
2017-05-21 15:36:06 +02:00
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);
painter->setBrush(brush);
painter->setPen(Qt::NoPen);
2017-05-22 16:35:17 +02:00
painter->setFont(QFont("Open Sans", 8, QFont::Black));
2017-05-21 15:36:06 +02:00
2017-08-20 12:47:22 +02:00
QRectF bubble(rect.width() - BubbleDiameter, rect.height() - BubbleDiameter, BubbleDiameter, BubbleDiameter);
2017-05-21 15:36:06 +02:00
painter->drawEllipse(bubble);
painter->setPen(QPen(textColor));
painter->setBrush(Qt::NoBrush);
painter->drawText(bubble, Qt::AlignCenter, QString::number(msgCount));
}
2017-08-20 12:47:22 +02:00
QIconEngine *
MsgCountComposedIcon::clone() const
2017-05-21 15:36:06 +02:00
{
return new MsgCountComposedIcon(*this);
}
TrayIcon::TrayIcon(const QString &filename, QWidget *parent)
2017-08-20 12:47:22 +02:00
: QSystemTrayIcon(parent)
2017-05-21 15:36:06 +02:00
{
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
setIcon(QIcon(filename));
#else
2017-05-21 15:36:06 +02:00
icon_ = new MsgCountComposedIcon(filename);
setIcon(QIcon(icon_));
#endif
2017-05-21 15:36:06 +02:00
QMenu *menu = new QMenu(parent);
2017-05-29 18:09:12 +02:00
viewAction_ = new QAction(tr("Show"), parent);
quitAction_ = new QAction(tr("Quit"), parent);
2017-05-21 15:36:06 +02:00
connect(viewAction_, SIGNAL(triggered()), parent, SLOT(show()));
2017-08-20 12:47:22 +02:00
connect(quitAction_, &QAction::triggered, this, [=]() { QApplication::quit(); });
2017-05-21 15:36:06 +02:00
menu->addAction(viewAction_);
menu->addAction(quitAction_);
setContextMenu(menu);
// We wait a little for the icon to load.
2017-08-20 12:47:22 +02:00
QTimer::singleShot(500, this, [=]() { show(); });
2017-05-21 15:36:06 +02:00
}
2017-08-20 12:47:22 +02:00
void
TrayIcon::setUnreadCount(int count)
2017-05-21 15:36:06 +02:00
{
// Use the native badge counter in MacOS.
2017-07-01 15:34:36 +02:00
#if defined(Q_OS_MAC)
if (count == 0)
QtMac::setBadgeLabelText("");
else
QtMac::setBadgeLabelText(QString::number(count));
#elif defined(Q_OS_WIN)
// FIXME: Find a way to use Windows apis for the badge counter (if any).
2017-07-01 15:34:36 +02:00
#else
// Custom drawing on Linux.
// FIXME: It doesn't seem to work on KDE.
2017-05-21 15:36:06 +02:00
MsgCountComposedIcon *tmp = static_cast<MsgCountComposedIcon *>(icon_->clone());
tmp->msgCount = count;
setIcon(QIcon(tmp));
icon_ = tmp;
2017-07-01 15:34:36 +02:00
#endif
2017-05-21 15:36:06 +02:00
}