forked from mirror/nheko
parent
c480f8f4bc
commit
a45582585d
@ -0,0 +1,58 @@ |
|||||||
|
/*
|
||||||
|
* 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/>.
|
||||||
|
*/ |
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <QAction> |
||||||
|
#include <QIcon> |
||||||
|
#include <QIconEngine> |
||||||
|
#include <QMenu> |
||||||
|
#include <QPainter> |
||||||
|
#include <QRect> |
||||||
|
#include <QSystemTrayIcon> |
||||||
|
|
||||||
|
class MsgCountComposedIcon : public QIconEngine |
||||||
|
{ |
||||||
|
public: |
||||||
|
MsgCountComposedIcon(const QString &filename); |
||||||
|
|
||||||
|
virtual void paint(QPainter *p, const QRect &rect, QIcon::Mode mode, QIcon::State state); |
||||||
|
virtual QIconEngine *clone() const; |
||||||
|
|
||||||
|
int msgCount = 0; |
||||||
|
|
||||||
|
private: |
||||||
|
const int BubbleDiameter = 15; |
||||||
|
|
||||||
|
QIcon icon_; |
||||||
|
}; |
||||||
|
|
||||||
|
class TrayIcon : public QSystemTrayIcon |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
public: |
||||||
|
TrayIcon(const QString &filename, QWidget *parent); |
||||||
|
|
||||||
|
public slots: |
||||||
|
void setUnreadCount(int count); |
||||||
|
|
||||||
|
private: |
||||||
|
QAction *viewAction_; |
||||||
|
QAction *quitAction_; |
||||||
|
|
||||||
|
MsgCountComposedIcon *icon_; |
||||||
|
}; |
@ -0,0 +1,93 @@ |
|||||||
|
/*
|
||||||
|
* 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 <QTimer> |
||||||
|
|
||||||
|
#include "TrayIcon.h" |
||||||
|
|
||||||
|
MsgCountComposedIcon::MsgCountComposedIcon(const QString &filename) |
||||||
|
: QIconEngine() |
||||||
|
{ |
||||||
|
icon_ = QIcon(filename); |
||||||
|
} |
||||||
|
|
||||||
|
void MsgCountComposedIcon::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) |
||||||
|
{ |
||||||
|
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); |
||||||
|
painter->setFont(QFont("Open Sans", 7, QFont::Black)); |
||||||
|
|
||||||
|
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)); |
||||||
|
} |
||||||
|
|
||||||
|
QIconEngine *MsgCountComposedIcon::clone() const |
||||||
|
{ |
||||||
|
return new MsgCountComposedIcon(*this); |
||||||
|
} |
||||||
|
|
||||||
|
TrayIcon::TrayIcon(const QString &filename, QWidget *parent) |
||||||
|
: QSystemTrayIcon(parent) |
||||||
|
{ |
||||||
|
icon_ = new MsgCountComposedIcon(filename); |
||||||
|
setIcon(QIcon(icon_)); |
||||||
|
|
||||||
|
QMenu *menu = new QMenu(parent); |
||||||
|
viewAction_ = new QAction("Show", parent); |
||||||
|
quitAction_ = new QAction("Quit", parent); |
||||||
|
|
||||||
|
connect(viewAction_, SIGNAL(triggered()), parent, SLOT(show())); |
||||||
|
connect(quitAction_, SIGNAL(triggered()), parent, SLOT(close())); |
||||||
|
|
||||||
|
menu->addAction(viewAction_); |
||||||
|
menu->addAction(quitAction_); |
||||||
|
|
||||||
|
setContextMenu(menu); |
||||||
|
|
||||||
|
// We wait a little for the icon to load.
|
||||||
|
QTimer::singleShot(500, this, [=]() { |
||||||
|
show(); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
void TrayIcon::setUnreadCount(int count) |
||||||
|
{ |
||||||
|
MsgCountComposedIcon *tmp = static_cast<MsgCountComposedIcon *>(icon_->clone()); |
||||||
|
tmp->msgCount = count; |
||||||
|
|
||||||
|
setIcon(QIcon(tmp)); |
||||||
|
|
||||||
|
icon_ = tmp; |
||||||
|
} |
Loading…
Reference in new issue