2017-05-21 16:36:06 +03: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/>.
|
|
|
|
*/
|
|
|
|
|
2017-05-21 23:21:02 +03:00
|
|
|
#include <QApplication>
|
2017-10-01 12:13:10 +03:00
|
|
|
#include <QList>
|
2017-10-28 15:46:39 +03:00
|
|
|
#include <QMenu>
|
2017-10-01 18:15:23 +03:00
|
|
|
#include <QTimer>
|
2017-05-21 16:36:06 +03:00
|
|
|
|
|
|
|
#include "TrayIcon.h"
|
|
|
|
|
2017-07-01 16:34:36 +03:00
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
#include <QtMacExtras>
|
|
|
|
#endif
|
|
|
|
|
2017-05-21 16:36:06 +03:00
|
|
|
MsgCountComposedIcon::MsgCountComposedIcon(const QString &filename)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QIconEngine()
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
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
|
|
|
{
|
2017-09-10 12:59:21 +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);
|
|
|
|
|
2018-10-07 13:27:20 +03:00
|
|
|
QFont f;
|
|
|
|
f.setPointSizeF(8);
|
|
|
|
f.setWeight(QFont::Thin);
|
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
painter->setBrush(brush);
|
|
|
|
painter->setPen(Qt::NoPen);
|
2018-10-07 13:27:20 +03:00
|
|
|
painter->setFont(f);
|
2017-09-10 12:59:21 +03:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
return new MsgCountComposedIcon(*this);
|
2017-05-21 16:36:06 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 12:13:10 +03:00
|
|
|
QList<QSize>
|
|
|
|
MsgCountComposedIcon::availableSizes(QIcon::Mode mode, QIcon::State state) const
|
|
|
|
{
|
|
|
|
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)
|
2017-10-01 12:13:10 +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;
|
|
|
|
}
|
|
|
|
|
2017-05-21 16:36:06 +03:00
|
|
|
TrayIcon::TrayIcon(const QString &filename, QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QSystemTrayIcon(parent)
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
2017-07-09 12:49:17 +03:00
|
|
|
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
|
2017-09-10 12:59:21 +03:00
|
|
|
setIcon(QIcon(filename));
|
2017-07-09 12:49:17 +03:00
|
|
|
#else
|
2017-09-10 12:59:21 +03:00
|
|
|
icon_ = new MsgCountComposedIcon(filename);
|
|
|
|
setIcon(QIcon(icon_));
|
2017-07-09 12:49:17 +03:00
|
|
|
#endif
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
QMenu *menu = new QMenu(parent);
|
|
|
|
viewAction_ = new QAction(tr("Show"), parent);
|
|
|
|
quitAction_ = new QAction(tr("Quit"), parent);
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
connect(viewAction_, SIGNAL(triggered()), parent, SLOT(show()));
|
2017-09-24 13:58:36 +03:00
|
|
|
connect(quitAction_, &QAction::triggered, this, QApplication::quit);
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
menu->addAction(viewAction_);
|
|
|
|
menu->addAction(quitAction_);
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
setContextMenu(menu);
|
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
|
|
|
{
|
2017-07-09 12:49:17 +03:00
|
|
|
// Use the native badge counter in MacOS.
|
2017-07-01 16:34:36 +03:00
|
|
|
#if defined(Q_OS_MAC)
|
2017-10-01 18:15:23 +03:00
|
|
|
auto labelText = count == 0 ? "" : QString::number(count);
|
|
|
|
|
|
|
|
if (labelText == QtMac::badgeLabelText())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QtMac::setBadgeLabelText(labelText);
|
2017-07-09 12:49:17 +03:00
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
// FIXME: Find a way to use Windows apis for the badge counter (if any).
|
2017-07-01 16:34:36 +03:00
|
|
|
#else
|
2017-10-01 18:15:23 +03:00
|
|
|
if (count == icon_->msgCount)
|
|
|
|
return;
|
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
// Custom drawing on Linux.
|
|
|
|
MsgCountComposedIcon *tmp = static_cast<MsgCountComposedIcon *>(icon_->clone());
|
|
|
|
tmp->msgCount = count;
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
setIcon(QIcon(tmp));
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
icon_ = tmp;
|
2017-07-01 16:34:36 +03:00
|
|
|
#endif
|
2017-05-21 16:36:06 +03:00
|
|
|
}
|