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

This commit is contained in:
2018-08-14 20:24:20 +02:00
parent b6001e4e81
commit 76786176d2
6 changed files with 89 additions and 21 deletions

View File

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

View File

@@ -20,6 +20,8 @@ ListModel {
property alias serviceUrl: nooLiteClient.serviceUrl property alias serviceUrl: nooLiteClient.serviceUrl
property bool isLoading: false property bool isLoading: false
property int channelsCount: 0
signal error(string text) signal error(string text)
onServiceUrlChanged: reload() onServiceUrlChanged: reload()
@@ -30,12 +32,25 @@ ListModel {
} }
function populateModel(data) { function populateModel(data) {
root.clear()
root.channelsCount = 0
data.groups.forEach(function (group) { data.groups.forEach(function (group) {
root.append(group) root.append(group)
root.channelsCount += group.channels.length
}) })
} }
function switchChannel(channelId) { 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)
} }
} }

View File

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

40
ServiceForm.qml Normal file
View File

@@ -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)
}
}
}
}
}

View File

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

View File

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