removed obsolete d-bus dependecy
[qbat.git] / batteryicon.cpp
blobc2e60ecb49b4215b0e6e37083b06f870b65dcdad
1 //
2 // C++ Implementation: batteryicon
3 //
4 // Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
5 //
6 // Copyright: See COPYING file that comes with this distribution
7 //
8 #include "batteryicon.h"
9 #include "common.h"
10 #include <string>
11 #include <QDir>
12 #include <QPainter>
14 namespace qbat {
15 using namespace std;
17 CBatteryIcon::CBatteryIcon(QString batteryName, Settings * settings, QMenu * contextMenu, QObject * parent) :
18 QSystemTrayIcon(parent),
19 m_BatteryName(batteryName),
20 m_Icon(28, 28),
21 m_Settings(settings)
23 m_Icon.fill(Qt::transparent);
24 setIcon(m_Icon);
25 setContextMenu(contextMenu);
26 //updateData();
27 show();
30 CBatteryIcon::~CBatteryIcon() {
33 void CBatteryIcon::updateIcon() {
34 m_Icon.fill(Qt::transparent);
35 QPainter painter(&m_Icon);
37 if (m_ChargeNow != m_ChargeFull) {
38 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN]));
39 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_EMPTY]));
40 painter.drawRect(0, 4, 27, 23);
42 int chargedPixels = (int)(22 * m_RelativeCharge / 100.0);
44 painter.fillRect(1, 5 + 22 - chargedPixels, 26, chargedPixels, QColor(m_Settings->colors[UI_COLOR_BRUSH_CHARGED]));
46 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE]));
48 else {
49 painter.setPen(QColor(m_Settings->colors[UI_COLOR_PEN_FULL]));
50 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_FULL]));
51 painter.drawRect(0, 4, 27, 23);
53 painter.setBrush(QColor(m_Settings->colors[UI_COLOR_BRUSH_POLE_FULL]));
55 painter.drawRect(7, 0, 13, 4);
57 painter.setBrush(Qt::NoBrush);
59 if (m_RelativeCharge < 100)
60 ((QFont&)painter.font()).setPixelSize(15);
61 else
62 ((QFont&)painter.font()).setPixelSize(12);
64 painter.setRenderHint(QPainter::TextAntialiasing);
65 ((QFont&)painter.font()).setBold(true);
66 painter.drawText(1, 9, 26, 16, Qt::AlignHCenter, QString::number(m_RelativeCharge));
68 setIcon(m_Icon);
71 void CBatteryIcon::updateToolTip() {
72 QString newToolTip = tr("QBat - %1: %2%").arg(m_BatteryName) +'\n';
74 if (m_RelativeCharge == -1)
75 newToolTip = newToolTip.arg('-');
76 else
77 newToolTip = newToolTip.arg(m_RelativeCharge);
79 switch (m_Status) {
80 case UI_BATTERY_DISCHARGING:
81 newToolTip += tr("status: %1").arg(tr("dischaging"));
82 if (m_CurrentNow) {
83 newToolTip += '\n';
84 qreal remainingTime = (qreal)m_ChargeNow / m_CurrentNow;
85 int remainingHours = (int)remainingTime;
86 int remainungMinutes = (int)(remainingTime * 60) % 60;
87 newToolTip += tr("remaining time: %1:%2").arg(remainingHours, 2, 10, QChar('0')).arg(remainungMinutes, 2, 10, QChar('0'));
89 break;
90 case UI_BATTERY_CHARGING:
91 newToolTip += tr("status: %1").arg(tr("charging"));
92 break;
93 case UI_BATTERY_FULL:
94 newToolTip += tr("status: %1").arg(tr("full"));
95 break;
96 default:
97 newToolTip += tr("status: %1").arg(tr("unknown"));
98 break;
100 newToolTip += '\n';
102 if (m_Status != UI_BATTERY_FULL)
103 newToolTip += tr("current rate: %1A").arg(qRound(m_CurrentNow / 100000.0) / 10.0) + '\n';
105 newToolTip += tr("current capacity: %2mAh").arg(m_ChargeNow / 1000) + '\n';
107 if (m_ChargeFull)
108 newToolTip += tr("last full capacity: %3mAh").arg(m_ChargeFull / 1000) + '\n';
110 if (m_ChargeFullDesign)
111 newToolTip += tr("design capacity: %4mAh").arg(m_ChargeFullDesign / 1000);
113 setToolTip(newToolTip);
116 void CBatteryIcon::updateData(int chargeFull, int chargeFullDesign, int chargeNow, int currentNow, int status) {
117 bool noupdate = true;
118 if (chargeNow != m_ChargeNow) {
119 noupdate = false;
120 m_ChargeNow = chargeNow;
123 if (currentNow != m_CurrentNow) {
124 noupdate = false;
125 m_CurrentNow = currentNow;
128 if (status != m_Status) {
129 noupdate = false;
130 m_Status = status;
133 if (chargeFull != m_ChargeFullDesign) {
134 noupdate = false;
135 m_ChargeFull = chargeFull;
138 if (chargeFullDesign != m_ChargeFullDesign) {
139 noupdate = false;
140 m_ChargeFullDesign = chargeFullDesign;
143 if (noupdate)
144 return;
146 qint8 newRelativeCharge = 0;
148 if (m_ChargeFull)
149 newRelativeCharge = (qint8)(100.0 * m_ChargeNow / m_ChargeFull);
150 else
151 newRelativeCharge = -1;
153 if (newRelativeCharge != m_RelativeCharge) {
154 m_RelativeCharge = newRelativeCharge;
155 updateIcon();
157 updateToolTip();