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"
17 CBatteryIcon::CBatteryIcon(QString batteryName
, Settings
* settings
, QMenu
* contextMenu
, QObject
* parent
) :
18 QSystemTrayIcon(parent
),
19 m_BatteryName(batteryName
),
24 m_Icon
.fill(Qt::transparent
);
26 setContextMenu(contextMenu
);
31 CBatteryIcon::~CBatteryIcon() {
34 void CBatteryIcon::updateIcon() {
35 m_Icon
.fill(Qt::transparent
);
36 QPainter
painter(&m_Icon
);
38 if (m_ChargeNow
!= m_ChargeFull
) {
39 painter
.setPen(QColor(m_Settings
->colors
[UI_COLOR_PEN
]));
40 painter
.setBrush(QColor(m_Settings
->colors
[UI_COLOR_BRUSH_EMPTY
]));
41 painter
.drawRect(0, 4, 27, 23);
43 int chargedPixels
= (int)(22 * m_RelativeCharge
/ 100.0);
45 painter
.fillRect(1, 5 + 22 - chargedPixels
, 26, chargedPixels
, QColor(m_Settings
->colors
[UI_COLOR_BRUSH_CHARGED
]));
47 painter
.setBrush(QColor(m_Settings
->colors
[UI_COLOR_BRUSH_POLE
]));
50 painter
.setPen(QColor(m_Settings
->colors
[UI_COLOR_PEN_FULL
]));
51 painter
.setBrush(QColor(m_Settings
->colors
[UI_COLOR_BRUSH_FULL
]));
52 painter
.drawRect(0, 4, 27, 23);
54 painter
.setBrush(QColor(m_Settings
->colors
[UI_COLOR_BRUSH_POLE_FULL
]));
56 painter
.drawRect(7, 0, 13, 4);
58 painter
.setBrush(Qt::NoBrush
);
60 if (m_RelativeCharge
< 100)
61 ((QFont
&)painter
.font()).setPixelSize(15);
63 ((QFont
&)painter
.font()).setPixelSize(12);
65 painter
.setRenderHint(QPainter::TextAntialiasing
);
66 ((QFont
&)painter
.font()).setBold(true);
67 painter
.drawText(1, 9, 26, 16, Qt::AlignHCenter
, QString::number(m_RelativeCharge
));
72 void CBatteryIcon::updateToolTip() {
73 QString newToolTip
= tr("QBat - %1: %2%").arg(m_BatteryName
) +'\n';
75 if (m_RelativeCharge
== -1)
76 newToolTip
= newToolTip
.arg('-');
78 newToolTip
= newToolTip
.arg(m_RelativeCharge
);
81 case UI_BATTERY_DISCHARGING
:
82 newToolTip
+= tr("status: %1").arg(tr("dischaging"));
85 qreal remainingTime
= (qreal
)m_ChargeNow
/ m_CurrentNow
;
86 int remainingHours
= (int)remainingTime
;
87 int remainungMinutes
= (int)(remainingTime
* 60) % 60;
88 newToolTip
+= tr("remaining time: %1:%2").arg(remainingHours
, 2, 10, QChar('0')).arg(remainungMinutes
, 2, 10, QChar('0'));
91 case UI_BATTERY_CHARGING
:
92 newToolTip
+= tr("status: %1").arg(tr("charging"));
93 if (m_CurrentNow
&& m_ChargeFull
) {
95 qreal remainingTime
= (qreal
)(m_ChargeFull
- m_ChargeNow
) / m_CurrentNow
;
96 int remainingHours
= (int)remainingTime
;
97 int remainungMinutes
= (int)(remainingTime
* 60) % 60;
98 newToolTip
+= tr("remaining time: %1:%2").arg(remainingHours
, 2, 10, QChar('0')).arg(remainungMinutes
, 2, 10, QChar('0'));
101 case UI_BATTERY_FULL
:
102 newToolTip
+= tr("status: %1").arg(tr("full"));
105 newToolTip
+= tr("status: %1").arg(tr("unknown"));
111 if (m_Status
!= UI_BATTERY_FULL
)
112 newToolTip
+= tr("current rate: %1W").arg(qRound(m_CurrentNow
/ 100000.0) / 10.0) + '\n';
114 newToolTip
+= tr("current capacity: %2mWh").arg(m_ChargeNow
/ 1000);
117 newToolTip
+= '\n' + tr("last full capacity: %3mWh").arg(m_ChargeFull
/ 1000);
119 if (m_ChargeFullDesign
)
120 newToolTip
+= '\n' + tr("design capacity: %4mWh").arg(m_ChargeFullDesign
/ 1000);
124 if (m_Status
!= UI_BATTERY_FULL
)
125 newToolTip
+= tr("current rate: %1A").arg(qRound(m_CurrentNow
/ 100000.0) / 10.0) + '\n';
127 newToolTip
+= tr("current capacity: %2mAh").arg(m_ChargeNow
/ 1000);
130 newToolTip
+= '\n' + tr("last full capacity: %3mAh").arg(m_ChargeFull
/ 1000);
132 if (m_ChargeFullDesign
)
133 newToolTip
+= '\n' + tr("design capacity: %4mAh").arg(m_ChargeFullDesign
/ 1000);
135 setToolTip(newToolTip
);
138 void CBatteryIcon::updateData(int chargeFull
, int chargeFullDesign
, int chargeNow
, int currentNow
, int status
, bool energyUnits
) {
139 bool noupdate
= true;
140 m_energyUnits
= energyUnits
;
142 if (chargeNow
!= m_ChargeNow
) {
144 m_ChargeNow
= chargeNow
;
147 if (currentNow
!= m_CurrentNow
) {
149 m_CurrentNow
= currentNow
;
152 if (status
!= m_Status
) {
157 if (chargeFull
!= m_ChargeFullDesign
) {
159 m_ChargeFull
= chargeFull
;
162 if (chargeFullDesign
!= m_ChargeFullDesign
) {
164 m_ChargeFullDesign
= chargeFullDesign
;
170 qint8 newRelativeCharge
= 0;
173 newRelativeCharge
= (qint8
)(100.0 * m_ChargeNow
/ m_ChargeFull
);
175 newRelativeCharge
= -1;
177 if (newRelativeCharge
!= m_RelativeCharge
) {
178 m_RelativeCharge
= newRelativeCharge
;