2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-06-22 16:16:04 +02:00
|
|
|
#include "macsystemtrayicon.h"
|
|
|
|
#include "qtsystemtrayicon.h"
|
2010-10-14 21:46:11 +02:00
|
|
|
#include "systemtrayicon.h"
|
2010-06-22 16:16:04 +02:00
|
|
|
|
2010-04-19 22:59:05 +02:00
|
|
|
#include <QApplication>
|
2009-12-24 20:16:07 +01:00
|
|
|
#include <QEvent>
|
|
|
|
#include <QWheelEvent>
|
2010-01-17 22:11:03 +01:00
|
|
|
#include <QPainter>
|
2010-04-19 22:59:05 +02:00
|
|
|
#include <QWidget>
|
2010-01-17 22:11:03 +01:00
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include <cmath>
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2016-01-14 10:40:26 +01:00
|
|
|
#include "ui/iconloader.h"
|
|
|
|
|
2009-12-24 20:16:07 +01:00
|
|
|
SystemTrayIcon::SystemTrayIcon(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QObject(parent),
|
2016-02-11 11:41:37 +01:00
|
|
|
percentage_(0) {
|
|
|
|
QIcon tiny_start = IconLoader::Load("tiny-start", IconLoader::Other);
|
|
|
|
playing_icon_ = tiny_start.pixmap(tiny_start.availableSizes().last());
|
|
|
|
QIcon tiny_pause = IconLoader::Load("tiny-pause", IconLoader::Other);
|
|
|
|
paused_icon_ = tiny_pause.pixmap(tiny_pause.availableSizes().last());
|
|
|
|
}
|
2009-12-24 20:16:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QPixmap SystemTrayIcon::CreateIcon(const QPixmap& icon,
|
|
|
|
const QPixmap& grey_icon) {
|
2010-06-22 13:52:55 +02:00
|
|
|
QRect rect(icon.rect());
|
2010-01-17 22:11:03 +01:00
|
|
|
|
|
|
|
// The angle of the line that's used to cover the icon.
|
|
|
|
// Centered on rect.topRight()
|
2010-06-22 13:52:55 +02:00
|
|
|
double angle = double(100 - song_progress()) / 100.0 * M_PI_2 + M_PI;
|
2010-01-17 22:11:03 +01:00
|
|
|
double length = sqrt(pow(rect.width(), 2.0) + pow(rect.height(), 2.0));
|
|
|
|
|
|
|
|
QPolygon mask;
|
|
|
|
mask << rect.topRight();
|
2014-02-07 16:34:20 +01:00
|
|
|
mask << rect.topRight() + QPoint(length * sin(angle), -length * cos(angle));
|
2010-01-17 22:11:03 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (song_progress() > 50) mask << rect.bottomLeft();
|
2010-01-17 22:11:03 +01:00
|
|
|
|
|
|
|
mask << rect.topLeft();
|
|
|
|
mask << rect.topRight();
|
|
|
|
|
2010-06-22 13:52:55 +02:00
|
|
|
QPixmap ret(icon);
|
|
|
|
QPainter p(&ret);
|
2010-04-19 15:53:26 +02:00
|
|
|
|
|
|
|
// Draw the grey bit over the orange icon
|
2010-01-17 22:11:03 +01:00
|
|
|
p.setClipRegion(mask);
|
2010-06-22 13:52:55 +02:00
|
|
|
p.drawPixmap(0, 0, grey_icon);
|
2010-04-19 15:53:26 +02:00
|
|
|
p.setClipping(false);
|
|
|
|
|
|
|
|
// Draw the playing or paused icon in the top-right
|
2010-06-22 13:52:55 +02:00
|
|
|
if (!current_state_icon().isNull()) {
|
2010-04-19 15:53:26 +02:00
|
|
|
int height = rect.height() / 2;
|
2014-02-07 16:34:20 +01:00
|
|
|
QPixmap scaled(
|
|
|
|
current_state_icon().scaledToHeight(height, Qt::SmoothTransformation));
|
2010-04-19 15:53:26 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QRect state_rect(rect.width() - scaled.width(), 0, scaled.width(),
|
|
|
|
scaled.height());
|
2010-04-19 15:53:26 +02:00
|
|
|
p.drawPixmap(state_rect, scaled);
|
|
|
|
}
|
|
|
|
|
2010-01-17 22:11:03 +01:00
|
|
|
p.end();
|
|
|
|
|
2010-06-22 13:52:55 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTrayIcon::SetProgress(int percentage) {
|
|
|
|
percentage_ = percentage;
|
|
|
|
UpdateIcon();
|
2010-01-17 22:11:03 +01:00
|
|
|
}
|
2010-04-19 15:53:26 +02:00
|
|
|
|
|
|
|
void SystemTrayIcon::SetPaused() {
|
|
|
|
current_state_icon_ = paused_icon_;
|
2010-06-22 13:52:55 +02:00
|
|
|
UpdateIcon();
|
2010-04-19 15:53:26 +02:00
|
|
|
}
|
|
|
|
|
2014-03-27 18:55:58 +01:00
|
|
|
void SystemTrayIcon::SetPlaying(bool enable_play_pause, bool enable_love) {
|
2010-04-19 15:53:26 +02:00
|
|
|
current_state_icon_ = playing_icon_;
|
2010-06-22 13:52:55 +02:00
|
|
|
UpdateIcon();
|
2010-04-19 15:53:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTrayIcon::SetStopped() {
|
|
|
|
current_state_icon_ = QPixmap();
|
2010-06-22 13:52:55 +02:00
|
|
|
UpdateIcon();
|
2010-04-19 15:53:26 +02:00
|
|
|
}
|
2010-06-22 16:16:04 +02:00
|
|
|
|
|
|
|
SystemTrayIcon* SystemTrayIcon::CreateSystemTrayIcon(QObject* parent) {
|
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
return new MacSystemTrayIcon(parent);
|
|
|
|
#else
|
|
|
|
return new QtSystemTrayIcon(parent);
|
|
|
|
#endif
|
|
|
|
}
|