Sensor save and load from storage, and deletion. Closes #96

This commit is contained in:
Stefan Persson 2012-01-17 15:10:46 +01:00
parent 5ce0b81e39
commit 645ef2098e
6 changed files with 237 additions and 129 deletions

View file

@ -5,10 +5,6 @@ Column {
id: sensorList id: sensorList
spacing: 1 spacing: 1
SensorView {
id: sensorView
}
BorderImage { BorderImage {
id: header id: header
source: "header_bg.png" source: "header_bg.png"
@ -22,6 +18,13 @@ Column {
anchors.leftMargin: 15 anchors.leftMargin: 15
} }
HeaderTitle { HeaderTitle {
text: ""
anchors.right: sensorid.left
visible: main.state == "EDIT"
width: 50
}
HeaderTitle {
id: sensorid
text: "ID" text: "ID"
anchors.right: modelTitle.left anchors.right: modelTitle.left
visible: main.state == "EDIT" visible: main.state == "EDIT"
@ -58,7 +61,7 @@ Column {
} }
Repeater { Repeater {
model: sensorModel model: sensorModel
delegate: sensorView delegate: SensorView{ state: main.state == "EDIT" ? 'EDIT' : ''}
} }
Row{ Row{
spacing: 20 spacing: 20
@ -75,23 +78,5 @@ Column {
} }
} }
} }
/*
Rectangle {
//TODO should this button exist at all, or always save?
width: 50
height: 20
visible: main.state == "EDIT"
Text{
anchors.centerIn: parent
text: "Cancel"
}
MouseArea{
anchors.fill: parent
onClicked: {
main.state ="VIEW"
}
}
}
*/
} }
} }

View file

