Plasma-framework

TextField.qml
1/*
2 SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7import QtQuick
8import QtQuick.Controls
9import QtQuick.Templates as T
10import org.kde.ksvg as KSvg
11//NOTE: importing PlasmaCore is necessary in order to make KSvg load the current Plasma Theme
12import org.kde.plasma.core as PlasmaCore
13import org.kde.kirigami as Kirigami
14import "mobiletextselection" as MobileTextSelection
15import "private" as Private
17T.TextField {
18 id: control
19
20 /**
21 * Whether the button to clear the text from TextField is visible.
22 * @since 5.73
23 * @deprecated since 5.93 Use SearchField instead
24 */
25 property bool clearButtonShown: false
26
27 // Can't guarantee that background will always be present or have the margins property
28 readonly property bool __hasBackgroundAndMargins: background && background.hasOwnProperty("margins")
29
30 // store information that echoMode was set to Password, regardless of its current value
31 property bool __isPassword: false
32 onEchoModeChanged: echoMode => {
33 __isPassword |= (echoMode === TextInput.Password);
34 }
35
36 // TextField doesn't have this property by default for whatever reason
37 property bool visualFocus: activeFocus && [
38 Qt.TabFocusReason,
39 Qt.BacktabFocusReason,
40 Qt.ShortcutFocusReason,
41 ].includes(focusReason)
42
43 /* It might be preferable to do background width OR content width if we
44 * want content to stay within the background rather than expanding the
45 * control, but this is maintaining compatibility with the pre-existing
46 * behavior. Use the following 2 lines if you want text to stay within the
47 * background:
48 implicitBackgroundWidth + leftInset + rightInset
49 || Math.ceil(Math.max(contentWidth + leftPadding + rightPadding, placeholder.implicitWidth))
50 */
51 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
52 Math.ceil(Math.max(contentWidth + leftPadding + rightPadding, placeholder.implicitWidth)))
53 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
54 Math.max(contentHeight + topPadding + bottomPadding, placeholder.implicitHeight, __isPassword ? passwordsizeholder.implicitHeight : 0))
55
56 topPadding: __hasBackgroundAndMargins ? background.margins.top : 0
57 leftPadding: (__hasBackgroundAndMargins ? background.margins.left : 0) + (control.effectiveHorizontalAlignment === TextInput.AlignRight ? inlineButtonRow.width : 0)
58 rightPadding: (__hasBackgroundAndMargins ? background.margins.right : 0) + (control.effectiveHorizontalAlignment === TextInput.AlignRight ? 0 : inlineButtonRow.width)
59 bottomPadding: __hasBackgroundAndMargins ? background.margins.bottom : 0
60
61 Kirigami.Theme.inherit: !background || !background.visible
62 Kirigami.Theme.colorSet: Kirigami.Theme.View
63
64 color: Kirigami.Theme.textColor
65 selectionColor: Kirigami.Theme.highlightColor
66 selectedTextColor: Kirigami.Theme.highlightedTextColor
67 placeholderTextColor: Kirigami.Theme.disabledTextColor
68
69 verticalAlignment: TextInput.AlignVCenter
70 // Manually setting this fixes alignment in RTL layouts
71 horizontalAlignment: TextInput.AlignLeft
72 opacity: control.enabled ? 1 : 0.6
73 hoverEnabled: !Kirigami.Settings.tabletMode
74
75 selectByMouse: !Kirigami.Settings.tabletMode
76
77 cursorDelegate: Kirigami.Settings.tabletMode ? mobileCursor : null
78 Component {
79 id: mobileCursor
80 MobileTextSelection.MobileCursor {
81 target: control
82 }
83 }
84 onFocusChanged: {
85 if (focus) {
86 MobileTextSelection.MobileTextActionsToolBar.controlRoot = control;
87 }
88 }
89
90 onTextChanged: MobileTextSelection.MobileTextActionsToolBar.shouldBeVisible = false;
91 onPressed: MobileTextSelection.MobileTextActionsToolBar.shouldBeVisible = true;
92
93 onPressAndHold: event => {
94 if (!Kirigami.Settings.tabletMode) {
95 return;
96 }
97 forceActiveFocus();
98 cursorPosition = positionAt(event.x, event.y);
99 selectWord();
100 }
101 MobileTextSelection.MobileCursor {
102 target: control
103 selectionStartHandle: true
104 property var rect: target.positionToRectangle(target.selectionStart)
105 //FIXME: this magic values seem to be always valid, for every font,every dpi, every scaling
106 x: rect.x + 5
107 y: rect.y + 6
108 }
109
110 Label {
111 id: placeholder
112 enabled: false
113 x: 0
114 y: 0
115 topPadding: control.topPadding
116 bottomPadding: control.bottomPadding
117 leftPadding: control.leftPadding
118 rightPadding: control.rightPadding
119 height: control.height
120 width: control.width
121 font: control.font
122 LayoutMirroring.enabled: false
123 horizontalAlignment: control.effectiveHorizontalAlignment
124 verticalAlignment: control.verticalAlignment
125 elide: Text.ElideRight
126 renderType: control.renderType
127 text: control.placeholderText
128 visible: !control.length && !control.preeditText && (!control.activeFocus || control.horizontalAlignment !== Qt.AlignHCenter)
129 color: control.placeholderTextColor
130 }
131
132 // Object holding the size (implicitHeight) of the password dot character,
133 // so that a password TextField never gets shorter than required to display it
134 Label {
135 id: passwordsizeholder
136 enabled: false
137 visible: false
138 topPadding: control.topPadding
139 bottomPadding: control.bottomPadding
140 leftPadding: control.leftPadding
141 rightPadding: control.rightPadding
142 font: control.font
143 horizontalAlignment: control.horizontalAlignment
144 verticalAlignment: control.verticalAlignment
145 elide: Text.ElideRight
146 renderType: control.renderType
147 text: control.passwordCharacter
148 }
149
150 Row {
151 id: inlineButtonRow
152 anchors.right: control.right
153 anchors.rightMargin: control.__hasBackgroundAndMargins ? background.margins.right : 0
154 anchors.verticalCenter: control.verticalCenter
155 LayoutMirroring.enabled: control.effectiveHorizontalAlignment === TextInput.AlignRight
156
157 Kirigami.Icon {
158 id: clearButton
159 //ltr confusingly refers to the direction of the arrow in the icon, not the text direction which it should be used in
160 source: clearButtonShown ? (control.effectiveHorizontalAlignment === TextInput.AlignRight ? "edit-clear-locationbar-ltr" : "edit-clear-locationbar-rtl") : ""
161 height: Kirigami.Units.iconSizes.small
162 width: height
163 opacity: (control.length > 0 && clearButtonShown && control.enabled) ? 1 : 0
164 visible: opacity > 0
165 Behavior on opacity {
166 enabled: Kirigami.Units.longDuration > 0
167 NumberAnimation {
168 duration: Kirigami.Units.longDuration
169 easing.type: Easing.InOutQuad
170 }
171 }
172 MouseArea {
173 anchors.fill: parent
174 onClicked: {
175 control.clear()
176 control.forceActiveFocus()
177 }
178 }
179 }
180 }
181
182 background: KSvg.FrameSvgItem {
183 implicitWidth: Kirigami.Units.gridUnit * 8 + margins.left + margins.right
184 implicitHeight: Kirigami.Units.gridUnit + margins.top + margins.bottom
185 imagePath: "widgets/lineedit"
186 prefix: "base"
187
189 anchors {
190 fill: parent
191 leftMargin: -margins.left
192 topMargin: -margins.top
193 rightMargin: -margins.right
194 bottomMargin: -margins.bottom
195 }
196 imagePath: "widgets/lineedit"
197 prefix: "hover"
198 visible: opacity > 0
199 opacity: control.hovered
200 Behavior on opacity {
201 enabled: control.hovered && Kirigami.Units.longDuration > 0
202 NumberAnimation {
203 duration: Kirigami.Units.longDuration
204 easing.type: Easing.OutCubic
205 }
206 }
207 }
209 z: hasElement("hint-focus-over-base") ? 0 : -1
210 anchors {
211 fill: parent
212 leftMargin: -margins.left
213 topMargin: -margins.top
214 rightMargin: -margins.right
215 bottomMargin: -margins.bottom
216 }
217 imagePath: "widgets/lineedit"
218 prefix: control.visualFocus && hasElement("focusframe-center") ? "focusframe" : "focus"
219 visible: opacity > 0
220 opacity: control.visualFocus || control.activeFocus
221 Behavior on opacity {
222 enabled: Kirigami.Units.longDuration > 0
223 NumberAnimation {
224 duration: Kirigami.Units.longDuration
225 easing.type: Easing.OutCubic
226 }
227 }
228 }
229 }
230}
Type type(const QSqlDatabase &db)
AlignRight
ElideRight
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:54:11 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.