From 3b08e4c358c58c836b9aaa2c8d41e5c300e18f9d Mon Sep 17 00:00:00 2001 From: Mateusz Grzywacz Date: Tue, 15 Dec 2020 13:09:11 +0100 Subject: [PATCH] [EGD-4932] new battery icons + battery in TopBar rework --- module-apps/Application.cpp | 14 ++-- module-apps/windows/AppWindow.cpp | 6 +- module-apps/windows/AppWindow.hpp | 2 +- module-gui/gui/widgets/TopBar.cpp | 86 ++++++++++++++----------- module-gui/gui/widgets/TopBar.hpp | 14 ++-- module-utils/common_data/EventStore.hpp | 1 + 6 files changed, 68 insertions(+), 55 deletions(-) diff --git a/module-apps/Application.cpp b/module-apps/Application.cpp index 340494002a62f8ccd363849b03cf8e69c118a253..09c70fd8193cea8e46ad254f8a549e602dda6cb5 100644 --- a/module-apps/Application.cpp +++ b/module-apps/Application.cpp @@ -126,7 +126,7 @@ namespace app if (state == State::ACTIVE_FORGROUND) { auto window = getCurrentWindow(); if (Store::Battery::get().state == Store::Battery::State::Charging) { - window->batteryCharging(true); + window->batteryChargingChange(true); } else { window->updateBatteryLevel(Store::Battery::get().level); @@ -298,7 +298,7 @@ namespace app sys::MessagePointer Application::handleBatteryLevel(sys::Message *msgl) { auto msg = static_cast(msgl); - LOG_INFO("Application battery level: %d", msg->levelPercents); + LOG_INFO("Battery level: %d", msg->levelPercents); if (getCurrentWindow()->updateBatteryLevel(msg->levelPercents)) { refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); @@ -310,16 +310,12 @@ namespace app { auto *msg = static_cast(msgl); if (msg->plugged == true) { - LOG_INFO("Application charger connected"); - getCurrentWindow()->batteryCharging(true); - refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); + LOG_INFO("Charger connected"); } else { - LOG_INFO("Application charger disconnected"); - getCurrentWindow()->batteryCharging(false); - refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); + LOG_INFO("Charger disconnected"); } - + getCurrentWindow()->batteryChargingChange(msg->plugged); refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); return msgHandled(); } diff --git a/module-apps/windows/AppWindow.cpp b/module-apps/windows/AppWindow.cpp index a1f74b8c1cbd04acfbb4f5e47afa295641d63cd5..4cab913d2306e448385a29a804a1b441ecd862c1 100644 --- a/module-apps/windows/AppWindow.cpp +++ b/module-apps/windows/AppWindow.cpp @@ -55,9 +55,9 @@ namespace gui topBar->setActive(TopBar::Elements::SIGNAL, false); } - bool AppWindow::batteryCharging(bool charging) + bool AppWindow::batteryChargingChange(bool charging) { - topBar->setBatteryCharging(charging); + topBar->setBatteryChargingChange(charging); return true; } @@ -73,7 +73,7 @@ namespace gui // get old value of battery level, calcualte new level and comapre both // if they are different make a change and return true, otherwise return false; if (topBar != nullptr) { - return topBar->setBatteryLevel(percentage); + return topBar->setBatteryBars(percentage); } return false; } diff --git a/module-apps/windows/AppWindow.hpp b/module-apps/windows/AppWindow.hpp index 52b163be76e0d83f0005f2512eef04afeece838a..583dd69ae7f8705623b16c6243c20833ce15341f 100644 --- a/module-apps/windows/AppWindow.hpp +++ b/module-apps/windows/AppWindow.hpp @@ -60,7 +60,7 @@ namespace gui }; virtual bool onDatabaseMessage(sys::Message *msg); - bool batteryCharging(bool charging); + bool batteryChargingChange(bool charging); bool setSIM(); // updates battery level in the window bool updateBatteryLevel(uint32_t percentage); diff --git a/module-gui/gui/widgets/TopBar.cpp b/module-gui/gui/widgets/TopBar.cpp index 749dc034e4b19c205ae9b59234304926a0a440cb..b31476c912953b8758d265c811e4c63e20ae69ea 100644 --- a/module-gui/gui/widgets/TopBar.cpp +++ b/module-gui/gui/widgets/TopBar.cpp @@ -43,16 +43,16 @@ namespace gui void TopBar::batteryShowBars(uint32_t val) { - if (val > battery.size()) { + if (val > batteryBars.size()) { LOG_ERROR("Trying to set battery level out of scope"); - val = battery.size(); + val = batteryBars.size(); } - for (unsigned int i = 0; i < battery.size(); ++i) { + for (unsigned int i = 0; i < batteryBars.size(); ++i) { if (elements.battery) { - battery[i]->setVisible(val >= i); + batteryBars[i]->setVisible(i == val); } else { - battery[i]->setVisible(false); + batteryBars[i]->setVisible(false); } } } @@ -69,7 +69,7 @@ namespace gui updateSignalStrength(); // icons for battery - battery = { + batteryBars = { new gui::Image(this, batteryOffset, 15, 0, 0, "battery_low_W_M"), new gui::Image(this, batteryOffset, 15, 0, 0, "battery1_W_M"), new gui::Image(this, batteryOffset, 15, 0, 0, "battery2_W_M"), @@ -79,12 +79,13 @@ namespace gui }; batteryShowBars(0); - charging = new Label(this, batteryOffset, 15, 30, this->drawArea.h); - charging->setFilled(false); - charging->setBorderColor(gui::ColorNoColor); - charging->setFont(style::header::font::title); - charging->setText("Z"); - charging->setVisible(false); + batteryChargings[Store::Battery::State::Charging] = + new gui::Image(this, batteryOffset, 15, 0, 0, "battery_charging_W_M"); + batteryChargings[Store::Battery::State::PluggedNotCharging] = + new gui::Image(this, batteryOffset, 15, 0, 0, "battery_charging_ready_W_M"); + for (auto &el : batteryChargings) { + el.second->setVisible(false); + } const auto design_sim_offset = 376; // this offset is not final, but it is pixel Purefect sim = new SIM(this, design_sim_offset, 12); @@ -123,15 +124,17 @@ namespace gui case Elements::BATTERY: { elements.battery = active; if (Store::Battery::get().state == Store::Battery::State::Discharging) { - setBatteryLevel(active ? Store::Battery::get().level : 0); + setBatteryBars(active ? Store::Battery::get().level : 0); } else { + // plugged if (active) { - setBatteryCharging(true); + setBatteryChargingChange(true); } else { - charging->setVisible(false); - setBatteryLevel(0); + for (auto &el : batteryChargings) { + el.second->setVisible(false); + } } } } break; @@ -162,10 +165,10 @@ namespace gui }; } - uint32_t calculateBatteryLavel(uint32_t percentage) + uint32_t calculateBatteryBars(uint32_t percentage) { uint32_t level = 0; - if (percentage <= 5) + if (percentage <= 5) // level critical level = 0; else if (percentage <= 27) level = 1; @@ -178,38 +181,49 @@ namespace gui else level = 5; - if (level >= batteryLevelCount) { + if (level >= batteryBarsCount) { LOG_ERROR("Battery level calculations are done wrong!"); - return batteryLevelCount - 1; + return batteryBarsCount - 1; } return level; } - bool TopBar::setBatteryLevel(uint32_t percent) + bool TopBar::setBatteryBars(uint32_t percent) { if (Store::Battery::get().state != Store::Battery::State::Discharging) { + return false; } - charging->setVisible(false); - batteryShowBars(calculateBatteryLavel(percent)); + for (auto &el : batteryChargings) { + el.second->setVisible(false); + } + batteryShowBars(calculateBatteryBars(percent)); return true; } - void TopBar::setBatteryCharging(bool plugged) + void TopBar::setBatteryChargingChange(bool plugged) { - if (plugged) { - batteryShowBars(0); - } - if (charging == nullptr) - return; - if (plugged) { - charging->setVisible(true); - batteryShowBars(0); - } - else { - charging->setVisible(false); - setBatteryLevel(Store::Battery::get().level); + switch (Store::Battery::get().state) { + + case Store::Battery::State::Discharging: + setBatteryBars(Store::Battery::get().level); + break; + case Store::Battery::State::Charging: + break; + case Store::Battery::State::PluggedNotCharging: + break; } + if (plugged) {} + // if (battery == nullptr) + // return; + // if (plugged) { + // chargingPending->setVisible(true); + // batteryShowBars(0); + // } + // else { + // chargingPending->setVisible(false); + // + // } } bool TopBar::updateSignalStrength() diff --git a/module-gui/gui/widgets/TopBar.hpp b/module-gui/gui/widgets/TopBar.hpp index dba3e42a5587a744e2b7d76052ffc3b3502b3b61..53078ebc827fb805421f6bd4f45677c4518a2706 100644 --- a/module-gui/gui/widgets/TopBar.hpp +++ b/module-gui/gui/widgets/TopBar.hpp @@ -9,11 +9,12 @@ #include "Rect.hpp" #include "TopBar/SIM.hpp" #include +#include namespace gui { - static const uint32_t batteryLevelCount = 6; + static const uint32_t batteryBarsCount = 6; static const uint32_t signalImgCount = 6; /// Header of most of design Windows @@ -55,9 +56,10 @@ namespace gui Label *networkAccessTechnologyLabel = nullptr; Image *signal[static_cast(Store::RssiBar::noOfSupprtedBars)]; Image *lock; - std::array battery = {nullptr}; - Label *charging = nullptr; - gui::SIM *sim = nullptr; + std::array batteryBars = {nullptr}; + std::map batteryChargings = { + {Store::Battery::State::Charging, nullptr}, {Store::Battery::State::PluggedNotCharging, nullptr}}; + gui::SIM *sim = nullptr; void prepareWidget(); static TimeMode timeMode; @@ -89,9 +91,9 @@ namespace gui * displayed. * @return if display should be refreshed or not */ - bool setBatteryLevel(uint32_t percent); + bool setBatteryBars(uint32_t percent); - void setBatteryCharging(bool plugged); + void setBatteryChargingChange(bool plugged); /** * @brief updates signal strength. This will cause appropriate image to be displayed. diff --git a/module-utils/common_data/EventStore.hpp b/module-utils/common_data/EventStore.hpp index 4b1a4e619202721162fb3682411f6add448e90b7..43e6c1eb88f31284885c5c14073c4fab341d4b00 100644 --- a/module-utils/common_data/EventStore.hpp +++ b/module-utils/common_data/EventStore.hpp @@ -25,6 +25,7 @@ namespace Store { Discharging, Charging, + PluggedNotCharging, } state = State::Discharging; unsigned int level = 0;