@ -1,20 +1,17 @@
import Qt 4.7 import Qt 4.7
import QtDesktop 0.1 import QtDesktop 0.1
Component {
id: sensorView
Item{ Item{
id: sensorViewItem id: sensorViewItem
visible: main.state == "EDIT" || modelData.showInList visible: state == "EDIT" || modelData.showInList
height: childrenRect.height height: childrenRect.height
width: parent.width width: parent ? parent.width : 0
property string state: main.state states:[
onStateChanged: { State {
if (state != "EDIT") { name: "EDIT"
modelData.setName(nameEdit.text)
}
} }
]
BorderImage { BorderImage {
source: "row_bg.png" source: "row_bg.png"
@ -24,7 +21,7 @@ Component {
width: parent.width width: parent.width
Text { Text {
visible: main.state == "VIEW" visible: sensorViewItem.state != "EDIT"
anchors.left: parent.left anchors.left: parent.left
anchors.leftMargin: 15 anchors.leftMargin: 15
height: 40 height: 40
@ -34,17 +31,36 @@ Component {
} }
TextField { TextField {
id: nameEdit id: nameEdit
visible: main.state == "EDIT" visible: sensorViewItem.state == "EDIT"
anchors.left: parent.left anchors.left: parent.left
anchors.leftMargin: 15 anchors.leftMargin: 15
anchors.top: parent.top anchors.top: parent.top
anchors.topMargin: (40-nameEdit.height)/2 anchors.topMargin: (40-nameEdit.height)/2
text: modelData.name; text: modelData.name;
placeholderText: 'Enter a name' placeholderText: 'Enter a name'
onTextChanged: modelData.name = text
}
Text{
anchors.right: sensorid.left
visible: sensorViewItem.state == "EDIT"
height: 40
verticalAlignment: Text.AlignVCenter
text: "Delete"
font.underline: true
color: "#004275"
width: 50
MouseArea{
anchors.fill: parent
onClicked: {
confirmDeletion.visible = true;
}
}
} }
Text{ Text{
id: sensorid
anchors.right: model.left anchors.right: model.left
visible: main.state == "EDIT" visible: sensorViewItem.state == "EDIT"
height: 40 height: 40
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
text: modelData.id text: modelData.id
@ -54,7 +70,7 @@ Component {
Text{ Text{
id: model id: model
anchors.right: visibleinlistcheckbox.left anchors.right: visibleinlistcheckbox.left
visible: main.state == "EDIT" visible: sensorViewItem.state == "EDIT"
height: 40 height: 40
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
text: modelData.model text: modelData.model
@ -70,7 +86,7 @@ Component {
id: checkBox id: checkBox
anchors.centerIn: parent anchors.centerIn: parent
width: checkBox.height width: checkBox.height
visible: main.state == "EDIT" visible: sensorViewItem.state == "EDIT"
checked: modelData.showInList checked: modelData.showInList
onClicked: modelData.setShowInList(!modelData.showInList) onClicked: modelData.setShowInList(!modelData.showInList)
} }
@ -94,5 +110,29 @@ Component {
} }
} }
} }
Dialog{
id: confirmDeletion
modal: true
title: "Confirm deletion"
Text{
id: descriptionHeadline
text:"Delete this sensor?"
font.bold: true
}
Text{
id: descriptionText
anchors.top: descriptionHeadline.bottom
anchors.topMargin: 10
width: parent.width - 20
anchors.left: parent.left
anchors.leftMargin: 10
text: "Please note that a sensor that is still transmitting will reappear here again, but it will be hidden in the list by default."
wrapMode: Text.Wrap
}
onAccepted: {
deleteSensor.callWith(modelData.protocol, modelData.model, modelData.id);
}
} }
} }

View file

@ -6,10 +6,13 @@ __postInit__ = function() {
} }
com.telldus.sensors = function() { com.telldus.sensors = function() {
var sensorList = new com.telldus.qml.array(); var sensorList;
function init() { function init() {
var sensorData = 0; var sensorData = 0;
sensorList = loadSensorModel();
sensorList.rowsRemoved.connect(function(){saveSensorModel();});
sensorList.rowsInserted.connect(function(){saveSensorModel();});
while(sensorData = com.telldus.core.sensor()) { while(sensorData = com.telldus.core.sensor()) {
var p = sensorData["protocol"]; var p = sensorData["protocol"];
var m = sensorData["model"]; var m = sensorData["model"];
@ -19,7 +22,7 @@ com.telldus.sensors = function() {
var tryFetchValue = function(p, m, id, types, type) { var tryFetchValue = function(p, m, id, types, type) {
if (types & type) { if (types & type) {
sensorValue = com.telldus.core.sensorValue(p, m, id, type); sensorValue = com.telldus.core.sensorValue(p, m, id, type);
sensorEvent(p, m, id, type, sensorValue["value"], sensorValue["timestamp"]); sensorEvent(p, m, id, type, sensorValue["value"], sensorValue["timestamp"], true);
} }
} }
tryFetchValue(p, m, id, types, com.telldus.core.TELLSTICK_TEMPERATURE); tryFetchValue(p, m, id, types, com.telldus.core.TELLSTICK_TEMPERATURE);
@ -28,14 +31,94 @@ com.telldus.sensors = function() {
} }
com.telldus.core.sensorEvent.connect(sensorEvent); com.telldus.core.sensorEvent.connect(sensorEvent);
view = new com.telldus.qml.view({}); view = new com.telldus.qml.view({
deleteSensor: deleteSensor
});
view.setProperty('sensorModel', sensorList); view.setProperty('sensorModel', sensorList);
saveSensorModel();
view.load("main.qml"); view.load("main.qml");
application.addWidget("sensors.gui", "icon.png", view); application.addWidget("sensors.gui", "icon.png", view);
} }
function sensorEvent(protocol, model, id, dataType, value, timestamp) { function createSensor(protocol, model, id, name, showInList){
var sensor = new com.telldus.sensors.sensor();
sensor.protocol = protocol;
sensor.model = model;
sensor.id = id;
sensor.name = name;
sensor.nameChanged.connect(function() { saveSensorModel(); });
sensor.showInList = showInList;
sensor.showInListChanged.connect(function() { saveSensorModel(); });
return sensor;
}
function deleteSensor(protocol, model, id){
var i = 0;
var found = false;
for (; i < sensorList.length; ++i) {
if (sensorList.get(i).protocol != protocol) {
continue;
}
if (sensorList.get(i).model != model) {
continue;
}
if (sensorList.get(i).id != id) {
continue;
}
found = true;
break;
}
if(found){
sensorList.removeLater(i);
}
}
function loadSensorModel(){
var settings = new com.telldus.settings();
var sensors = new com.telldus.qml.array();
var sensorProperties = settings.value("sensors", "");
if(sensorProperties){
for (var i = 0; i < sensorProperties.length; i++) {
var sensor = createSensor(sensorProperties[i].protocol, sensorProperties[i].model, sensorProperties[i].id, sensorProperties[i].name, sensorProperties[i].showInList=="true");
for (var j = 0; j < sensorProperties[i].values.length; j++) {
sensor.setValue(sensorProperties[i].values[j].type, sensorProperties[i].values[j].value, sensorProperties[i].values[j].lastUpdated)
}
sensors.push(sensor);
}
}
return sensors;
}
function saveSensorModel(){
var settings = new com.telldus.settings();
var sensorProperties = new Array();
for (var i = 0; i < sensorList.length; ++i) {
var sensor = sensorList.get(i);
var allValues = new Array();
if(sensor.hasHumidity){
var sensorValue = sensor.sensorValue(com.telldus.core.TELLSTICK_HUMIDITY);
var value = {type: com.telldus.core.TELLSTICK_HUMIDITY, lastUpdated: sensorValue.lastUpdated, value: sensorValue.value};
allValues.push(value);
}
if(sensor.hasTemperature){
var sensorValue = sensor.sensorValue(com.telldus.core.TELLSTICK_TEMPERATURE);
var value = {type: com.telldus.core.TELLSTICK_TEMPERATURE, lastUpdated: sensorValue.lastUpdated, value: sensorValue.value};
allValues.push(value);
}
var sensorProp = {protocol:sensor.protocol, model:sensor.model, id:sensor.id, values:allValues, name:sensor.name, showInList:sensor.showInList};
sensorProperties.push(sensorProp);
}
settings.setValue("sensors", sensorProperties);
}
function sensorEvent(protocol, model, id, dataType, value, timestamp, avoidSave) {
var sensor = 0; var sensor = 0;
for (var i = 0; i < sensorList.length; ++i) { for (var i = 0; i < sensorList.length; ++i) {
if (sensorList.get(i).protocol != protocol) { if (sensorList.get(i).protocol != protocol) {
@ -52,19 +135,15 @@ com.telldus.sensors = function() {
} }
if (!sensor) { if (!sensor) {
sensor = new com.telldus.sensors.sensor(); sensor = createSensor(protocol, model, id, "", false);
sensor.protocol = protocol;
sensor.model = model;
sensor.id = id;
sensor.showInList = false;
sensorList.push(sensor); sensorList.push(sensor);
print("Create new");
} else {
print("Update");
} }
print("Sensor event", protocol, model, id, dataType, value, timestamp);
sensor.setValue(dataType, value, timestamp); sensor.setValue(dataType, value, timestamp);
if(!avoidSave){
saveSensorModel();
}
} }
return { //Public functions return { //Public functions

View file

@ -17,6 +17,7 @@ Sensor::Sensor(QObject *parent) :
{ {
d = new PrivateData; d = new PrivateData;
d->id = 0; d->id = 0;
d->showInList = false;
} }
Sensor::~Sensor() { Sensor::~Sensor() {
@ -50,6 +51,9 @@ QString Sensor::name() const {
} }
void Sensor::setName(const QString &name) { void Sensor::setName(const QString &name) {
if (name == d->name) {
return;
}
d->name = name; d->name = name;
emit nameChanged(); emit nameChanged();
} }
@ -67,7 +71,7 @@ bool Sensor::hasTemperature() const {
return d->values.contains(TELLSTICK_TEMPERATURE); return d->values.contains(TELLSTICK_TEMPERATURE);
} }
SensorValue * Sensor::sensorValue(int type) { QObject * Sensor::sensorValue(int type) {
return (d->values.contains(type) ? d->values[type] : 0); return (d->values.contains(type) ? d->values[type] : 0);
} }
@ -90,7 +94,6 @@ void Sensor::setValue(int type, const QString &value, const QDateTime &timestamp
} }
bool Sensor::showInList() const{ bool Sensor::showInList() const{
//TODO showInList and name must be persistent...
return d->showInList; return d->showInList;
} }

View file

@ -17,7 +17,7 @@ class Sensor : public QObject
Q_PROPERTY(QString model READ model WRITE setModel NOTIFY modelChanged) Q_PROPERTY(QString model READ model WRITE setModel NOTIFY modelChanged)
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString protocol READ protocol WRITE setProtocol NOTIFY protocolChanged) Q_PROPERTY(QString protocol READ protocol WRITE setProtocol NOTIFY protocolChanged)
Q_PROPERTY(bool showInList READ showInList NOTIFY showInListChanged) Q_PROPERTY(bool showInList READ showInList WRITE setShowInList NOTIFY showInListChanged)
public: public:
explicit Sensor(QObject *parent = 0); explicit Sensor(QObject *parent = 0);
@ -32,7 +32,6 @@ public:
void setModel(const QString &model); void setModel(const QString &model);
QString name() const; QString name() const;
//void setName(const QString &name);
QString protocol() const; QString protocol() const;
void setProtocol(const QString &protocol); void setProtocol(const QString &protocol);
@ -40,7 +39,7 @@ public:
bool hasTemperature() const; bool hasTemperature() const;
bool showInList() const; bool showInList() const;
Q_INVOKABLE SensorValue *sensorValue(int type); Q_INVOKABLE QObject *sensorValue(int type);
Q_INVOKABLE void setValue(int type, const QString &value, const QDateTime &timestamp); Q_INVOKABLE void setValue(int type, const QString &value, const QDateTime &timestamp);
Q_INVOKABLE void setName(const QString &name); Q_INVOKABLE void setName(const QString &name);
Q_INVOKABLE void setShowInList(bool show); Q_INVOKABLE void setShowInList(bool show);

View file

@ -26,10 +26,12 @@ void Settings::setValue( const QString & key, const QVariant & value ) {
d->s.beginGroup(key); d->s.beginGroup(key);
d->s.setValue("size", list.size()); d->s.setValue("size", list.size());
d->s.setValue("type", "array"); d->s.setValue("type", "array");
//d->s.beginWriteArray("list"); //TODO write or read? What prefix?
for (int i = 0; i < list.size(); ++i) { for (int i = 0; i < list.size(); ++i) {
d->s.setArrayIndex(i); //d->s.setArrayIndex(i);
this->setValue(QString::number(i), list.at(i)); this->setValue(QString::number(i), list.at(i));
} }
//d->s.endArray();
d->s.endGroup(); d->s.endGroup();
} else if (value.type() == QVariant::Map) { } else if (value.type() == QVariant::Map) {