From 2fafd0603824ca400038b9abe07b288ec74a45ee Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Sun, 26 Nov 2017 17:08:01 +0100 Subject: [PATCH] add nicer graphs --- guh-control/devicemanager.cpp | 2 +- guh-control/discovery/upnpdiscovery.cpp | 1 + guh-control/guh-control.pro | 2 +- guh-control/models/valuelogsproxymodel.cpp | 2 +- guh-control/ui/SettingsPage.qml | 18 +++++ guh-control/ui/components/Graph.qml | 45 ++++++++++- guh-control/ui/customviews/SensorView.qml | 88 +++------------------- guh-control/ui/main.qml | 9 ++- 8 files changed, 77 insertions(+), 90 deletions(-) diff --git a/guh-control/devicemanager.cpp b/guh-control/devicemanager.cpp index 5061897d..a30146db 100644 --- a/guh-control/devicemanager.cpp +++ b/guh-control/devicemanager.cpp @@ -133,7 +133,7 @@ void DeviceManager::getSupportedDevicesResponse(const QVariantMap ¶ms) QVariantList deviceClassList = params.value("params").toMap().value("deviceClasses").toList(); foreach (QVariant deviceClassVariant, deviceClassList) { DeviceClass *deviceClass = JsonTypes::unpackDeviceClass(deviceClassVariant.toMap(), Engine::instance()->deviceManager()->deviceClasses()); -// qDebug() << "Server has device class:" << deviceClass->name() << deviceClass->id(); + qDebug() << "Server has device class:" << deviceClass->name() << deviceClass->id(); m_deviceClasses->addDeviceClass(deviceClass); } } diff --git a/guh-control/discovery/upnpdiscovery.cpp b/guh-control/discovery/upnpdiscovery.cpp index a1366414..73993567 100644 --- a/guh-control/discovery/upnpdiscovery.cpp +++ b/guh-control/discovery/upnpdiscovery.cpp @@ -126,6 +126,7 @@ void UpnpDiscovery::writeDiscoveryPacket() "MX:2\r\n" "ST: ssdp:all\r\n\r\n"); + qDebug() << "sending discovery packet"; writeDatagram(ssdpSearchMessage, m_host, m_port); } diff --git a/guh-control/guh-control.pro b/guh-control/guh-control.pro index 4bdf0d04..2fa7abf9 100644 --- a/guh-control/guh-control.pro +++ b/guh-control/guh-control.pro @@ -3,7 +3,7 @@ TARGET=guh-control include(../guh-control.pri) -QT += qml quick quickcontrols2 websockets svg +QT += qml quick quickcontrols2 websockets svg charts INCLUDEPATH += $$top_srcdir/libguh-common LIBS += -L$$top_builddir/libguh-common/ -lguh-common diff --git a/guh-control/models/valuelogsproxymodel.cpp b/guh-control/models/valuelogsproxymodel.cpp index bc6656d1..30dc657c 100644 --- a/guh-control/models/valuelogsproxymodel.cpp +++ b/guh-control/models/valuelogsproxymodel.cpp @@ -114,7 +114,7 @@ void ValueLogsProxyModel::logsReply(const QVariantMap &data) } else { continue; } - LogEntry *entry = new LogEntry(startTime().addSecs(stepSize * i).addSecs(stepSize * .5), avg, this); + LogEntry *entry = new LogEntry(startTime().addSecs(stepSize * i)/*.addSecs(stepSize * .5)*/, avg, this); m_list.append(entry); // qDebug() << "**" << m_minimumValue << entry->value(); diff --git a/guh-control/ui/SettingsPage.qml b/guh-control/ui/SettingsPage.qml index c16f2b3c..dbfe2580 100644 --- a/guh-control/ui/SettingsPage.qml +++ b/guh-control/ui/SettingsPage.qml @@ -43,5 +43,23 @@ Page { onClicked: settings.returnToHome = checked } } + RowLayout { + Layout.fillWidth: true + Label { + Layout.fillWidth: true + text: "Graph style" + } + RadioButton { + checked: settings.graphStyle === "bars" + text: "Bars" + onClicked: settings.graphStyle = "bars" + } + RadioButton { + checked: settings.graphStyle === "bezier" + text: "Lines" + onClicked: settings.graphStyle = "bezier" + } + + } } } diff --git a/guh-control/ui/components/Graph.qml b/guh-control/ui/components/Graph.qml index fd41e3a8..aabf06e4 100644 --- a/guh-control/ui/components/Graph.qml +++ b/guh-control/ui/components/Graph.qml @@ -8,6 +8,7 @@ Item { property var model: null property color color: "grey" + property string mode: "bezier" // "bezier" or "bars" Connections { target: model @@ -107,8 +108,13 @@ Item { paintGrid(ctx) enumerate(ctx) -// paintWifis(ctx, canvas.selectedIndex) - paintGraph(ctx) + + if (root.mode == "bezier") { + paintGraph(ctx) + } else { + paintBars(ctx) + } + ctx.restore(); } @@ -170,8 +176,8 @@ Item { // enumerate Y axis var lastTextX = -1; - for (var i = 0; i < model.count - 1; i++) { - var x = contentWidth / (model.count - 1) * i; + for (var i = 0; i < model.count; i++) { + var x = contentWidth / (model.count) * i; if (x < lastTextX) continue; var label = model.get(i).dayString @@ -310,6 +316,37 @@ Item { } } + function paintBars(ctx) { + if (model.count <= 1) { + return; + } + + var tempInterval = (maxTemp - minTemp) / sections; + + ctx.globalAlpha = 1; + ctx.lineWidth = 2; + var graphStroke = root.color; + var grapFill = Qt.rgba(root.color.r, root.color.g, root.color.b, .2); + + ctx.strokeStyle = graphStroke; + ctx.fillStyle = grapFill; + + + for (var i = 0; i < model.count; i++) { + ctx.beginPath(); + var value = model.get(i).value; + var x = contentWidth / (model.count) * i; + var y = contentHeight - (value - minTemp) / tempInterval * pps; + + var slotWidth = contentWidth / model.count + ctx.rect(x,y, slotWidth - 5, contentHeight - y) + ctx.fillRect(x,y, slotWidth - 5, contentHeight - y); + ctx.stroke(); + ctx.fill(); + ctx.closePath(); + } + } + function hourToX(hour) { var entries = root.day.count; return canvas.contentWidth / entries * hour diff --git a/guh-control/ui/customviews/SensorView.qml b/guh-control/ui/customviews/SensorView.qml index 8a3baa1f..1da2a975 100644 --- a/guh-control/ui/customviews/SensorView.qml +++ b/guh-control/ui/customviews/SensorView.qml @@ -2,7 +2,6 @@ import QtQuick 2.5 import QtQuick.Controls 2.1 import QtQuick.Controls.Material 2.1 import QtQuick.Layouts 1.3 -//import QtCharts 2.1 import "../components" import Guh 1.0 @@ -25,19 +24,6 @@ CustomViewBase { onAverageChanged: updateTimer.start() onStartTimeChanged: updateTimer.start(); - onBusyChanged: { - if (!busy) { - -// lineSeries1.clear() -// splineSeries.clear() -// print("---", axisX.min, axisX.max) -// for (var i = 0; i < logsModel.count; i++) { -// print("adding", logsModel.get(i).timestamp, logsModel.get(i).value) -// lineSeries1.append(logsModel.get(i).timestamp, logsModel.get(i).value) -// splineSeries.append(logsModel.get(i).timestamp, logsModel.get(i).value) -// } - } - } } Timer { @@ -78,6 +64,8 @@ CustomViewBase { property date startTime: { var date = new Date(); date.setHours(new Date().getHours() - 6) + date.setMinutes(0) + date.setSeconds(0) return date; } } @@ -87,6 +75,8 @@ CustomViewBase { property date startTime: { var date = new Date(); date.setHours(new Date().getHours() - 24); + date.setMinutes(0) + date.setSeconds(0) return date; } } @@ -96,6 +86,9 @@ CustomViewBase { property date startTime: { var date = new Date(); date.setDate(new Date().getDate() - 7); + date.setHours(0) + date.setMinutes(0) + date.setSeconds(0) return date; } } @@ -105,72 +98,9 @@ CustomViewBase { Layout.fillWidth: true Layout.preferredHeight: 200 model: logsModel + mode: settings.graphStyle color: app.interfaceToColor(root.interfaceName) } - -// ChartView { -// id: chartView -// Layout.fillWidth: true -// Layout.preferredHeight: 300 -// animationOptions: ChartView.SeriesAnimations -// theme: ChartView.ChartThemeQt -// backgroundColor: Material.background -// property bool openGL: false -// property bool openGLSupported: true -// onOpenGLChanged: { -// if (openGLSupported) { -// series("signal 1").useOpenGL = openGL; -// series("signal 2").useOpenGL = openGL; -// } -// } -// Component.onCompleted: { -// if (!series("signal 1").useOpenGL) { -// openGLSupported = false -// openGL = false -// } -// } - -// ValueAxis { -// id: axisY1 -// min: logsModel.minimumValue - 1 -// max: logsModel.maximumValue + 1 -// } - -// DateTimeAxis { -// id: axisX -// min: logsModel.startTime -// max: logsModel.endTime -// format: { -// switch (logsModel.average) { -// case ValueLogsProxyModel.AverageMinute: -// case ValueLogsProxyModel.AverageHourly: -// case ValueLogsProxyModel.AverageQuarterHour: -// return "hh:mm" -// } -// return "ddd
dd.MM.
hh:mm" -// } -// } - - -// AreaSeries { -// axisX: axisX -// axisY: axisY1 -// borderWidth: 0 -// name: app.interfaceToString(interfaceName) -// borderColor: app.interfaceToColor(interfaceName) -// color: Qt.rgba(borderColor.r, borderColor.g, borderColor.b, .4) -// useOpenGL: chartView.openGL -// upperSeries: LineSeries { -// id: lineSeries1 -// } -// } -// SplineSeries { -// id: splineSeries -// axisX: axisX -// axisY: axisY1 -// width: 3 -// color: app.interfaceToColor(interfaceName) -// } -// } } } + diff --git a/guh-control/ui/main.qml b/guh-control/ui/main.qml index c682fdf5..ae7140e3 100644 --- a/guh-control/ui/main.qml +++ b/guh-control/ui/main.qml @@ -31,6 +31,7 @@ ApplicationWindow { property string lastConnectedHost: "" property bool fullscreen: false property bool returnToHome: false + property string graphStyle: "bars" } Component.onCompleted: { @@ -90,6 +91,10 @@ ApplicationWindow { id: discovery } +// ZeroconfDiscovery { +// id: discovery +// } + Connections { target: Qt.application enabled: Engine.jsonRpcClient.connected && settings.returnToHome @@ -163,10 +168,6 @@ ApplicationWindow { return "grey"; } -// ZeroconfDiscovery { -// id: discovery -// } - Component { id: invalidVersionComponent Popup {