Debian Codegraph @

src/qml/ModeSelector.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.Layouts 1.15
7 import "Style.js" as Style
8
9 Page {
10 id: modeSelector
11
12 property alias headerTitle: label.text
13 property var headerBackground: Style.ModeSelector.Header.background
14 property alias contentParentObject: contentsAnchor.objectName
15 property var contentBackground: Style.ModeSelector.Body.background
16
17 /// Fired when the selected mode changed.
18 /// @param mode key of the new selected mode
19 signal selectionChanged(string mode)
20
21 /// Fired when a new child is added
22 /// @param added child
23 signal childAdded(var child)
24
25 // Sets the modes of the selector.
26 // @param modes array of elements where each pair of elements
27 // corresponds to [mode_key, mode_text] for each [i, i+1]
28 function setModes(modes) {
29 listModel.clear()
30
31 for (var i = 0; i < modes.length; i+=2) {
32 var element = listElement.createObject()
33 element.mode = modes[i]
34 element.text = modes[i + 1]
35
36 listModel.append(element)
37 }
38
39 cbMode.updateWidth()
40 }
41
42 /// Selects a mode.
43 /// @param mode key of the mode to be selected
44 function select(mode) {
45 if (cbMode.currentMode !== mode)
46 cbMode.setMode(mode)
47 }
48
49 ListModel {
50 id: listModel
51 }
52
53 Component {
54 id: listElement
55
56 ListElement {
57 property string text
58 property string mode
59 }
60 }
61
62 header: Pane {
63 padding: Style.ModeSelector.Header.padding
64
65 RowLayout {
66 Label {
67 id: label
68 rightPadding: 20
69 }
70
71 CComboBox {
72 id: cbMode
73 model: listModel
74
75 property string currentMode: ""
76
77 function setMode(mode) {
78 for (var i = 0; i < listModel.count; i++) {
79 if (listModel.get(i).mode === mode) {
80 var lastMode = currentMode
81 currentMode = mode
82 currentIndex = i
83 contentsAnchor.toggleActive(lastMode, currentMode)
84 break;
85 }
86 }
87 }
88
89 onActivated: {
90 if (currentMode !== model.get(currentIndex).mode) {
91 var lastMode = currentMode
92 currentMode = model.get(currentIndex).mode
93 contentsAnchor.toggleActive(lastMode, currentMode)
94 }
95 }
96 }
97
98 Rectangle {
99 Layout.fillWidth: true
100 }
101 }
102
103 background: Rectangle {
104 anchors.fill: parent
105 color: headerBackground
106 }
107 }
108
109 Pane {
110 padding: 0
111 anchors.fill: parent
112
113 Row {
114 id: contentsAnchor
115 anchors.centerIn: parent
116
117 function toggleActive(from, to) {
118 if (childrenAdded[from] !== undefined &&
119 childrenAdded[to] !== undefined) {
120 childrenAdded[from].activate(false)
121 }
122
123 if (childrenAdded[to] !== undefined) {
124 childrenAdded[to].activate(true)
125 modeSelector.selectionChanged(to)
126 }
127 }
128
129 property var childrenAdded: []
130 onChildrenChanged: {
131 for (var i = 0; i < children.length; ++i) {
132 if (childrenAdded[children[i].objectName] === undefined) {
133 childrenAdded[children[i].objectName] = children[i]
134 modeSelector.childAdded(children[i])
135 }
136 }
137 }
138 }
139
140 background: Rectangle {
141 anchors.fill: parent
142 color: contentBackground
143 }
144 }
145 }