feat:add battery display

This commit is contained in:
fpc_diesel 2020-05-18 16:05:08 +08:00
parent 3354169738
commit f5c9133591
5 changed files with 82 additions and 16 deletions

View File

@ -20,6 +20,7 @@ set_target_properties(${PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ../syst
target_include_directories(${PLUGIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS}
${Qt5DBus_INCLUDE_DIRS}
${DFrameworkDBus_INCLUDE_DIRS}
${QGSettings_INCLUDE_DIRS}
../../interfaces)
target_link_libraries(${PLUGIN_NAME} PRIVATE
${DtkWidget_LIBRARIES}
@ -27,6 +28,7 @@ target_link_libraries(${PLUGIN_NAME} PRIVATE
${Qt5Svg_LIBRARIES}
${Qt5DBus_LIBRARIES}
${DFrameworkDBus_LIBRARIES}
${QGSettings_LIBRARIES}
)
install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins/system-trays)

View File

@ -23,14 +23,21 @@
#include "dbus/dbusaccount.h"
#include <QIcon>
#include <QGSettings>
#define PLUGIN_STATE_KEY "enable"
PowerPlugin::PowerPlugin(QObject *parent)
: QObject(parent),
static QGSettings *GSettingsByApp()
{
static QGSettings settings("com.deepin.dde.dock.module.power");
return &settings;
}
m_pluginLoaded(false),
m_tipsLabel(new TipsWidget)
PowerPlugin::PowerPlugin(QObject *parent)
: QObject(parent)
, m_pluginLoaded(false)
, m_showTimeToFull(true)
, m_tipsLabel(new TipsWidget)
{
m_tipsLabel->setVisible(false);
m_tipsLabel->setObjectName("power");
@ -181,6 +188,7 @@ void PowerPlugin::loadPlugin()
m_systemPowerInter = new SystemPowerInter("com.deepin.system.Power", "/com/deepin/system/Power", QDBusConnection::systemBus(), this);
m_systemPowerInter->setSync(true);
connect(GSettingsByApp(), &QGSettings::changed, this, &PowerPlugin::onGSettingsChanged);
connect(m_systemPowerInter, &SystemPowerInter::BatteryStatusChanged, this, &PowerPlugin::refreshTipsData);
connect(m_systemPowerInter, &SystemPowerInter::BatteryTimeToEmptyChanged, this, &PowerPlugin::refreshTipsData);
connect(m_systemPowerInter, &SystemPowerInter::BatteryTimeToFullChanged, this, &PowerPlugin::refreshTipsData);
@ -188,6 +196,8 @@ void PowerPlugin::loadPlugin()
connect(m_powerInter, &DBusPower::BatteryPercentageChanged, this, &PowerPlugin::updateBatteryVisible);
updateBatteryVisible();
onGSettingsChanged("showtimetofull");
}
void PowerPlugin::refreshPluginItemsVisible()
@ -203,6 +213,20 @@ void PowerPlugin::refreshPluginItemsVisible()
}
}
void PowerPlugin::onGSettingsChanged(const QString &key)
{
if (key != "showtimetofull") {
return;
}
if (GSettingsByApp()->keys().contains("showtimetofull")) {
const bool isEnable = GSettingsByApp()->keys().contains("showtimetofull") && GSettingsByApp()->get("showtimetofull").toBool();
m_showTimeToFull = isEnable && GSettingsByApp()->get("showtimetofull").toBool();
}
refreshTipsData();
}
void PowerPlugin::refreshTipsData()
{
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
@ -220,9 +244,19 @@ void PowerPlugin::refreshTipsData()
if (min == 0)
tips = tr("Charged");
else
tips = tr("Capacity %1, %2 min remaining").arg(value).arg(min);
{
if(m_showTimeToFull)
tips = tr("Capacity %1, %2 min remaining").arg(value).arg(min);
else {
tips = tr("Capacity %1").arg(value);
}
}
} else {
tips = tr("Capacity %1, %2 hr %3 min remaining").arg(value).arg(hour).arg(min);
if(m_showTimeToFull)
tips = tr("Capacity %1, %2 hr %3 min remaining").arg(value).arg(hour).arg(min);
else {
tips = tr("Capacity %1").arg(value).arg(hour);
}
}
m_tipsLabel->setText(tips);
@ -237,9 +271,17 @@ void PowerPlugin::refreshTipsData()
if(timeToFull == 0) { // 电量已充満或电量计算中,剩余充满时间会返回0
tips = tr("Capacity %1 ....").arg(value);
} else if (hour == 0) {
tips = tr("Charging %1, %2 min until full").arg(value).arg(min);
if(m_showTimeToFull)
tips = tr("Charging %1, %2 min until full").arg(value).arg(min);
else {
tips = tr("Charging %1").arg(value);
}
} else {
tips = tr("Charging %1, %2 hr %3 min until full").arg(value).arg(hour).arg(min);
if(m_showTimeToFull)
tips = tr("Charging %1, %2 hr %3 min until full").arg(value).arg(hour).arg(min);
else {
tips = tr("Charging %1").arg(value);
}
}
m_tipsLabel->setText(tips);

View File

@ -72,10 +72,12 @@ private:
void updateBatteryVisible();
void loadPlugin();
void refreshPluginItemsVisible();
void onGSettingsChanged(const QString &key);
void refreshTipsData();
private:
bool m_pluginLoaded;
bool m_showTimeToFull;
PowerStatusWidget *m_powerStatusWidget;
TipsWidget *m_tipsLabel;

View File

@ -71,22 +71,34 @@ QPixmap PowerStatusWidget::getBatteryIcon()
const bool plugged = !m_powerInter->onBattery();
/*根据新需求,电池电量显示分别是*/
/* 0-5%; 6-20% 21-40% 41-60% 61-80% 81-100% */
/* 0-5%、6-10%、11%-20%、21-30%、31-40%、41-50%、51-60%、61%-70%、71-80%、81-90%、91-100% */
QString percentageStr;
if (percentage <= 5 && percentage >= 0) {
if (percentage < 0) {
percentageStr = "000";
} else if (percentage <= 5 && percentage >= 0) {
percentageStr = "000";
} else if (percentage <= 10) {
percentageStr = "010";
} else if (percentage <= 20) {
percentageStr = "020";
} else if (percentage <= 40) {
} else if (percentage <= 30) {
percentageStr = "030";
}else if (percentage <= 40) {
percentageStr = "040";
} else if (percentage <= 60) {
} else if (percentage <= 50) {
percentageStr = "050";
}else if (percentage <= 60) {
percentageStr = "060";
} else if (percentage < 80) {
} else if (percentage <= 70) {
percentageStr = "070";
}else if (percentage < 80) {
percentageStr = "080";
} else if (percentage <= 100){
} else if (percentage <= 90) {
percentageStr = "090";
}else if (percentage <= 100){
percentageStr = "100";
} else {
percentageStr = "000";
percentageStr = "100";
}
QString iconStr = QString("battery-%1-%2")

View File

@ -265,7 +265,7 @@
</message>
<message>
<source>Charging %1 ....</source>
<translation>Charging %1 ....</translation>
<translation type="vanished">Charging %1 ....</translation>
</message>
<message>
<source>Charged</source>
@ -275,6 +275,14 @@
<source>Capacity %1 ....</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Capacity %1</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Charging %1</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>ShowDesktopPlugin</name>