Fix broken state detection, move state translations to QML

This commit is contained in:
Matti Viljanen 2019-01-09 00:59:23 +02:00
parent 8eafae5808
commit 722cac24b9
No known key found for this signature in database
GPG key ID: CF32A1495158F888
3 changed files with 16 additions and 22 deletions

View file

@ -22,7 +22,14 @@ import Nemo.Notifications 1.0
Page { Page {
id: page id: page
property variant statusText: {
"idle": qsTr("idle", "Charger plugged in, not using nor charging battery"),
"discharging": qsTr("discharging", "Charger not plugged in, battery discharging"),
"charging": qsTr("charging", "Charger plugged in and battery charging"),
"unknown": qsTr("unknown", "Battery not detected, or faulty, or something")
}
// Finally, emit the signal
// The effective value will be restricted by ApplicationWindow.allowedOrientations // The effective value will be restricted by ApplicationWindow.allowedOrientations
allowedOrientations: Orientation.All allowedOrientations: Orientation.All
@ -112,7 +119,7 @@ Page {
+qsTr("State:") +qsTr("State:")
value: battery.charge+"%\n" value: battery.charge+"%\n"
+(battery.charging ? qsTr("yes") : qsTr("no"))+"\n" +(battery.charging ? qsTr("yes") : qsTr("no"))+"\n"
+qsTr(battery.state) +statusText[battery.state]
} }
Label { Label {
x: Theme.paddingLarge x: Theme.paddingLarge

View file

@ -54,21 +54,9 @@ void Battery::updateData()
chargingFile->close(); chargingFile->close();
} }
if(stateFile->open(QIODevice::ReadOnly)) { if(stateFile->open(QIODevice::ReadOnly)) {
nextStatus = (QString(stateFile->readAll())); nextState = (QString(stateFile->readAll()));
if(nextStatus != state) { if(nextState != state) {
state = nextStatus; state = nextState;
// Update translated text accordingly
if(state == "")
stateTr = tr("idle", "Charger plugged in, not using nor charging battery");
else if(state == "discharging")
stateTr = tr("discharging", "Charger not plugged in, battery discharging");
else if(state == "charging")
stateTr = tr("charging", "Charger plugged in and battery charging");
else if(state == "unknown")
stateTr = tr("unknown", "Battery not detected, or faulty, or something");
// Finally, emit the signal
emit stateChanged(); emit stateChanged();
} }
stateFile->close(); stateFile->close();
@ -79,4 +67,4 @@ int Battery::getCharge(){ return charge; }
bool Battery::getCharging() { return charging; } bool Battery::getCharging() { return charging; }
QString Battery::getState() { return stateTr; } QString Battery::getState() { return state; }

View file

@ -48,12 +48,11 @@ private:
// Default values: // Default values:
int charge = 100; // 100% full int charge = 100; // 100% full
bool charging = true; // Charger plugged in bool charging = true; // Charger plugged in
QString state = "idle"; // Not charging QString state = "idle"; // dis/charging, idle, unknown
QString stateTr = state; // Translated state text
int nextCharge = charge; int nextCharge = charge;
bool nextCharging = charging; bool nextCharging = charging;
QString nextStatus = state; QString nextState = state;
signals: signals:
int chargeChanged(); int chargeChanged();