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
|
import QtQuick 2.0
Flickable {
id: display
contentWidth: parent.width - (anchors.margins*2)
contentHeight: contentItem.childrenRect.height
// List model that feeds the view
property var model
property int lastModelItemIndex: 0
property int lastColumnUsed: -1
property bool isLoading: false
property bool isLoaded: true
// Component to be used to display each item
property Component delegate
// Number of columns to use
property int columns: 1
property var columnItems: Array()
// Vertical and horizontal spacing between delegate components
property int rowSpacing: 0
property int colSpacing: 0
// Attempt to balance column sizes by adding items to the shortest
// column, rather than the next one in order
property bool balanced: false
property var columnHeights: Array()
function clear() {
isLoaded = false
isLoading = true
var existingColumns = columnItems.length
for (var i = 0; i < existingColumns; i++) {
if (columnItems[i].children == null)
continue;
var childCount = columnItems[i].children.length
for (var j = columnItems[i].children.length-1; j >= 0; j--) {
//console.log('i='+i+', j='+j)
columnItems[i].children[j].destroy()
delete columnItems[i].children[j];
}
columnHeights[i] = 0;
}
isLoading = false
isLoaded = true
}
Row {
width: parent.width
spacing: rowSpacing
Repeater {
model: columns
onModelChanged: {
console.log('Number of columns changed to: '+columns);
update();
loadItems();
}
width: parent.width / columns
Column {
spacing: colSpacing
Component.onCompleted: {
//console.log("Created Column: "+index)
columnItems[index] = this;
columnHeights[index] = 0;
}
}
}
}
Component {
id: hiddenDelegate
Rectangle {
id: hiddenDelegatePlaceholder
}
}
Component {
id: delegateItemLoader
Loader {
property variant model
property int index
property int fixedHeight
width: (display.contentItem.width - (rowSpacing * (columns-1))) / columns
opacity: ((y+height) >= display.contentY) && (y <= (display.contentY + display.height)) ? 1 : 0
onOpacityChanged: {
if (this.hasOwnProperty('item') && this.item != null) {
//console.log('Loader item opacity changed to: '+opacity)
if (opacity > 0.0) {
this.item.visible = true;
this.sourceComponent = delegate;
} else {
this.height = this.item.height
this.item.visible = false;
this.sourceComponent = hiddenDelegate;
}
}
}
}
}
Connections {
target: model
onCountChanged: {
// TODO: Be smarter about items being removed
if (model.count < lastModelItemIndex) {
lastModelItemIndex = 0;
clear();
} else {
loadFrom(lastModelItemIndex);
}
}
}
function getNextColumn() {
if (balanced) {
var shortestColumn = -1
var shortestColumnHeight = -1
for (var colId = columns-1; colId >= 0; colId--) {
if (shortestColumnHeight == -1 || columnHeights[colId] < shortestColumnHeight) {
shortestColumn = colId;
shortestColumnHeight = columnHeights[colId];
}
}
return shortestColumn;
} else {
if (lastColumnUsed >= columns-1) {
lastColumnUsed = 0;
} else {
lastColumnUsed++;
}
}
return lastColumnUsed;
}
function loadFrom(start) {
if (isLoaded == false) {
//console.log('Initial load hasn\'t finished, aborting')
return
}
if (isLoading == true) {
//console.log('Another load is in progress, aborting')
return
}
isLoading = true
//console.log('Loading more items')
lastModelItemIndex = model.count
for (var i = start; i < model.count; i++) {
//console.log("Post: "+i);
var modelObject = model.get(i);
var properties = {
'sourceComponent': delegate,
'model': modelObject,
'index': i,
}
var colIndex = getNextColumn();
var delegateItem = delegateItemLoader.createObject(columnItems[colIndex], properties)
//console.log('New item height: '+delegateItem.height)
columnHeights[colIndex] += delegateItem.height + rowSpacing
}
isLoading = false
}
function loadItems() {
if (isLoading) {
//console.log('Another load is in progress, aborting')
return;
}
isLoading = true
//console.log('Loading items from model')
if (model === null) {
//console.log("No model defined, nothing to load")
return;
}
for (var i = 0; i < model.count; i++) {
lastModelItemIndex = i
var modelObject = model.get(i);
var properties = {
'sourceComponent': delegate,
'model': modelObject,
'index': i,
}
var colIndex = getNextColumn();
var delegateItem = delegateItemLoader.createObject(columnItems[colIndex], properties)
//console.log('New item height: '+(delegateItem.height + rowSpacing))
columnHeights[colIndex] += 0 + delegateItem.height + rowSpacing
}
isLoading = false;
}
}
|