summaryrefslogtreecommitdiff
path: root/components/custom/Splitter.qml
blob: a2c0d0986d724332cd6437571c75d9f19abe6f38 (plain)
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
import QtQuick 2.0
import QtDesktop 0.2

Splitter {
    id: root
    default property alias items: splitterItems.children
    property alias handles: splitterHandles.children
    property Component handleBackground: Rectangle { width:3; color: "black" }
    property int handleWidth: -1
    property real preferredSize: 0
    property int orientation: Qt.Horizontal

    clip: true
    Component.onCompleted: d.init();
    onWidthChanged: d.updateLayout();
    onHeightChanged: d.updateLayout();

    QtObject {
        id: d

        property bool horizontal: orientation == Qt.Horizontal
        property string size: horizontal ? "width" : "height"
        property string minimum: horizontal ? "minimumWidth" : "minimumHeight"
        property string maximum: horizontal ? "maximumWidth" : "maximumHeight"

        property string offset: horizontal ? "x" : "y"
        property int expandingIndex: -1
        property bool updateLayoutGuard: true
        property bool itemWidthGuard: false
        property bool itemExpandingGuard: true

        function init()
        {
            for (var i=0; i<items.length; ++i) {
                var item = items[i];

                item.Splitter.itemIndex = i
                // Assign one, and only one, item to be expanding:
                if (item.Splitter.expanding === true) {
                    if (d.expandingIndex === -1 && item.visible === true)
                        d.expandingIndex = i
                    else
                        item.Splitter.expanding = false
                }

                // Anchor each item to fill out all space vertically:
                if (d.horizontal) {
                    item.anchors.top = splitterItems.top
                    item.anchors.bottom = splitterItems.bottom
                } else  {
                    item.anchors.left = splitterItems.left
                    item.anchors.right = splitterItems.right

                }

                // Listen for changes to width and expanding:
                propertyChangeListener.createObject(item, {"itemIndex":i});
                if (i < items.length-1) {
                    // Create a handle for the item, unless its the last:
                    var handle = handleBackgroundLoader.createObject(splitterHandles, {"handleIndex":i});

                    if (d.horizontal) {
                        handle.anchors.top = splitterHandles.top
                        handle.anchors.bottom = splitterHandles.bottom
                    } else {
                        handle.anchors.left = splitterHandles.left
                        handle.anchors.right = splitterHandles.right
                    }
                }
            }

            if (d.expandingIndex === -1) {
                // INVARIANT: No item was set as expanding.
                // We then choose the last visible item instead:
                d.expandingIndex = items.length - 1
                for (i=items.length-1; i>=0; --i) {
                    var item = items[i]
                    if (item.visible === true) {
                        d.expandingIndex = i
                        item = items[i]
                        break
                    }
                }
                item.Splitter.expanding = true
            }

            d.itemExpandingGuard = false
            d.updateLayoutGuard = false
            d.updateLayout()
        }

        function accumulatedSize(firstIndex, lastIndex, includeExpandingMinimum)
        {
            // Go through items and handles, and
            // calculate their acummulated width.
            var w = 0
            for (var i=firstIndex; i<lastIndex; ++i) {
                var item = items[i]
                if (item.visible) {
                    if (i !== d.expandingIndex)
                        w += item[d.size];
                    else if (includeExpandingMinimum && item.Splitter[minimum] != -1)
                        w += item[minimum]
                }

                var handle = handles[i]
                if (handle && items[i + ((d.expandingIndex > i) ? 0 : 1)].visible)
                    w += handle[d.size]
            }
            return w
        }

        function updateLayout()
        {
            // This function will reposition both handles and
            // items according to the _width of the each item_
            if (items.length === 0)
                return;
            if (d.updateLayoutGuard === true)
                return
            d.updateLayoutGuard = true

            // Use a temporary variable to store values to avoid breaking
            // property bindings when the value does not actually change:
            var newValue

            // Ensure all items within min/max:
            for (var i=0; i<items.length; ++i) {
                if (i !== d.expandingIndex) {
                    item = items[i];
                    // If the item is using percentage width, convert
                    // that number to real width now:
                    if (item.Splitter.percentageSize !== -1) {
                        newValue = item.Splitter.percentageSize * (root[d.size] / 100)
                        if (newValue !== item[d.size])
                            item[d.size] = newValue
                    }
                    // Ensure item width is not more than maximumSize:
                    if (item.Splitter[maximum] !== -1) {
                        newValue = Math.min(item[d.size], item.Splitter[maximum])
                        if (newValue !== item[d.size])
                            item[d.size] = newValue
                    }
                    // Ensure item width is not more less minimumWidth:
                    if (item.Splitter[minimum] !== -1) {
                        newValue = Math.max(item[d.size], item.Splitter[minimum])
                        if (newValue !== item[d.size])
                            item[d.size] = newValue
                    }
                }
            }

            // Special case: set width of expanding item to available space:
            newValue = root[d.size] - d.accumulatedSize(0, items.length, false);
            var expandingItem = items[d.expandingIndex]
            var expandingMinimum = 0
            if (expandingItem.Splitter[minimum] !== -1)
                expandingMinimum = expandingItem.Splitter[minimum]
            newValue = Math.max(newValue, expandingMinimum)
            if (expandingItem[d.size] !== 0 && expandingItem.Splitter.percentageSize !== -1)
                expandingItem.Splitter.percentageSize = newValue * (100 / root[d.size])
            if (expandingItem[d.size] !== newValue)
                expandingItem[d.size] = newValue

            // Then, position items and handles according to their width:
            var item, lastVisibleItem
            var handle, lastVisibleHandle
            var newpreferredSize = expandingMinimum - expandingItem[d.size]

            for (i=0; i<items.length; ++i) {
                // Position item to the right of the previous visible handle:
                item = items[i];
                if (item.visible) {
                    if (lastVisibleHandle) {
                        newValue = lastVisibleHandle[d.offset] + lastVisibleHandle[d.size]
                        if (newValue !== item[d.offset])
                            item[d.offset] = newValue
                    } else {
                        newValue = 0
                        if (newValue !== item[d.offset])
                            item[d.offset] = newValue
                    }
                    newpreferredSize += item[d.size]
                    lastVisibleItem = item
                }

                // Position handle to the right of the previous visible item. We use an alterative way of
                // checking handle visibility because that property might not have updated correctly yet:
                handle = handles[i]
                if (handle && items[i + ((d.expandingIndex > i) ? 0 : 1)].visible) {
                    newValue = lastVisibleItem[d.offset] + Math.max(0, lastVisibleItem[d.size])
                    if (newValue !== handle[d.offset])
                        handle[d.offset] = newValue
                    newpreferredSize += handle[d.size]
                    lastVisibleHandle = handle
                }
            }

            root.preferredSize = newpreferredSize
            d.updateLayoutGuard = false
        }
    }

    Component {
        id: handleBackgroundLoader
        Loader {
            id: myHandle
            property int handleIndex: 0
            property Item handle: myHandle
            property Item splitterItem: items[handleIndex + ((d.expandingIndex > handleIndex) ? 0 : 1)]

             // 'splitterRow' should be an alias, but that fails to resolve runtime:
            property Item splitterRow: root
            property Item background: item

            visible: splitterItem.visible
            sourceComponent: handleBackground
            onWidthChanged: d.updateLayout()

            onXChanged: {
                // Moving the handle means resizing an item. Which one,
                // left or right, depends on where the expanding item is.
                // 'updateLayout' will override in case new width violates max/min.
                // And 'updateLayout will be triggered when an item changes width.
                if (d.updateLayoutGuard)
                    return

                var leftHandle, leftItem, rightItem, rightHandle
                var leftEdge, rightEdge, newWidth, leftStopX, rightStopX
                var i

                if (d.expandingIndex > handleIndex) {
                    // Resize item to the left.
                    // Ensure that the handle is not crossing other handles. So
                    // find the first visible handle to the left to determine the left edge:
                    leftEdge = 0
                    for (i=handleIndex-1; i>=0; --i) {
                        leftHandle = handles[i]
                        if (leftHandle.visible) {
                            leftEdge = leftHandle[d.offset] + leftHandle[d.size]
                            break;
                        }
                    }

                    // Ensure: leftStopX >= myHandle[d.offset] >= rightStopX
                    var min = d.accumulatedSize(handleIndex+1, items.length, true)
                    rightStopX = root[d.size] - min - myHandle[d.size]
                    leftStopX = Math.max(leftEdge, myHandle[d.offset])
                    myHandle[d.offset] = Math.min(rightStopX, Math.max(leftStopX, myHandle[d.offset]))

                    newWidth = myHandle[d.offset] - leftEdge
                    leftItem = items[handleIndex]
                    if (root[d.size] != 0 && leftItem.Splitter.percentageSize !== -1)
                        leftItem.Splitter.percentageSize = newWidth * (100 / root[d.size])
                    // The next line will trigger 'updateLayout' inside 'propertyChangeListener':
                    leftItem[d.size] = newWidth
                } else {
                    // Resize item to the right.
                    // Ensure that the handle is not crossing other handles. So
                    // find the first visible handle to the right to determine the right edge:
                    rightEdge = root[d.size]
                    for (i=handleIndex+1; i<handles.length; ++i) {
                        rightHandle = handles[i]
                        if (rightHandle.visible) {
                            rightEdge = rightHandle[d.offset]
                            break;
                        }
                    }

                    // Ensure: leftStopX <= myHandle[d.offset] <= rightStopX
                    var min = d.accumulatedSize(0, handleIndex+1, true)
                    leftStopX = min - myHandle[d.size]
                    rightStopX = Math.min((rightEdge - myHandle[d.size]), myHandle[d.offset])
                    myHandle[d.offset] = Math.max(leftStopX, Math.min(myHandle[d.offset], rightStopX))

                    newWidth = rightEdge - (myHandle[d.offset] + myHandle[d.size])
                    rightItem = items[handleIndex+1]
                    if (root[d.size] !== 0 && rightItem[d.percentageSize] !== -1)
                        rightItem.Splitter.percentageSize = newWidth * (100 / root[d.size])
                    // The next line will trigger 'updateLayout' inside 'propertyChangeListener':
                    rightItem[d.size] = newWidth
                }
            }
        }
    }

    Item {
        id: splitterItems
        anchors.fill: parent
    }
    Item {
        id: splitterHandles
        anchors.fill: parent
    }

    Component {
        // This dummy item becomes a child of all
        // items it the splitter, just to provide a way
        // to listen for changes to their width, expanding etc.
        id: propertyChangeListener
        Item {
            id: target
            width: parent[d.size]
            property bool expanding: parent.Splitter.expanding
            property real percentageSize: parent.Splitter.percentageSize
            property real minimumWidth: parent.Splitter[d.minimum]
            property real maximumSize: parent.Splitter[d.maximum]
            property int itemIndex: parent.Splitter.itemIndex

            onPercentageSizeChanged: d.updateLayout();
            onMinimumWidthChanged: d.updateLayout();
            onMaximumSizeChanged: d.updateLayout();
            onExpandingChanged: updateExpandingIndex()

            function updateExpandingIndex()
            {
                // The following code is needed to avoid a binding
                // loop, since we might change 'expanding' again to a different item:
                if (d.itemExpandingGuard === true)
                    return
                d.itemExpandingGuard = true
                // break binding:
                expanding = false

                // 'expanding' follows radio button behavior:
                // First, find the new expanding item:
                var newIndex = items.length-1
                for (var i=0; i<items.length; ++i) {
                    var item = items[i]
                    if (i !== d.expandingIndex && item.Splitter.expanding === true && item.visible === true) {
                        newIndex = i
                        break
                    }
                }
                item = items[newIndex]
                if (item.visible === false) {
                    // So now we ended up with the last item in the splitter to be
                    // expanding, but it turns out to not be visible. So we need to
                    // traverse backwards again to find one that is visible...
                    for (i=items.length-2; i>=0; --i) {
                        var item = items[i]
                        if (item.visible === true) {
                            newIndex = i
                            item = items[newIndex]
                           break
                        }
                    }
                }

                // Tell the found item that it is expanding:
                if (item.Splitter.expanding !== true)
                    item.Splitter.expanding = true
                // ...and the old one that it is not:
                if (newIndex !== d.expandingIndex) {
                    item = items[d.expandingIndex]
                    if (item.Splitter.expanding !== false)
                        item.Splitter.expanding = false
                }
                // update index:
                d.expandingIndex = newIndex
                d.updateLayout();
                // recreate binding:
                expanding = function() { return parent.Splitter.expanding }
                d.itemExpandingGuard = false
            }

            function handleSizeChanged() {
                // We need to update the layout.
                // The following code is needed to avoid a binding
                // loop, since we might change 'width' again to a different value:
                if (d.itemWidthGuard === true)
                    return
                d.itemWidthGuard = true
                // Break binding:
                this[d.size] = 0

                d.updateLayout()

                // Restablish binding:
                width = function() { return parent[d.size]; }
                d.itemWidthGuard = false
            }

            onWidthChanged:  handleSizeChanged()
            onHeightChanged: handleSizeChanged()
            onVisibleChanged: {
                // Hiding the expanding item forces us to
                // select a new one (and therefore not recommended):
                if (d.expandingIndex === itemIndex) {
                    updateExpandingIndex()
                } else {
                    if (visible) {
                        // Try to keep all items within the SplitterRow. When an item
                        // has been hidden, the expanding item might no longer be large enough
                        // to give away space to the new items width. So we need to resize:
                        var overflow = d.accumulatedSize(0, items.length, true) - root[d.size];
                        if (overflow > 0)
                            parent[d.size] -= overflow
                    }
                    d.updateLayout()
                }
            }
        }
    }
}