-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQObjectTreeView.qml
More file actions
414 lines (330 loc) · 13.8 KB
/
QObjectTreeView.qml
File metadata and controls
414 lines (330 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
pragma ComponentBehavior: Bound
pragma ValueTypeBehavior: Addressable
import QtQuick as QtQ
import QtQuick.Controls as QtC
import QtQuick.Layouts as QtL
import QObjectTreeModel as QObjectTreeModel
QtQ.TreeView {
id: treeView
width: parent.width / 2
height: parent.height
alternatingRows: true
property int _counter: 0
readonly property int _insertDropAreaDefaultHeight: 15
required property QObjectTreeModel.QObjectTreeModel qobjectTreeModel
model: this.qobjectTreeModel
// The below "Add child" button will add an instance of this component as a
// child to the associated QObject.
QtQ.Component {
id: childComponent
QtQ.QtObject {}
}
QtQ.Connections {
target: treeView.model
function onRowsInserted(parent, first, last) {
// Must defer this via Qt.callLater due to this bug:
// https://qt-project.atlassian.net/browse/QTBUG-144391
Qt.callLater(() => {
treeView.expandRecursively()
})
}
function onRowsMoved(sourceParent, sourceFirst, sourceLast, destinationParent, destinationRow) {
Qt.callLater(() => {
treeView.expandRecursively()
})
}
}
QtC.ScrollBar.vertical: QtC.ScrollBar {
orientation: Qt.Vertical
}
delegate: QtQ.Rectangle {
id: treeDelegate
required property var model
required property int index
implicitWidth: Math.max(dragTarget.implicitWidth, this.treeView.width)
implicitHeight: dragTarget.height
readonly property real indent: 20
// Assigned to by TreeView:
required property QtQ.TreeView treeView
required property bool isTreeNode
required property bool expanded
required property int hasChildren
required property int depth
required property int row
required property int column
required property string objectName
required property string metaTypeName
function getObject() : QtQ.QtObject {
const model = treeView.qobjectTreeModel
const modelIndex = treeView.index(treeDelegate.row, treeDelegate.column)
if (!modelIndex || !modelIndex.valid) {
return null
}
return model.objectFromIndex(modelIndex)
}
color: treeDelegate.model.row % 2 == 0 || !treeDelegate.treeView.alternatingRows ? "#E2E2E2" : "#F0F0F0"
QtQ.Rectangle {
id: dragTarget
readonly property int row: treeDelegate.row
property QtQ.QtObject object
implicitWidth: content.implicitWidth
width: treeDelegate.width
height: content.height
opacity: dragArea.drag.active ? 0.7 : 1
color: treeDelegate.color
QtQ.Drag.active: dragArea.drag.active
QtQ.Drag.source: dragTarget
QtQ.Drag.hotSpot.x: dragArea.mouseX
QtQ.Drag.hotSpot.y: dragArea.mouseY
QtQ.MouseArea {
id: dragArea
anchors.fill: parent
drag.target: dragTarget
drag.axis: QtQ.Drag.YAxis
onPressed: mouse => {
dragTarget.object = treeDelegate.getObject()
}
onReleased: mouse => {
dragTarget.QtQ.Drag.drop()
dragTarget.y = 0
}
}
QtL.RowLayout {
id: content
width: dragTarget.width
QtC.Label {
QtL.Layout.leftMargin: (treeDelegate.depth * treeDelegate.indent) + 16
color: "black"
text: "objectName: " + treeDelegate.objectName + " (" + treeDelegate.metaTypeName + ")"
font.bold: true
}
QtC.Button {
id: expandButton
text: expandButton.checked ? "▼" : "▲"
QtL.Layout.maximumWidth: 20
QtL.Layout.maximumHeight: 20
checkable: true
checked: treeDelegate.expanded
onToggled: treeDelegate.treeView.toggleExpanded(
treeDelegate.row)
visible: treeDelegate.hasChildren
}
QtQ.Item {
QtL.Layout.fillWidth: true
QtL.Layout.minimumHeight: expandButton.height
}
QtC.Button {
text: "Update name"
// Update the objects objectName to check display values update.
onClicked: {
const object = treeDelegate.getObject()
if (!object) {
return
}
object.objectName = "child " + treeView._counter++
}
}
QtC.Button {
text: "Add child"
// Add a new child.
onClicked: {
const object = treeDelegate.getObject()
if (!object) {
return
}
let child = childComponent.createObject(object, {})
child.objectName = "child " + treeView._counter++
}
}
QtC.Button {
text: "Delete"
// Delete the current object.
onClicked: {
const model = treeView.qobjectTreeModel
const modelIndex = treeView.index(treeDelegate.row, treeDelegate.column)
model.removeRows(modelIndex.row, 1, modelIndex.parent)
}
}
QtC.Button {
text: "Debug"
// Print debug info.
onClicked: {
const object = treeDelegate.getObject()
if (!object) {
return
}
console.log()
console.log(
"//////////////////// Object Info /////////////////")
console.log("index:", treeDelegate.index)
console.log("row:", treeDelegate.row)
console.log("column:", treeDelegate.column)
const modelIndex = treeDelegate.treeView.model.indexOf(object)
console.log("modelIndex:", modelIndex)
const qObjectTreeModel = treeView.model as QObjectTreeModel.QObjectTreeModel
if (!qObjectTreeModel) {
console.warn("model is not a QObjectTreeModel")
return
}
console.log("dumpObjectInfo:")
qObjectTreeModel.dumpObjectInfo(object)
console.log("dumpObjectTree:")
qObjectTreeModel.dumpObjectTree(object)
}
}
}
}
QtQ.DropArea {
id: dropArea
anchors.fill: parent
// Prevent drags from dropping onto itself
enabled: !dragArea.drag.active
property bool validDrag: false
onEntered: drag => {
const source = drag.source
if (!source || !source.object) {
dropArea.validDrag = false
return
}
const delegateObject = treeDelegate.getObject()
// Reject if the dragged object is an ancestor of (or is) the
// drop target — reparenting would create a circular hierarchy.
dropArea.validDrag = delegateObject
&& !treeView.qobjectTreeModel.IsAncestorOf(source.object, delegateObject)
}
onExited: {
dropArea.validDrag = false
}
onDropped: drop => {
drop.action = Qt.MoveAction
dropArea.validDrag = false
const delegateObject = treeDelegate.getObject()
if (!delegateObject) {
return
}
const dragTarget = drop.source
const object = dragTarget.object
const model = treeView.qobjectTreeModel
const oldParent = model.GetParent(object)
const newParent = delegateObject
if (oldParent !== newParent && newParent !== object
&& newParent
&& !treeView.qobjectTreeModel.IsAncestorOf(object, newParent)) {
model.SetParent(object, newParent)
}
}
QtQ.Rectangle {
anchors.fill: parent
opacity: dropArea.validDrag ? 0.5 : 0
color: dropArea.validDrag ? "#FFFF0000" : "#00FF0000"
}
}
QtQ.DropArea {
id: insertBeforeDropArea
height: treeView._insertDropAreaDefaultHeight
width: parent.width
z: 2
// Cannot move before a root object
enabled: treeDelegate.depth > 0
property bool validDrag: false
onEntered: drag => {
const source = drag.source
if (!source || !source.object) {
insertBeforeDropArea.validDrag = false
return
}
const delegateObject = treeDelegate.getObject()
const model = treeView.qobjectTreeModel
const newParent = delegateObject ? model.GetParent(delegateObject) : null
insertBeforeDropArea.validDrag = newParent
&& !treeView.qobjectTreeModel.IsAncestorOf(source.object, newParent)
}
onExited: {
insertBeforeDropArea.validDrag = false
}
onDropped: drop => {
drop.action = Qt.MoveAction
insertBeforeDropArea.validDrag = false
const delegateObject = treeDelegate.getObject()
if (!delegateObject) {
return
}
const model = treeView.qobjectTreeModel
const dragTarget = drop.source
const object = dragTarget.object
const oldParent = model.GetParent(object)
const newParent = model.GetParent(delegateObject)
let insertIndex = model.indexOf(delegateObject).row
if (oldParent === newParent) {
const currentIndex = model.indexOf(object).row
if (currentIndex < insertIndex) {
insertIndex--
}
}
if (newParent !== object && newParent
&& !treeView.qobjectTreeModel.IsAncestorOf(object, newParent)) {
model.SetParent(object, newParent, insertIndex)
}
}
QtQ.Rectangle {
anchors.fill: parent
opacity: insertBeforeDropArea.validDrag ? 0.5 : 0
color: insertBeforeDropArea.validDrag ? "#FF00FF00" : "#0000FF00"
}
}
QtQ.DropArea {
id: insertAfterDropArea
y: parent.height - insertAfterDropArea.height
height: treeView._insertDropAreaDefaultHeight
width: parent.width
z: 2
// Cannot move after a root object
enabled: treeDelegate.depth > 0
property bool validDrag: false
onEntered: drag => {
const source = drag.source
if (!source || !source.object) {
insertAfterDropArea.validDrag = false
return
}
const delegateObject = treeDelegate.getObject()
const model = treeView.qobjectTreeModel
const newParent = delegateObject ? model.GetParent(delegateObject) : null
insertAfterDropArea.validDrag = newParent
&& !treeView.qobjectTreeModel.IsAncestorOf(source.object, newParent)
}
onExited: {
insertAfterDropArea.validDrag = false
}
onDropped: drop => {
drop.action = Qt.MoveAction
insertAfterDropArea.validDrag = false
const delegateObject = treeDelegate.getObject()
if (!delegateObject) {
return
}
const model = treeView.qobjectTreeModel
const dragTarget = drop.source
const object = dragTarget.object
const oldParent = model.GetParent(object)
const newParent = model.GetParent(delegateObject)
let insertIndex = model.indexOf(delegateObject).row + 1
if (oldParent === newParent) {
const currentIndex = model.indexOf(object).row
if (currentIndex < insertIndex) {
insertIndex--
}
}
if (newParent !== object && newParent
&& !treeView.qobjectTreeModel.IsAncestorOf(object, newParent)) {
model.SetParent(object, newParent, insertIndex)
}
}
QtQ.Rectangle {
anchors.fill: parent
opacity: insertAfterDropArea.validDrag ? 0.5 : 0
color: insertAfterDropArea.validDrag ? "#FF0000FF" : "#000000FF"
}
}
}
}