Automatically change tray icon

This commit is contained in:
Pierre HUBERT 2020-06-13 17:19:43 +02:00
parent 2635e92b8d
commit 1cbae38ee5
2 changed files with 36 additions and 0 deletions

View File

@ -1,5 +1,7 @@
#include <QApplication> #include <QApplication>
#include <QMenu> #include <QMenu>
#include <QPixmap>
#include <QPainter>
#include "trayicon.h" #include "trayicon.h"
@ -27,6 +29,7 @@ void TrayIcon::onNewNumber(const NotificationsNumber &number)
} }
mOldNumber = number.sum(); mOldNumber = number.sum();
refreshIcon();
} }
void TrayIcon::onQuit() void TrayIcon::onQuit()
@ -58,3 +61,31 @@ void TrayIcon::showNotification(const NotificationsNumber &n)
mTrayIcon.showMessage(tr("Comunic"), msg, QIcon(":/logo_large.png")); mTrayIcon.showMessage(tr("Comunic"), msg, QIcon(":/logo_large.png"));
} }
void TrayIcon::refreshIcon()
{
if(mOldNumber == 0) {
mTrayIcon.setIcon(QIcon(":/logo_large.png"));
return;
}
QPixmap bm = QPixmap::fromImage(QImage(":/logo_large.png"), Qt::ColorOnly);
QPainter engine(&bm);
QColor red(255, 0, 0, 255);
QRect r(QPoint(bm.width()/3, 0), QPoint(bm.width(), bm.height() - bm.height()/3));
engine.fillRect(r, red);
engine.setPen(QColor(255,255,255,255));
QFont f = engine.font();
f.setFamily("Arial");
f.setPointSize(bm.width()/1.8);
f.setBold(true);
engine.setFont(f);
engine.drawText(r, Qt::AlignCenter, QString::number(mOldNumber));
mTrayIcon.setIcon(QIcon(bm));
}

View File

@ -35,6 +35,11 @@ private:
*/ */
void showNotification(const NotificationsNumber &n); void showNotification(const NotificationsNumber &n);
/**
* Refresh the icon shown in the taskbar
*/
void refreshIcon();
// Class members // Class members
QMenu *mMenu; QMenu *mMenu;
QSystemTrayIcon mTrayIcon; QSystemTrayIcon mTrayIcon;