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
),
23 m_Icon
.fill(Qt::transparent
);
25 setContextMenu(contextMenu
);
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
]));
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);
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
));
71 void CBatteryIcon::updateToolTip() {
72 QString newToolTip
= tr("QBat - %1: %2%").arg(m_BatteryName
) +'\n';
74 if (m_RelativeCharge
== -1)
75 newToolTip
= newToolTip
.arg('-');
77 newToolTip
= newToolTip
.arg(m_RelativeCharge
);
80 case UI_BATTERY_DISCHARGING
:
81 newToolTip
+= tr("status: %1").arg(tr("dischaging"));
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'));
90 case UI_BATTERY_CHARGING
:
91 newToolTip
+= tr("status: %1").arg(tr("charging"));
94 newToolTip
+= tr("status: %1").arg(tr("full"));
97 newToolTip
+= tr("status: %1").arg(tr("unknown"));
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';
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
) {
120 m_ChargeNow
= chargeNow
;
123 if (currentNow
!= m_CurrentNow
) {
125 m_CurrentNow
= currentNow
;
128 if (status
!= m_Status
) {
133 if (chargeFull
!= m_ChargeFullDesign
) {
135 m_ChargeFull
= chargeFull
;
138 if (chargeFullDesign
!= m_ChargeFullDesign
) {
140 m_ChargeFullDesign
= chargeFullDesign
;
146 qint8 newRelativeCharge
= 0;
149 newRelativeCharge
= (qint8
)(100.0 * m_ChargeNow
/ m_ChargeFull
);
151 newRelativeCharge
= -1;
153 if (newRelativeCharge
!= m_RelativeCharge
) {
154 m_RelativeCharge
= newRelativeCharge
;