1 /* This file is part of Clementine.
2    Copyright 2010, David Sansome <me@davidsansome.com>
3 
4    Clementine is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    Clementine is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "macsystemtrayicon.h"
19 #include "qtsystemtrayicon.h"
20 #include "systemtrayicon.h"
21 
22 #include <QApplication>
23 #include <QEvent>
24 #include <QPainter>
25 #include <QSystemTrayIcon>
26 #include <QWheelEvent>
27 #include <QWidget>
28 #include <QtDebug>
29 
30 #include <cmath>
31 
32 #include "ui/iconloader.h"
33 
SystemTrayIcon(QObject * parent)34 SystemTrayIcon::SystemTrayIcon(QObject* parent)
35     : QObject(parent),
36       percentage_(0) {
37   QIcon tiny_start = IconLoader::Load("tiny-start", IconLoader::Other);
38   playing_icon_ = tiny_start.pixmap(tiny_start.availableSizes().last());
39   QIcon tiny_pause = IconLoader::Load("tiny-pause", IconLoader::Other);
40   paused_icon_ = tiny_pause.pixmap(tiny_pause.availableSizes().last());
41 }
42 
CreateIcon(const QPixmap & icon,const QPixmap & grey_icon)43 QPixmap SystemTrayIcon::CreateIcon(const QPixmap& icon,
44                                    const QPixmap& grey_icon) {
45   QRect rect(icon.rect());
46 
47   // The angle of the line that's used to cover the icon.
48   // Centered on rect.topRight()
49   double angle = double(100 - song_progress()) / 100.0 * M_PI_2 + M_PI;
50   double length = sqrt(pow(rect.width(), 2.0) + pow(rect.height(), 2.0));
51 
52   QPolygon mask;
53   mask << rect.topRight();
54   mask << rect.topRight() + QPoint(length * sin(angle), -length * cos(angle));
55 
56   if (song_progress() > 50) mask << rect.bottomLeft();
57 
58   mask << rect.topLeft();
59   mask << rect.topRight();
60 
61   QPixmap ret(icon);
62   QPainter p(&ret);
63 
64   // Draw the grey bit over the orange icon
65   p.setClipRegion(mask);
66   p.drawPixmap(0, 0, grey_icon);
67   p.setClipping(false);
68 
69   // Draw the playing or paused icon in the top-right
70   if (!current_state_icon().isNull()) {
71     int height = rect.height() / 2;
72     QPixmap scaled(
73         current_state_icon().scaledToHeight(height, Qt::SmoothTransformation));
74 
75     QRect state_rect(rect.width() - scaled.width(), 0, scaled.width(),
76                      scaled.height());
77     p.drawPixmap(state_rect, scaled);
78   }
79 
80   p.end();
81 
82   return ret;
83 }
84 
SetProgress(int percentage)85 void SystemTrayIcon::SetProgress(int percentage) {
86   percentage_ = percentage;
87   UpdateIcon();
88 }
89 
SetPaused()90 void SystemTrayIcon::SetPaused() {
91   current_state_icon_ = paused_icon_;
92   UpdateIcon();
93 }
94 
SetPlaying(bool enable_play_pause,bool enable_love)95 void SystemTrayIcon::SetPlaying(bool enable_play_pause, bool enable_love) {
96   current_state_icon_ = playing_icon_;
97   UpdateIcon();
98 }
99 
SetStopped()100 void SystemTrayIcon::SetStopped() {
101   current_state_icon_ = QPixmap();
102   UpdateIcon();
103 }
104 
CreateSystemTrayIcon(QObject * parent)105 SystemTrayIcon* SystemTrayIcon::CreateSystemTrayIcon(QObject* parent) {
106 #ifdef Q_OS_DARWIN
107   return new MacSystemTrayIcon(parent);
108 #else
109   if (QSystemTrayIcon::isSystemTrayAvailable())
110     return new QtSystemTrayIcon(parent);
111   else
112     return nullptr;
113 #endif
114 }
115