Debian Codegraph @

src/GUI/qml/SettingControl.qml

raw
1 // SPDX-FileCopyrightText: 2018 James Turner
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 import QtQuick 2.12
5 import FlightGear.Launcher 1.0
6 import FlightGear 1.0
7
8 Item {
9 id: root
10 property bool advanced: false
11 property string description: ""
12 property string label: ""
13 property var keywords: []
14 property bool enabled: true
15 property string option: ""
16 property string setting: ""
17
18 // define a new visibility property so we can control built-in 'visible' ourselves
19 property bool hidden: false
20
21 implicitWidth: parent.width // which is assumed to be the section
22
23 visible: {
24 // override so advanced items show up in searches
25 if (_launcher.isSearchActive && _launcher.matchesSearch(_launcher.settingsSearchTerm, keywords)) {
26 return true;
27 }
28
29 return !hidden && (!advanced || parent.showAdvanced)
30 }
31
32 readonly property bool __isDefault: (this.value === this.defaultValue);
33
34 Component.onCompleted: {
35 restoreState();
36 }
37
38 Connections {
39 target: _config
40 // only invoke apply if 'option' is set, otherwise we assume
41 // there is specialised apply code
42 enabled: root.option != ""
43 function onCollect() { apply(); }
44 }
45
46 Connections {
47 target: _config
48 function onRestore() { root.restoreState(); }
49 function onSave() { root.saveState(); }
50 }
51
52 Rectangle {
53 // this is the 'search hit highlight effect'
54 anchors.fill: parent
55 radius: Style.margin
56 border.color: "yellow"
57 border.width: 1
58 color: "#ffff7f"
59
60 z: -1
61 visible: _launcher.isSearchActive && _launcher.matchesSearch(_launcher.settingsSearchTerm, keywords)
62 }
63
64 function apply()
65 {
66 console.warn("Implement apply() on " + root.setting)
67 }
68
69 function saveState()
70 {
71 if (root.setting === "")
72 return;
73
74 if (this.hasOwnProperty("value")) {
75 _config.setValueForKey("", root.setting, this.value)
76 } else {
77 console.warn("No value property on " + this);
78 }
79 }
80
81 function restoreState()
82 {
83 if (root.setting === "")
84 return;
85
86 if (!"value" in root) {
87 console.warn("No value property on " + root);
88 return;
89 }
90
91 var defaultValue = ("defaultValue" in root) ? root.defaultValue : undefined;
92 var rawValue = _config.getValueForKey("", root.setting, defaultValue);
93
94 //console.warn("restoring state for " + root.setting + ", got raw value " + rawValue + " with type " + typeof(rawValue))
95 if (rawValue !== undefined) {
96 setValue(rawValue);
97 }
98 }
99
100 function setValue(newValue)
101 {
102 // hook method so controls can override
103 root.value = newValue
104 }
105 }