Debian Codegraph @

src/GUI/qml/AirportEntry.qml

raw
1 // SPDX-FileCopyrightText: 2018 James Turner
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 import QtQuick 2.4
5 import FlightGear 1.0
6 import FlightGear.Launcher 1.0
7
8 FocusScope
9 {
10 id: root
11 readonly property Positioned airport: airport
12 implicitWidth: edit.implicitWidth + nameDisplay.width
13 implicitHeight: edit.implicitHeight
14
15 property alias label: edit.label
16
17 signal clickedName();
18
19 Positioned {
20 id: airport
21 }
22
23 function selectAirport(guid)
24 {
25 airport.guid = guid
26 edit.text = airport.ident
27 // we don't want this to trigger a search ....
28 searchTimer.stop();
29 }
30
31 signal pickAirport(var guid);
32
33 NavaidSearch {
34 id: searchCompleter
35 airportsOnly: true
36 maxResults: 20
37
38 onSearchComplete: {
39 if (exactMatch !== 0) {
40 pickAirport(exactMatch)
41 return;
42 }
43 }
44 }
45
46 Timer {
47 id: searchTimer
48 interval: 400
49 onTriggered: {
50 if (edit.text.length >= 2) {
51 searchCompleter.setSearch(edit.text, _launcher.aircraftType)
52 } else {
53 // ensure we update with no search (cancel, effectively)
54 searchCompleter.clear();
55 }
56 }
57 }
58
59
60 LineEdit
61 {
62 id: edit
63 placeholder: "KSFO"
64 suggestedWidthString: "XXXX"
65 commitOnReturn: false
66 focus: true
67
68 onActiveFocusChanged: {
69 if (activeFocus) {
70 OverlayShared.globalOverlay.showOverlayAtItemOffset(overlay, root,
71 Qt.point(xOffsetForEditFrame, root.height + Style.margin),
72 false /* don't offset */);
73 } else {
74 OverlayShared.globalOverlay.dismissOverlay()
75 searchCompleter.clear();
76 if (!airport.valid) {
77 text = ""; // ensure we always contain a valid ICAO or nothing
78 // we don't want this to trigger a search ....
79 searchTimer.stop();
80 }
81 }
82 }
83
84 onTextChanged: {
85 searchTimer.restart();
86 }
87
88 Keys.onReturnPressed: {
89 // start a search right now, if we don't have one active
90 if (!searchCompleter.haveExistingSearch && (edit.text.length >= 2)) {
91 searchCompleter.setSearch(edit.text, _launcher.aircraftType)
92 }
93
94 // if we have a search and at least one valid result, select it
95 // combined with the previous behaviour, this means entering an ICAO
96 // code (which are returned synchronously inside setSearch) and
97 // hitting enter/return works as expected
98 if (searchCompleter.haveExistingSearch && (searchCompleter.numResults > 0)) {
99 root.pickAirport(searchCompleter.guidAtIndex(0));
100 root.focus = false
101 }
102 }
103 }
104
105 ClickableText {
106 id: nameDisplay
107 anchors.left: edit.right
108 anchors.leftMargin: Style.margin
109 anchors.verticalCenter: parent.verticalCenter
110 text: airport.name
111 visible: airport.valid
112 width: Style.strutSize * 3
113 elide: Text.ElideRight
114 onClicked: root.clickedName()
115 }
116
117 Component {
118 id: overlay
119
120 Rectangle {
121 id: selectionPopup
122 color: "white"
123 height: choicesColumn.childrenRect.height + Style.margin * 2
124 width: choicesColumn.width + Style.margin * 2
125 visible: searchCompleter.haveExistingSearch
126
127 Rectangle {
128 border.width: 1
129 border.color: Style.minorFrameColor
130 anchors.fill: parent
131 }
132
133 // choice layout column
134 Column {
135 id: choicesColumn
136 spacing: Style.margin
137 x: Style.margin
138 y: Style.margin
139 width: menuWidth
140
141
142 function calculateMenuWidth()
143 {
144 var minWidth = 0;
145 for (var i = 0; i < choicesRepeater.count; i++) {
146 minWidth = Math.max(minWidth, choicesRepeater.itemAt(i).implicitWidth);
147 }
148 return minWidth;
149 }
150
151 readonly property int menuWidth: calculateMenuWidth()
152
153 // main item repeater
154 Repeater {
155 id: choicesRepeater
156 model: searchCompleter
157 delegate:
158 Text {
159 id: choiceText
160
161 text: model.ident + " - " + model.name
162 height: implicitHeight + Style.margin
163 font.pixelSize: Style.baseFontPixelSize
164
165 readonly property bool isCurrent: (selectionPopup.currentIndex === model.index)
166 color: choiceArea.containsMouse ? Style.themeColor : Style.baseTextColor
167
168 MouseArea {
169 id: choiceArea
170 width: selectionPopup.width // full width of the popup
171 height: parent.height
172 hoverEnabled: true
173
174 onClicked: {
175 root.pickAirport(model.guid)
176 root.focus = false
177 }
178 }
179 } // of Text delegate
180 } // text repeater
181 } // text column
182 }
183 } // of overlay component
184 }