From 10cbc9941b5b56ae06104d0544e543d58cf9837e Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Wed, 24 Oct 2018 11:06:31 +0200 Subject: [PATCH] initial take on QtCharts --- libnymea-app-core/libnymea-app-core.pro | 2 +- libnymea-app-core/models/logsmodelng.cpp | 17 ++++++- libnymea-app-core/models/logsmodelng.h | 9 ++++ nymea-app/main.cpp | 6 +-- nymea-app/nymea-app.pro | 2 +- nymea-app/ruletemplates/template.json | 58 +++++++++++++++++++++ nymea-app/ui/devicepages/StateLogPage.qml | 62 +++++++++++++++++++++++ 7 files changed, 149 insertions(+), 7 deletions(-) create mode 100644 nymea-app/ruletemplates/template.json diff --git a/libnymea-app-core/libnymea-app-core.pro b/libnymea-app-core/libnymea-app-core.pro index a0a3e3a0..514e2cce 100644 --- a/libnymea-app-core/libnymea-app-core.pro +++ b/libnymea-app-core/libnymea-app-core.pro @@ -15,7 +15,7 @@ include(../nymea-remoteproxy/libnymea-remoteproxyclient/libnymea-remoteproxyclie QT -= gui -QT += network websockets bluetooth +QT += network websockets bluetooth charts LIBS += -lssl -lcrypto diff --git a/libnymea-app-core/models/logsmodelng.cpp b/libnymea-app-core/models/logsmodelng.cpp index 5e0f89cc..d1a28c27 100644 --- a/libnymea-app-core/models/logsmodelng.cpp +++ b/libnymea-app-core/models/logsmodelng.cpp @@ -7,7 +7,8 @@ #include "types/logentry.h" #include "logmanager.h" -LogsModelNg::LogsModelNg(QObject *parent) : QAbstractListModel(parent) +LogsModelNg::LogsModelNg(QObject *parent) : QAbstractListModel(parent), + m_lineSeries(new QtCharts::QLineSeries(this)) { } @@ -133,9 +134,19 @@ void LogsModelNg::setEndTime(const QDateTime &endTime) } } +QtCharts::QLineSeries *LogsModelNg::lineSeries() const +{ + return m_lineSeries; +} + +void LogsModelNg::setLineSeries(QtCharts::QLineSeries *lineSeries) +{ + m_lineSeries = lineSeries; +} + void LogsModelNg::logsReply(const QVariantMap &data) { - qDebug() << "logs reply" << data; +// qDebug() << "logs reply" << data; m_busy = false; emit busyChanged(); @@ -170,6 +181,8 @@ void LogsModelNg::logsReply(const QVariantMap &data) beginInsertRows(QModelIndex(), offset, offset + newBlock.count() - 1); for (int i = 0; i < newBlock.count(); i++) { m_list.insert(offset + i, newBlock.at(i)); + qDebug() << "Adding line series point:" << i << newBlock.at(i)->timestamp().toSecsSinceEpoch() << newBlock.at(i)->value().toReal(); + m_lineSeries->insert(offset + i, QPointF(newBlock.at(i)->timestamp().toSecsSinceEpoch(), newBlock.at(i)->value().toReal())); } endInsertRows(); emit countChanged(); diff --git a/libnymea-app-core/models/logsmodelng.h b/libnymea-app-core/models/logsmodelng.h index 8881fb1d..ad0910af 100644 --- a/libnymea-app-core/models/logsmodelng.h +++ b/libnymea-app-core/models/logsmodelng.h @@ -4,6 +4,7 @@ #include #include #include +#include class LogEntry; class Engine; @@ -20,6 +21,8 @@ class LogsModelNg : public QAbstractListModel Q_PROPERTY(QDateTime startTime READ startTime WRITE setStartTime NOTIFY startTimeChanged) Q_PROPERTY(QDateTime endTime READ endTime WRITE setEndTime NOTIFY endTimeChanged) + Q_PROPERTY(QtCharts::QLineSeries *lineSeries READ lineSeries WRITE setLineSeries NOTIFY lineSeriesChanged) + public: enum Roles { RoleTimestamp, @@ -56,6 +59,9 @@ public: QDateTime endTime() const; void setEndTime(const QDateTime &endTime); + QtCharts::QLineSeries *lineSeries() const; + void setLineSeries(QtCharts::QLineSeries *lineSeries); + protected: virtual void fetchMore(const QModelIndex &parent = QModelIndex()) override; virtual bool canFetchMore(const QModelIndex &parent = QModelIndex()) const override; @@ -69,6 +75,7 @@ signals: void startTimeChanged(); void endTimeChanged(); void engineChanged(); + void lineSeriesChanged(); private slots: void newLogEntryReceived(const QVariantMap &data); @@ -89,6 +96,8 @@ private: int m_blockSize = 100; bool m_canFetchMore = true; + QtCharts::QLineSeries *m_lineSeries = nullptr; + QList > m_fetchedPeriods; }; diff --git a/nymea-app/main.cpp b/nymea-app/main.cpp index b317e307..b6561961 100644 --- a/nymea-app/main.cpp +++ b/nymea-app/main.cpp @@ -18,7 +18,7 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#include +#include #include #include #include @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - QGuiApplication application(argc, argv); + QApplication application(argc, argv); application.setApplicationName("nymea-app"); application.setOrganizationName("nymea"); @@ -71,7 +71,7 @@ int main(int argc, char *argv[]) applicationFont.setCapitalization(QFont::MixedCase); applicationFont.setPixelSize(16); applicationFont.setWeight(QFont::Normal); - QGuiApplication::setFont(applicationFont); + QApplication::setFont(applicationFont); QTranslator qtTranslator; qtTranslator.load("qt_" + QLocale::system().name(), diff --git a/nymea-app/nymea-app.pro b/nymea-app/nymea-app.pro index 5f2313bc..75da82e2 100644 --- a/nymea-app/nymea-app.pro +++ b/nymea-app/nymea-app.pro @@ -2,7 +2,7 @@ TEMPLATE=app TARGET=nymea-app include(../config.pri) -QT += network qml quick quickcontrols2 svg websockets bluetooth +QT += network qml quick quickcontrols2 svg websockets bluetooth charts INCLUDEPATH += $$top_srcdir/libnymea-common \ $$top_srcdir/libnymea-app-core diff --git a/nymea-app/ruletemplates/template.json b/nymea-app/ruletemplates/template.json new file mode 100644 index 00000000..b3079327 --- /dev/null +++ b/nymea-app/ruletemplates/template.json @@ -0,0 +1,58 @@ +{ + "templates": [ + { + "interfaceName": "", + "description": "", + "ruleNameTemplate": "%0 ...", + "eventDescriptorTemplates": [ // optional + { + "interfaceName": "", + "interfaceEvent": "", + "selectionId": 0, + "params": [ // optional + { + "name": "", + "value": "", //optional + } + ] + } + ], + "stateEvaluatorTemplate": { + "stateDescriptorTemplate": { + "interfaceName": "", + "interfaceState": "", + "selectionId": 1, + "operator": "ValueOperatorEquals", + "value": true + } + }, + "ruleActionTemplates": [ + { + "interfaceName": "", + "interfaceAction": "", + "selectionId": 2, + "params": [ + { + "name": "", + "value": "" + } + ] + } + ], + "ruleExitActionTemplates": [ + { + "interfaceName": "", + "interfaceAction": "", + "selectionId": 2, + "params": [ + { + "name": "", + "value": "" + } + ] + } + ] + } + ] +} + diff --git a/nymea-app/ui/devicepages/StateLogPage.qml b/nymea-app/ui/devicepages/StateLogPage.qml index 393361f7..0547c8ba 100644 --- a/nymea-app/ui/devicepages/StateLogPage.qml +++ b/nymea-app/ui/devicepages/StateLogPage.qml @@ -4,6 +4,7 @@ import QtQuick.Layouts 1.1 import Nymea 1.0 import "../components" import "../customviews" +import QtCharts 2.2 Page { id: root @@ -43,6 +44,7 @@ Page { deviceId: root.device.id typeIds: [root.stateType.id] live: true + lineSeries: lineSeries1 } ColumnLayout { @@ -58,6 +60,9 @@ Page { TabButton { text: qsTr("Graph") } + TabButton { + text: qsTr("Graph NG") + } } SwipeView { @@ -163,6 +168,63 @@ Page { } } + + Item { + width: swipeView.width + height: swipeView.height + + ChartView { + id: chartView + anchors.fill: parent + ValueAxis { + id: yAxis + min: 0 + max: 50 + } + + ValueAxis { + id: xAxis + min: Math.floor(startTime.getTime() / 1000) + max: Math.floor(endTime.getTime() / 1000) + + property date startTime: { + var date = new Date(); + date.setHours(date.getHours() - 6); + return date; + } + + property date endTime: new Date() + Component.onCompleted: print("creating axis:", startTime, endTime) + } + + LineSeries { + id: lineSeries1 + axisX: xAxis + axisY: yAxis + } + + MouseArea { + anchors.fill: parent + property int lastX: 0 + property int lastY: 0 + onPressed: { + lastX = mouse.x + lastY = mouse.y + } + + onPositionChanged: { + if (lastX !== mouse.x) { + chartView.scrollRight(lastX - mouse.x) + lastX = mouse.x + } + if (lastY !== mouse.y) { + chartView.scrollDown(lastY - mouse.y) + lastY = mouse.y + } + } + } + } + } } } }