From b6001e4e816a2dd16f0a304cf83cab909bdcc854 Mon Sep 17 00:00:00 2001 From: "Denis V. Dedkov" Date: Sun, 12 Aug 2018 10:06:17 +0200 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=B0=D0=BD=D0=B0=D0=BB=D0=BE=D0=B2,=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88=D0=BE=D0=B9=20=D1=80?= =?UTF-8?q?=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GradientButton.qml | 2 ++ HomeForm.qml | 10 +++++++++- HttpClient.qml | 30 ---------------------------- LightGroup.qml | 6 ++++++ LightsModel.qml | 14 ++++++++----- NooLiteClient.qml | 50 ++++++++++++++++++++++++++++++++++++++++++++++ qml.qrc | 2 +- 7 files changed, 77 insertions(+), 37 deletions(-) delete mode 100644 HttpClient.qml create mode 100644 NooLiteClient.qml diff --git a/GradientButton.qml b/GradientButton.qml index af2902d..b450890 100644 --- a/GradientButton.qml +++ b/GradientButton.qml @@ -26,6 +26,8 @@ Rectangle { id: ma anchors.fill: parent + + onClicked: root.clicked() } gradient: ma.pressed ? pressedGradient : normalGradient diff --git a/HomeForm.qml b/HomeForm.qml index 67502a6..6d95862 100644 --- a/HomeForm.qml +++ b/HomeForm.qml @@ -4,12 +4,16 @@ import QtQuick.Controls 2.0 Page { title: qsTr("nooLight") + function showError(text) { + ToolTip.show(text, 1000) + } + LightsModel { id: lightsModel serviceUrl: settings.serviceUrl - onError: console.log(text) + onError: showError(text) } ListView { @@ -25,6 +29,10 @@ Page { title: groupName || "" lights: channels + + onChannelClicked: { + lightsModel.switchChannel(channelId) + } } } diff --git a/HttpClient.qml b/HttpClient.qml deleted file mode 100644 index 802b605..0000000 --- a/HttpClient.qml +++ /dev/null @@ -1,30 +0,0 @@ -import QtQuick 2.0 - -QtObject { - id: root - - signal reply(var data) - signal error(string text) - - function get(url) { - var request = new XMLHttpRequest() - - request.open('GET', url) - request.onreadystatechange = function () { - if (request.readyState !== XMLHttpRequest.DONE) { - return - } - - if (request.status === 200) { - root.reply(JSON.parse(request.responseText)) - return - } - - root.error(qsTr("[%1] Request error: %2"). - arg(request.status). - arg(request.statusText)) - } - - request.send() - } -} diff --git a/LightGroup.qml b/LightGroup.qml index dc7ae29..3bd563b 100644 --- a/LightGroup.qml +++ b/LightGroup.qml @@ -7,6 +7,8 @@ Item { property string title: "" property QtObject lights: undefined + signal channelClicked(int channelId) + Column { width: root.width @@ -71,6 +73,10 @@ Item { text: name } } + + onClicked: { + root.channelClicked(id) + } } } } diff --git a/LightsModel.qml b/LightsModel.qml index 4e8b1c6..a5d3c2e 100644 --- a/LightsModel.qml +++ b/LightsModel.qml @@ -3,21 +3,21 @@ import QtQml.Models 2.1 ListModel { id: root - readonly property var httpClient: HttpClient { - id: httpClient + readonly property var client: NooLiteClient { + id: nooLiteClient onError: { root.error(text) root.isLoading = false } - onReply: { + onModelLoad: { root.populateModel(data) root.isLoading = false } } - property string serviceUrl: undefined + property alias serviceUrl: nooLiteClient.serviceUrl property bool isLoading: false signal error(string text) @@ -25,7 +25,7 @@ ListModel { onServiceUrlChanged: reload() function reload() { - root.httpClient.get(root.serviceUrl + '/static/channels.js') + root.client.loadModel() root.isLoading = true } @@ -34,4 +34,8 @@ ListModel { root.append(group) }) } + + function switchChannel(channelId) { + root.client.switchChannel(channelId) + } } diff --git a/NooLiteClient.qml b/NooLiteClient.qml new file mode 100644 index 0000000..caba1f2 --- /dev/null +++ b/NooLiteClient.qml @@ -0,0 +1,50 @@ +import QtQuick 2.0 + +QtObject { + id: root + + property string serviceUrl: undefined + + signal modelLoad(var data) + signal error(string text) + + function _get(url, callback) { + var request = new XMLHttpRequest() + + request.open('GET', url) + request.onreadystatechange = function () { + if (request.readyState !== XMLHttpRequest.DONE) { + return + } + + if (request.status === 200) { + if (callback !== undefined) { + callback(JSON.parse(request.responseText)) + } + + return + } + + root.error(qsTr("[%1] Request error: %2"). + arg(request.status). + arg(request.statusText)) + } + + request.send() + } + + function loadModel() { + _get(root.serviceUrl + '/static/channels.js', root.modelLoad) + } + + function switchChannel(channelId) { + _get(root.serviceUrl + '/noolite/switch/%1'.arg(channelId), function (data) { + if (data.error) { + root.error(qsTr("Server error: %1").arg(data.error)) + return + } + + console.log(data.command, data.channel) + }) + } +} diff --git a/qml.qrc b/qml.qrc index 5c3e215..9f8acd6 100644 --- a/qml.qrc +++ b/qml.qrc @@ -11,6 +11,6 @@ GradientButton.qml lamp.png Off.png - HttpClient.qml + NooLiteClient.qml