Implement enable/disable notifications, fix emitting signals, move charger control buttons to MainPage

This commit is contained in:
Matti Viljanen 2020-03-21 03:22:47 +02:00
parent 59950ddb7a
commit 3fe4589c21
No known key found for this signature in database
GPG key ID: CF32A1495158F888
5 changed files with 109 additions and 138 deletions

View file

@ -60,8 +60,8 @@ ApplicationWindow
Timer { Timer {
id: alertTimer id: alertTimer
interval: settings.interval * 1000 // sec -> msec interval: settings.interval * 1000
running: true running: settings.notificationsEnabled
repeat: true repeat: true
onTriggered: { onTriggered: {
if(battery.charge <= settings.lowerLimit && battery.state === "discharging") { if(battery.charge <= settings.lowerLimit && battery.state === "discharging") {

View file

@ -40,7 +40,7 @@ Page {
SilicaFlickable { SilicaFlickable {
id: mainFlickable id: mainFlickable
anchors.fill: parent anchors.fill: parent
contentHeight: column.height contentHeight: column.height + Theme.horizontalPageMargin
VerticalScrollDecorator { flickable: mainFlickable } VerticalScrollDecorator { flickable: mainFlickable }
@ -112,9 +112,46 @@ Page {
color: Theme.primaryColor color: Theme.primaryColor
font.pixelSize: Theme.fontSizeSmall font.pixelSize: Theme.fontSizeSmall
} }
Item { Label {
width: parent.width x: Theme.paddingLarge
height: Theme.paddingLarge text: qsTr("Charger control")
color: Theme.highlightColor
}
Label {
x: Theme.paddingLarge*2
width: parent.width - x*2;
wrapMode: Text.Wrap
text: qsTr("Using these controls overrides the automated settings.")
color: Theme.primaryColor
font.pixelSize: Theme.fontSizeSmall
}
Row {
anchors {
left: parent.left
right: parent.right
}
height: resumeButton.height
Column {
width: parent.width / 2
Button {
id: resumeButton
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Resume")
onClicked: battery.chargingEnabled = true
enabled: !battery.chargingEnabled
}
}
Column {
width: parent.width / 2
Button {
id: pauseButton
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Pause")
onClicked: battery.chargingEnabled = false
enabled: battery.chargingEnabled
}
}
} }
} }
} }

View file

@ -26,6 +26,8 @@ Page {
anchors.fill: parent anchors.fill: parent
contentHeight: header.height + settingsColumn.height + Theme.horizontalPageMargin contentHeight: header.height + settingsColumn.height + Theme.horizontalPageMargin
VerticalScrollDecorator { flickable: mainFlickable }
PageHeader { PageHeader {
id: header id: header
title: qsTr("Settings") title: qsTr("Settings")
@ -43,64 +45,15 @@ Page {
Label { Label {
x: Theme.paddingLarge x: Theme.paddingLarge
text: qsTr("Alert settings") text: qsTr("Charging settings")
color: Theme.highlightColor color: Theme.highlightColor
} }
MySlider {
width: parent.width
label: qsTr("Alert interval")
minimumValue: 60
maximumValue: 600
stepSize: 10
value: settings.interval
valueText: Math.floor(value / 60) + (value % 60 < 10 ? ":0" + value % 60 : ":" + value % 60)
onValueChanged: settings.interval = value
}
Label {
x: Theme.paddingLarge*2
width: parent.width - x*2;
wrapMode: Text.Wrap
text: qsTr("Set the maximum and minimum target charge levels.")
color: Theme.primaryColor
font.pixelSize: Theme.fontSizeSmall
}
MySlider {
id: highAlertSlider
width: parent.width
label: qsTr("Charging limit")
minimumValue: 11
maximumValue: 100
stepSize: 1
value: settings.highAlert
valueText: value + "%"
highlightDirection: Qt.RightToLeft
onValueChanged: {
settings.highAlert = value
if(lowAlertSlider.value >= value)
lowAlertSlider.value = value - 1
}
}
MySlider {
id: lowAlertSlider
width: parent.width
label: qsTr("Discharging limit")
minimumValue: 10
maximumValue: 99
stepSize: 1
value: settings.lowAlert
valueText: value + "%"
onValueChanged: {
settings.lowAlert = value
if(highAlertSlider.value <= value)
highAlertSlider.value = value + 1
}
}
TextSwitch { TextSwitch {
id: autoStopCharging id: autoStopCharging
text: qsTr("Stop charging when limit reached") text: qsTr("Stop charging when limit reached")
description: qsTr("This option stops charging when battery has reached the percentage set in Charging limit value, and resumes charging when charge has decreased below Continue charge limit value. Generally a value close to the Charging limit value is recommened, such as 80% and 75%.") description: qsTr("This option stops charging when battery has reached the percentage set in Charging limit value, and resumes charging when charge has decreased below Continue charge limit value. Generally a value close to the Charging limit value is recommened, such as 80% and 75%.")
checked: settings.limitEnabled
onCheckedChanged: settings.limitEnabled = checked onCheckedChanged: settings.limitEnabled = checked
Component.onCompleted: checked = settings.limitEnabled
} }
MySlider { MySlider {
@ -111,7 +64,7 @@ Page {
minimumValue: 21 minimumValue: 21
maximumValue: 95 maximumValue: 95
stepSize: 1 stepSize: 1
value: settings.highLimit Component.onCompleted: value = settings.highLimit
valueText: value + "%" valueText: value + "%"
highlightDirection: Qt.RightToLeft highlightDirection: Qt.RightToLeft
onValueChanged: { onValueChanged: {
@ -128,7 +81,7 @@ Page {
minimumValue: 20 minimumValue: 20
maximumValue: 94 maximumValue: 94
stepSize: 1 stepSize: 1
value: settings.lowLimit Component.onCompleted: value = settings.lowLimit
valueText: value + "%" valueText: value + "%"
onValueChanged: { onValueChanged: {
settings.lowLimit = value settings.lowLimit = value
@ -136,85 +89,58 @@ Page {
highLimitSlider.value = value + 1 highLimitSlider.value = value + 1
} }
} }
Label {
x: Theme.paddingLarge*2
width: parent.width - x*2;
wrapMode: Text.Wrap
text: qsTr("You can also independently stop and resume charging.")
color: Theme.primaryColor
font.pixelSize: Theme.fontSizeSmall
}
Row {
anchors {
left: parent.left
right: parent.right
}
height: resumeButton.height
Column {
width: parent.width / 2
Button {
id: resumeButton
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Resume")
onClicked: battery.chargingEnabled = true
enabled: !battery.chargingEnabled
}
}
Column {
width: parent.width / 2
Button {
id: pauseButton
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Pause")
onClicked: battery.chargingEnabled = false
enabled: battery.chargingEnabled
}
}
}
Label { Label {
x: Theme.paddingLarge x: Theme.paddingLarge
text: qsTr("Alert tests") text: qsTr("Notification settings")
color: Theme.highlightColor color: Theme.highlightColor
} }
TextSwitch {
Label { id: notificationsSwitch
x: Theme.paddingLarge*2 text: qsTr("Use notifications")
width: parent.width - x*2; description: qsTr("When the application is minimized, display visual and audible notifications about reached battery charge levels.")
wrapMode: Text.Wrap Component.onCompleted: checked = settings.notificationsEnabled
text: qsTr("Click the buttons to test the sound and notification.") onCheckedChanged: settings.notificationsEnabled = checked
color: Theme.primaryColor
font.pixelSize: Theme.fontSizeSmall
} }
Row { MySlider {
anchors { id: highAlertSlider
left: parent.left width: parent.width
right: parent.right label: qsTr("Charging limit")
} minimumValue: 11
height: resumeButton.height maximumValue: 100
stepSize: 1
Column { Component.onCompleted: value = settings.highAlert
width: parent.width / 2 valueText: value + "%"
Button { highlightDirection: Qt.RightToLeft
anchors.horizontalCenter: parent.horizontalCenter onValueChanged: {
text: qsTr("Charged") settings.highAlert = value
onClicked: { if(lowAlertSlider.value >= value)
alertHigh.play() lowAlertSlider.value = value - 1
notification.republishTest()
} }
} }
} MySlider {
Column { id: lowAlertSlider
width: parent.width / 2 width: parent.width
Button { label: qsTr("Discharging limit")
anchors.horizontalCenter: parent.horizontalCenter minimumValue: 10
text: qsTr("Discharged") maximumValue: 99
onClicked: { stepSize: 1
alertLow.play() Component.onCompleted: value = settings.lowAlert
notification.republishTest() valueText: value + "%"
} onValueChanged: {
settings.lowAlert = value
if(highAlertSlider.value <= value)
highAlertSlider.value = value + 1
} }
} }
MySlider {
width: parent.width
label: qsTr("Notification interval")
minimumValue: 60
maximumValue: 600
stepSize: 10
Component.onCompleted: value = settings.interval
valueText: Math.floor(value / 60) + (value % 60 < 10 ? ":0" + value % 60 : ":" + value % 60)
onValueChanged: settings.interval = value
} }
} }
} }

View file

@ -37,6 +37,7 @@ Settings::Settings(QObject *parent) : QObject(parent)
loadInteger(sHighAlert, &highAlert, 11, 100); loadInteger(sHighAlert, &highAlert, 11, 100);
loadInteger(sInterval, &interval, 60, 600); loadInteger(sInterval, &interval, 60, 600);
loadInteger(sLimitEnabled, &limitEnabled, 0, 1); loadInteger(sLimitEnabled, &limitEnabled, 0, 1);
loadInteger(sNotificationsEnabled, &notificationsEnabled, 0, 1);
loadInteger(sLowLimit, &lowLimit, 20, 94); loadInteger(sLowLimit, &lowLimit, 20, 94);
loadInteger(sHighLimit, &highLimit, 21, 95); loadInteger(sHighLimit, &highLimit, 21, 95);
qInfo() << "Loaded" << sLimitEnabled << limitEnabled; qInfo() << "Loaded" << sLimitEnabled << limitEnabled;
@ -48,6 +49,7 @@ Settings::~Settings()
mySettings.setValue(sHighAlert, QByteArray::number(highAlert)); mySettings.setValue(sHighAlert, QByteArray::number(highAlert));
mySettings.setValue(sInterval, QByteArray::number(interval)); mySettings.setValue(sInterval, QByteArray::number(interval));
mySettings.setValue(sLimitEnabled, QByteArray::number(limitEnabled)); mySettings.setValue(sLimitEnabled, QByteArray::number(limitEnabled));
mySettings.setValue(sNotificationsEnabled, QByteArray::number(notificationsEnabled));
mySettings.setValue(sLowLimit, QByteArray::number(lowLimit)); mySettings.setValue(sLowLimit, QByteArray::number(lowLimit));
mySettings.setValue(sHighLimit, QByteArray::number(highLimit)); mySettings.setValue(sHighLimit, QByteArray::number(highLimit));
qInfo() << "Settings saved"; qInfo() << "Settings saved";

View file

@ -31,6 +31,7 @@ class Settings : public QObject
Q_PROPERTY(int highLimit READ getHighLimit WRITE setHighLimit NOTIFY highLimitChanged) Q_PROPERTY(int highLimit READ getHighLimit WRITE setHighLimit NOTIFY highLimitChanged)
Q_PROPERTY(int lowLimit READ getLowLimit WRITE setLowLimit NOTIFY lowLimitChanged) 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(bool notificationsEnabled READ getNotificationsEnabled WRITE setNotificationsEnabled NOTIFY notificationsEnabledChanged)
Q_PROPERTY(QString lowAlertFile READ getLowAlertFile NOTIFY lowAlertFileChanged) Q_PROPERTY(QString lowAlertFile READ getLowAlertFile NOTIFY lowAlertFileChanged)
Q_PROPERTY(QString highAlertFile READ getHighAlertFile NOTIFY highAlertFileChanged) Q_PROPERTY(QString highAlertFile READ getHighAlertFile NOTIFY highAlertFileChanged)
@ -44,15 +45,17 @@ public:
int getLowLimit() { return lowLimit; } int getLowLimit() { return lowLimit; }
int getHighLimit() { return highLimit; } int getHighLimit() { return highLimit; }
bool getLimitEnabled() { return limitEnabled == 1; } bool getLimitEnabled() { return limitEnabled == 1; }
bool getNotificationsEnabled() { return notificationsEnabled == 1; }
QString getLowAlertFile() { return lowAlertFile; } QString getLowAlertFile() { return lowAlertFile; }
QString getHighAlertFile() { return highAlertFile;}
void setLowAlert(int newLimit) { lowAlert = newLimit; } void setLowAlert(int newLimit) { lowAlert = newLimit; emit lowAlertChanged(); }
void setHighAlert(int newLimit) { highAlert = newLimit; } void setHighAlert(int newLimit) { highAlert = newLimit; emit highAlertChanged(); }
void setInterval(int newInterval) { interval = newInterval; } void setInterval(int newInterval) { interval = newInterval; emit intervalChanged(); }
void setLowLimit(int newLimit) { lowLimit = newLimit; } void setLowLimit(int newLimit) { lowLimit = newLimit; emit lowLimitChanged(); }
void setHighLimit(int newLimit) { highLimit = newLimit; } void setHighLimit(int newLimit) { highLimit = newLimit; emit highLimitChanged(); }
void setLimitEnabled(bool newEnabled) { limitEnabled = (newEnabled ? 1 : 0); } void setLimitEnabled(bool newEnabled) { limitEnabled = (newEnabled ? 1 : 0); emit limitEnabledChanged(); }
QString getHighAlertFile() { return highAlertFile; } void setNotificationsEnabled(bool newEnabled) { notificationsEnabled = (newEnabled ? 1 : 0); emit notificationsEnabledChanged(); }
private: private:
@ -63,6 +66,7 @@ private:
int highAlert = 75; int highAlert = 75;
int interval = 60; int interval = 60;
int limitEnabled = 0; // Converted to boolean for QML int limitEnabled = 0; // Converted to boolean for QML
int notificationsEnabled = 1; // Converted to boolean for QML
int lowLimit = 65; int lowLimit = 65;
int highLimit = 70; 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";
@ -73,6 +77,7 @@ private:
const char* sHighAlert = "highAlert"; const char* sHighAlert = "highAlert";
const char* sInterval = "interval"; const char* sInterval = "interval";
const char* sLimitEnabled = "limitEnabled"; const char* sLimitEnabled = "limitEnabled";
const char* sNotificationsEnabled = "notificationsEnabled";
const char* sLowLimit = "lowLimit"; const char* sLowLimit = "lowLimit";
const char* sHighLimit = "highLimit"; const char* sHighLimit = "highLimit";
const char* sLowAlertFile = "lowAlertFile"; const char* sLowAlertFile = "lowAlertFile";
@ -86,6 +91,7 @@ signals:
int highAlertChanged(); int highAlertChanged();
int intervalChanged(); int intervalChanged();
bool limitEnabledChanged(); bool limitEnabledChanged();
bool notificationsEnabledChanged();
int lowLimitChanged(); int lowLimitChanged();
int highLimitChanged(); int highLimitChanged();
QString lowAlertFileChanged(); QString lowAlertFileChanged();