Ver Fonte

Добавил загрузку модели, добавил рыбу делегата

Denis V. Dedkov há 7 anos atrás
pai
commit
c624c23fee
4 ficheiros alterados com 83 adições e 4 exclusões
  1. 21 4
      HomeForm.qml
  2. 21 0
      LightGroup.qml
  3. 39 0
      LightsModel.qml
  4. 2 0
      qml.qrc

+ 21 - 4
HomeForm.qml

@@ -2,10 +2,27 @@ import QtQuick 2.0
 import QtQuick.Controls 2.0
 
 Page {
-    title: qsTr("Home")
+    title: qsTr("nooLight")
 
-    Label {
-        text: qsTr("You are on the home page. " + settings.serviceUrl)
-        anchors.centerIn: parent
+    LightsModel {
+        id: lightsModel
+
+        serviceUrl: settings.serviceUrl
+
+        onError: console.log(text)
+    }
+
+    ListView {
+        anchors.fill: parent
+
+        model: lightsModel
+        spacing: 5
+
+        delegate: LightGroup {
+            width: parent.width
+            height: childrenRect.height
+
+            title: groupName || ""
+        }
     }
 }

+ 21 - 0
LightGroup.qml

@@ -0,0 +1,21 @@
+import QtQuick 2.0
+import QtQuick.Controls 2.2
+
+Item {
+    id: root
+
+    property string title: ""
+    property var lights: []
+
+    Rectangle {
+        width: parent.width
+        height: 20
+
+        color: "green"
+
+        Label {
+            text: root.title
+            anchors.centerIn: parent
+        }
+    }
+}

+ 39 - 0
LightsModel.qml

@@ -0,0 +1,39 @@
+import QtQml.Models 2.1
+
+ListModel {
+    id: root
+
+    property string serviceUrl: undefined
+
+    signal error(string text)
+
+    onServiceUrlChanged: reload()
+
+    function reload() {
+        var request = new XMLHttpRequest()
+
+        request.open('GET', root.serviceUrl + '/static/channels.js')
+        request.onreadystatechange = function () {
+            if (request.readyState !== XMLHttpRequest.DONE) {
+                return
+            }
+
+            if (request.status === 200) {
+                populateModel(JSON.parse(request.responseText))
+                return
+            }
+
+            root.error(qsTr("[%1] Request error: %2").
+                      arg(request.status).
+                      arg(request.statusText))
+        }
+
+        request.send()
+    }
+
+    function populateModel(data) {
+        data.groups.forEach(function (group) {
+            root.append(group)
+        })
+    }
+}

+ 2 - 0
qml.qrc

@@ -6,5 +6,7 @@
         <file>SettingsForm.qml</file>
         <file>MenuBackButton.qml</file>
         <file>SubtitledItemDelegate.qml</file>
+        <file>LightsModel.qml</file>
+        <file>LightGroup.qml</file>
     </qresource>
 </RCC>