Refactor systemtrayicon code

This commit is contained in:
Jonas Kvinge
2021-05-20 21:40:08 +02:00
parent 264d47caf4
commit 67f831beba
18 changed files with 335 additions and 442 deletions

View File

@@ -23,29 +23,18 @@
#include <cmath>
#include <QObject>
#include <QSystemTrayIcon>
#include <QPixmap>
#include <QPainter>
#include <QPoint>
#include <QPolygon>
#include <QRect>
#include <QVector>
#include "systemtrayicon.h"
#include "qtsystemtrayicon.h"
#ifdef Q_OS_MACOS
# include "macsystemtrayicon.h"
#else
# include "qtsystemtrayicon.h"
#endif
SystemTrayIcon::SystemTrayIcon(QObject *parent)
: QObject(parent),
percentage_(0),
playing_icon_(":/pictures/tiny-play.png"),
paused_icon_(":/pictures/tiny-pause.png")
{
}
QPixmap SystemTrayIcon::CreateIcon(const QPixmap &icon, const QPixmap &grey_icon) {
QRect rect(icon.rect());
@@ -56,14 +45,14 @@ QPixmap SystemTrayIcon::CreateIcon(const QPixmap &icon, const QPixmap &grey_icon
if (trayicon_progress_) {
// The angle of the line that's used to cover the icon.
// Centered on rect.topLeft()
double angle = double(100 - song_progress()) / 100.0 * M_PI_2;
double angle = double(100 - song_progress_) / 100.0 * M_PI_2;
double length = sqrt(pow(rect.width(), 2.0) + pow(rect.height(), 2.0));
QPolygon mask;
mask << rect.topLeft();
mask << rect.topLeft() + QPoint(static_cast<int>(length * sin(angle)), static_cast<int>(length * cos(angle)));
if (song_progress() > 50) mask << rect.bottomRight();
if (song_progress_ > 50) mask << rect.bottomRight();
mask << rect.topRight();
mask << rect.topLeft();
@@ -75,9 +64,9 @@ QPixmap SystemTrayIcon::CreateIcon(const QPixmap &icon, const QPixmap &grey_icon
}
// Draw the playing or paused icon in the top-right
if (!current_state_icon().isNull()) {
if (!current_state_icon_.isNull()) {
int height = rect.height() / 2;
QPixmap scaled(current_state_icon().scaledToHeight(height, Qt::SmoothTransformation));
QPixmap scaled(current_state_icon_.scaledToHeight(height, Qt::SmoothTransformation));
QRect state_rect(rect.width() - scaled.width(), 0, scaled.width(), scaled.height());
p.drawPixmap(state_rect, scaled);
@@ -88,36 +77,3 @@ QPixmap SystemTrayIcon::CreateIcon(const QPixmap &icon, const QPixmap &grey_icon
return ret;
}
void SystemTrayIcon::SetProgress(int percentage) {
percentage_ = percentage;
UpdateIcon();
}
void SystemTrayIcon::SetPaused() {
current_state_icon_ = paused_icon_;
UpdateIcon();
}
void SystemTrayIcon::SetPlaying(bool enable_play_pause) {
Q_UNUSED(enable_play_pause);
current_state_icon_ = playing_icon_;
UpdateIcon();
}
void SystemTrayIcon::SetStopped() {
current_state_icon_ = QPixmap();
UpdateIcon();
}
SystemTrayIcon *SystemTrayIcon::CreateSystemTrayIcon(QObject *parent) {
#ifdef Q_OS_MACOS
return new MacSystemTrayIcon(parent);
#else
if (QSystemTrayIcon::isSystemTrayAvailable()) return new QtSystemTrayIcon(parent);
else return nullptr;
#endif
}