Plasma-framework

ActionTextField.qml
1// SPDX-FileCopyrightText: 2019 Carl Schwan <carl@carlschwan.eu>
2// SPDX-License-Identifier: LGPL-2.0-or-later
3
4import QtQuick
5import QtQuick.Controls as QQC2
6import QtQuick.Templates as T
7import org.kde.plasma.components as PlasmaComponents3
8import org.kde.kirigami as Kirigami
9
10/**
11 * This is advanced textfield. It is recommended to use this class when there
12 * is a need to create a create a textfield with action buttons (e.g a clear
13 * action).
14 *
15 * For common pattern like, a search field or a password field, prefer using the
16 * more specifig org::kde::extras::SearchField or org::kde::extras::PasswordField.
17 *
18 * Example usage for a search field:
19 * @code
20 * import QtQuick.Controls as QQC2
21 * import org.kde.plasma.extras as PlasmaExtras
22 *
23 * PlasmaExtras.ActionTextField {
24 * id: searchField
25 *
26 * placeholderText: "Search…"
27 *
28 * focusSequence: StandardKey.Find
29 *
30 * rightActions: [
31 * QQC2.Action {
32 * icon.name: "edit-clear"
33 * enabled: searchField.text !== ""
34 * onTriggered: {
35 * searchField.clear()
36 * searchField.accepted()
37 * }
38 * }
39 * ]
40 *
41 * onAccepted: console.log("Search text is " + searchField.text)
42 * }
43 * @endcode
44 *
45 * @inherit QtQuick.Controls.TextField
46 * @since 5.93
47 * @author Carl Schwan <carl@carlschwan.eu>
48 */
49PlasmaComponents3.TextField {
50 id: root
51
52 /**
53 * This property holds a shortcut sequence that will focus the text field.
54 *
55 * @property QtQuick.Shortcut.sequence focusSequence
56 * @since 5.93
57 */
58 property alias focusSequence: focusShortcut.sequence
59
60 /**
61 * This property holds a list of actions that will be displayed on the left side of the text field.
62 *
63 * By default this list is empty.
64 *
65 * @since 5.93
66 */
67 property list<T.Action> leftActions
68
69 /**
70 * This property holds a list of actions that will be displayed on the right side of the text field.
71 *
72 * By default this list is empty.
73 *
74 * @since 5.93
75 */
76 property list<T.Action> rightActions
77
78 property alias _leftActionsRow: leftActionsRow
79 property alias _rightActionsRow: rightActionsRow
80
81 hoverEnabled: true
82
83 topPadding: __hasBackgroundAndMargins ? background.margins.top : 0
84 bottomPadding: __hasBackgroundAndMargins ? background.margins.bottom : 0
85
86 leftPadding: if (root.effectiveHorizontalAlignment === TextInput.AlignRight) {
87 return _rightActionsRow.width + (__hasBackgroundAndMargins ? background.margins.left : 0);
88 } else {
89 return _leftActionsRow.width + (__hasBackgroundAndMargins ? background.margins.left : 0);
90 }
91 rightPadding: if (root.effectiveHorizontalAlignment === TextInput.AlignRight) {
92 return _leftActionsRow.width + (__hasBackgroundAndMargins ? background.margins.right : 0);
93 } else {
94 return _rightActionsRow.width + (__hasBackgroundAndMargins ? background.margins.right : 0);
95 }
96
97 Behavior on leftPadding {
98 enabled: Kirigami.Units.longDuration > 0
99 NumberAnimation {
100 duration: Kirigami.Units.longDuration
101 easing.type: Easing.InOutQuad
102 }
103 }
104
105 Behavior on rightPadding {
106 enabled: Kirigami.Units.longDuration > 0
107 NumberAnimation {
108 duration: Kirigami.Units.longDuration
109 easing.type: Easing.InOutQuad
110 }
111 }
112
113 Shortcut {
114 id: focusShortcut
115 onActivated: {
116 root.forceActiveFocus(Qt.ShortcutFocusReason)
117 root.selectAll()
118 }
119
120 // here to make it private
121 component ActionIcon: Kirigami.Icon {
122 id: button
123
124 required property T.Action modelData
125
126 implicitWidth: Kirigami.Units.iconSizes.small
127 implicitHeight: Kirigami.Units.iconSizes.small
128
129 anchors.verticalCenter: parent.verticalCenter
130
131 source: modelData.icon.name.length > 0 ? modelData.icon.name : modelData.icon.source
132 visible: !(modelData instanceof Kirigami.Action) || modelData.visible
133 MouseArea {
134 onClicked: button.modelData.trigger()
135 cursorShape: Qt.ArrowCursor
136 anchors.fill: parent
137 }
138 }
139 }
140
141 PlasmaComponents3.ToolTip.visible: focusShortcut.nativeText.length > 0 && root.text.length === 0 && !rightActionsRow.hovered && !leftActionsRow.hovered && hovered
142 PlasmaComponents3.ToolTip.text: focusShortcut.nativeText
143 PlasmaComponents3.ToolTip.delay: Kirigami.Settings.tabletMode ? Qt.styleHints.mousePressAndHoldInterval : Kirigami.Units.toolTipDelay
144
145 Row {
146 id: leftActionsRow
147 padding: visible ? Kirigami.Units.smallSpacing : 0
148 LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
149 anchors.left: parent.left
150 anchors.leftMargin: Kirigami.Units.smallSpacing
151 anchors.verticalCenter: parent.verticalCenter
152 height: root.implicitHeight - 2 * Kirigami.Units.smallSpacing
153 visible: root.leftActions.length > 0
154 Repeater {
155 model: root.leftActions
156 ActionIcon { }
157 }
158 }
159
160 Row {
161 id: rightActionsRow
162 padding: visible ? Kirigami.Units.smallSpacing : 0
163 layoutDirection: Qt.RightToLeft
164 LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
165 anchors.right: parent.right
166 anchors.rightMargin: Kirigami.Units.smallSpacing
167 anchors.verticalCenter: parent.verticalCenter
168 height: root.implicitHeight - 2 * Kirigami.Units.smallSpacing
169 visible: root.rightActions.length > 0
170 Repeater {
171 model: root.rightActions
172 ActionIcon { }
173 }
174 }
175}
An Item managing a Plasma-themed tooltip.
Definition tooltip.h:51
KIOCORE_EXPORT QStringList list(const QString &fileClass)
AlignRight
ArrowCursor
RightToLeft
QTextStream & left(QTextStream &stream)
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.