QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCTextField.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3import QtQuick.Layouts
4
5import QGroundControl
6import QGroundControl.Controls
7
8TextField {
9 id: control
10 color: qgcPal.textFieldText
11 selectionColor: qgcPal.textFieldText
12 selectedTextColor: qgcPal.textField
13 activeFocusOnPress: true
14 antialiasing: true
15 font.pointSize: ScreenTools.defaultFontPointSize
16 font.family: ScreenTools.normalFontFamily
17 inputMethodHints: numericValuesOnly && !ScreenTools.isiOS ?
18 Qt.ImhFormattedNumbersOnly: // Forces use of virtual numeric keyboard instead of full keyboard
19 Qt.ImhNone // iOS numeric keyboard has no done button, we can't use it.
20 leftPadding: _marginPadding
21 rightPadding: _marginPadding + unitsHelpLayout.width
22 topPadding: _marginPadding
23 bottomPadding: _marginPadding
24 EnterKey.type: Qt.EnterKeyDone
25
26 property bool showUnits: false
27 property bool showHelp: false
28 property string unitsLabel: ""
29 property string extraUnitsLabel: ""
30 property bool numericValuesOnly: false // true: Used as hint for mobile devices to show numeric only keyboard
31 property alias textColor: control.color
32 property bool validationError: false
33
34 property real _helpLayoutWidth: 0
35 property real _marginPadding: ScreenTools.defaultFontPixelHeight / 3
36
37 signal helpClicked
38
39 Component.onCompleted: checkActiveFocus()
40 onActiveFocusChanged: checkActiveFocus()
41
42 QGCPalette { id: qgcPal; colorGroupEnabled: enabled }
43
44 onEditingFinished: {
45 if (ScreenTools.isMobile) {
46 // Toss focus on mobile after Done on virtual keyboard. Prevent strange interactions.
47 focus = false
48 }
49 }
50
51 function checkActiveFocus() {
52 if (activeFocus) {
53 selectAll()
54 if (validationError) {
55 validationToolTip.visible = true
56 }
57 } else {
58 validationToolTip.visible = false
59 }
60 }
61
62 function showValidationError(errorString, originalValidValue = undefined, preventViewSiwtch = true) {
63 validationToolTip.text = errorString
64 validationToolTip.originalValidValue = originalValidValue
65 validationToolTip.visible = true
66 if (!validationError) {
67 validationError = true
68 if (preventViewSiwtch) {
69 globals.validationErrorCount++
70 }
71 }
72 }
73
74 function clearValidationError(preventViewSiwtch = true) {
75 validationToolTip.visible = false
76 validationToolTip.originalValidValue = undefined
77 if (validationError) {
78 validationError = false
79 if (preventViewSiwtch) {
80 globals.validationErrorCount--
81 }
82 }
83 }
84
85 background: Rectangle {
86 border.width: control.validationError ? 2 : (qgcPal.globalTheme === QGCPalette.Light ? 1 : 0)
87 border.color: control.validationError ? qgcPal.colorRed : qgcPal.buttonBorder
88 radius: ScreenTools.defaultBorderRadius
89 color: qgcPal.textField
90 implicitWidth: ScreenTools.implicitTextFieldWidth
91 implicitHeight: ScreenTools.implicitTextFieldHeight
92
93 RowLayout {
94 id: unitsHelpLayout
95 anchors.top: parent.top
96 anchors.bottom: parent.bottom
97 anchors.right: parent.right
98 anchors.rightMargin: control.activeFocus ? 2 : control._marginPadding
99 spacing: ScreenTools.defaultFontPixelWidth / 4
100 layoutDirection: Qt.RightToLeft
101
102 Component.onCompleted: control._helpLayoutWidth = unitsHelpLayout.width
103 onWidthChanged: control._helpLayoutWidth = unitsHelpLayout.width
104
105 // Help button
106 Rectangle {
107 id: helpButton
108 Layout.margins: 2
109 Layout.leftMargin: 0
110 Layout.rightMargin: 1
111 Layout.fillHeight: true
112 Layout.preferredWidth: helpLabel.contentWidth * 3
113 Layout.alignment: Qt.AlignVCenter
114 color: control.color
115 visible: control.showHelp && control.activeFocus
116
117 QGCLabel {
118 id: helpLabel
119 anchors.centerIn: parent
120 color: qgcPal.textField
121 text: qsTr("?")
122 }
123
124 }
125
126 // Extra units
127 Text {
128 Layout.alignment: Qt.AlignVCenter
129 text: control.extraUnitsLabel
130 font.pointSize: ScreenTools.smallFontPointSize
131 font.family: ScreenTools.normalFontFamily
132 antialiasing: true
133 color: control.color
134 visible: control.showUnits && text !== ""
135 }
136
137 // Units
138 Text {
139 Layout.alignment: Qt.AlignVCenter
140 text: control.unitsLabel
141 font.pointSize: control.activeFocus ? ScreenTools.smallFontPointSize : ScreenTools.defaultFontPointSize
142 font.family: ScreenTools.normalFontFamily
143 antialiasing: true
144 color: control.color
145 visible: control.showUnits && text !== ""
146 }
147 }
148 }
149
150 ToolTip {
151 id: validationToolTip
152
153 property var originalValidValue: undefined
154
155 QGCMouseArea {
156 anchors.fill: parent
157 onClicked: {
158 if (validationToolTip.originalValidValue !== undefined) {
159 control.text = validationToolTip.originalValidValue
160 control.clearValidationError()
161 }
162 }
163 }
164 }
165
166 MouseArea {
167 anchors.top: parent.top
168 anchors.bottom: parent.bottom
169 anchors.right: parent.right
170 width: control._helpLayoutWidth
171 enabled: helpButton.visible
172 onClicked: control.helpClicked()
173 }
174}