Kirigami2

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

KDE's Doxygen guidelines are available online.