1 /*
2  * Strawberry Music Player
3  * This file was part of Clementine.
4  * Copyright 2010, David Sansome <me@davidsansome.com>
5  * Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
6  *
7  * Strawberry is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Strawberry is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Strawberry.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "config.h"
23 
24 #include <cmath>
25 
26 #include <QPixmap>
27 #include <QPainter>
28 #include <QPoint>
29 #include <QPolygon>
30 #include <QRect>
31 
32 #ifdef Q_OS_MACOS
33 #  include "macsystemtrayicon.h"
34 #else
35 #  include "qtsystemtrayicon.h"
36 #endif
37 
CreateIcon(const QPixmap & icon,const QPixmap & grey_icon)38 QPixmap SystemTrayIcon::CreateIcon(const QPixmap &icon, const QPixmap &grey_icon) {
39 
40   QRect rect(icon.rect());
41 
42   QPixmap ret(icon);
43   QPainter p(&ret);
44 
45   if (trayicon_progress_) {
46     // The angle of the line that's used to cover the icon.
47     // Centered on rect.topLeft()
48     double angle = static_cast<double>(100 - song_progress_) / 100.0 * M_PI_2;
49     double length = sqrt(pow(rect.width(), 2.0) + pow(rect.height(), 2.0));
50 
51     QPolygon mask;
52     mask << rect.topLeft();
53     mask << rect.topLeft() + QPoint(static_cast<int>(length * sin(angle)), static_cast<int>(length * cos(angle)));
54 
55     if (song_progress_ > 50) mask << rect.bottomRight();
56 
57     mask << rect.topRight();
58     mask << rect.topLeft();
59 
60     // Draw the grey bit
61     p.setClipRegion(mask);
62     p.drawPixmap(0, 0, grey_icon);
63     p.setClipping(false);
64   }
65 
66   // Draw the playing or paused icon in the top-right
67   if (!current_state_icon_.isNull()) {
68     int height = rect.height() / 2;
69     QPixmap scaled(current_state_icon_.scaledToHeight(height, Qt::SmoothTransformation));
70 
71     QRect state_rect(rect.width() - scaled.width(), 0, scaled.width(), scaled.height());
72     p.drawPixmap(state_rect, scaled);
73   }
74 
75   p.end();
76 
77   return ret;
78 
79 }
80