Code cleanup; rename variables. Separate alert and charger limits.

This commit is contained in:
Matti Viljanen 2020-03-20 20:57:06 +02:00
parent 799ecd40db
commit e1b59a98a5
No known key found for this signature in database
GPG key ID: CF32A1495158F888
3 changed files with 117 additions and 93 deletions

View file

@ -64,29 +64,34 @@ Page {
font.pixelSize: Theme.fontSizeSmall font.pixelSize: Theme.fontSizeSmall
} }
Slider { Slider {
id: upperChargeLimit id: highAlertSlider
width: parent.width width: parent.width
label: qsTr("Charging limit") label: qsTr("Charging limit")
minimumValue: 60 minimumValue: 10
maximumValue: 99 maximumValue: 99
stepSize: 1 stepSize: 1
value: settings.upperLimit value: settings.highAlert
valueText: value + "%" valueText: value + "%"
onValueChanged: { onValueChanged: {
settings.upperLimit = value settings.highLimit = value
if((value - 2) < continueChargeLimit.value) if(lowAlertSlider.value >= value)
continueChargeLimit.value = value - 2 lowAlertSlider.value = value - 1
} }
} }
Slider { Slider {
id: lowAlertSlider
width: parent.width width: parent.width
label: qsTr("Discharging limit") label: qsTr("Discharging limit")
minimumValue: 10 minimumValue: 10
maximumValue: 40 maximumValue: 99
stepSize: 1 stepSize: 1
value: settings.lowerLimit value: settings.lowAlert
valueText: value + "%" valueText: value + "%"
onValueChanged: settings.lowerLimit = value onValueChanged: {
settings.highLimit = value
if(highAlertSlider.value <= value)
highAlertSlider.value = value + 1
}
} }
TextSwitch { TextSwitch {
id: autoStopCharging id: autoStopCharging
@ -97,16 +102,36 @@ Page {
} }
Slider { Slider {
id: continueChargeLimit id: highLimitSlider
handleVisible: enabled
width: parent.width
label: qsTr("Stop charging limit")
minimumValue: 20
maximumValue: 95
stepSize: 1
value: settings.lowLimit
valueText: value + "%"
onValueChanged: {
settings.lowLimit = value
if(lowLimitSlider.value >= value)
lowLimitSlider.value = value - 1
}
}
Slider {
id: lowLimitSlider
handleVisible: enabled handleVisible: enabled
width: parent.width width: parent.width
label: qsTr("Resume charging limit") label: qsTr("Resume charging limit")
minimumValue: 50 minimumValue: 20
maximumValue: upperChargeLimit.value - 2 maximumValue: 95
stepSize: 1 stepSize: 1
value: settings.chargeLimit value: settings.highLimit
valueText: value + "%" valueText: value + "%"
onValueChanged: settings.chargeLimit = value onValueChanged: {
settings.highLimit = value
if(highLimitSlider.value <= value)
highLimitSlider.value = value + 1
}
} }
Label { Label {
x: Theme.paddingLarge*2 x: Theme.paddingLarge*2
@ -129,7 +154,7 @@ Page {
id: button id: button
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Enable") text: qsTr("Enable")
onClicked: battery.chargingEnabled = true onClicked: chargingEnabled = true
} }
} }
Column { Column {
@ -137,7 +162,7 @@ Page {
Button { Button {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Disable") text: qsTr("Disable")
onClicked: battery.chargingEnabled = false onClicked: chargingEnabled = false
} }
} }
} }

View file

