Changeset 645ef2


Ignore:
Timestamp:
01/17/12 15:18:03 (4 months ago)
Author:
Stefan Persson <stefan.persson@…>
Branches:
('master', 'deebf2045e7119c339412580f37a1e653f7d5715')('controller-upgrade', '00f95d22e12d96ef089e0902ef62ae8ce841dc6f')
Children:
deebf2045e7119c339412580f37a1e653f7d5715
Parents:
5ce0b81e39f7ef7fca38b915ed42b6d3bef1ce91
git-author:
Stefan Persson <stefan.persson@telldus.se>2012-01-17 15:10:46+01:00
git-committer:
Stefan Persson <stefan.persson@telldus.se>2012-01-17 15:18:03+01:00
Message:

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

Location:
telldus-gui/Plugins
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • telldus-gui/Plugins/Sensors/SensorList.qml

    r64e2b3 r645ef2  
    55        id: sensorList 
    66        spacing: 1 
    7  
    8         SensorView { 
    9                 id: sensorView 
    10         } 
    117 
    128        BorderImage { 
     
    2319                } 
    2420                HeaderTitle { 
     21                        text: "" 
     22                        anchors.right: sensorid.left 
     23                        visible: main.state == "EDIT" 
     24                        width: 50 
     25                } 
     26                HeaderTitle { 
     27                        id: sensorid 
    2528                        text: "ID" 
    2629                        anchors.right: modelTitle.left 
     
    5962        Repeater { 
    6063                model: sensorModel 
    61                 delegate: sensorView 
     64                delegate: SensorView{ state: main.state == "EDIT" ? 'EDIT' : ''} 
    6265        } 
    6366        Row{ 
     
    7275                                } 
    7376                                else{ 
    74                                         main.state  ="VIEW" 
     77                                        main.state = "VIEW" 
    7578                                } 
    7679                        } 
    7780                } 
    78                 /* 
    79                         Rectangle { 
    80                                 //TODO should this button exist at all, or always save? 
    81                                 width: 50 
    82                                 height: 20 
    83                                 visible: main.state == "EDIT" 
    84                                 Text{ 
    85                                         anchors.centerIn: parent 
    86                                         text: "Cancel" 
    87                                 } 
    88                                 MouseArea{ 
    89                                         anchors.fill: parent 
    90                                         onClicked: { 
    91                                                 main.state  ="VIEW" 
    92                                         } 
    93                                 } 
    94                         } 
    95                         */ 
    9681        } 
    9782} 
  • telldus-gui/Plugins/Sensors/SensorView.qml

    r99c2b0 r645ef2  
    22import QtDesktop 0.1 
    33 
    4 Component { 
    5         id: sensorView 
    6         Item{ 
    7                 id: sensorViewItem 
    8                 visible: main.state == "EDIT" || modelData.showInList 
    9                 height: childrenRect.height 
     4Item{ 
     5        id: sensorViewItem 
     6        visible: state == "EDIT" || modelData.showInList 
     7        height: childrenRect.height 
     8        width: parent ? parent.width : 0 
     9 
     10        states:[ 
     11                State { 
     12                        name: "EDIT" 
     13                } 
     14        ] 
     15 
     16        BorderImage { 
     17                source: "row_bg.png" 
     18                border.left: 5; border.top: 5 
     19                border.right: 5; border.bottom: 5 
     20                height: sensorInfo.height 
    1021                width: parent.width 
    1122 
    12                 property string state: main.state 
    13                 onStateChanged: { 
    14                         if (state != "EDIT") { 
    15                                 modelData.setName(nameEdit.text) 
     23                Text { 
     24                        visible: sensorViewItem.state != "EDIT" 
     25                        anchors.left: parent.left 
     26                        anchors.leftMargin: 15 
     27                        height: 40 
     28                        verticalAlignment: Text.AlignVCenter 
     29                        text: modelData.name == '' ? '<unnamed>' : modelData.name; 
     30                        color: "#004275" 
     31                } 
     32                TextField { 
     33                        id: nameEdit 
     34                        visible: sensorViewItem.state == "EDIT" 
     35                        anchors.left: parent.left 
     36                        anchors.leftMargin: 15 
     37                        anchors.top: parent.top 
     38                        anchors.topMargin: (40-nameEdit.height)/2 
     39                        text: modelData.name; 
     40                        placeholderText: 'Enter a name' 
     41                        onTextChanged: modelData.name = text 
     42                } 
     43 
     44                Text{ 
     45                        anchors.right: sensorid.left 
     46                        visible: sensorViewItem.state == "EDIT" 
     47                        height: 40 
     48                        verticalAlignment: Text.AlignVCenter 
     49                        text: "Delete" 
     50                        font.underline: true 
     51                        color: "#004275" 
     52                        width: 50 
     53                        MouseArea{ 
     54                                anchors.fill: parent 
     55                                onClicked: { 
     56                                        confirmDeletion.visible = true; 
     57                                } 
     58                        } 
     59                } 
     60                Text{ 
     61                        id: sensorid 
     62                        anchors.right: model.left 
     63                        visible: sensorViewItem.state == "EDIT" 
     64                        height: 40 
     65                        verticalAlignment: Text.AlignVCenter 
     66                        text: modelData.id 
     67                        color: "#004275" 
     68                        width: 50 
     69                } 
     70                Text{ 
     71                        id: model 
     72                        anchors.right: visibleinlistcheckbox.left 
     73                        visible: sensorViewItem.state == "EDIT" 
     74                        height: 40 
     75                        verticalAlignment: Text.AlignVCenter 
     76                        text: modelData.model 
     77                        color: "#004275" 
     78                        width: 100 
     79                } 
     80                Item  { 
     81                        id: visibleinlistcheckbox 
     82                        height: 40 
     83                        width: 100 
     84                        anchors.right: sensorInfo.left 
     85                        CheckBox { 
     86                                id: checkBox 
     87                                anchors.centerIn: parent 
     88                                width: checkBox.height 
     89                                visible: sensorViewItem.state == "EDIT" 
     90                                checked: modelData.showInList 
     91                                onClicked: modelData.setShowInList(!modelData.showInList) 
    1692                        } 
    1793                } 
    1894 
    19                 BorderImage { 
    20                         source: "row_bg.png" 
    21                         border.left: 5; border.top: 5 
    22                         border.right: 5; border.bottom: 5 
    23                         height: sensorInfo.height 
    24                         width: parent.width 
    25  
    26                         Text { 
    27                                 visible: main.state == "VIEW" 
    28                                 anchors.left: parent.left 
    29                                 anchors.leftMargin: 15 
    30                                 height: 40 
    31                                 verticalAlignment: Text.AlignVCenter 
    32                                 text: modelData.name == '' ? '<unnamed>' : modelData.name; 
    33                                 color: "#004275" 
     95                Column { 
     96                        id: sensorInfo 
     97                        anchors.right: parent.right 
     98                        width: 250 
     99                        SensorValue { 
     100                                visible: modelData.hasTemperature 
     101                                text: visible ? modelData.sensorValue(1).value + '°C' : '' 
     102                                icon: "icon_temp.png" 
     103                                lastUpdated: visible ? modelData.sensorValue(1).lastUpdated : new Date() 
    34104                        } 
    35                         TextField { 
    36                                 id: nameEdit 
    37                                 visible: main.state == "EDIT" 
    38                                 anchors.left: parent.left 
    39                                 anchors.leftMargin: 15 
    40                                 anchors.top: parent.top 
    41                                 anchors.topMargin: (40-nameEdit.height)/2 
    42                                 text: modelData.name; 
    43                                 placeholderText: 'Enter a name' 
    44                         } 
    45                         Text{ 
    46                                 anchors.right: model.left 
    47                                 visible: main.state == "EDIT" 
    48                                 height: 40 
    49                                 verticalAlignment: Text.AlignVCenter 
    50                                 text: modelData.id 
    51                                 color: "#004275" 
    52                                 width: 50 
    53                         } 
    54                         Text{ 
    55                                 id: model 
    56                                 anchors.right: visibleinlistcheckbox.left 
    57                                 visible: main.state == "EDIT" 
    58                                 height: 40 
    59                                 verticalAlignment: Text.AlignVCenter 
    60                                 text: modelData.model 
    61                                 color: "#004275" 
    62                                 width: 100 
    63                         } 
    64                         Item  { 
    65                                 id: visibleinlistcheckbox 
    66                                 height: 40 
    67                                 width: 100 
    68                                 anchors.right: sensorInfo.left 
    69                                 CheckBox { 
    70                                         id: checkBox 
    71                                         anchors.centerIn: parent 
    72                                         width: checkBox.height 
    73                                         visible: main.state == "EDIT" 
    74                                         checked: modelData.showInList 
    75                                         onClicked: modelData.setShowInList(!modelData.showInList) 
    76                                 } 
    77                         } 
    78  
    79                         Column { 
    80                                 id: sensorInfo 
    81                                 anchors.right: parent.right 
    82                                 width: 250 
    83                                 SensorValue { 
    84                                         visible: modelData.hasTemperature 
    85                                         text: visible ? modelData.sensorValue(1).value + '°C' : '' 
    86                                         icon: "icon_temp.png" 
    87                                         lastUpdated: visible ? modelData.sensorValue(1).lastUpdated : new Date() 
    88                                 } 
    89                                 SensorValue { 
    90                                         visible: modelData.hasHumidity 
    91                                         text: visible ? modelData.sensorValue(2).value + '%' : '' 
    92                                         icon: "icon_humidity.png" 
    93                                         lastUpdated: visible ? modelData.sensorValue(2).lastUpdated : new Date() 
    94                                 } 
     105                        SensorValue { 
     106                                visible: modelData.hasHumidity 
     107                                text: visible ? modelData.sensorValue(2).value + '%' : '' 
     108                                icon: "icon_humidity.png" 
     109                                lastUpdated: visible ? modelData.sensorValue(2).lastUpdated : new Date() 
    95110                        } 
    96111                } 
    97112        } 
     113 
     114        Dialog{ 
     115                id: confirmDeletion 
     116                modal: true 
     117                title: "Confirm deletion" 
     118                Text{ 
     119                        id: descriptionHeadline 
     120                        text:"Delete this sensor?" 
     121                        font.bold: true 
     122                } 
     123                Text{ 
     124                        id: descriptionText 
     125                        anchors.top: descriptionHeadline.bottom 
     126                        anchors.topMargin: 10 
     127                        width: parent.width - 20 
     128                        anchors.left: parent.left 
     129                        anchors.leftMargin: 10 
     130                        text: "Please note that a sensor that is still transmitting will reappear here again, but it will be hidden in the list by default." 
     131                        wrapMode: Text.Wrap 
     132                } 
     133 
     134                onAccepted: { 
     135                        deleteSensor.callWith(modelData.protocol, modelData.model, modelData.id); 
     136                } 
     137        } 
    98138} 
  • telldus-gui/Plugins/Sensors/__init__.js

    ref9a20 r645ef2  
    77 
    88com.telldus.sensors = function() { 
    9         var sensorList = new com.telldus.qml.array(); 
    10  
     9        var sensorList; 
    1110        function init() { 
    1211                var sensorData = 0; 
     12                sensorList = loadSensorModel(); 
     13                sensorList.rowsRemoved.connect(function(){saveSensorModel();}); 
     14                sensorList.rowsInserted.connect(function(){saveSensorModel();}); 
     15 
    1316                while(sensorData = com.telldus.core.sensor()) { 
    1417                        var p = sensorData["protocol"]; 
     
    2023                                if (types & type) { 
    2124                                        sensorValue = com.telldus.core.sensorValue(p, m, id, type); 
    22                                         sensorEvent(p, m, id, type, sensorValue["value"], sensorValue["timestamp"]); 
     25                                        sensorEvent(p, m, id, type, sensorValue["value"], sensorValue["timestamp"], true); 
    2326                                } 
    2427                        } 
     
    2932 
    3033                com.telldus.core.sensorEvent.connect(sensorEvent); 
    31                 view = new com.telldus.qml.view({}); 
     34                view = new com.telldus.qml.view({ 
     35                        deleteSensor: deleteSensor 
     36                }); 
    3237 
    3338                view.setProperty('sensorModel', sensorList); 
     39                saveSensorModel(); 
    3440                view.load("main.qml"); 
    3541                application.addWidget("sensors.gui", "icon.png", view); 
    3642        } 
    3743 
    38         function sensorEvent(protocol, model, id, dataType, value, timestamp) { 
     44        function createSensor(protocol, model, id, name, showInList){ 
     45                var sensor = new com.telldus.sensors.sensor(); 
     46                sensor.protocol = protocol; 
     47                sensor.model = model; 
     48                sensor.id = id; 
     49                sensor.name = name; 
     50                sensor.nameChanged.connect(function() { saveSensorModel(); }); 
     51                sensor.showInList = showInList; 
     52                sensor.showInListChanged.connect(function() { saveSensorModel(); }); 
     53                return sensor; 
     54        } 
     55 
     56        function deleteSensor(protocol, model, id){ 
     57                var i = 0; 
     58                var found = false; 
     59                for (; i < sensorList.length; ++i) { 
     60                        if (sensorList.get(i).protocol != protocol) { 
     61                                continue; 
     62                        } 
     63                        if (sensorList.get(i).model != model) { 
     64                                continue; 
     65                        } 
     66                        if (sensorList.get(i).id != id) { 
     67                                continue; 
     68                        } 
     69                        found = true; 
     70                        break; 
     71                } 
     72                if(found){ 
     73                        sensorList.removeLater(i); 
     74                } 
     75        } 
     76 
     77        function loadSensorModel(){ 
     78                var settings = new com.telldus.settings(); 
     79                var sensors = new com.telldus.qml.array(); 
     80 
     81                var sensorProperties = settings.value("sensors", ""); 
     82                if(sensorProperties){ 
     83                        for (var i = 0; i < sensorProperties.length; i++) { 
     84                                var sensor = createSensor(sensorProperties[i].protocol, sensorProperties[i].model, sensorProperties[i].id, sensorProperties[i].name, sensorProperties[i].showInList=="true"); 
     85                                for (var j = 0; j < sensorProperties[i].values.length; j++) { 
     86                                        sensor.setValue(sensorProperties[i].values[j].type, sensorProperties[i].values[j].value, sensorProperties[i].values[j].lastUpdated) 
     87                                } 
     88                                sensors.push(sensor); 
     89                        } 
     90                } 
     91                return sensors; 
     92        } 
     93 
     94        function saveSensorModel(){ 
     95                var settings = new com.telldus.settings(); 
     96                var sensorProperties = new Array(); 
     97 
     98                for (var i = 0; i < sensorList.length; ++i) { 
     99                        var sensor = sensorList.get(i); 
     100 
     101                        var allValues = new Array(); 
     102                        if(sensor.hasHumidity){ 
     103                                var sensorValue = sensor.sensorValue(com.telldus.core.TELLSTICK_HUMIDITY); 
     104                                var value = {type: com.telldus.core.TELLSTICK_HUMIDITY, lastUpdated: sensorValue.lastUpdated, value: sensorValue.value}; 
     105                                allValues.push(value); 
     106                        } 
     107                        if(sensor.hasTemperature){ 
     108                                var sensorValue = sensor.sensorValue(com.telldus.core.TELLSTICK_TEMPERATURE); 
     109                                var value = {type: com.telldus.core.TELLSTICK_TEMPERATURE, lastUpdated: sensorValue.lastUpdated, value: sensorValue.value}; 
     110                                allValues.push(value); 
     111                        } 
     112 
     113                        var sensorProp = {protocol:sensor.protocol, model:sensor.model, id:sensor.id, values:allValues, name:sensor.name, showInList:sensor.showInList}; 
     114                        sensorProperties.push(sensorProp); 
     115                } 
     116 
     117                settings.setValue("sensors", sensorProperties); 
     118        } 
     119 
     120        function sensorEvent(protocol, model, id, dataType, value, timestamp, avoidSave) { 
     121 
    39122                var sensor = 0; 
    40123                for (var i = 0; i < sensorList.length; ++i) { 
     
    53136 
    54137                if (!sensor) { 
    55                         sensor = new com.telldus.sensors.sensor(); 
    56                         sensor.protocol = protocol; 
    57                         sensor.model = model; 
    58                         sensor.id = id; 
    59                         sensor.showInList = false; 
     138                        sensor = createSensor(protocol, model, id, "", false); 
    60139                        sensorList.push(sensor); 
    61                         print("Create new"); 
    62                 } else { 
    63                         print("Update"); 
    64140                } 
    65141 
    66                 print("Sensor event", protocol, model, id, dataType, value, timestamp); 
    67142                sensor.setValue(dataType, value, timestamp); 
     143 
     144                if(!avoidSave){ 
     145                        saveSensorModel(); 
     146                } 
    68147        } 
    69148 
  • telldus-gui/Plugins/Sensors/sensor.cpp

    r99c2b0 r645ef2  
    1818        d = new PrivateData; 
    1919        d->id = 0; 
     20        d->showInList = false; 
    2021} 
    2122 
     
    5152 
    5253void Sensor::setName(const QString &name) { 
     54        if (name == d->name) { 
     55                return; 
     56        } 
    5357        d->name = name; 
    5458        emit nameChanged(); 
     
    6872} 
    6973 
    70 SensorValue * Sensor::sensorValue(int type) { 
     74QObject * Sensor::sensorValue(int type) { 
    7175        return (d->values.contains(type) ? d->values[type] : 0); 
    7276} 
     
    9195 
    9296bool Sensor::showInList() const{ 
    93         //TODO showInList and name must be persistent... 
    9497        return d->showInList; 
    9598} 
  • telldus-gui/Plugins/Sensors/sensor.h

    ref9a20 r645ef2  
    1818        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) 
    1919        Q_PROPERTY(QString protocol READ protocol WRITE setProtocol NOTIFY protocolChanged) 
    20         Q_PROPERTY(bool showInList READ showInList NOTIFY showInListChanged) 
     20        Q_PROPERTY(bool showInList READ showInList WRITE setShowInList NOTIFY showInListChanged) 
    2121 
    2222public: 
     
    3333 
    3434        QString name() const; 
    35         //void setName(const QString &name); 
    3635 
    3736        QString protocol() const; 
     
    4140        bool showInList() const; 
    4241 
    43         Q_INVOKABLE SensorValue *sensorValue(int type); 
     42        Q_INVOKABLE QObject *sensorValue(int type); 
    4443        Q_INVOKABLE void setValue(int type, const QString &value, const QDateTime &timestamp); 
    4544        Q_INVOKABLE void setName(const QString &name); 
  • telldus-gui/Plugins/Settings/settings.cpp

    r6281cf r645ef2  
    2727                d->s.setValue("size", list.size()); 
    2828                d->s.setValue("type", "array"); 
     29                //d->s.beginWriteArray("list"); //TODO write or read? What prefix? 
    2930                for (int i = 0; i < list.size(); ++i) { 
    30                         d->s.setArrayIndex(i); 
     31                        //d->s.setArrayIndex(i); 
    3132                        this->setValue(QString::number(i), list.at(i)); 
    3233                } 
     34                //d->s.endArray(); 
    3335                d->s.endGroup(); 
    3436 
Note: See TracChangeset for help on using the changeset viewer.