listenerri 67635df6fd refactor: handle click event of system trays
Change-Id: I31b9ecf03e4b9937fa19a3209f5e64e6219b2327
2018-10-25 16:52:13 +08:00

134 lines
4.0 KiB
C++

/*
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
*
* 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/>.
*/
#include "powertraywidget.h"
#include <QPainter>
#include <QIcon>
#include <QMouseEvent>
#define BATTERY_DISCHARED 2
#define BATTERY_FULL 4
PowerTrayWidget::PowerTrayWidget(QWidget *parent)
: AbstractSystemTrayWidget(parent),
m_powerInter(new DBusPower(this)),
m_tipsLabel(new TipsWidget)
{
m_tipsLabel->setVisible(false);
connect(m_powerInter, &DBusPower::BatteryPercentageChanged, this, &PowerTrayWidget::updateIcon);
connect(m_powerInter, &DBusPower::BatteryStateChanged, this, &PowerTrayWidget::updateIcon);
connect(m_powerInter, &DBusPower::OnBatteryChanged, this, &PowerTrayWidget::updateIcon);
}
void PowerTrayWidget::setActive(const bool active)
{
}
void PowerTrayWidget::updateIcon()
{
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
const uint value = qMin(100.0, qMax(0.0, data.value("Display")));
const int percentage = std::round(value);
const bool plugged = !m_powerInter->onBattery();
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";
}
const QString iconStr = QString("battery-%1-%2")
.arg(percentageStr)
.arg(plugged ? "plugged-symbolic" : "symbolic");
const auto ratio = devicePixelRatioF();
m_pixmap = QIcon::fromTheme(iconStr).pixmap(QSize(16, 16) * ratio);
m_pixmap.setDevicePixelRatio(ratio);
update();
}
const QImage PowerTrayWidget::trayImage()
{
return m_pixmap.toImage();
}
QWidget *PowerTrayWidget::trayTipsWidget()
{
const BatteryPercentageMap data = m_powerInter->batteryPercentage();
if (data.isEmpty())
{
m_tipsLabel->setText(tr("Shut down"));
return m_tipsLabel;
}
const uint percentage = qMin(100.0, qMax(0.0, data.value("Display")));
const QString value = QString("%1%").arg(std::round(percentage));
const bool charging = !m_powerInter->onBattery();
if (!charging)
m_tipsLabel->setText(tr("Remaining Capacity %1").arg(value));
else
{
const int batteryState = m_powerInter->batteryState()["Display"];
if (batteryState == BATTERY_FULL || percentage == 100.)
m_tipsLabel->setText(tr("Charged %1").arg(value));
else
m_tipsLabel->setText(tr("Charging %1").arg(value));
}
return m_tipsLabel;
}
const QString PowerTrayWidget::trayClickCommand()
{
return QString("dbus-send --print-reply --dest=com.deepin.dde.ControlCenter /com/deepin/dde/ControlCenter com.deepin.dde.ControlCenter.ShowModule \"string:power\"");
}
QSize PowerTrayWidget::sizeHint() const
{
return QSize(26, 26);
}
void PowerTrayWidget::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
const auto ratio = devicePixelRatioF();
QPainter painter(this);
painter.drawPixmap(rect().center() - m_pixmap.rect().center() / ratio, m_pixmap);
}