@ -20,70 +20,53 @@
Settings::Settings(QObject *parent) : QObject(parent) Settings::Settings(QObject *parent) : QObject(parent)
{ {
int tempValue; int tempValue;
if(mySettings.contains("lowerLimit")) { if(mySettings.contains(sLowAlert)) {
tempValue = mySettings.value("lowerLimit").toInt(); tempValue = mySettings.value(sLowAlert).toInt();
if(tempValue >= 10 && tempValue <= 50) { if(tempValue >= 10 && tempValue <= 99) {
lowerLimit = tempValue; lowAlert = tempValue;
emit lowerLimitChanged(); emit lowAlertChanged();
} }
} }
if(mySettings.contains("upperLimit")) { if(mySettings.contains(sHighAlert)) {
tempValue = mySettings.value("upperLimit").toInt(); tempValue = mySettings.value(sHighAlert).toInt();
if(tempValue >= 60 && tempValue <= 100) { if(tempValue >= 10 && tempValue <= 99) {
upperLimit = tempValue; highAlert = tempValue;
emit upperLimitChanged(); emit highAlertChanged();
} }
} }
if(mySettings.contains("interval")) { if(mySettings.contains(sInterval)) {
tempValue = mySettings.value("interval").toInt(); tempValue = mySettings.value(sInterval).toInt();
if(tempValue >= 60 && tempValue <= 600) { if(tempValue >= 60 && tempValue <= 600) {
interval = tempValue; interval = tempValue;
emit intervalChanged(); emit intervalChanged();
} }
} }
if(mySettings.contains("limitEnabled")) { if(mySettings.contains(sLimitEnabled)) {
limitEnabled = (mySettings.value("limitEnabled").toInt() == 1); limitEnabled = (mySettings.value(sLimitEnabled).toInt() == 1);
emit limitEnabledChanged(); emit limitEnabledChanged();
} }
if(mySettings.contains("chargeLimit")) { if(mySettings.contains(sLowLimit)) {
tempValue = mySettings.value("chargeLimit").toInt(); tempValue = mySettings.value(sLowLimit).toInt();
if(tempValue >= 50 && tempValue <= (upperLimit - 2)) { if(tempValue >= 20 && tempValue <= 95) {
chargeLimit = tempValue; lowLimit = tempValue;
emit chargeLimitChanged(); emit lowLimitChanged();
}
}
if(mySettings.contains(sHighLimit)) {
tempValue = mySettings.value(sHighLimit).toInt();
if(tempValue >= 20 && tempValue <= 95) {
highLimit = tempValue;
emit highLimitChanged();
} }
} }
} }
Settings::~Settings() Settings::~Settings()
{ {
int limitValue = limitEnabled ? 1 : 0; mySettings.setValue(sLowAlert, QByteArray::number(lowAlert));
mySettings.setValue("lowerLimit", QByteArray::number(lowerLimit)); mySettings.setValue(sHighAlert, QByteArray::number(highAlert));
mySettings.setValue("upperLimit", QByteArray::number(upperLimit)); mySettings.setValue(sInterval, QByteArray::number(interval));
mySettings.setValue("interval", QByteArray::number(interval)); mySettings.setValue(sLimitEnabled, QByteArray::number(limitEnabled ? 1 : 0));
mySettings.setValue("limitEnabled", QByteArray::number(limitValue)); mySettings.setValue(sLowLimit, QByteArray::number(lowLimit));
mySettings.setValue("chargeLimit", QByteArray::number(chargeLimit)); mySettings.setValue(sHighLimit, QByteArray::number(highLimit));
} }
int Settings::getLowerLimit() { return lowerLimit; }
int Settings::getUpperLimit() { return upperLimit; }
int Settings::getInterval() { return interval; }
QString Settings::getLowAlert() { return lowAlertFile; }
QString Settings::getHighAlert() { return highAlertFile; }
bool Settings::getLimitEnabled() { return limitEnabled; }
int Settings::getChargeLimit() { return chargeLimit; }
void Settings::setLowerLimit(int newLimit) { lowerLimit = newLimit; }
void Settings::setUpperLimit(int newLimit) { upperLimit = newLimit; }
void Settings::setInterval(int newInterval) { interval = newInterval; }
void Settings::setLimitEnabled(bool newEnabled) { limitEnabled = newEnabled; }
void Settings::setChargeLimit(int newLimit) { chargeLimit = newLimit; }

View file

