-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathQMLRearrangeableTreeView.qml
More file actions
187 lines (155 loc) · 5.18 KB
/
QMLRearrangeableTreeView.qml
File metadata and controls
187 lines (155 loc) · 5.18 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
import QtQuick
import QtQuick.Controls
Item {
id: root;
// Our list model defaults to the one specified in QML by default. In the C++ project this
// is overridden by a C++ implementation. Pass null to use the built-in sample data.
property var model: null;
// Resolved model: use the provided model, or fall back to the built-in sample data.
readonly property var activeModel: model || sampleList;
// Number of items at the top of the list that can never be reordered
// or put into folders.
property int numStationary: 0;
// Scale factor for DPI awareness.
property real scaleFactor: 1.0;
// How much to indent folder contents.
property int folderIndent: 25;
// Vertical margin around folder groups.
property int folderMargin: 0;
// Number of rows in the model.
readonly property alias numRows: treeView.count;
// This is used for generating UIDs for folders. This method is simplistic
// and isn't intended for production code.
property int uid: 10;
function uidNext() {
return ++uid;
}
// Inserts a folder at the given index and returns its UID.
// If the model provides insertFolder(), we use that (C++ models).
// Otherwise, we create the folder directly (QML ListModel).
function insertFolder(index) {
if (typeof activeModel.insertFolder === "function") {
return activeModel.insertFolder(index);
}
var uid = uidNext();
console.log("insert folder ", index);
activeModel.insert(index, {
"uid": uid,
"title": "New folder",
"dropTarget":"none",
"isFolder":true,
"parentFolder":-1,
"folderOpen": true,
"draggable": true
});
return uid;
}
ScrollView {
id: scrollView;
anchors.fill: parent;
ListView {
id: treeView;
// Only enable scrolling if there's a need.
interactive: height < childrenRect.height;
delegate: TitleDelegate {
numStationary: root.numStationary;
scaleFactor: root.scaleFactor;
folderIndent: root.folderIndent;
folderMargin: root.folderMargin;
}
model: root.activeModel;
// Perform an animation when the list is rearranged.
displaced: Transition {
NumberAnimation { properties: "x,y"; duration: 50 }
}
}
}
// Default sample data. This also demonstrates the required properties and their data types.
ListModel {
id: sampleList;
ListElement {
title: "All Items";
// Required:
uid: 1; // Unique id (integer)
dropTarget: "none"; // Used for drag and drop UI. (Persistence not required.)
isFolder: false; // True if a folder, else false
parentFolder: -1; // -1 if not in a folder, else the uid of the parent
folderOpen: true; // For folders, this indicates whether their children are
// displayed. Otherwise, indicates if visible.
// Optional:
draggable: false; // Per-item drag control (e.g. for "special" items)
}
ListElement {
title: "two";
uid: 2;
dropTarget: "none";
isFolder: false;
parentFolder: -1;
folderOpen: true;
draggable: true;
}
ListElement {
title: "three";
uid: 3;
dropTarget: "none";
isFolder: false;
parentFolder: -1;
folderOpen: true;
draggable: true;
}
ListElement {
title: "four";
uid: 4;
dropTarget: "none";
isFolder: false;
parentFolder: -1;
folderOpen: true;
draggable: true;
}
ListElement {
title: "five";
uid: 5;
dropTarget: "none";
isFolder: false;
parentFolder: -1;
folderOpen: true;
draggable: true;
}
ListElement {
title: "six";
uid: 6;
dropTarget: "none";
isFolder: false;
parentFolder: -1;
folderOpen: true;
draggable: true;
}
ListElement {
title: "seven";
uid: 7;
dropTarget: "none";
isFolder: false;
parentFolder: -1;
folderOpen: true;
draggable: true;
}
ListElement {
title: "eight";
uid: 8;
dropTarget: "none";
isFolder: false;
parentFolder: -1;
folderOpen: true;
draggable: true;
}
ListElement {
title: "nine";
uid: 9;
dropTarget: "none";
isFolder: false;
parentFolder: -1;
folderOpen: true;
draggable: true;
}
}
}