| 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.Layouts 1.15 |
| 7 |
import "Style.js" as Style |
| 8 |
import "Settings.js" as Settings |
| 9 |
|
| 10 |
ColumnLayout { |
| 11 |
|
| 12 |
signal settingsChanged() |
| 13 |
|
| 14 |
function opened() { |
| 15 |
if (sensorComponentsModel.count < 1) |
| 16 |
p.initialize() |
| 17 |
else |
| 18 |
p.refreshState() |
| 19 |
} |
| 20 |
|
| 21 |
function accepted() { |
| 22 |
p.updateSettings() |
| 23 |
} |
| 24 |
|
| 25 |
Component.onCompleted: p.readSettings() |
| 26 |
|
| 27 |
QtObject { |
| 28 |
id: p |
| 29 |
|
| 30 |
property var ignoredSensors: [] |
| 31 |
|
| 32 |
function sensorIsIgnored(sensor) { |
| 33 |
return ignoredSensors.some(function(element) { |
| 34 |
return element === sensor |
| 35 |
}) |
| 36 |
} |
| 37 |
|
| 38 |
function initialize() { |
| 39 |
sensorComponentsModel.clear() |
| 40 |
|
| 41 |
for (var component in Settings.ComponentData.components) { |
| 42 |
var element = sensorComponentsModelElement.createObject() |
| 43 |
element.id = component |
| 44 |
element.text = Settings.ComponentData.components[component] |
| 45 |
sensorComponentsModel.append(element) |
| 46 |
|
| 47 |
var sensorsModelElement = sensorsModel.createObject() |
| 48 |
sensorComponentsModel.sensorsModels.push(sensorsModelElement) |
| 49 |
|
| 50 |
var componentIdx = sensorComponentsModel.count - 1 |
| 51 |
var sensors = Settings.ComponentData.sensors[component] |
| 52 |
for (var i = 0; i < sensors.length; ++i) { |
| 53 |
sensorsModelElement.append({"_componentIdx": componentIdx, |
| 54 |
"_index": i, |
| 55 |
"_id": sensors[i].id, |
| 56 |
"_label": sensors[i].label, |
| 57 |
"_active": !sensorIsIgnored(component + "/" + sensors[i].id)}) |
| 58 |
} |
| 59 |
|
| 60 |
var sensorListViewElement = sensorListView.createObject(sensorViews) |
| 61 |
sensorListViewElement.parent = sensorViews |
| 62 |
sensorListViewElement.model = sensorsModelElement |
| 63 |
sensorListViewElement.visible = false |
| 64 |
sensorViews.views.push(sensorListViewElement) |
| 65 |
} |
| 66 |
|
| 67 |
if (sensorComponentsModel.count > 0) { |
| 68 |
cbSensorsComponents.currentIndex = 0 |
| 69 |
cbSensorsComponents.updateWidth() |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
function refreshState() { |
| 74 |
sensorComponentsModel.sensorsModels.forEach(function(sensorModel) { |
| 75 |
for (var i = 0; i < sensorModel.count; ++i) { |
| 76 |
var sensor = sensorModel.get(i) |
| 77 |
var sensorName = sensorComponentsModel.get(sensor._componentIdx).id + "/" + sensor._id |
| 78 |
sensorModel.set(i, {"_active": !sensorIsIgnored(sensorName)}) |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
|
| 83 |
function readSettings() { |
| 84 |
ignoredSensors = settings.getStringList("Workarounds/ignoredSensors", []) |
| 85 |
} |
| 86 |
|
| 87 |
function updateSettings() { |
| 88 |
var newIgnoredSensors = [] |
| 89 |
|
| 90 |
sensorComponentsModel.sensorsModels.forEach(function(sensorModel) { |
| 91 |
for (var i = 0; i < sensorModel.count; ++i) { |
| 92 |
var sensor = sensorModel.get(i) |
| 93 |
if (!sensor._active) { |
| 94 |
var sensorName = sensorComponentsModel.get(sensor._componentIdx).id + "/" + sensor._id |
| 95 |
newIgnoredSensors.push(sensorName) |
| 96 |
} |
| 97 |
} |
| 98 |
}) |
| 99 |
|
| 100 |
if (!(ignoredSensors.length === newIgnoredSensors.length && |
| 101 |
ignoredSensors.every(function(element, index) { |
| 102 |
return element === newIgnoredSensors[index]; |
| 103 |
}))) { |
| 104 |
|
| 105 |
ignoredSensors = newIgnoredSensors |
| 106 |
settings.setStringList("Workarounds/ignoredSensors", ignoredSensors) |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
CGroupBox { |
| 112 |
title: qsTr("Sensors") |
| 113 |
showLine: false |
| 114 |
|
| 115 |
Layout.fillWidth: true |
| 116 |
Layout.fillHeight: true |
| 117 |
padding: 0 |
| 118 |
|
| 119 |
ColumnLayout { |
| 120 |
anchors.fill: parent |
| 121 |
spacing: 10 |
| 122 |
|
| 123 |
TextArea { |
| 124 |
text: qsTr("Disabled sensors won't be updated from hardware") |
| 125 |
font.pointSize: Style.g_text.size - 1 |
| 126 |
readOnly: true |
| 127 |
activeFocusOnPress: false |
| 128 |
wrapMode: TextEdit.WordWrap |
| 129 |
horizontalAlignment: TextEdit.AlignHCenter |
| 130 |
width: 280 |
| 131 |
} |
| 132 |
|
| 133 |
RowLayout { |
| 134 |
Label { text: qsTr("Device") } |
| 135 |
|
| 136 |
CComboBox { |
| 137 |
id: cbSensorsComponents |
| 138 |
model: sensorComponentsModel |
| 139 |
|
| 140 |
hoverEnabled: Style.g_hover |
| 141 |
|
| 142 |
property int lastIndex: -1 |
| 143 |
onCurrentIndexChanged: { |
| 144 |
if (lastIndex !== -1) |
| 145 |
sensorViews.views[lastIndex].visible = false |
| 146 |
|
| 147 |
if (currentIndex !== -1) |
| 148 |
sensorViews.views[currentIndex].visible = true |
| 149 |
|
| 150 |
lastIndex = currentIndex |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
Rectangle { |
| 156 |
id: sensorViews |
| 157 |
property var views: [] |
| 158 |
|
| 159 |
Layout.fillWidth: true |
| 160 |
height: 120 |
| 161 |
color: Style.Dialog.list_color |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
ListModel { |
| 167 |
id: sensorComponentsModel |
| 168 |
|
| 169 |
property var sensorsModels: [] |
| 170 |
} |
| 171 |
|
| 172 |
Component { |
| 173 |
id: sensorComponentsModelElement |
| 174 |
|
| 175 |
ListElement { |
| 176 |
property string id |
| 177 |
property string text |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
Component { |
| 182 |
id: sensorsModel |
| 183 |
|
| 184 |
ListModel {} |
| 185 |
} |
| 186 |
|
| 187 |
Component { |
| 188 |
id: sensorsModelDelegate |
| 189 |
|
| 190 |
RowLayout { |
| 191 |
CheckBox { |
| 192 |
checked: _active |
| 193 |
hoverEnabled: Style.g_hover |
| 194 |
|
| 195 |
leftPadding: 0 |
| 196 |
rightPadding: 0 |
| 197 |
topPadding: 1 |
| 198 |
bottomPadding: 1 |
| 199 |
|
| 200 |
onToggled: { |
| 201 |
if (sensorComponentsModel.sensorsModels[_componentIdx] !== undefined) { |
| 202 |
sensorComponentsModel.sensorsModels[_componentIdx].set(_index, {"_active": checked}) |
| 203 |
settingsChanged() |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
Label { |
| 209 |
text: _label |
| 210 |
Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
Component { |
| 216 |
id: sensorListView |
| 217 |
|
| 218 |
ListView { |
| 219 |
delegate: sensorsModelDelegate |
| 220 |
|
| 221 |
anchors.fill: parent |
| 222 |
clip: true |
| 223 |
|
| 224 |
ScrollBar.vertical: ScrollBar { |
| 225 |
policy: ScrollBar.AlwaysOn |
| 226 |
visible: size < 1 |
| 227 |
hoverEnabled: Style.g_hover |
| 228 |
} |
| 229 |
|
| 230 |
ScrollBar.horizontal: ScrollBar { |
| 231 |
policy: ScrollBar.AlwaysOn |
| 232 |
visible: size < 1 |
| 233 |
hoverEnabled: Style.g_hover |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |