QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VehicleSummaryMasonry.qml
Go to the documentation of this file.
1import QtQuick
2
3/// Lays the delegates out in as many columns as fit, left to right, placing each one in the column that is currently
4/// shortest so a row never leaves a gap under a shorter item. Delegates that are not visible are skipped.
5Item {
6 id: control
7
8 property alias model: repeater.model
9 property alias delegate: repeater.delegate
10
11 property real minColumnWidth: 100
12 property real spacing: 0
13
14 readonly property int columnCount: Math.max(1, Math.floor((width + spacing) / (minColumnWidth + spacing)))
15
16 onWidthChanged: _scheduleLayout()
17 onColumnCountChanged: _scheduleLayout()
18 onSpacingChanged: _scheduleLayout()
19
20 function _scheduleLayout() {
21 layoutTimer.restart()
22 }
23
24 function _layout() {
25 const columnWidth = (width - spacing * (columnCount - 1)) / columnCount
26 const columnBottoms = new Array(columnCount).fill(0)
27 for (let i = 0; i < repeater.count; i++) {
28 const item = repeater.itemAt(i)
29 if (!item || !item.visible) {
30 continue
31 }
32 let column = 0
33 for (let c = 1; c < columnCount; c++) {
34 if (columnBottoms[c] < columnBottoms[column]) {
35 column = c
36 }
37 }
38 item.width = columnWidth
39 item.x = column * (columnWidth + spacing)
40 item.y = columnBottoms[column]
41 columnBottoms[column] += item.height + spacing
42 }
43 implicitHeight = Math.max(0, Math.max(...columnBottoms) - spacing)
44 }
45
46 Timer {
47 id: layoutTimer
48 interval: 0
49 onTriggered: control._layout()
50 }
51
52 Repeater {
53 id: repeater
54
55 onItemAdded: (index, item) => {
56 item.heightChanged.connect(control._scheduleLayout)
57 item.visibleChanged.connect(control._scheduleLayout)
58 control._scheduleLayout()
59 }
60 onItemRemoved: (index, item) => {
61 item.heightChanged.disconnect(control._scheduleLayout)
62 item.visibleChanged.disconnect(control._scheduleLayout)
63 control._scheduleLayout()
64 }
65 }
66
67 // The items are destroyed after this item, and must not call back into it
68 Component.onDestruction: {
69 for (let i = 0; i < repeater.count; i++) {
70 const item = repeater.itemAt(i)
71 if (item) {
72 item.heightChanged.disconnect(control._scheduleLayout)
73 item.visibleChanged.disconnect(control._scheduleLayout)
74 }
75 }
76 }
77}