[apps] Add a battery icon view

Change-Id: I20a90a2330c7851362dd87bec390241253ff70ef
This commit is contained in:
Émilie Feral
2017-01-23 18:24:19 +01:00
parent 0704ab8776
commit 91efb72253
3 changed files with 59 additions and 0 deletions

37
apps/battery_view.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "battery_view.h"
BatteryView::BatteryView() :
View(),
m_chargeState(Ion::Battery::Charge::SOMEWHERE_INBETWEEN)
{
}
void BatteryView::setChargeState(Ion::Battery::Charge chargeState) {
if (chargeState != m_chargeState) {
m_chargeState = chargeState;
markRectAsDirty(bounds());
}
}
void BatteryView::drawRect(KDContext * ctx, KDRect rect) const {
/* We draw from left to right. The middle part representing the battery
*'content' depends on the charge */
ctx->fillRect(KDRect(0, 0, k_elementWidth, k_batteryHeight), KDColorWhite);
if (m_chargeState == Ion::Battery::Charge::EMPTY) {
ctx->fillRect(KDRect(k_elementWidth+k_separatorThickness, 0, 2*k_elementWidth, k_batteryHeight), Palette::LowBattery);
ctx->fillRect(KDRect(3*k_elementWidth+k_separatorThickness, 0, k_batteryWidth-5*k_elementWidth-2*k_separatorThickness, k_batteryHeight), Palette::YellowTwo);
}
if (m_chargeState == Ion::Battery::Charge::SOMEWHERE_INBETWEEN) {
ctx->fillRect(KDRect(k_elementWidth+k_separatorThickness, 0, (k_batteryWidth-3*k_elementWidth-2*k_separatorThickness)/2, k_batteryHeight), KDColorWhite);
ctx->fillRect(KDRect(k_elementWidth+k_separatorThickness+(k_batteryWidth-3*k_elementWidth-2*k_separatorThickness)/2, 0, (k_batteryWidth-3*k_elementWidth-2*k_separatorThickness)/2, k_batteryHeight), Palette::YellowTwo);
}
if (m_chargeState == Ion::Battery::Charge::FULL) {
ctx->fillRect(KDRect(k_elementWidth+k_separatorThickness, 0, k_batteryWidth-3*k_elementWidth-2*k_separatorThickness, k_batteryHeight), KDColorWhite);
}
ctx->fillRect(KDRect(k_batteryWidth-2*k_elementWidth, 0, k_elementWidth, k_batteryHeight), KDColorWhite);
ctx->fillRect(KDRect(k_batteryWidth-k_elementWidth, (k_batteryHeight-k_capHeight)/2, k_elementWidth, k_capHeight), KDColorWhite);
}
KDSize BatteryView::minimalSizeForOptimalDisplay() {
return KDSize(k_batteryWidth, k_batteryHeight);
}