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