QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
PX4SerialPortsComponent.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Layouts
4
5import QGroundControl
6import QGroundControl.Controls
7import QGroundControl.FactControls
8
9SetupPage {
10 id: serialPortsPage
11 pageComponent: pageComponent
12
13 property real _margins: ScreenTools.defaultFontPixelHeight * 0.5
14 property real _cellPadding: ScreenTools.defaultFontPixelWidth * 1.5
15 property bool _showInUseOnly: false
16
17 readonly property int _rowsPerPort: 3 // separator, port settings, conflict warning
18 // Reverting or rebooting before earlier writes finish could undo the revert or apply only part of a change
19 readonly property bool _pendingWrites: controller.vehicle ? controller.vehicle.parameterManager.pendingWrites : false
20 readonly property int _shownPortCount: {
21 let count = 0
22 for (let i = 0; i < controller.ports.count; i++) {
23 if (_portShown(controller.ports.get(i))) {
24 count++
25 }
26 }
27 return count
28 }
29
30 // shared: several functions' options are shown together, so name the function instance
31 function _optionLabel(paramName, shared) {
32 const labels = {
33 "MODE": qsTr("Mode"),
34 "RATE": qsTr("Send rate"),
35 "FORWARD": qsTr("Forwarding"),
36 "FLOW_CTRL": qsTr("Flow control"),
37 "UDP_PRT": qsTr("UDP port"),
38 "REMOTE_PRT": qsTr("Remote port"),
39 "BROADCAST": qsTr("Broadcast"),
40 "PROTOCOL": qsTr("Protocol")
41 }
42 const suffix = paramName.replace(/^[A-Z]+_\d_/, "")
43 const label = labels[suffix] !== undefined ? labels[suffix] : paramName
44 const instance = paramName.match(/^[A-Z]+_(\d)_/)
45 return (shared && instance) ? qsTr("%1 (instance %2)").arg(label).arg(instance[1]) : label
46 }
47
48 // Ethernet is the only shared port. PX4 has no baud rate there, so a rate of 0 is a fixed value.
49 function _optionNote(paramName, ethernet) {
50 const notes = {
51 "RATE": ethernet ? qsTr("0 uses 100000 bytes/s.") : qsTr("0 uses half the link bandwidth.")
52 }
53 const note = notes[paramName.replace(/^[A-Z]+_\d_/, "")]
54 return note !== undefined ? note : ""
55 }
56
57 // A port whose function was just removed stays shown until the change is reverted or applied
58 function _portShown(port) {
59 return !_showInUseOnly || port.assignments.length > 0 || port.rebootRequired
60 }
61
62 PX4SerialPortsController {
63 id: controller
64 }
65
66 QGCPalette {
67 id: qgcPal
68 }
69
70 Component {
71 id: pageComponent
72
73 ColumnLayout {
74 width: Math.max(availableWidth, implicitWidth)
75 spacing: _margins
76
77 Rectangle {
78 Layout.fillWidth: true
79 implicitHeight: rebootRow.implicitHeight + _margins * 2
80 visible: controller.changedPortCount > 0
81 color: "transparent"
82 border.color: qgcPal.warningText
83 radius: ScreenTools.defaultFontPixelHeight / 4
84
85 RowLayout {
86 id: rebootRow
87 anchors.margins: _margins
88 anchors.fill: parent
89 spacing: _margins
90
91 QGCLabel {
92 Layout.fillWidth: true
93 wrapMode: Text.WordWrap
94 color: qgcPal.warningText
95 text: controller.changedPortCount === 1 ?
96 qsTr("1 port changed. Changes take effect after the vehicle reboots.") :
97 qsTr("%1 ports changed. Changes take effect after the vehicle reboots.").arg(controller.changedPortCount)
98 }
99
100 QGCButton {
101 text: qsTr("Revert Changes")
102 enabled: !_pendingWrites
103 onClicked: controller.revertChanges()
104 }
105
106 QGCButton {
107 text: qsTr("Reboot Vehicle")
108 primary: true
109 enabled: controller.vehicle && !controller.vehicle.armed && !_pendingWrites
110 onClicked: controller.vehicle.rebootVehicle()
111 }
112 }
113 }
114
115 Rectangle {
116 Layout.fillWidth: true
117 implicitWidth: panelLayout.implicitWidth + _margins * 2
118 implicitHeight: panelLayout.implicitHeight + _margins * 2
119 color: qgcPal.windowPanel
120 border.color: qgcPal.borderSubtle
121 radius: ScreenTools.defaultBorderRadius * 2
122
123 ColumnLayout {
124 id: panelLayout
125 anchors.fill: parent
126 anchors.margins: _margins
127 spacing: _margins
128
129 RowLayout {
130 Layout.fillWidth: true
131 spacing: _margins
132
133 QGCRadioButtonBar {
134 objectName: "ports_filter"
135 model: [ qsTr("All Ports"), qsTr("In Use") ]
136 currentIndex: _showInUseOnly ? 1 : 0
137 onActivated: (index) => { _showInUseOnly = index === 1 }
138 }
139
140 QGCLabel {
141 objectName: "ports_summary"
142 Layout.fillWidth: true
143 color: qgcPal.textSecondary
144 text: _showInUseOnly ?
145 qsTr("Showing %1 of %2 ports.").arg(_shownPortCount).arg(controller.ports.count) :
146 qsTr("Showing all %1 ports.").arg(controller.ports.count)
147 }
148 }
149
150 GridLayout {
151 Layout.fillWidth: true
152 columns: 4
153 columnSpacing: _margins
154 rowSpacing: _margins * 0.75
155
156 Repeater {
157 model: controller.ports
158
159 Rectangle {
160 Layout.row: index * _rowsPerPort
161 Layout.column: 0
162 Layout.columnSpan: 4
163 Layout.fillWidth: true
164 implicitHeight: 1
165 color: qgcPal.borderSubtle
166 visible: _portShown(object)
167
168 required property int index
169 required property var object
170 }
171 }
172
173 Repeater {
174 model: controller.ports
175
176 ColumnLayout {
177 objectName: "ports_port_" + object.portId
178 Layout.row: 1 + index * _rowsPerPort
179 Layout.column: 0
180 Layout.alignment: Qt.AlignTop
181 Layout.topMargin: _cellPadding
182 spacing: ScreenTools.defaultFontPixelHeight * 0.25
183 visible: _portShown(object)
184
185 required property int index
186 required property var object
187
188 readonly property bool _inUse: object.assignments.length > 0
189 readonly property bool _attention: object.rebootRequired || object.conflictText !== ""
190
191 RowLayout {
192 spacing: ScreenTools.defaultFontPixelWidth
193
194 Rectangle {
195 implicitWidth: ScreenTools.defaultFontPixelHeight * 0.5
196 implicitHeight: implicitWidth
197 radius: implicitWidth / 2
198 color: _attention ? qgcPal.warningText : (_inUse ? qgcPal.colorGreen : "transparent")
199 border.color: (_attention || _inUse) ? color : qgcPal.textSecondary
200 }
201
202 QGCLabel {
203 text: object.label
204 font.bold: true
205 font.capitalization: Font.AllUppercase
206 font.letterSpacing: ScreenTools.defaultFontPixelWidth * 0.1
207 }
208 }
209
210 Repeater {
211 model: object.assignments
212
213 QGCLabel {
214 text: modelData
215 font.family: ScreenTools.fixedFontFamily
216 font.pointSize: ScreenTools.smallFontPointSize
217 color: qgcPal.textSecondary
218
219 required property string modelData
220 }
221 }
222
223 QGCLabel {
224 text: qsTr("No function assigned")
225 font.pointSize: ScreenTools.smallFontPointSize
226 color: qgcPal.textSecondary
227 visible: !_inUse
228 }
229
230 RowLayout {
231 spacing: ScreenTools.defaultFontPixelWidth / 2
232 visible: object.shared || object.rebootRequired
233
234 QGCChip {
235 text: qsTr("Shared")
236 visible: object.shared
237 }
238
239 QGCChip {
240 text: qsTr("Reboot required")
241 textColor: qgcPal.warningText
242 visible: object.rebootRequired
243 }
244 }
245 }
246 }
247
248 Repeater {
249 model: controller.ports
250
251 QGCCaptionCell {
252 id: functionCell
253 Layout.row: 1 + index * _rowsPerPort
254 Layout.column: 1
255 Layout.alignment: Qt.AlignTop
256 Layout.fillWidth: true
257 Layout.horizontalStretchFactor: 0
258 caption: object.shared ? qsTr("Functions") : qsTr("Function")
259 visible: _portShown(object)
260
261 required property int index
262 required property var object
263
264 QGCComboBox {
265 id: functionCombo
266 Layout.fillWidth: true
267 model: functionCell.object.functionLabels
268 currentIndex: functionCell.object.functionIndex
269 sizeToContents: true
270 visible: !functionCell.object.shared
271
272 onActivated: (comboIndex) => {
273 controller.setPortFunction(functionCell.object.portId, comboIndex)
274 currentIndex = Qt.binding(function() { return functionCell.object.functionIndex })
275 }
276
277 // A model change resets the index, so restore the binding once the row has updated
278 onModelChanged: Qt.callLater(function() {
279 currentIndex = Qt.binding(function() { return functionCell.object.functionIndex })
280 })
281 }
282
283 // Shown instead of the combo on ports several functions can use at once
284 Repeater {
285 // Skip "None", which is index 0
286 model: functionCell.object.shared ? functionCell.object.functionLabels.length - 1 : 0
287
288 QGCCheckBoxSlider {
289 Layout.fillWidth: true
290 text: functionCell.object.functionLabels[functionIndex]
291 checked: functionCell.object.functionEnabled[functionIndex] === true
292
293 required property int index
294 readonly property int functionIndex: index + 1
295
296 onClicked: {
297 controller.setPortFunctionEnabled(functionCell.object.portId, functionIndex, checked)
298 checked = Qt.binding(function() { return functionCell.object.functionEnabled[functionIndex] === true })
299 }
300 }
301 }
302
303 QGCLabel {
304 Layout.fillWidth: true
305 Layout.preferredWidth: functionCombo.implicitWidth
306 wrapMode: Text.WordWrap
307 font.pointSize: ScreenTools.smallFontPointSize
308 color: qgcPal.textSecondary
309 text: qsTr("%1 always runs on this port unless it is assigned to another port.")
310 .arg(functionCell.object.functionLabels[functionCell.object.functionIndex])
311 visible: functionCell.object.defaultFunctionOnPort && functionCell.object.conflictText === ""
312 }
313 }
314 }
315
316 Repeater {
317 model: controller.ports
318
319 QGCCaptionCell {
320 id: baudCell
321 Layout.row: 1 + index * _rowsPerPort
322 Layout.column: 2
323 Layout.alignment: Qt.AlignTop
324 Layout.fillWidth: true
325 Layout.horizontalStretchFactor: 0
326 caption: qsTr("Baud")
327 visible: _portShown(object)
328
329 required property int index
330 required property var object
331
332 FactComboBox {
333 Layout.fillWidth: true
334 fact: baudCell.object.baudFact
335 indexModel: false
336 sizeToContents: true
337 visible: baudCell.object.baudFact
338 }
339
340 QGCLabel {
341 text: baudCell.object.baudLoading ? qsTr("Loading...") : qsTr("N/A")
342 color: qgcPal.textSecondary
343 visible: !baudCell.object.baudFact
344 }
345 }
346 }
347
348 // Only the Options column takes spare width. The other columns size to their widest cell.
349 // Options wrap instead of widening the page, so the column only needs its widest single cell.
350 Repeater {
351 model: controller.ports
352
353 Flow {
354 id: optionsFlow
355 Layout.row: 1 + index * _rowsPerPort
356 Layout.column: 3
357 Layout.alignment: Qt.AlignTop
358 Layout.fillWidth: true
359 Layout.horizontalStretchFactor: 1
360 Layout.minimumWidth: _widestChildWidth
361 Layout.preferredWidth: _widestChildWidth
362 spacing: _margins
363 visible: _portShown(object)
364
365 required property int index
366 required property var object
367
368 // The message wraps, so only option cells set the column's minimum width
369 readonly property real _widestChildWidth: {
370 let widest = optionsMessage.visible ? ScreenTools.defaultFontPixelWidth * 20 : 0
371 for (let i = 0; i < children.length; i++) {
372 if (children[i].visible && children[i] !== optionsMessage) {
373 widest = Math.max(widest, children[i].implicitWidth)
374 }
375 }
376 return widest
377 }
378
379 Repeater {
380 model: optionsFlow.object.optionFacts
381
382 QGCCaptionCell {
383 caption: _optionLabel(modelData.name, optionsFlow.object.shared)
384
385 required property var modelData
386
387 FactComboBox {
388 Layout.fillWidth: true
389 fact: modelData
390 indexModel: false
391 sizeToContents: true
392 visible: modelData.enumStrings.length > 0
393 }
394
395 FactTextField {
396 Layout.preferredWidth: ScreenTools.defaultFontPixelWidth * 12
397 fact: modelData
398 visible: modelData.enumStrings.length === 0
399 }
400
401 QGCLabel {
402 Layout.fillWidth: true
403 Layout.preferredWidth: Math.min(implicitWidth, ScreenTools.defaultFontPixelWidth * 20)
404 wrapMode: Text.WordWrap
405 font.pointSize: ScreenTools.smallFontPointSize
406 color: qgcPal.textSecondary
407 text: _optionNote(modelData.name, optionsFlow.object.shared)
408 visible: text !== ""
409 }
410 }
411 }
412
413 QGCLabel {
414 id: optionsMessage
415 width: Math.min(implicitWidth, optionsFlow.width)
416 topPadding: _cellPadding
417 wrapMode: Text.WordWrap
418 color: qgcPal.textSecondary
419 text: optionsFlow.object.optionsLoading ? qsTr("Loading...") :
420 (optionsFlow.object.conflictText !== "" ?
421 qsTr("Options are hidden while more than one function is assigned.") :
422 qsTr("No options for this port."))
423 visible: optionsFlow.object.optionsLoading || optionsFlow.object.optionFacts.length === 0
424 }
425 }
426 }
427
428 Repeater {
429 model: controller.ports
430
431 Rectangle {
432 id: conflictBox
433 Layout.row: 2 + index * _rowsPerPort
434 Layout.column: 0
435 Layout.columnSpan: 4
436 Layout.fillWidth: true
437 implicitHeight: conflictLabel.implicitHeight + _margins * 2
438 color: "transparent"
439 border.color: qgcPal.warningText
440 radius: ScreenTools.defaultBorderRadius * 2
441 visible: object.conflictText !== "" && _portShown(object)
442
443 required property int index
444 required property var object
445
446 QGCLabel {
447 id: conflictLabel
448 anchors.left: parent.left
449 anchors.right: parent.right
450 anchors.margins: _cellPadding
451 anchors.verticalCenter: parent.verticalCenter
452 wrapMode: Text.WordWrap
453 color: qgcPal.warningText
454 text: conflictBox.object.conflictText
455 }
456 }
457 }
458 }
459 }
460 }
461 }
462 }
463
464 // Opening on the ports in use keeps the page short on boards with many ports
465 Component.onCompleted: {
466 for (let i = 0; i < controller.ports.count; i++) {
467 if (controller.ports.get(i).assignments.length > 0) {
468 _showInUseOnly = true
469 return
470 }
471 }
472 }
473}