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)(21 * m_RelativeCharge
/ 100.0);
44 painter
.fillRect(1, 1 + 26 - 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(8, 0, 11, 4);
57 painter
.setBrush(Qt::NoBrush
);
59 ((QFont
&)painter
.font()).setPixelSize(14);
60 ((QFont
&)painter
.font()).setBold(true);
61 painter
.drawText(1, 10, 26, 15, Qt::AlignHCenter
, QString::number(m_RelativeCharge
));
66 void CBatteryIcon::updateToolTip() {
67 QString newToolTip
= tr("QBat - %1: %2%").arg(m_BatteryName
) +'\n';
69 if (m_RelativeCharge
== -1)
70 newToolTip
= newToolTip
.arg('-');
72 newToolTip
= newToolTip
.arg(m_RelativeCharge
);
75 case UI_BATTERY_DISCHARGING
:
76 newToolTip
+= tr("status: %1").arg(tr("dischaging"));
79 qreal remainingTime
= (qreal
)m_ChargeNow
/ m_CurrentNow
;
80 int remainingHours
= (int)remainingTime
;
81 int remainungMinutes
= (int)(remainingTime
* 60) % 60;
82 newToolTip
+= tr("remaining time: %1:%2").arg(remainingHours
, 2, 10, QChar('0')).arg(remainungMinutes
, 2, 10, QChar('0'));
85 case UI_BATTERY_CHARGING
:
86 newToolTip
+= tr("status: %1").arg(tr("charging"));
89 newToolTip
+= tr("status: %1").arg(tr("full"));
92 newToolTip
+= tr("status: %1").arg(tr("unknown"));
97 if (m_Status
!= UI_BATTERY_FULL
)
98 newToolTip
+= tr("current rate: %1A").arg(qRound(m_CurrentNow
/ 100000.0) / 10.0) + '\n';
100 newToolTip
+= tr("current capacity: %2mAh").arg(m_ChargeNow
/ 1000) + '\n';
103 newToolTip
+= tr("last full capacity: %3mAh").arg(m_ChargeFull
/ 1000) + '\n';
105 if (m_ChargeFullDesign
)
106 newToolTip
+= tr("design capacity: %4mAh").arg(m_ChargeFullDesign
/ 1000);
108 setToolTip(newToolTip
);
111 void CBatteryIcon::updateData(int chargeFull
, int chargeFullDesign
, int chargeNow
, int currentNow
, int status
) {
113 if (chargeNow
== m_ChargeNow
)
116 m_ChargeNow
= chargeNow
;
118 if (currentNow
== m_CurrentNow
)
121 m_CurrentNow
= currentNow
;
123 if (status
== m_Status
)
128 if (chargeFull
== m_ChargeFullDesign
)
131 m_ChargeFullDesign
= chargeFull
;
133 if (chargeFullDesign
== m_ChargeFullDesign
)
136 m_ChargeFullDesign
= chargeFullDesign
;
141 qint8 newRelativeCharge
= 0;
144 newRelativeCharge
= (qint8
)(100.0 * m_ChargeNow
/ m_ChargeFull
);
146 newRelativeCharge
= -1;
148 if (newRelativeCharge
!= m_RelativeCharge
) {
149 m_RelativeCharge
= newRelativeCharge
;