Kirigami-addons

FormPasswordFieldDelegate.qml
1/*
2 * Copyright 2022 Carl Schwan <carl@carlschwan.eu>
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6import QtQuick 2.15
7import QtQuick.Controls 2.15
8import QtQuick.Layouts 1.15
9
10import org.kde.kirigami 2.19 as Kirigami
11
12/**
13 * @brief A Form delegate that corresponds to a password field.
14 *
15 * ```qml
16 * FormCard.FormHeader {
17 * title: "Information"
18 * }
19 *
20 * FormCard.FormCard {
21 * FormCard.FormTextFieldDelegate {
22 * label: "Account name"
23 * }
24 *
25 * FormCard.FormPasswordFieldDelegate {
26 * label: "Password"
27 * statusMessage: "Password incorrect"
28 * status: Kirigami.MessageType.Error
29 * text: "666666666"
30 * }
31 *
32 * FormCard.FormPasswordFieldDelegate {
33 * label: "Password repeat"
34 * statusMessage: "Password match"
35 * text: "4242424242"
36 * status: Kirigami.MessageType.Positive
37 * echoMode: TextInput.Password
38 * }
39 * }
40 * ```
41 *
42 * @since KirigamiAddons 0.11.0
43 *
44 * @inherit AbstractFormDelegate
45 */
46AbstractFormDelegate {
47 id: root
48
49 /**
50 * @brief A label containing primary text that appears above and
51 * to the left the text field.
52 */
53 required property string label
54
55 /**
56 * @brief set the maximum length of the text inside the TextField if maxLength > 0
57 */
58 property alias maximumLength: textField.maximumLength
59
60 /**
61 * @brief This hold the activeFocus state of the internal TextField.
62 */
63 property alias fieldActiveFocus: textField.activeFocus
64
65 /**
66 * @brief This hold the `readOnly` state of the internal TextField.
67 */
68 property alias readOnly: textField.readOnly
69
70 /**
71 * @brief This property holds the `echoMode` of the internal TextField.
72 *
73 * This consists of how the text inside the text field will be
74 * displayed to the user.
75 *
76 * @see <a href="https://doc.qt.io/qt-6/qml-qtquick-textinput.html#echoMode-prop">TextInput.echoMode</a>
77 */
78 property alias echoMode: textField.echoMode
79
80 /**
81 * @brief This property holds the `inputMethodHints` of the
82 * internal TextField.
83 *
84 * This consists of hints on the expected content or behavior of
85 * the text field, be it sensitive data, in a date format, or whether
86 * the characters will be hidden, for example.
87 *
88 * @see <a href="https://doc.qt.io/qt-6/qml-qtquick-textinput.html#inputMethodHints-prop">TextInput.inputMethodHints</a>
89 */
90 property alias inputMethodHints: textField.inputMethodHints
91
92 /**
93 * @brief This property holds the `placeholderText` of the
94 * internal TextField.
95 *
96 * This consists of secondary text shown by default on the text field
97 * if no text has been written in it.
98 */
99 property alias placeholderText: textField.placeholderText
100
101 /**
102 * @brief This property holds the `validator` of the internal TextField.
103 */
104 property alias validator: textField.validator
105
106 /**
107 * @brief This property holds the `acceptableInput` of the internal TextField.
108 */
109 property alias acceptableInput: textField.acceptableInput
110
111 /**
112 * @brief This property holds the current status message type of
113 * the text field.
114 *
115 * This consists of an inline message with a colorful background
116 * and an appropriate icon.
117 *
118 * The status property will affect the color of ::statusMessage used.
119 *
120 * Accepted values:
121 * - `Kirigami.MessageType.Information` (blue color)
122 * - `Kirigami.MessageType.Positive` (green color)
123 * - `Kirigami.MessageType.Warning` (orange color)
124 * - `Kirigami.MessageType.Error` (red color)
126 * default: `Kirigami.MessageType.Information` if ::statusMessage is set,
127 * nothing otherwise.
128 *
129 * @see Kirigami.MessageType
130 */
131 property var status: Kirigami.MessageType.Information
132
133 /**
134 * @brief This property holds the current status message of
135 * the text field.
136 *
137 * If this property is not set, no ::status will be shown.
138 */
139 property string statusMessage: ""
140
141 /**
142 * @This signal is emitted when the Return or Enter key is pressed.
144 * Note that if there is a validator or inputMask set on the text input,
145 * the signal will only be emitted if the input is in an acceptable
146 * state.
147 */
148 signal accepted();
149
150 /**
151 * @brief This signal is emitted when the Return or Enter key is pressed
152 * or the text input loses focus.
153 *
154 * Note that if there is a validator or inputMask set on the text input
155 * and enter/return is pressed, this signal will only be emitted if
156 * the input follows the inputMask and the validator returns an
157 * acceptable state.
158 */
159 signal editingFinished();
161 /**
162 * @brief This signal is emitted whenever the text is edited.
163 *
164 * Unlike textChanged(), this signal is not emitted when the text
165 * is changed programmatically, for example, by changing the
166 * value of the text property or by calling ::clear().
167 */
168 signal textEdited();
169
170 /**
171 * @brief Clears the contents of the text input and resets partial
172 * text input from an input method.
173 */
174 function clear() {
175 textField.clear();
176 }
177
178 /**
179 * Inserts text into the TextInput at position.
180 */
181 function insert(position: int, text: string) {
182 textField.insert(position, text);
183 }
184
185 onActiveFocusChanged: { // propagate focus to the text field
186 if (activeFocus) {
187 textField.forceActiveFocus();
188 }
189 }
190
191 onClicked: textField.forceActiveFocus()
192 background: null
193 Accessible.role: Accessible.EditableText
194
195 contentItem: ColumnLayout {
196 spacing: Kirigami.Units.smallSpacing
197 RowLayout {
198 spacing: Kirigami.Units.largeSpacing
199 Label {
200 Layout.fillWidth: true
201 text: label
202 elide: Text.ElideRight
203 color: root.enabled ? Kirigami.Theme.textColor : Kirigami.Theme.disabledTextColor
204 wrapMode: Text.Wrap
205 maximumLineCount: 2
206 }
207 Label {
208 TextMetrics {
209 id: metrics
210 text: label(root.maximumLength, root.maximumLength)
211 font: Kirigami.Theme.smallFont
212
213 function label(current: int, maximum: int): string {
214 return i18nc("@label %1 is current text length, %2 is maximum length of text field", "%1/%2", current, maximum)
215 }
216 }
217 // 32767 is the default value for TextField.maximumLength
218 visible: root.maximumLength < 32767
219 text: metrics.label(textField.text.length, root.maximumLength)
220 font: Kirigami.Theme.smallFont
221 color: textField.text.length === root.maximumLength
222 ? Kirigami.Theme.neutralTextColor
223 : Kirigami.Theme.textColor
224 horizontalAlignment: Text.AlignRight
225
226 Layout.margins: Kirigami.Units.smallSpacing
227 Layout.preferredWidth: metrics.advanceWidth
228 }
229 }
230 Kirigami.PasswordField {
231 id: textField
232 Accessible.description: label
233 Layout.fillWidth: true
234 placeholderText: root.placeholderText
235 text: root.text
236 onTextChanged: root.text = text
237 onAccepted: root.accepted()
238 onEditingFinished: root.editingFinished()
239 onTextEdited: root.textEdited()
240 activeFocusOnTab: false
241 }
242
243 Kirigami.InlineMessage {
244 id: formErrorHandler
245 visible: root.statusMessage.length > 0
246 Layout.topMargin: visible ? Kirigami.Units.smallSpacing : 0
247 Layout.fillWidth: true
248 text: root.statusMessage
249 type: root.status
250 }
251 }
252}
253
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:46:57 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.