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