2023-02-16 13:51:55 +08:00
|
|
|
|
// Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
|
|
|
|
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
2022-09-06 11:36:55 +08:00
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2017-09-18 14:33:44 +08:00
|
|
|
|
|
2016-07-19 16:10:58 +08:00
|
|
|
|
#include "powerstatuswidget.h"
|
2019-05-02 19:13:31 +02:00
|
|
|
|
#include "powerplugin.h"
|
2021-12-31 14:54:12 +08:00
|
|
|
|
#include "dbus/dbuspower.h"
|
|
|
|
|
|
2019-10-11 12:24:11 +08:00
|
|
|
|
#include <DGuiApplicationHelper>
|
2016-07-19 16:10:58 +08:00
|
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QIcon>
|
2016-08-15 09:26:45 +08:00
|
|
|
|
#include <QMouseEvent>
|
2023-11-22 16:27:30 +08:00
|
|
|
|
#include <DIconTheme>
|
2016-07-19 16:10:58 +08:00
|
|
|
|
|
2019-10-11 12:24:11 +08:00
|
|
|
|
DGUI_USE_NAMESPACE
|
|
|
|
|
|
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
|
|
|
|
|
2022-05-25 13:09:26 +08:00
|
|
|
|
connect(m_powerInter, &DBusPower::BatteryPercentageChanged, this, &PowerStatusWidget::refreshIcon);
|
|
|
|
|
connect(m_powerInter, &DBusPower::BatteryStateChanged, this, &PowerStatusWidget::refreshIcon);
|
|
|
|
|
connect(m_powerInter, &DBusPower::OnBatteryChanged, this, &PowerStatusWidget::refreshIcon);
|
|
|
|
|
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &PowerStatusWidget::refreshIcon);
|
2016-07-19 16:10:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 18:05:30 +08:00
|
|
|
|
void PowerStatusWidget::refreshIcon()
|
|
|
|
|
{
|
|
|
|
|
update();
|
2022-05-25 13:09:26 +08:00
|
|
|
|
Q_EMIT iconChanged();
|
2018-12-06 18:05:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 16:10:58 +08:00
|
|
|
|
void PowerStatusWidget::paintEvent(QPaintEvent *e)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
|
2022-12-01 12:38:38 +08:00
|
|
|
|
int themeType = DGuiApplicationHelper::instance()->themeType();
|
|
|
|
|
if (height() <= PLUGIN_BACKGROUND_MIN_SIZE && themeType == DGuiApplicationHelper::LightType)
|
|
|
|
|
themeType = DGuiApplicationHelper::DarkType;
|
|
|
|
|
const QPixmap icon = getBatteryIcon(themeType);
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-12-01 12:38:38 +08:00
|
|
|
|
QPixmap PowerStatusWidget::getBatteryIcon(int themeType)
|
2016-07-19 16:10:58 +08:00
|
|
|
|
{
|
2016-12-26 17:05:14 +08:00
|
|
|
|
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
|
2020-06-12 09:51:42 +08:00
|
|
|
|
const uint value = uint(qMin(100.0, qMax(0.0, data.value("Display"))));
|
|
|
|
|
const int percentage = int(std::round(value));
|
2020-06-22 11:05:00 +08:00
|
|
|
|
// onBattery应该表示的是当前是否使用电池在供电,为true表示没插入电源
|
2019-06-13 15:08:24 +08:00
|
|
|
|
const bool plugged = !m_powerInter->onBattery();
|
2020-06-22 11:05:00 +08:00
|
|
|
|
const BatteryState batteryState = static_cast<BatteryState>(m_powerInter->batteryState()["Display"]);
|
2016-07-19 16:10:58 +08:00
|
|
|
|
|
2020-02-26 16:41:31 +08:00
|
|
|
|
/*根据新需求,电池电量显示分别是*/
|
2020-05-18 16:05:08 +08:00
|
|
|
|
/* 0-5%、6-10%、11%-20%、21-30%、31-40%、41-50%、51-60%、61%-70%、71-80%、81-90%、91-100% */
|
2016-07-19 16:10:58 +08:00
|
|
|
|
QString percentageStr;
|
2020-06-22 11:05:00 +08:00
|
|
|
|
if (percentage <= 5) {
|
2020-05-18 16:05:08 +08:00
|
|
|
|
percentageStr = "000";
|
|
|
|
|
} else if (percentage <= 10) {
|
|
|
|
|
percentageStr = "010";
|
2020-02-26 16:41:31 +08:00
|
|
|
|
} else if (percentage <= 20) {
|
2016-07-19 16:10:58 +08:00
|
|
|
|
percentageStr = "020";
|
2020-05-18 16:05:08 +08:00
|
|
|
|
} else if (percentage <= 30) {
|
|
|
|
|
percentageStr = "030";
|
2020-06-05 16:16:59 +08:00
|
|
|
|
} else if (percentage <= 40) {
|
2016-07-19 16:10:58 +08:00
|
|
|
|
percentageStr = "040";
|
2020-05-18 16:05:08 +08:00
|
|
|
|
} else if (percentage <= 50) {
|
|
|
|
|
percentageStr = "050";
|
2020-06-05 16:16:59 +08:00
|
|
|
|
} else if (percentage <= 60) {
|
2016-07-19 16:10:58 +08:00
|
|
|
|
percentageStr = "060";
|
2020-05-18 16:05:08 +08:00
|
|
|
|
} else if (percentage <= 70) {
|
|
|
|
|
percentageStr = "070";
|
2020-06-27 17:53:19 +08:00
|
|
|
|
} else if (percentage <= 80) {
|
2016-07-19 16:10:58 +08:00
|
|
|
|
percentageStr = "080";
|
2020-05-18 16:05:08 +08:00
|
|
|
|
} else if (percentage <= 90) {
|
|
|
|
|
percentageStr = "090";
|
2016-07-19 16:10:58 +08:00
|
|
|
|
} else {
|
2020-05-18 16:05:08 +08:00
|
|
|
|
percentageStr = "100";
|
2016-07-19 16:10:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-22 11:05:00 +08:00
|
|
|
|
QString iconStr;
|
|
|
|
|
if (batteryState == BatteryState::FULLY_CHARGED && plugged) {
|
|
|
|
|
iconStr = QString("battery-full-charged-symbolic");
|
|
|
|
|
} else {
|
|
|
|
|
iconStr = QString("battery-%1-%2")
|
|
|
|
|
.arg(percentageStr)
|
|
|
|
|
.arg(plugged ? "plugged-symbolic" : "symbolic");
|
|
|
|
|
}
|
2019-10-11 12:24:11 +08:00
|
|
|
|
|
2023-11-22 16:27:30 +08:00
|
|
|
|
if (themeType == DGuiApplicationHelper::ColorType::DarkType) {
|
|
|
|
|
iconStr.append(PLUGIN_MIN_ICON_NAME);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-19 11:15:10 +08:00
|
|
|
|
const auto ratio = devicePixelRatioF();
|
2022-11-30 16:46:54 +08:00
|
|
|
|
QSize pixmapSize = QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? QSize(20, 20) : (QSize(20, 20) * ratio);
|
2023-11-22 16:27:30 +08:00
|
|
|
|
QIcon qrcIcon = QIcon(":/batteryicons/resources/batteryicons/" + iconStr + ".svg");
|
|
|
|
|
QIcon finalIcon = DIconTheme::findQIcon(iconStr, qrcIcon, DIconTheme::IgnoreBuiltinIcons);
|
|
|
|
|
QPixmap pix = finalIcon.pixmap(pixmapSize);
|
2017-10-19 11:15:10 +08:00
|
|
|
|
pix.setDevicePixelRatio(ratio);
|
|
|
|
|
|
|
|
|
|
return pix;
|
2016-07-19 16:10:58 +08:00
|
|
|
|
}
|
2019-12-14 20:12:18 +08:00
|
|
|
|
|
|
|
|
|
void PowerStatusWidget::resizeEvent(QResizeEvent *event)
|
|
|
|
|
{
|
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
|
|
|
|
|
|
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
|
|
|
|
|
// 保持横纵比
|
|
|
|
|
if (position == Dock::Bottom || position == Dock::Top) {
|
|
|
|
|
setMaximumWidth(height());
|
|
|
|
|
setMaximumHeight(QWIDGETSIZE_MAX);
|
|
|
|
|
} else {
|
|
|
|
|
setMaximumHeight(width());
|
|
|
|
|
setMaximumWidth(QWIDGETSIZE_MAX);
|
|
|
|
|
}
|
|
|
|
|
}
|