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
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
|
|
|
|
#include "powerplugin.h"
|
|
|
|
|
#include "dbus/dbusaccount.h"
|
2020-06-29 15:35:51 +08:00
|
|
|
|
#include "../widgets/tipswidget.h"
|
2020-08-19 14:13:35 +08:00
|
|
|
|
#include "../frame/util/utils.h"
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
|
|
|
|
#include <QIcon>
|
2020-05-18 16:05:08 +08:00
|
|
|
|
#include <QGSettings>
|
2023-01-30 13:14:32 +08:00
|
|
|
|
#include <QHBoxLayout>
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
2023-01-30 13:14:32 +08:00
|
|
|
|
#include <DFontSizeManager>
|
2020-12-10 17:10:59 +08:00
|
|
|
|
#include <DDBusSender>
|
2023-02-21 15:57:42 +08:00
|
|
|
|
#include <DConfig>
|
2020-12-10 17:10:59 +08:00
|
|
|
|
|
2018-11-13 20:07:52 +08:00
|
|
|
|
#define PLUGIN_STATE_KEY "enable"
|
2020-06-03 16:36:37 +08:00
|
|
|
|
#define DELAYTIME (20 * 1000)
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
2023-02-21 15:57:42 +08:00
|
|
|
|
DCORE_USE_NAMESPACE
|
2020-06-29 15:35:51 +08:00
|
|
|
|
using namespace Dock;
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
2020-05-18 16:05:08 +08:00
|
|
|
|
PowerPlugin::PowerPlugin(QObject *parent)
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
, m_pluginLoaded(false)
|
|
|
|
|
, m_showTimeToFull(true)
|
2021-05-25 13:05:45 +08:00
|
|
|
|
, m_powerStatusWidget(nullptr)
|
2020-05-18 16:05:08 +08:00
|
|
|
|
, m_tipsLabel(new TipsWidget)
|
2021-05-25 13:05:45 +08:00
|
|
|
|
, m_systemPowerInter(nullptr)
|
|
|
|
|
, m_powerInter(nullptr)
|
2023-02-21 15:57:42 +08:00
|
|
|
|
, m_dconfig(new DConfig(QString("org.deepin.dde.dock.power"), QString()))
|
2020-06-11 10:54:56 +08:00
|
|
|
|
, m_preChargeTimer(new QTimer(this))
|
2023-01-30 13:14:32 +08:00
|
|
|
|
, m_quickPanel(nullptr)
|
2018-11-13 20:07:52 +08:00
|
|
|
|
{
|
2023-02-01 13:29:58 +08:00
|
|
|
|
initUi();
|
|
|
|
|
initConnection();
|
2018-11-13 20:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString PowerPlugin::pluginName() const
|
|
|
|
|
{
|
|
|
|
|
return "power";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString PowerPlugin::pluginDisplayName() const
|
|
|
|
|
{
|
2023-02-01 13:29:58 +08:00
|
|
|
|
return tr("Battery");
|
2018-11-13 20:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *PowerPlugin::itemWidget(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
if (itemKey == POWER_KEY)
|
2021-05-25 13:05:45 +08:00
|
|
|
|
return m_powerStatusWidget.data();
|
2023-01-30 13:14:32 +08:00
|
|
|
|
if (itemKey == QUICK_ITEM_KEY)
|
|
|
|
|
return m_quickPanel;
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *PowerPlugin::itemTipsWidget(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
|
|
|
|
|
|
|
|
|
|
if (data.isEmpty()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_tipsLabel->setObjectName(itemKey);
|
|
|
|
|
|
2019-05-13 11:38:51 +08:00
|
|
|
|
refreshTipsData();
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
2021-05-25 13:05:45 +08:00
|
|
|
|
return m_tipsLabel.data();
|
2018-11-13 20:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerPlugin::init(PluginProxyInterface *proxyInter)
|
|
|
|
|
{
|
|
|
|
|
m_proxyInter = proxyInter;
|
|
|
|
|
|
2022-12-01 19:36:53 +08:00
|
|
|
|
loadPlugin();
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
2023-02-01 13:29:58 +08:00
|
|
|
|
onThemeTypeChanged(DGuiApplicationHelper::instance()->themeType());
|
2018-11-13 20:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString PowerPlugin::itemCommand(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
if (itemKey == POWER_KEY)
|
2023-01-30 13:14:32 +08:00
|
|
|
|
return QString("dbus-send --print-reply --dest=org.deepin.dde.ControlCenter1 /org/deepin/dde/ControlCenter1 org.deepin.dde.ControlCenter1.ShowPage string:power");
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(itemKey)
|
|
|
|
|
Q_UNUSED(checked)
|
|
|
|
|
|
2020-12-10 17:10:59 +08:00
|
|
|
|
if (menuId == "power") {
|
|
|
|
|
DDBusSender()
|
2022-08-16 10:29:10 +00:00
|
|
|
|
.service("org.deepin.dde.ControlCenter1")
|
|
|
|
|
.interface("org.deepin.dde.ControlCenter1")
|
|
|
|
|
.path("/org/deepin/dde/ControlCenter1")
|
2022-06-14 12:57:16 +00:00
|
|
|
|
.method(QString("ShowPage"))
|
2020-12-10 17:10:59 +08:00
|
|
|
|
.arg(QString("power"))
|
|
|
|
|
.call();
|
|
|
|
|
}
|
2018-11-13 20:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 18:05:30 +08:00
|
|
|
|
void PowerPlugin::refreshIcon(const QString &itemKey)
|
|
|
|
|
{
|
|
|
|
|
if (itemKey == POWER_KEY) {
|
|
|
|
|
m_powerStatusWidget->refreshIcon();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 20:07:52 +08:00
|
|
|
|
int PowerPlugin::itemSortKey(const QString &itemKey)
|
|
|
|
|
{
|
2020-01-11 10:35:29 +08:00
|
|
|
|
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
|
2018-11-13 20:07:52 +08:00
|
|
|
|
|
2021-03-09 13:29:02 +08:00
|
|
|
|
return m_proxyInter->getValue(this, key, 5).toInt();
|
2018-11-13 20:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerPlugin::setSortKey(const QString &itemKey, const int order)
|
|
|
|
|
{
|
2020-01-11 10:35:29 +08:00
|
|
|
|
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
|
2018-11-21 18:22:22 +08:00
|
|
|
|
|
|
|
|
|
m_proxyInter->saveValue(this, key, order);
|
2018-11-13 20:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 15:41:34 +08:00
|
|
|
|
QIcon PowerPlugin::icon(const DockPart &dockPart, DGuiApplicationHelper::ColorType themeType)
|
2019-01-30 18:00:46 +08:00
|
|
|
|
{
|
2023-02-01 13:29:58 +08:00
|
|
|
|
const QPixmap pixmap = m_powerStatusWidget->getBatteryIcon(themeType);
|
2023-01-30 13:14:32 +08:00
|
|
|
|
static QIcon batteryIcon;
|
2022-05-25 13:09:26 +08:00
|
|
|
|
batteryIcon.detach();
|
|
|
|
|
batteryIcon.addPixmap(pixmap);
|
2022-05-30 20:29:53 +08:00
|
|
|
|
return batteryIcon;
|
2022-05-25 13:09:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 14:37:54 +08:00
|
|
|
|
PluginFlags PowerPlugin::flags() const
|
|
|
|
|
{
|
2023-01-30 13:14:32 +08:00
|
|
|
|
// 电池插件在任务栏上面展示,在快捷面板展示,并且可以拖动,可以在其前面插入其他插件,能在控制中心设置是否显示隐藏
|
2022-11-28 14:37:54 +08:00
|
|
|
|
return PluginFlag::Type_Common
|
|
|
|
|
| PluginFlag::Attribute_CanDrag
|
|
|
|
|
| PluginFlag::Attribute_CanInsert
|
2023-01-30 13:14:32 +08:00
|
|
|
|
| PluginFlag::Attribute_CanSetting
|
|
|
|
|
| PluginFlag::Quick_Single;
|
2019-01-30 18:00:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 20:07:52 +08:00
|
|
|
|
void PowerPlugin::updateBatteryVisible()
|
|
|
|
|
{
|
|
|
|
|
const bool exist = !m_powerInter->batteryPercentage().isEmpty();
|
|
|
|
|
|
2022-12-01 19:36:53 +08:00
|
|
|
|
if (exist)
|
2018-11-13 20:07:52 +08:00
|
|
|
|
m_proxyInter->itemAdded(this, POWER_KEY);
|
2022-12-01 19:36:53 +08:00
|
|
|
|
else
|
|
|
|
|
m_proxyInter->itemRemoved(this, POWER_KEY);
|
2018-11-13 20:07:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerPlugin::loadPlugin()
|
|
|
|
|
{
|
|
|
|
|
if (m_pluginLoaded) {
|
|
|
|
|
qDebug() << "power plugin has been loaded! return";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_pluginLoaded = true;
|
|
|
|
|
|
2021-05-25 13:05:45 +08:00
|
|
|
|
m_powerStatusWidget.reset(new PowerStatusWidget);
|
2022-05-25 13:09:26 +08:00
|
|
|
|
|
|
|
|
|
connect(m_powerStatusWidget.get(), &PowerStatusWidget::iconChanged, this, [ this ] {
|
2023-01-30 13:14:32 +08:00
|
|
|
|
m_proxyInter->updateDockInfo(this, DockPart::QuickPanel);
|
|
|
|
|
m_proxyInter->updateDockInfo(this, DockPart::QuickShow);
|
2022-05-25 13:09:26 +08:00
|
|
|
|
m_proxyInter->itemUpdate(this, POWER_KEY);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-13 20:07:52 +08:00
|
|
|
|
m_powerInter = new DBusPower(this);
|
|
|
|
|
|
2022-11-18 17:08:32 +08:00
|
|
|
|
m_systemPowerInter = new SystemPowerInter("org.deepin.dde.Power1", "/org/deepin/dde/Power1", QDBusConnection::systemBus(), this);
|
2019-05-13 16:15:37 +08:00
|
|
|
|
m_systemPowerInter->setSync(true);
|
|
|
|
|
|
2023-02-21 15:57:42 +08:00
|
|
|
|
connect(m_dconfig, &DConfig::valueChanged, this, &PowerPlugin::onGSettingsChanged);
|
2020-06-11 10:54:56 +08:00
|
|
|
|
connect(m_systemPowerInter, &SystemPowerInter::BatteryStatusChanged, [&](uint value) {
|
2020-06-03 16:36:37 +08:00
|
|
|
|
if (value == BatteryState::CHARGING)
|
2020-06-11 10:54:56 +08:00
|
|
|
|
m_preChargeTimer->start();
|
2020-06-03 16:36:37 +08:00
|
|
|
|
refreshTipsData();
|
|
|
|
|
});
|
2019-05-13 16:15:37 +08:00
|
|
|
|
connect(m_systemPowerInter, &SystemPowerInter::BatteryTimeToEmptyChanged, this, &PowerPlugin::refreshTipsData);
|
|
|
|
|
connect(m_systemPowerInter, &SystemPowerInter::BatteryTimeToFullChanged, this, &PowerPlugin::refreshTipsData);
|
|
|
|
|
|
2018-11-13 20:07:52 +08:00
|
|
|
|
connect(m_powerInter, &DBusPower::BatteryPercentageChanged, this, &PowerPlugin::updateBatteryVisible);
|
|
|
|
|
|
|
|
|
|
updateBatteryVisible();
|
2020-05-18 16:05:08 +08:00
|
|
|
|
|
|
|
|
|
onGSettingsChanged("showtimetofull");
|
2018-11-13 20:07:52 +08:00
|
|
|
|
}
|
2019-01-30 18:00:46 +08:00
|
|
|
|
|
2020-05-18 16:05:08 +08:00
|
|
|
|
void PowerPlugin::onGSettingsChanged(const QString &key)
|
|
|
|
|
{
|
|
|
|
|
if (key != "showtimetofull") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 15:57:42 +08:00
|
|
|
|
if (m_dconfig->isValid())
|
|
|
|
|
m_showTimeToFull = m_dconfig->value("showtimetofull").toBool();
|
2020-05-18 16:05:08 +08:00
|
|
|
|
|
|
|
|
|
refreshTipsData();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-13 11:38:51 +08:00
|
|
|
|
void PowerPlugin::refreshTipsData()
|
|
|
|
|
{
|
|
|
|
|
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
|
|
|
|
|
const uint percentage = qMin(100.0, qMax(0.0, data.value("Display")));
|
2023-03-17 18:24:09 +08:00
|
|
|
|
QString value = QString("%1%").arg(std::round(percentage));
|
2019-05-13 11:38:51 +08:00
|
|
|
|
const int batteryState = m_powerInter->batteryState()["Display"];
|
2023-03-17 18:24:09 +08:00
|
|
|
|
QFontMetrics ftm(m_labelText->font());
|
|
|
|
|
value = ftm.elidedText(value, Qt::TextElideMode::ElideMiddle, m_labelText->width());
|
2023-01-30 13:14:32 +08:00
|
|
|
|
m_labelText->setText(value);
|
2020-06-23 11:22:09 +08:00
|
|
|
|
if (m_preChargeTimer->isActive() && m_showTimeToFull) {
|
2020-06-11 10:54:56 +08:00
|
|
|
|
// 插入电源后,20秒内算作预充电时间,此时计算剩余充电时间是不准确的
|
|
|
|
|
QString tips = tr("Capacity %1 ...").arg(value);
|
|
|
|
|
m_tipsLabel->setText(tips);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 14:37:37 +08:00
|
|
|
|
if (batteryState == BatteryState::DIS_CHARGING || batteryState == BatteryState::NOT_CHARGED || batteryState == BatteryState::UNKNOWN) {
|
2020-05-07 14:03:51 +08:00
|
|
|
|
QString tips;
|
2019-05-13 16:15:37 +08:00
|
|
|
|
qulonglong timeToEmpty = m_systemPowerInter->batteryTimeToEmpty();
|
2019-05-13 11:38:51 +08:00
|
|
|
|
QDateTime time = QDateTime::fromTime_t(timeToEmpty).toUTC();
|
|
|
|
|
uint hour = time.toString("hh").toUInt();
|
|
|
|
|
uint min = time.toString("mm").toUInt();
|
2020-08-14 13:32:52 +08:00
|
|
|
|
uint sencond = time.toString("ss").toInt();
|
|
|
|
|
if (sencond > 0)
|
|
|
|
|
min += 1;
|
2020-06-23 11:22:09 +08:00
|
|
|
|
if (!m_showTimeToFull) {
|
|
|
|
|
tips = tr("Capacity %1").arg(value);
|
2020-05-07 14:03:51 +08:00
|
|
|
|
} else {
|
2020-06-23 11:22:09 +08:00
|
|
|
|
if (hour == 0) {
|
2021-06-16 11:02:02 +08:00
|
|
|
|
tips = tr("Capacity %1, %2 min remaining").arg(value).arg(min);
|
2020-06-23 11:22:09 +08:00
|
|
|
|
} else {
|
2020-05-18 16:05:08 +08:00
|
|
|
|
tips = tr("Capacity %1, %2 hr %3 min remaining").arg(value).arg(hour).arg(min);
|
|
|
|
|
}
|
2019-05-13 11:38:51 +08:00
|
|
|
|
}
|
|
|
|
|
m_tipsLabel->setText(tips);
|
2020-05-07 14:03:51 +08:00
|
|
|
|
} else if (batteryState == BatteryState::FULLY_CHARGED || percentage == 100.) {
|
2020-06-05 10:06:59 +08:00
|
|
|
|
m_tipsLabel->setText(tr("Capacity %1, fully charged").arg(value));
|
2020-05-07 14:03:51 +08:00
|
|
|
|
} else {
|
2019-12-16 15:34:10 +08:00
|
|
|
|
qulonglong timeToFull = m_systemPowerInter->batteryTimeToFull();
|
|
|
|
|
QDateTime time = QDateTime::fromTime_t(timeToFull).toUTC();
|
|
|
|
|
uint hour = time.toString("hh").toUInt();
|
|
|
|
|
uint min = time.toString("mm").toUInt();
|
|
|
|
|
QString tips;
|
2020-06-23 11:22:09 +08:00
|
|
|
|
if (!m_showTimeToFull) {
|
|
|
|
|
tips = tr("Charging %1").arg(value);
|
2020-06-11 10:54:56 +08:00
|
|
|
|
} else {
|
2020-06-23 11:22:09 +08:00
|
|
|
|
if (timeToFull == 0) { // 电量已充満或电量计算中,剩余充满时间会返回0
|
|
|
|
|
tips = tr("Capacity %1 ...").arg(value);
|
|
|
|
|
} else {
|
|
|
|
|
hour == 0 ? tips = tr("Charging %1, %2 min until full").arg(value).arg(min)
|
|
|
|
|
: tips = tr("Charging %1, %2 hr %3 min until full").arg(value).arg(hour).arg(min);
|
2020-05-18 16:05:08 +08:00
|
|
|
|
}
|
2019-05-13 11:38:51 +08:00
|
|
|
|
}
|
2019-12-16 15:34:10 +08:00
|
|
|
|
m_tipsLabel->setText(tips);
|
2019-05-13 11:38:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-30 13:14:32 +08:00
|
|
|
|
|
2023-02-01 13:29:58 +08:00
|
|
|
|
void PowerPlugin::onThemeTypeChanged(DGuiApplicationHelper::ColorType themeType)
|
2023-01-30 13:14:32 +08:00
|
|
|
|
{
|
2023-02-01 13:29:58 +08:00
|
|
|
|
const QPixmap pixmap = m_powerStatusWidget->getBatteryIcon(themeType);
|
|
|
|
|
m_imageLabel->setPixmap(pixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerPlugin::initUi()
|
|
|
|
|
{
|
|
|
|
|
m_tipsLabel->setVisible(false);
|
|
|
|
|
m_tipsLabel->setObjectName("power");
|
|
|
|
|
m_preChargeTimer->setInterval(DELAYTIME);
|
|
|
|
|
m_preChargeTimer->setSingleShot(true);
|
|
|
|
|
|
2023-01-30 13:14:32 +08:00
|
|
|
|
m_quickPanel = new QWidget();
|
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout(m_quickPanel);
|
|
|
|
|
layout->setAlignment(Qt::AlignVCenter);
|
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
layout->setSpacing(0);
|
|
|
|
|
m_imageLabel = new QLabel(m_quickPanel);
|
|
|
|
|
m_imageLabel->setObjectName("imageLabel");
|
|
|
|
|
m_imageLabel->setFixedHeight(24);
|
|
|
|
|
m_imageLabel->setAlignment(Qt::AlignCenter);
|
|
|
|
|
|
|
|
|
|
m_labelText = new QLabel(m_quickPanel);
|
|
|
|
|
m_labelText->setObjectName("textLabel");
|
2023-03-17 18:24:09 +08:00
|
|
|
|
m_labelText->setFixedHeight(15);
|
2023-01-30 13:14:32 +08:00
|
|
|
|
m_labelText->setAlignment(Qt::AlignCenter);
|
|
|
|
|
m_labelText->setFont(Dtk::Widget::DFontSizeManager::instance()->t10());
|
|
|
|
|
layout->addWidget(m_imageLabel);
|
|
|
|
|
layout->addSpacing(7);
|
|
|
|
|
layout->addWidget(m_labelText);
|
|
|
|
|
}
|
2023-02-01 13:29:58 +08:00
|
|
|
|
|
|
|
|
|
void PowerPlugin::initConnection()
|
|
|
|
|
{
|
|
|
|
|
connect(m_preChargeTimer,&QTimer::timeout,this,&PowerPlugin::refreshTipsData);
|
|
|
|
|
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &PowerPlugin::onThemeTypeChanged);
|
|
|
|
|
}
|