diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-07-21 01:44:32 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-07-21 01:55:58 +0200 |
commit | 0d99be449a7410644e4fc744adfda1c7ccc31da3 (patch) | |
tree | eebce6110375270c6f120008cd5f0d3f77bcdc61 /framework/qml/TreeView.qml | |
parent | e683441ee9a9e904b70be313ef19417dac20b4c6 (diff) | |
download | kube-0d99be449a7410644e4fc744adfda1c7ccc31da3.tar.gz kube-0d99be449a7410644e4fc744adfda1c7ccc31da3.zip |
The TreeView needs to be a FocusScope for the focus to work at all.
Diffstat (limited to 'framework/qml/TreeView.qml')
-rw-r--r-- | framework/qml/TreeView.qml | 274 |
1 files changed, 139 insertions, 135 deletions
diff --git a/framework/qml/TreeView.qml b/framework/qml/TreeView.qml index 12fee752..871a21975 100644 --- a/framework/qml/TreeView.qml +++ b/framework/qml/TreeView.qml | |||
@@ -25,180 +25,184 @@ import QtQuick.Layouts 1.1 | |||
25 | import QtQml.Models 2.2 | 25 | import QtQml.Models 2.2 |
26 | 26 | ||
27 | import org.kube.framework 1.0 as Kube | 27 | import org.kube.framework 1.0 as Kube |
28 | 28 | FocusScope { | |
29 | Flickable { | ||
30 | id: root | 29 | id: root |
31 | |||
32 | default property alias __columns: treeView.__columns | 30 | default property alias __columns: treeView.__columns |
33 | property alias model: treeView.model | 31 | property alias model: treeView.model |
34 | property alias currentIndex: treeView.currentIndex | 32 | property alias currentIndex: treeView.currentIndex |
35 | |||
36 | signal dropped(QtObject drop, QtObject model) | 33 | signal dropped(QtObject drop, QtObject model) |
37 | signal activated(var index) | 34 | signal activated(var index) |
38 | 35 | ||
39 | Controls2.ScrollBar.vertical: Controls2.ScrollBar {} | 36 | Flickable { |
40 | clip: true | 37 | id: flickableItem |
41 | contentWidth: root.width | ||
42 | contentHeight: treeView.implicitHeight | ||
43 | Kube.ScrollHelper { | ||
44 | id: scrollHelper | ||
45 | flickable: root | ||
46 | } | ||
47 | 38 | ||
48 | TreeView { | 39 | anchors.fill: parent |
49 | id: treeView | ||
50 | 40 | ||
51 | anchors { | 41 | Controls2.ScrollBar.vertical: Controls2.ScrollBar {} |
52 | left: parent.left | 42 | clip: true |
53 | right: parent.right | 43 | contentWidth: root.width |
44 | contentHeight: treeView.implicitHeight | ||
45 | Kube.ScrollHelper { | ||
46 | id: scrollHelper | ||
47 | flickable: flickableItem | ||
54 | } | 48 | } |
55 | implicitHeight: __listView.contentItem.height | ||
56 | height: __listView.contentItem.height | ||
57 | focus: true | ||
58 | 49 | ||
59 | verticalScrollBarPolicy: Qt.ScrollBarAlwaysOff | 50 | TreeView { |
60 | horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff | 51 | id: treeView |
61 | 52 | ||
62 | Kube.MouseProxy { | 53 | anchors { |
63 | anchors.fill: parent | 54 | left: parent.left |
64 | target: scrollHelper | 55 | right: parent.right |
65 | forwardWheelEvents: true | 56 | } |
66 | } | 57 | implicitHeight: __listView.contentItem.height |
58 | height: __listView.contentItem.height | ||
59 | focus: true | ||
67 | 60 | ||
68 | flickableItem.boundsBehavior: Flickable.StopAtBounds | 61 | verticalScrollBarPolicy: Qt.ScrollBarAlwaysOff |
62 | horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff | ||
69 | 63 | ||
70 | selection: ItemSelectionModel { | 64 | Kube.MouseProxy { |
71 | model: treeView.model | 65 | anchors.fill: parent |
72 | //TODO once we don't loose focus to the next view | 66 | target: scrollHelper |
73 | // onCurrentChanged: { | 67 | forwardWheelEvents: true |
74 | // treeView.activated(selection.currentIndex) | 68 | } |
75 | // } | ||
76 | } | ||
77 | 69 | ||
78 | function selectFirst() | 70 | flickableItem.boundsBehavior: Flickable.StopAtBounds |
79 | { | ||
80 | treeView.selection.setCurrentIndex(model.index(0, 0), ItemSelectionModel.ClearAndSelect) | ||
81 | } | ||
82 | 71 | ||
83 | function selectNext() | 72 | selection: ItemSelectionModel { |
84 | { | 73 | model: treeView.model |
85 | //If we have already expanded children go to them instead | 74 | //TODO once we don't loose focus to the next view |
86 | var childIndex = model.index(0, 0, selection.currentIndex) | 75 | // onCurrentChanged: { |
87 | if (childIndex.valid && treeView.isExpanded(selection.currentIndex)) { | 76 | // treeView.activated(selection.currentIndex) |
88 | treeView.selection.setCurrentIndex(childIndex, ItemSelectionModel.ClearAndSelect) | 77 | // } |
89 | } else { | 78 | //TODO scroll view so the current index is always visible |
90 | //Otherwise just advance to the next index, if we can | ||
91 | var nextIndex = model.sibling(selection.currentIndex.row + 1, 0, selection.currentIndex) | ||
92 | if (nextIndex.valid) { | ||
93 | treeView.selection.setCurrentIndex(nextIndex, ItemSelectionModel.ClearAndSelect) | ||
94 | } else { | ||
95 | //Try to go to the next of the parent instead TODO do this recursively | ||
96 | var parentIndex = model.parent(selection.currentIndex) | ||
97 | if (parentIndex.valid) { | ||
98 | var parentNext = model.sibling(parentIndex.row + 1, 0, parentIndex) | ||
99 | treeView.selection.setCurrentIndex(parentNext, ItemSelectionModel.ClearAndSelect) | ||
100 | } | ||
101 | } | ||
102 | } | 79 | } |
103 | } | ||
104 | 80 | ||
105 | function selectPrevious() | 81 | function selectFirst() |
106 | { | 82 | { |
107 | var previousIndex = model.sibling(selection.currentIndex.row - 1, 0, selection.currentIndex) | 83 | treeView.selection.setCurrentIndex(model.index(0, 0), ItemSelectionModel.ClearAndSelect) |
108 | if (previousIndex.valid) { | ||
109 | treeView.selection.setCurrentIndex(previousIndex, ItemSelectionModel.ClearAndSelect) | ||
110 | //TODO if the previous index is expanded, go to the last visible child instead (recursively) | ||
111 | } else { | ||
112 | var parentIndex = model.parent(selection.currentIndex) | ||
113 | if (parentIndex.valid) { | ||
114 | treeView.selection.setCurrentIndex(parentIndex, ItemSelectionModel.ClearAndSelect) | ||
115 | } | ||
116 | } | 84 | } |
117 | } | ||
118 | 85 | ||
119 | onActiveFocusChanged: { | 86 | function selectNext() |
120 | //Set an initially focused item when the list view receives focus | 87 | { |
121 | if (activeFocus && !selection.hasSelection) { | 88 | //If we have already expanded children go to them instead |
122 | selectFirst() | 89 | var childIndex = model.index(0, 0, selection.currentIndex) |
90 | if (childIndex.valid && treeView.isExpanded(selection.currentIndex)) { | ||
91 | treeView.selection.setCurrentIndex(childIndex, ItemSelectionModel.ClearAndSelect) | ||
92 | } else { | ||
93 | //Otherwise just advance to the next index, if we can | ||
94 | var nextIndex = model.sibling(selection.currentIndex.row + 1, 0, selection.currentIndex) | ||
95 | if (nextIndex.valid) { | ||
96 | treeView.selection.setCurrentIndex(nextIndex, ItemSelectionModel.ClearAndSelect) | ||
97 | } else { | ||
98 | //Try to go to the next of the parent instead TODO do this recursively | ||
99 | var parentIndex = model.parent(selection.currentIndex) | ||
100 | if (parentIndex.valid) { | ||
101 | var parentNext = model.sibling(parentIndex.row + 1, 0, parentIndex) | ||
102 | treeView.selection.setCurrentIndex(parentNext, ItemSelectionModel.ClearAndSelect) | ||
103 | } | ||
104 | } | ||
105 | } | ||
123 | } | 106 | } |
124 | } | ||
125 | 107 | ||
126 | Keys.onDownPressed: { | 108 | function selectPrevious() |
127 | if (!selection.hasSelection) { | 109 | { |
128 | selectFirst() | 110 | var previousIndex = model.sibling(selection.currentIndex.row - 1, 0, selection.currentIndex) |
129 | } else { | 111 | if (previousIndex.valid) { |
130 | selectNext(); | 112 | treeView.selection.setCurrentIndex(previousIndex, ItemSelectionModel.ClearAndSelect) |
113 | //TODO if the previous index is expanded, go to the last visible child instead (recursively) | ||
114 | } else { | ||
115 | var parentIndex = model.parent(selection.currentIndex) | ||
116 | if (parentIndex.valid) { | ||
117 | treeView.selection.setCurrentIndex(parentIndex, ItemSelectionModel.ClearAndSelect) | ||
118 | } | ||
119 | } | ||
131 | } | 120 | } |
132 | } | ||
133 | |||
134 | Keys.onUpPressed: selectPrevious() | ||
135 | Keys.onReturnPressed: treeView.activated(selection.currentIndex) | ||
136 | Keys.onRightPressed: treeView.expand(selection.currentIndex) | ||
137 | Keys.onLeftPressed: treeView.collapse(selection.currentIndex) | ||
138 | |||
139 | //Forward the signal because on a desktopsystem activated is only triggerd by double clicks | ||
140 | onClicked: treeView.activated(index) | ||
141 | |||
142 | onActivated: root.activated(index) | ||
143 | 121 | ||
144 | alternatingRowColors: false | 122 | onActiveFocusChanged: { |
145 | headerVisible: false | 123 | //Set an initially focused item when the list view receives focus |
146 | 124 | if (activeFocus && !selection.hasSelection) { | |
147 | style: TreeViewStyle { | 125 | selectFirst() |
148 | 126 | } | |
149 | rowDelegate: Rectangle { | ||
150 | color: styleData.selected ? Kube.Colors.highlightColor : Kube.Colors.textColor | ||
151 | height: Kube.Units.gridUnit * 1.5 | ||
152 | width: parent.width | ||
153 | } | 127 | } |
154 | 128 | ||
155 | frame: Rectangle { | 129 | Keys.onDownPressed: { |
156 | color: Kube.Colors.textColor | 130 | if (!selection.hasSelection) { |
131 | selectFirst() | ||
132 | } else { | ||
133 | selectNext(); | ||
134 | } | ||
157 | } | 135 | } |
158 | 136 | ||
159 | branchDelegate: Item { | 137 | Keys.onUpPressed: selectPrevious() |
160 | width: 16 | 138 | Keys.onReturnPressed: treeView.activated(selection.currentIndex) |
161 | height: 16 | 139 | Keys.onRightPressed: treeView.expand(selection.currentIndex) |
162 | 140 | Keys.onLeftPressed: treeView.collapse(selection.currentIndex) | |
163 | Kube.Label { | 141 | |
164 | anchors.centerIn: parent | 142 | //Forward the signal because on a desktopsystem activated is only triggerd by double clicks |
165 | 143 | onClicked: treeView.activated(index) | |
166 | color: Kube.Colors.viewBackgroundColor | 144 | |
167 | text: styleData.isExpanded ? "-" : "+" | 145 | onActivated: root.activated(index) |
146 | |||
147 | alternatingRowColors: false | ||
148 | headerVisible: false | ||
149 | |||
150 | //TODO instead of highlighting the current selection: underline the current selection, and highlight the last activated index, so it corresponds | ||
151 | //to what we have in the maillist view. | ||
152 | //* underline: activefocus | ||
153 | //* glow: hover | ||
154 | //* highlight: selected | ||
155 | style: TreeViewStyle { | ||
156 | rowDelegate: Rectangle { | ||
157 | color: styleData.selected ? Kube.Colors.highlightColor : Kube.Colors.textColor | ||
158 | height: Kube.Units.gridUnit * 1.5 | ||
159 | width: parent.width | ||
168 | } | 160 | } |
169 | 161 | ||
170 | //radius: styleData.isExpanded ? 0 : 100 | 162 | frame: Rectangle { |
171 | } | 163 | color: Kube.Colors.textColor |
172 | 164 | } | |
173 | itemDelegate: Item { | ||
174 | 165 | ||
175 | DropArea { | 166 | branchDelegate: Item { |
176 | anchors.fill: parent | 167 | width: 16 |
168 | height: 16 | ||
169 | Kube.Label { | ||
170 | anchors.centerIn: parent | ||
177 | 171 | ||
178 | Rectangle { | ||
179 | anchors.fill: parent | ||
180 | color: Kube.Colors.viewBackgroundColor | 172 | color: Kube.Colors.viewBackgroundColor |
181 | opacity: 0.3 | 173 | text: styleData.isExpanded ? "-" : "+" |
182 | visible: parent.containsDrag | ||
183 | } | 174 | } |
184 | onDropped: root.dropped(drop, model) | 175 | //radius: styleData.isExpanded ? 0 : 100 |
185 | } | 176 | } |
186 | 177 | ||
187 | Kube.Label { | 178 | itemDelegate: Item { |
188 | anchors { | 179 | DropArea { |
189 | verticalCenter: parent.verticalCenter | 180 | anchors.fill: parent |
190 | left: parent.left | 181 | Rectangle { |
191 | right: parent.right | 182 | anchors.fill: parent |
183 | color: Kube.Colors.viewBackgroundColor | ||
184 | opacity: 0.3 | ||
185 | visible: parent.containsDrag | ||
186 | } | ||
187 | onDropped: root.dropped(drop, model) | ||
188 | } | ||
189 | |||
190 | Kube.Label { | ||
191 | anchors { | ||
192 | verticalCenter: parent.verticalCenter | ||
193 | left: parent.left | ||
194 | right: parent.right | ||
195 | } | ||
196 | text: styleData.value | ||
197 | elide: Qt.ElideRight | ||
198 | font.underline: treeView.activeFocus && styleData.selected | ||
199 | color: Kube.Colors.viewBackgroundColor | ||
192 | } | 200 | } |
193 | text: styleData.value | ||
194 | elide: Qt.ElideRight | ||
195 | font.underline: treeView.activeFocus | ||
196 | color: Kube.Colors.viewBackgroundColor | ||
197 | } | 201 | } |
198 | } | ||
199 | 202 | ||
200 | backgroundColor: Kube.Colors.textColor | 203 | backgroundColor: Kube.Colors.textColor |
201 | highlightedTextColor: Kube.Colors.highlightedTextColor | 204 | highlightedTextColor: Kube.Colors.highlightedTextColor |
205 | } | ||
202 | } | 206 | } |
203 | } | 207 | } |
204 | } | 208 | } |