2 // C++ Implementation: batteryicon
4 // Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
6 // Copyright: See COPYING file that comes with this distribution
8 #include "batteryicon.h"
13 QDir
CBatteryIcon::sysfsDir(UI_PATH_SYSFS_DIR
);
15 CBatteryIcon::CBatteryIcon(Settings
* settings
, QString batteryName
, QObject
* parent
) :
16 QSystemTrayIcon(parent
),
20 m_Data
.name
= batteryName
;
23 CBatteryIcon::~CBatteryIcon() {
26 void CBatteryIcon::updateIcon() {
27 m_Icon
.fill(Qt::transparent
);
28 QPainter
painter(&m_Icon
);
30 if (m_Data
.currentCapacity
!= m_Data
.fullCapacity
) {
31 painter
.setPen(QColor(m_Settings
->colors
[UI_COLOR_PEN
]));
32 painter
.setBrush(QColor(m_Settings
->colors
[UI_COLOR_BRUSH_EMPTY
]));
33 painter
.drawRect(0, 4, 27, 23);
35 int chargedPixels
= (int)(22 * m_Data
.relativeCharge
/ 100.0);
37 painter
.fillRect(1, 5 + 22 - chargedPixels
, 26, chargedPixels
, QColor(m_Settings
->colors
[UI_COLOR_BRUSH_CHARGED
]));
39 painter
.setBrush(QColor(m_Settings
->colors
[UI_COLOR_BRUSH_POLE
]));
42 painter
.setPen(QColor(m_Settings
->colors
[UI_COLOR_PEN_FULL
]));
43 painter
.setBrush(QColor(m_Settings
->colors
[UI_COLOR_BRUSH_FULL
]));
44 painter
.drawRect(0, 4, 27, 23);
46 painter
.setBrush(QColor(m_Settings
->colors
[UI_COLOR_BRUSH_POLE_FULL
]));
48 painter
.drawRect(7, 0, 13, 4);
50 painter
.setBrush(Qt::NoBrush
);
52 if (m_Data
.relativeCharge
< 100)
53 ((QFont
&)painter
.font()).setPixelSize(15);
55 ((QFont
&)painter
.font()).setPixelSize(12);
57 painter
.setRenderHint(QPainter::TextAntialiasing
);
58 ((QFont
&)painter
.font()).setBold(true);
59 if (m_Data
.relativeCharge
== -1)
60 painter
.drawText(1, 9, 26, 16, Qt::AlignHCenter
, QString('?'));
62 painter
.drawText(1, 9, 26, 16, Qt::AlignHCenter
, QString::number(m_Data
.relativeCharge
));
67 void CBatteryIcon::updateToolTip() {
68 QString newToolTip
= tr("QBat - %1: %2%").arg(m_Data
.name
) +'\n';
70 if (m_Data
.relativeCharge
== -1)
71 newToolTip
= newToolTip
.arg('-');
73 newToolTip
= newToolTip
.arg(m_Data
.relativeCharge
);
75 switch (m_Data
.status
) {
76 case UI_BATTERY_DISCHARGING
:
77 newToolTip
+= tr("status: %1").arg(tr("dischaging"));
80 qreal remainingTime
= (qreal
)(m_Data
.currentCapacity
) / m_Data
.rate
;
81 int remainingHours
= (int)remainingTime
;
82 int remainungMinutes
= (int)(remainingTime
* 60) % 60;
83 newToolTip
+= tr("remaining time: %1:%2").arg(remainingHours
, 2, 10, QChar('0')).arg(remainungMinutes
, 2, 10, QChar('0'));
86 case UI_BATTERY_CHARGING
:
87 newToolTip
+= tr("status: %1").arg(tr("charging"));
88 if (m_Data
.rate
&& m_Data
.fullCapacity
) {
90 qreal remainingTime
= (qreal
)(m_Data
.fullCapacity
- m_Data
.currentCapacity
) / m_Data
.rate
;
91 int remainingHours
= (int)remainingTime
;
92 int remainungMinutes
= (int)(remainingTime
* 60) % 60;
93 newToolTip
+= tr("remaining time: %1:%2").arg(remainingHours
, 2, 10, QChar('0')).arg(remainungMinutes
, 2, 10, QChar('0'));
97 newToolTip
+= tr("status: %1").arg(tr("full"));
100 newToolTip
+= tr("status: %1").arg(tr("unknown"));
105 if (m_Data
.energyUnits
) {
106 if ((m_Data
.rate
) && (m_Data
.status
!= UI_BATTERY_FULL
)) {
107 double rateW
= qRound(m_Data
.rate
/ 100000.0) / 10.0;
108 double rateA
= qRound((m_Data
.rate
/ m_Data
.voltage
) / 1000.0) / 10.0;
109 newToolTip
+= tr("current rate: %1W / %2A").arg(rateW
).arg(rateA
) + '\n';
112 newToolTip
+= tr("current capacity: %1mWh").arg(m_Data
.currentCapacity
/ 1000);
114 if (m_Data
.fullCapacity
)
115 newToolTip
+= '\n' + tr("last full capacity: %1mWh").arg(m_Data
.fullCapacity
/ 1000);
117 if (m_Data
.designCapacity
)
118 newToolTip
+= '\n' + tr("design capacity: %1mWh").arg(m_Data
.designCapacity
/ 1000);
122 if ((m_Data
.rate
) && (m_Data
.status
!= UI_BATTERY_FULL
)) {
123 double rateA
= m_Data
.rate
/ 100000.0;
124 double rateW
= qRound(rateA
* m_Data
.voltage
/ 100.0) / 10.0;
125 newToolTip
+= tr("current rate: %1W / %2A").arg(rateW
).arg(qRound(rateA
) / 10.0) + '\n';
128 newToolTip
+= tr("current capacity: %1mAh").arg(m_Data
.currentCapacity
/ 1000);
130 if (m_Data
.fullCapacity
)
131 newToolTip
+= '\n' + tr("last full capacity: %1mAh").arg(m_Data
.fullCapacity
/ 1000);
133 if (m_Data
.designCapacity
)
134 newToolTip
+= '\n' + tr("design capacity: %1mAh").arg(m_Data
.designCapacity
/ 1000);
136 setToolTip(newToolTip
);
139 void CBatteryIcon::updateData(int currentCapacity
, int fullCapacity
, int designCapacity
, int rate
, int voltage
, int status
, bool energyUnits
) {
140 m_Data
.energyUnits
= energyUnits
;
142 bool noupdate
= true;
144 if (rate
!= m_Data
.rate
) {
149 if (voltage
!= m_Data
.voltage
) {
151 m_Data
.voltage
= voltage
;
154 if (status
!= m_Data
.status
) {
156 m_Data
.status
= status
;
159 if (fullCapacity
!= m_Data
.fullCapacity
) {
161 m_Data
.fullCapacity
= fullCapacity
;
164 if (designCapacity
!= m_Data
.designCapacity
) {
166 m_Data
.designCapacity
= designCapacity
;
169 if (currentCapacity
!= m_Data
.currentCapacity
) {
171 m_Data
.currentCapacity
= currentCapacity
;
177 qint8 newRelativeCharge
= calcRelativeDef(currentCapacity
, fullCapacity
);
179 if (newRelativeCharge
!= m_Data
.relativeCharge
) {
180 m_Data
.relativeCharge
= newRelativeCharge
;
190 void CBatteryIcon::updateData() {
191 int currentCapacity
= 0;
192 int fullCapacity
= 0;
193 int designCapacity
= 0;
197 bool energyUnits
= sysfsDir
.exists(m_Data
.name
+ UI_CAPTION_NOW(UI_CAPTION_ENERGY
));
199 rate
= readIntSysFile(sysfsDir
.filePath(m_Data
.name
+ "/current_now").toAscii().constData());
200 voltage
= readIntSysFile(sysfsDir
.filePath(m_Data
.name
+ UI_CAPTION_NOW(UI_CAPTION_VOLTAGE
)).toAscii().constData()) / 10000;
202 char buffer
[BUF_SIZE
];
203 readStringFromFile(buffer
, sysfsDir
.filePath(m_Data
.name
+ "/status").toAscii().constData());
204 status
= toStatusInt(buffer
);
207 fullCapacity
= readIntSysFile(sysfsDir
.filePath(m_Data
.name
+ UI_CAPTION_FULL(UI_CAPTION_ENERGY
)).toAscii().constData());
208 designCapacity
= readIntSysFile(sysfsDir
.filePath(m_Data
.name
+ UI_CAPTION_DESIGN(UI_CAPTION_ENERGY
)).toAscii().constData());
209 currentCapacity
= readIntSysFile(sysfsDir
.filePath(m_Data
.name
+ UI_CAPTION_NOW(UI_CAPTION_ENERGY
)).toAscii().constData());
212 fullCapacity
= readIntSysFile(sysfsDir
.filePath(m_Data
.name
+ UI_CAPTION_FULL(UI_CAPTION_CHARGE
)).toAscii().constData());
213 designCapacity
= readIntSysFile(sysfsDir
.filePath(m_Data
.name
+ UI_CAPTION_DESIGN(UI_CAPTION_CHARGE
)).toAscii().constData());
214 currentCapacity
= readIntSysFile(sysfsDir
.filePath(m_Data
.name
+ UI_CAPTION_NOW(UI_CAPTION_CHARGE
)).toAscii().constData());
217 updateData(currentCapacity
, fullCapacity
, designCapacity
, rate
, voltage
, status
, energyUnits
);