strawberry-audio-player-win.../src/core/systemtrayicon.cpp

80 lines
2.2 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <cmath>
#include <QPixmap>
2018-02-27 18:06:05 +01:00
#include <QPainter>
#include <QPoint>
#include <QPolygon>
#include <QRect>
2018-02-27 18:06:05 +01:00
2018-07-01 22:26:46 +02:00
#ifdef Q_OS_MACOS
# include "macsystemtrayicon.h"
2021-05-20 21:40:08 +02:00
#else
# include "qtsystemtrayicon.h"
2018-07-01 22:26:46 +02:00
#endif
2018-02-27 18:06:05 +01:00
QPixmap SystemTrayIcon::CreateIcon(const QPixmap &icon, const QPixmap &grey_icon) {
QRect rect(icon.rect());
QPixmap ret(icon);
QPainter p(&ret);
2018-02-27 18:06:05 +01:00
if (trayicon_progress_) {
// The angle of the line that's used to cover the icon.
// Centered on rect.topLeft()
2021-10-11 22:28:28 +02:00
double angle = static_cast<double>(100 - song_progress_) / 100.0 * M_PI_2;
double length = sqrt(pow(rect.width(), 2.0) + pow(rect.height(), 2.0));
2018-02-27 18:06:05 +01:00
QPolygon mask;
mask << rect.topLeft();
2021-03-21 18:53:02 +01:00
mask << rect.topLeft() + QPoint(static_cast<int>(length * sin(angle)), static_cast<int>(length * cos(angle)));
2018-02-27 18:06:05 +01:00
2021-05-20 21:40:08 +02:00
if (song_progress_ > 50) mask << rect.bottomRight();
2018-02-27 18:06:05 +01:00
mask << rect.topRight();
mask << rect.topLeft();
2018-02-27 18:06:05 +01:00
// Draw the grey bit
p.setClipRegion(mask);
p.drawPixmap(0, 0, grey_icon);
p.setClipping(false);
}
2018-02-27 18:06:05 +01:00
// Draw the playing or paused icon in the top-right
2021-05-20 21:40:08 +02:00
if (!current_state_icon_.isNull()) {
2018-02-27 18:06:05 +01:00
int height = rect.height() / 2;
2021-05-20 21:40:08 +02:00
QPixmap scaled(current_state_icon_.scaledToHeight(height, Qt::SmoothTransformation));
2018-02-27 18:06:05 +01:00
QRect state_rect(rect.width() - scaled.width(), 0, scaled.width(), scaled.height());
p.drawPixmap(state_rect, scaled);
}
p.end();
return ret;
}