Debian Codegraph @

src/qml/SensorGraph.qml

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 import QtQuick 2.15
5 import QtQuick.Controls 2.15
6 import QtQuick.Controls.Universal 2.12
7 import QtQuick.Layouts 1.15
8 import QtCharts 2.15
9 import "Style.js" as Style
10
11 Rectangle {
12 id: graph
13 objectName: "SensorGraph"
14
15 implicitWidth: parent.width
16 implicitHeight: 210
17
18 color: Style.Graph.background
19
20 function addItem(item) {
21 p.addGrapItem(item)
22 }
23
24 function ignoredSensors(sensors) {
25 p.refreshIgnored(sensors)
26 }
27
28 QtObject { // private stuff
29 id: p
30
31 property var itemsArray: []
32
33 function refreshIgnored(sensors) {
34 for (var i = 0; i < itemsArray.length; ++i) {
35 itemsArray[i].ignored = sensors.some(function(item) {
36 return itemsArray[i].name === item
37 })
38 }
39 }
40
41 function updateItems() {
42 for (var i = 0; i < itemsArray.length; ++i)
43 itemsArray[i].update()
44 }
45
46 function activateGraphItem(index, active) {
47 itemsArray[index].active = active
48 controlsModel.set(index, { "_active": active })
49 }
50
51 function addGrapItem(item) {
52 itemsArray.push(item)
53 configureItem(item)
54 addItemControl(item)
55 }
56
57 function configureItem(item) {
58 var xAxis = axis.createObject(chart)
59 var yAxis = axis.createObject(chart)
60 var series = chart.createSeries(ChartView.SeriesTypeLine,
61 qsTranslate("SensorGraph", item.name),
62 xAxis, yAxis)
63 item.configure(series, xAxis, yAxis)
64 }
65
66 function addItemControl(item) {
67 var itemIndex = controlsModel.count
68
69 item.valueChanged.connect(function (value) {
70 controlsModel.set(itemIndex, { "_value": value })
71 })
72
73 item.activeChanged.connect(function (active) {
74 controlsModel.set(itemIndex, { "_active": active })
75 })
76
77 item.colorChanged.connect(function (color) {
78 controlsModel.set(itemIndex, { "_color": color })
79 })
80
81 item.ignoredChanged.connect(function (ignored) {
82 controlsModel.set(itemIndex, { "_ignored": ignored})
83 })
84
85 item.yAxisRangeChanged.connect(function (min, max) {
86 for (var i = 0; i < itemsArray.length; ++i)
87 if (itemsArray[i] !== item && itemsArray[i].unit === item.unit)
88 itemsArray[i].updateYAxisRange(min, max)
89 })
90
91 controlsModel.append({ "_index": itemIndex,
92 "_label": qsTranslate("SensorGraph", item.name),
93 "_name": item.name,
94 "_value": item.value,
95 "_unit": item.unit,
96 "_color": item.color,
97 "_active": true,
98 "_ignored": false })
99 }
100 }
101
102 Component {
103 id: axis
104 ValueAxis { visible: false }
105 }
106
107 Component {
108 id: controlsModelDelegate
109
110 RowLayout {
111 property var index: _index
112 spacing: 0
113 enabled: !_ignored
114
115 CheckBox {
116 checked: _active
117
118 leftPadding: -5
119 rightPadding: 0
120 topPadding: -3
121 bottomPadding: -3
122
123 scale: 0.75
124 Universal.accent: _color
125
126 onToggled: p.activateGraphItem(index, checked)
127 }
128
129 Label {
130 text: _label + ": "
131 font.pointSize: Style.g_text.size - 1
132 Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
133 }
134
135 Label {
136 text: _ignored ? qsTr("n/a") : _value
137 font.pointSize: Style.g_text.size - 1
138 Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
139 }
140
141 Label {
142 text: " " + _unit
143 visible: !_ignored
144 font.pointSize: Style.g_text.size - 1
145 Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
146 }
147 }
148 }
149
150 ListModel {
151 id: controlsModel
152 }
153
154 Connections {
155 target: settings
156
157 function onSettingChanged(key, value) {
158 if (key === "UI/splitview-sensorgraph-controls-width") {
159 controls.SplitView.preferredWidth = value
160 }
161 }
162 }
163
164 CSplitView {
165 anchors.fill: parent
166 orientation: Qt.Horizontal
167
168 onResizingChanged: {
169 if (!resizing) {
170 settings.setValue("UI/splitview-sensorgraph-controls-width",
171 controls.SplitView.preferredWidth)
172 }
173 }
174
175 Rectangle {
176 id: controls
177 implicitWidth: 220
178 color: Style.Graph.ctl_background
179
180 ScrollView {
181 id: ctlScrollView
182 anchors.fill: parent
183
184 leftPadding: 8
185 rightPadding: 8
186 topPadding: 10
187 bottomPadding: 10
188
189 clip: true
190 ScrollBar.horizontal.policy: ScrollBar.AlwaysOn
191 ScrollBar.vertical.policy: ScrollBar.AlwaysOn
192 ScrollBar.horizontal.visible: ScrollBar.horizontal.size < 1
193 ScrollBar.vertical.visible: ScrollBar.vertical.size < 1
194
195 ColumnLayout {
196 id: columnLayout
197
198 Repeater {
199 model: controlsModel
200 delegate: controlsModelDelegate
201 }
202 }
203 }
204 }
205
206 ChartView {
207 id: chart
208 SplitView.fillWidth: true
209
210 antialiasing: true
211 legend.visible: false
212 backgroundColor: "#00000000"
213
214 margins.left: 0
215 margins.right: 0
216
217 Timer {
218 interval: 1000
219 running: true
220 repeat: true
221 onTriggered: p.updateItems()
222 }
223 }
224 }
225 }