2017-09-18 14:33:44 +08:00
|
|
|
/*
|
2018-02-07 11:52:47 +08:00
|
|
|
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
2017-09-18 14:33:44 +08:00
|
|
|
*
|
|
|
|
* Author: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* Maintainer: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-07-19 16:10:58 +08:00
|
|
|
#include "powerstatuswidget.h"
|
2019-05-02 19:13:31 +02:00
|
|
|
#include "powerplugin.h"
|
2016-07-19 16:10:58 +08:00
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QIcon>
|
2016-08-15 09:26:45 +08:00
|
|
|
#include <QMouseEvent>
|
2016-07-19 16:10:58 +08:00
|
|
|
|
|
|
|
PowerStatusWidget::PowerStatusWidget(QWidget *parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
|
|
|
|
m_powerInter(new DBusPower(this))
|
|
|
|
{
|
2017-03-02 09:41:35 +08:00
|
|
|
// QIcon::setThemeName("deepin");
|
2016-08-17 14:20:57 +08:00
|
|
|
|
|
|
|
connect(m_powerInter, &DBusPower::BatteryPercentageChanged, this, static_cast<void (PowerStatusWidget::*)()>(&PowerStatusWidget::update));
|
|
|
|
connect(m_powerInter, &DBusPower::BatteryStateChanged, this, static_cast<void (PowerStatusWidget::*)()>(&PowerStatusWidget::update));
|
2016-08-30 10:16:58 +08:00
|
|
|
connect(m_powerInter, &DBusPower::OnBatteryChanged, this, static_cast<void (PowerStatusWidget::*)()>(&PowerStatusWidget::update));
|
2016-07-19 16:10:58 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 18:05:30 +08:00
|
|
|
void PowerStatusWidget::refreshIcon()
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-07-19 16:10:58 +08:00
|
|
|
QSize PowerStatusWidget::sizeHint() const
|
|
|
|
{
|
2016-08-09 14:51:03 +08:00
|
|
|
return QSize(26, 26);
|
2016-07-19 16:10:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PowerStatusWidget::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
|
|
|
const QPixmap icon = getBatteryIcon();
|
2017-10-24 13:19:10 +08:00
|
|
|
const auto ratio = devicePixelRatioF();
|
2016-07-19 16:10:58 +08:00
|
|
|
|
|
|
|
QPainter painter(this);
|
2018-11-20 19:46:34 +08:00
|
|
|
const QRectF &rf = QRectF(rect());
|
|
|
|
const QRectF &rfp = QRectF(icon.rect());
|
|
|
|
painter.drawPixmap(rf.center() - rfp.center() / ratio, icon);
|
2016-07-19 16:10:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap PowerStatusWidget::getBatteryIcon()
|
|
|
|
{
|
2016-12-26 17:05:14 +08:00
|
|
|
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
|
|
|
|
const uint value = qMin(100.0, qMax(0.0, data.value("Display")));
|
|
|
|
const int percentage = std::round(value);
|
2019-05-02 19:13:31 +02:00
|
|
|
const int batteryState = m_powerInter->batteryState()["Display"];
|
2019-06-13 15:08:24 +08:00
|
|
|
const bool plugged = !m_powerInter->onBattery();
|
2016-07-19 16:10:58 +08:00
|
|
|
|
|
|
|
QString percentageStr;
|
|
|
|
if (percentage < 10 && percentage >= 0) {
|
|
|
|
percentageStr = "000";
|
|
|
|
} else if (percentage < 30) {
|
|
|
|
percentageStr = "020";
|
|
|
|
} else if (percentage < 50) {
|
|
|
|
percentageStr = "040";
|
|
|
|
} else if (percentage < 70) {
|
|
|
|
percentageStr = "060";
|
|
|
|
} else if (percentage < 90) {
|
|
|
|
percentageStr = "080";
|
|
|
|
} else if (percentage <= 100){
|
|
|
|
percentageStr = "100";
|
|
|
|
} else {
|
|
|
|
percentageStr = "000";
|
|
|
|
}
|
|
|
|
|
2018-01-02 14:48:27 +08:00
|
|
|
const QString iconStr = QString("battery-%1-%2")
|
|
|
|
.arg(percentageStr)
|
|
|
|
.arg(plugged ? "plugged-symbolic" : "symbolic");
|
2017-10-19 11:15:10 +08:00
|
|
|
const auto ratio = devicePixelRatioF();
|
|
|
|
QPixmap pix = QIcon::fromTheme(iconStr).pixmap(QSize(16, 16) * ratio);
|
|
|
|
pix.setDevicePixelRatio(ratio);
|
|
|
|
|
|
|
|
return pix;
|
2016-07-19 16:10:58 +08:00
|
|
|
}
|