Просмотр исходного кода

Добавил сервисную страницу, небольшой рефакторинг

Denis V. Dedkov 7 лет назад
Родитель
Сommit
76786176d2
6 измененных файлов с 89 добавлено и 21 удалено
  1. 0 12
      HomeForm.qml
  2. 16 1
      LightsModel.qml
  3. 2 2
      NooLiteClient.qml
  4. 40 0
      ServiceForm.qml
  5. 30 6
      main.qml
  6. 1 0
      qml.qrc

+ 0 - 12
HomeForm.qml

@@ -4,18 +4,6 @@ import QtQuick.Controls 2.0
 Page {
     title: qsTr("nooLight")
 
-    function showError(text) {
-        ToolTip.show(text, 1000)
-    }
-
-    LightsModel {
-        id: lightsModel
-
-        serviceUrl: settings.serviceUrl
-
-        onError: showError(text)
-    }
-
     ListView {
         anchors.fill: parent
         anchors.margins: 5

+ 16 - 1
LightsModel.qml

@@ -20,6 +20,8 @@ ListModel {
     property alias serviceUrl: nooLiteClient.serviceUrl
     property bool isLoading: false
 
+    property int channelsCount: 0
+
     signal error(string text)
 
     onServiceUrlChanged: reload()
@@ -30,12 +32,25 @@ ListModel {
     }
 
     function populateModel(data) {
+        root.clear()
+        root.channelsCount = 0
+
         data.groups.forEach(function (group) {
             root.append(group)
+
+            root.channelsCount += group.channels.length
         })
     }
 
     function switchChannel(channelId) {
-        root.client.switchChannel(channelId)
+        root.client.sendCommand("switch", channelId)
+    }
+
+    function bindChannel(channelId) {
+        root.client.sendCommand("bind", channelId)
+    }
+
+    function unbindChannel(channelId) {
+        root.client.sendCommand("unbind", channelId)
     }
 }

+ 2 - 2
NooLiteClient.qml

@@ -37,8 +37,8 @@ QtObject {
         _get(root.serviceUrl + '/static/channels.js', root.modelLoad)
     }
 
-    function switchChannel(channelId) {
-        _get(root.serviceUrl + '/noolite/switch/%1'.arg(channelId), function (data) {
+    function sendCommand(command, channelId) {
+        _get(root.serviceUrl + '/noolite/%1/%2'.arg(command).arg(channelId), function (data) {
             if (data.error) {
                 root.error(qsTr("Server error: %1").arg(data.error))
                 return

+ 40 - 0
ServiceForm.qml

@@ -0,0 +1,40 @@
+import QtQuick 2.0
+import QtQuick.Controls 2.2
+
+Page {
+    id: root
+
+    title: qsTr("Service")
+
+    ListView {
+        model: lightsModel.channelsCount
+
+        anchors.fill: parent
+
+        delegate: Row {
+            spacing: 4
+
+            anchors.horizontalCenter: parent.horizontalCenter
+
+            Button {
+                width: 160
+
+                text: qsTr("Bind channel %1").arg(index)
+
+                onClicked: {
+                    lightsModel.bindChannel(index)
+                }
+            }
+
+            Button {
+                width: 160
+
+                text: qsTr("Unbind channel %1").arg(index)
+
+                onClicked: {
+                    lightsModel.unbindChannel(index)
+                }
+            }
+        }
+    }
+}

+ 30 - 6
main.qml

@@ -16,6 +16,14 @@ ApplicationWindow {
         property string serviceUrl: ""
     }
 
+    LightsModel {
+        id: lightsModel
+
+        serviceUrl: settings.serviceUrl
+
+        onError: stackView.showError(text)
+    }
+
     header: ToolBar {
         contentHeight: 36
 
@@ -73,15 +81,18 @@ ApplicationWindow {
             }
 
             ItemDelegate {
-                text: qsTr("Settings")
+                text: qsTr("Service")
                 width: parent.width
                 onClicked: {
-                    if (stackView.depth > 1) {
-                        stackView.pop()
-                    }
+                    stackView.openPage("ServiceForm.qml")
+                }
+            }
 
-                    stackView.push("SettingsForm.qml")
-                    drawer.close()
+            ItemDelegate {
+                text: qsTr("Settings")
+                width: parent.width
+                onClicked: {
+                    stackView.openPage("SettingsForm.qml")
                 }
             }
         }
@@ -91,6 +102,19 @@ ApplicationWindow {
         id: stackView
         initialItem: "HomeForm.qml"
         anchors.fill: parent
+
+        function openPage(page) {
+            if (depth > 1) {
+                pop()
+            }
+
+            push(page)
+            drawer.close()
+        }
+
+        function showError(text) {
+            ToolTip.show(text, 1000)
+        }
     }
 
     onClosing: {

+ 1 - 0
qml.qrc

@@ -12,5 +12,6 @@
         <file>lamp.png</file>
         <file>Off.png</file>
         <file>NooLiteClient.qml</file>
+        <file>ServiceForm.qml</file>
     </qresource>
 </RCC>