Debian Codegraph @

src/GUI/qml/OverlayMenu.qml

raw
1 import QtQuick 2.4
2 import QtQuick.Controls 2.2
3 import FlightGear 1.0
4
5 Rectangle {
6 id: root
7
8 property var model: undefined
9 property int currentIndex: 0
10 property string headerText: ""
11 property int maximumPermittedHeight: 450
12 property string displayRole: "display"
13
14 color: Style.panelBackground
15 border.width: 1
16 border.color: Style.minorFrameColor
17 width: flick.width + Style.margin
18 height: flick.height + Style.margin
19 clip: true
20
21 // Overlay control properties
22 property bool dismissOnClickOutside: true
23 property bool alignRightEdge: false
24
25 signal select(var index);
26
27 Component.onCompleted: {
28 computeMenuWidth();
29 }
30
31 y: -OverlayShared.globalOverlay.verticalOverflowFor(height)
32 x: alignRightEdge ? -width : 0
33
34 function close()
35 {
36 OverlayShared.globalOverlay.dismissOverlay()
37 }
38
39 function doSelect(index)
40 {
41 select(index);
42 close();
43 }
44
45 function computeMenuWidth()
46 {
47 var minWidth = 0;
48 for (var i = 0; i < choicesRepeater.count; i++) {
49 minWidth = Math.max(minWidth, choicesRepeater.itemAt(i).implicitWidth);
50 }
51 if (root.haveHeader())
52 minWidth = Math.max(minWidth, header.width);
53 flick.width = minWidth
54 }
55
56 function haveHeader()
57 {
58 return headerText !== "";
59 }
60
61 Flickable {
62 id: flick
63 anchors.centerIn: parent
64 ScrollBar.vertical: ScrollBar {}
65 height: Math.min(root.maximumPermittedHeight, contentHeight);
66 contentHeight: itemsColumn.childrenRect.height
67
68 Column {
69 id: itemsColumn
70 width: flick.width
71 spacing: Style.margin
72
73 PopupChoiceItem {
74 id: header
75 text: root.headerText
76 visible: root.haveHeader()
77 onSelect: root.doSelect(-1);
78 }
79
80 // main item repeater
81 Repeater {
82 id: choicesRepeater
83 model: root.model
84 delegate: PopupChoiceItem {
85 isCurrentIndex: root.currentIndex === model.index
86 onSelect: function select(index) {
87 root.doSelect(index)
88 }
89
90 text: (Array.isArray(model) ? modelData[displayRole] : model[displayRole])
91 // text: model && model.hasOwnProperty(displayRole) ? model[displayRole] // Qml ListModel and QAbstractItemModel
92 // : modelData && modelData.hasOwnProperty(displayRole) ? modelData[displayRole] // QObjectList / QObject
93 // : modelData != undefined ? modelData : "" // Models without role
94 }
95 } // menu item repeater
96 } // of menu contents column
97 }
98 }