Kirigami2

ActionTextField.qml
1/*
2 * SPDX-FileCopyrightText: 2019 Carl-Lucien Schwan <carl@carlschwan.eu>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7import QtQuick
8import QtQuick.Controls as QQC2
9import QtQuick.Templates as T
10import org.kde.kirigami as Kirigami
11
12/**
13 * This is advanced textfield. It is recommended to use this class when there
14 * is a need to create a create a textfield with action buttons (e.g a clear
15 * action).
16 *
17 * Example usage for a search field:
18 * @code
19 * import org.kde.kirigami 2.20 as Kirigami
20 *
21 * Kirigami.ActionTextField {
22 * id: searchField
23 *
24 * placeholderText: i18n("Search…")
25 *
26 * focusSequence: StandardKey.Find
27 *
28 * rightActions: Kirigami.Action {
29 * icon.name: "edit-clear"
30 * visible: searchField.text.length > 0
31 * onTriggered: {
32 * searchField.clear();
33 * searchField.accepted();
34 * }
35 * }
36 *
37 * onAccepted: console.log("Search text is " + searchField.text);
38 * }
39 * @endcode
40 *
41 * @since 5.56
42 * @inherit QtQuick.Controls.TextField
43 */
44QQC2.TextField {
45 id: root
46
47 /**
48 * @brief This property holds a shortcut sequence that will focus the text field.
49 * @since 5.56
50 */
51 property alias focusSequence: focusShortcut.sequence
52
53 /**
54 * @brief This property holds a list of actions that will be displayed on the left side of the text field.
55 *
56 * By default this list is empty.
57 *
58 * @since 5.56
59 */
60 property list<T.Action> leftActions
61
62 /**
63 * @brief This property holds a list of actions that will be displayed on the right side of the text field.
64 *
65 * By default this list is empty.
66 *
67 * @since 5.56
68 */
69 property list<T.Action> rightActions
70
71 property alias _leftActionsRow: leftActionsRow
72 property alias _rightActionsRow: rightActionsRow
73
74 hoverEnabled: true
75
76 // Manually setting this fixes alignment in RTL layouts
77 horizontalAlignment: TextInput.AlignLeft
78
79 leftPadding: Kirigami.Units.smallSpacing + (root.effectiveHorizontalAlignment === TextInput.AlignRight ? rightActionsRow : leftActionsRow).width
80 rightPadding: Kirigami.Units.smallSpacing + (root.effectiveHorizontalAlignment === TextInput.AlignRight ? leftActionsRow : rightActionsRow).width
81
82 Behavior on leftPadding {
83 NumberAnimation {
84 duration: Kirigami.Units.longDuration
85 easing.type: Easing.InOutQuad
86 }
87 }
88
89 Behavior on rightPadding {
90 NumberAnimation {
91 duration: Kirigami.Units.longDuration
92 easing.type: Easing.InOutQuad
93 }
94 }
95
96 Shortcut {
97 id: focusShortcut
98 enabled: root.visible && root.enabled
99 onActivated: {
100 root.forceActiveFocus(Qt.ShortcutFocusReason)
101 root.selectAll()
102 }
103 }
104
105 QQC2.ToolTip {
106 visible: focusShortcut.nativeText.length > 0 && root.text.length === 0 && !rightActionsRow.hovered && !leftActionsRow.hovered && root.hovered
107 text: focusShortcut.nativeText
108 }
109
110 component ActionIconMouseArea: MouseArea {
111 anchors.fill: parent
112 activeFocusOnTab: true
113 cursorShape: Qt.PointingHandCursor
114 hoverEnabled: true
115 Accessible.role: Accessible.Button
116 Keys.onPressed: event => {
117 switch (event.key) {
118 case Qt.Key_Space:
119 case Qt.Key_Enter:
120 case Qt.Key_Return:
121 case Qt.Key_Select:
122 clicked(null);
123 event.accepted = true;
124 break;
125 }
126 }
127 }
128
129 Row {
130 id: leftActionsRow
131 padding: Kirigami.Units.smallSpacing
132 spacing: Kirigami.Units.smallSpacing
133 layoutDirection: Qt.LeftToRight
134 LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
135 anchors.left: parent.left
136 anchors.leftMargin: Kirigami.Units.smallSpacing
137 anchors.top: parent.top
138 anchors.topMargin: parent.topPadding
139 anchors.bottom: parent.bottom
140 anchors.bottomMargin: parent.bottomPadding
141 Repeater {
142 model: root.leftActions
143 Kirigami.Icon {
144 id: delegate
145
146 required property T.Action modelData
147
148 implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
149 implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
150
151 anchors.verticalCenter: parent.verticalCenter
152
153 source: modelData.icon.name.length > 0 ? modelData.icon.name : modelData.icon.source
154 active: actionArea.containsPress || actionArea.activeFocus
155 visible: !(modelData instanceof Kirigami.Action) || modelData.visible
156 enabled: modelData.enabled
157
158 ActionIconMouseArea {
159 id: actionArea
160 Accessible.name: delegate.modelData.text
161 onClicked: mouse => delegate.modelData.trigger()
162 }
163
164 QQC2.ToolTip {
165 visible: (actionArea.containsMouse || actionArea.activeFocus) && (delegate.modelData.text.length > 0)
166 text: delegate.modelData.text
167 }
168 }
169 }
170 }
171
172 Row {
173 id: rightActionsRow
174 padding: Kirigami.Units.smallSpacing
175 spacing: Kirigami.Units.smallSpacing
176 layoutDirection: Qt.RightToLeft
177 LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
178 anchors.right: parent.right
179 anchors.rightMargin: Kirigami.Units.smallSpacing
180 anchors.top: parent.top
181 anchors.topMargin: parent.topPadding
182 anchors.bottom: parent.bottom
183 anchors.bottomMargin: parent.bottomPadding
184 Repeater {
185 model: root.rightActions
186 Kirigami.Icon {
187 id: delegate
188
189 required property T.Action modelData
190
191 implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
192 implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
193
194 anchors.verticalCenter: parent.verticalCenter
195
196 source: modelData.icon.name.length > 0 ? modelData.icon.name : modelData.icon.source
197 active: actionArea.containsPress || actionArea.activeFocus
198 visible: !(modelData instanceof Kirigami.Action) || modelData.visible
199 enabled: modelData.enabled
200
201 ActionIconMouseArea {
202 id: actionArea
203 Accessible.name: delegate.modelData.text
204 onClicked: mouse => delegate.modelData.trigger()
205 }
206
207 QQC2.ToolTip {
208 visible: (actionArea.containsMouse || actionArea.activeFocus) && (delegate.modelData.text.length > 0)
209 text: delegate.modelData.text
210 }
211 }
212 }
213 }
214}
An item that represents an abstract Action.
Definition Action.qml:15
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
Type type(const QSqlDatabase &db)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString name(StandardShortcut id)
AlignRight
RightToLeft
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:45:17 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.