@ -24,52 +24,68 @@
class Settings : public QObject class Settings : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int lowerLimit READ getLowerLimit WRITE setLowerLimit NOTIFY lowerLimitChanged) Q_PROPERTY(int lowAlert READ getLowAlert WRITE setLowAlert NOTIFY lowAlertChanged)
Q_PROPERTY(int upperLimit READ getUpperLimit WRITE setUpperLimit NOTIFY upperLimitChanged) Q_PROPERTY(int highAlert READ getHighAlert WRITE setHighAlert NOTIFY highAlertChanged)
Q_PROPERTY(int interval READ getInterval WRITE setInterval NOTIFY intervalChanged) Q_PROPERTY(int interval READ getInterval WRITE setInterval NOTIFY intervalChanged)
Q_PROPERTY(int highLimit READ getHighLimit WRITE setHighLimit NOTIFY highLimitChanged)
Q_PROPERTY(int lowLimit READ getLowLimit WRITE setLowLimit NOTIFY lowLimitChanged)
Q_PROPERTY(bool limitEnabled READ getLimitEnabled WRITE setLimitEnabled NOTIFY limitEnabledChanged) Q_PROPERTY(bool limitEnabled READ getLimitEnabled WRITE setLimitEnabled NOTIFY limitEnabledChanged)
Q_PROPERTY(int chargeLimit READ getChargeLimit WRITE setChargeLimit NOTIFY chargeLimitChanged) Q_PROPERTY(QString lowAlertFile READ getLowAlertFile NOTIFY lowAlertFileChanged)
Q_PROPERTY(QString lowAlertFile READ getLowAlert NOTIFY lowAlertChanged) Q_PROPERTY(QString highAlertFile READ getHighAlertFile NOTIFY highAlertFileChanged)
Q_PROPERTY(QString highAlertFile READ getHighAlert NOTIFY highAlertChanged)
public: public:
Settings(QObject* parent = nullptr); Settings(QObject* parent = nullptr);
~Settings(); ~Settings();
// QML-exposed stuff int getLowAlert() { return lowAlert; }
int getLowerLimit(); int getHighAlert() { return highAlert; }
int getUpperLimit(); int getInterval() { return interval; }
int getInterval(); int getLowLimit() { return lowLimit; }
bool getLimitEnabled(); int getHighLimit() { return highLimit; }
int getChargeLimit(); bool getLimitEnabled() { return limitEnabled; }
QString getLowAlert(); QString getLowAlertFile() { return lowAlertFile; }
QString getHighAlert();
void setLowerLimit(int newLimit); void setLowAlert(int newLimit) { lowAlert = newLimit; }
void setUpperLimit(int newLimit); void setHighAlert(int newLimit) { highAlert = newLimit; }
void setInterval(int newInterval); void setInterval(int newInterval) { interval = newInterval; }
void setLimitEnabled(bool newEnabled); void setLowLimit(int newLimit) { lowLimit = newLimit; }
void setChargeLimit(int newLimit); void setHighLimit(int newLimit) { highLimit = newLimit; }
void setLimitEnabled(bool newEnabled) { limitEnabled = newEnabled; }
QString getHighAlertFile() { return highAlertFile; }
private: private:
QSettings mySettings; QSettings mySettings;
// Default values // Default values
int lowerLimit = 25; int lowAlert = 25;
int upperLimit = 75; int highAlert = 75;
int interval = 60; int interval = 60;
bool limitEnabled = false; bool limitEnabled = false;
int chargeLimit = 70; int lowLimit = 65;
int highLimit = 70;
QString lowAlertFile = "/usr/share/sounds/jolla-ambient/stereo/general_warning.wav"; QString lowAlertFile = "/usr/share/sounds/jolla-ambient/stereo/general_warning.wav";
QString highAlertFile = "/usr/share/sounds/jolla-ambient/stereo/positive_confirmation.wav"; QString highAlertFile = "/usr/share/sounds/jolla-ambient/stereo/positive_confirmation.wav";
// To avoid repeating the same string over and over and over...
const char* sLowAlert = "lowAlert";
const char* sHighAlert = "highAlert";
const char* sInterval = "interval";
const char* sLimitEnabled = "limitEnabled";
const char* sLowLimit = "lowLimit";
const char* sHighLimit = "highLimit";
const char* sLowAlertFile = "lowAlertFile";
const char* sHighAlertFile = "highAlertFile";
signals: signals:
int lowerLimitChanged(); int lowAlertChanged();
int upperLimitChanged(); int highAlertChanged();
int intervalChanged(); int intervalChanged();
bool limitEnabledChanged(); bool limitEnabledChanged();
int chargeLimitChanged(); int lowLimitChanged();
QString lowAlertChanged(); int highLimitChanged();
QString highAlertChanged(); QString lowAlertFileChanged();
QString highAlertFileChanged();
}; };
#endif // SETTINGS_H #endif // SETTINGS_H