From c624c23fee8e2816f17b3f5dc3009f30a649f97b Mon Sep 17 00:00:00 2001 From: "Denis V. Dedkov" Date: Mon, 23 Jul 2018 21:22:56 +0200 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D1=83=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B8,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=80=D1=8B=D0=B1=D1=83=20=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=B3=D0=B0=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HomeForm.qml | 25 +++++++++++++++++++++---- LightGroup.qml | 21 +++++++++++++++++++++ LightsModel.qml | 39 +++++++++++++++++++++++++++++++++++++++ qml.qrc | 2 ++ 4 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 LightGroup.qml create mode 100644 LightsModel.qml diff --git a/HomeForm.qml b/HomeForm.qml index 4d8b3a6..ba72a28 100644 --- a/HomeForm.qml +++ b/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 || "" + } } } diff --git a/LightGroup.qml b/LightGroup.qml new file mode 100644 index 0000000..02e2be6 --- /dev/null +++ b/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 + } + } +} diff --git a/LightsModel.qml b/LightsModel.qml new file mode 100644 index 0000000..fe2bb45 --- /dev/null +++ b/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) + }) + } +} diff --git a/qml.qrc b/qml.qrc index d5c7474..ff00483 100644 --- a/qml.qrc +++ b/qml.qrc @@ -6,5 +6,7 @@ SettingsForm.qml MenuBackButton.qml SubtitledItemDelegate.qml + LightsModel.qml + LightGroup.qml