Warning, /frameworks/kirigami/src/controls/ActionTextField.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  SPDX-FileCopyrightText: 2019 Carl-Lucien Schwan <carl@carlschwan.eu>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 import QtQuick 2.15
0008 import QtQuick.Controls 2.15 as QQC2
0009 import org.kde.kirigami 2.20 as Kirigami
0010 
0011 /**
0012  * This is advanced textfield. It is recommended to use this class when there
0013  * is a need to create a create a textfield with action buttons (e.g a clear
0014  * action).
0015  *
0016  * Example usage:
0017  * @code{.qml}
0018  * import org.kde.kirigami 2.20 as Kirigami
0019  *
0020  * Kirigami.ActionTextField {
0021  *     id: searchField
0022  *
0023  *     placeholderText: i18n("Search…")
0024  *
0025  *     focusSequence: StandardKey.Find
0026  *
0027  *     rightActions: Kirigami.Action {
0028  *         icon.name: "edit-clear"
0029  *         visible: searchField.text !== ""
0030  *         onTriggered: {
0031  *             searchField.clear();
0032  *             searchField.accepted();
0033  *         }
0034  *     }
0035  *
0036  *     onAccepted: console.log("Search text is " + searchField.text);
0037  * }
0038  * @endcode
0039  * @since KDE Frameworks 5.56
0040  * @inherit QtQuick.Controls.TextField
0041  */
0042 QQC2.TextField {
0043     id: root
0044 
0045     /**
0046      * @brief This property holds a shortcut sequence that will focus the text field.
0047      * @since KDE Frameworks 5.56
0048      */
0049     property alias focusSequence: focusShortcut.sequence
0050 
0051     /**
0052      * @brief This property holds a list of actions that will be displayed on the left side of the text field.
0053      *
0054      * By default, this list is empty.
0055      *
0056      * @since KDE Frameworks 5.56
0057      */
0058     property list<QtObject> leftActions
0059 
0060     /**
0061      * @brief This property holds a list of actions that will be displayed on the right side of the text field.
0062      *
0063      * By default, this list is empty.
0064      *
0065      * @since KDE Frameworks 5.56
0066      */
0067     property list<QtObject> rightActions
0068 
0069     /** @internal */
0070     property alias _leftActionsRow: leftActionsRow
0071 
0072     /** @internal */
0073     property alias _rightActionsRow: rightActionsRow
0074 
0075     hoverEnabled: true
0076 
0077     // Manually setting this fixes alignment in RTL layouts
0078     horizontalAlignment: TextInput.AlignLeft
0079 
0080     leftPadding: Kirigami.Units.smallSpacing + (root.effectiveHorizontalAlignment === TextInput.AlignRight ? rightActionsRow : leftActionsRow).width
0081     rightPadding: Kirigami.Units.smallSpacing + (root.effectiveHorizontalAlignment === TextInput.AlignRight ? leftActionsRow : rightActionsRow).width
0082 
0083     Behavior on leftPadding {
0084         NumberAnimation {
0085             duration: Kirigami.Units.longDuration
0086             easing.type: Easing.InOutQuad
0087         }
0088     }
0089 
0090     Behavior on rightPadding {
0091         NumberAnimation {
0092             duration: Kirigami.Units.longDuration
0093             easing.type: Easing.InOutQuad
0094         }
0095     }
0096 
0097     Shortcut {
0098         id: focusShortcut
0099         enabled: root.visible && root.enabled
0100         onActivated: {
0101             root.forceActiveFocus(Qt.ShortcutFocusReason)
0102             root.selectAll()
0103         }
0104     }
0105 
0106     QQC2.ToolTip {
0107         visible: focusShortcut.nativeText.length > 0 && root.text.length === 0 && !rightActionsRow.hovered && !leftActionsRow.hovered && hovered
0108         text: focusShortcut.nativeText
0109     }
0110 
0111     component ActionIconMouseArea: MouseArea {
0112         anchors.fill: parent
0113         activeFocusOnTab: true
0114         cursorShape: Qt.PointingHandCursor
0115         hoverEnabled: true
0116         Accessible.role: Accessible.Button
0117         Keys.onPressed: event => {
0118             switch (event.key) {
0119             case Qt.Key_Space:
0120             case Qt.Key_Enter:
0121             case Qt.Key_Return:
0122             case Qt.Key_Select:
0123                 clicked(null);
0124                 event.accepted = true;
0125                 break;
0126             }
0127         }
0128     }
0129 
0130     Row {
0131         id: leftActionsRow
0132         padding: Kirigami.Units.smallSpacing
0133         spacing: Kirigami.Units.smallSpacing
0134         layoutDirection: Qt.LeftToRight
0135         LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
0136         anchors.left: parent.left
0137         anchors.leftMargin: Kirigami.Units.smallSpacing
0138         anchors.top: parent.top
0139         anchors.topMargin: parent.topPadding
0140         anchors.bottom: parent.bottom
0141         anchors.bottomMargin: parent.bottomPadding
0142         Repeater {
0143             model: root.leftActions
0144             Kirigami.Icon {
0145                 implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
0146                 implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
0147 
0148                 anchors.verticalCenter: parent.verticalCenter
0149 
0150                 source: modelData.icon.name.length > 0 ? modelData.icon.name : modelData.icon.source
0151                 active: leftActionArea.containsPress || leftActionArea.activeFocus
0152                 visible: !(modelData instanceof Kirigami.Action) || modelData.visible
0153                 enabled: modelData.enabled
0154 
0155                 ActionIconMouseArea {
0156                     id: leftActionArea
0157                     Accessible.name: modelData.text
0158                     onClicked: mouse => modelData.trigger()
0159                 }
0160 
0161                 QQC2.ToolTip {
0162                     visible: (rightActionArea.containsMouse || rightActionArea.activeFocus) && (modelData.text.length > 0)
0163                     text: modelData.text
0164                 }
0165             }
0166         }
0167     }
0168 
0169     Row {
0170         id: rightActionsRow
0171         padding: Kirigami.Units.smallSpacing
0172         spacing: Kirigami.Units.smallSpacing
0173         layoutDirection: Qt.RightToLeft
0174         LayoutMirroring.enabled: root.effectiveHorizontalAlignment === TextInput.AlignRight
0175         anchors.right: parent.right
0176         anchors.rightMargin: Kirigami.Units.smallSpacing
0177         anchors.top: parent.top
0178         anchors.topMargin: parent.topPadding
0179         anchors.bottom: parent.bottom
0180         anchors.bottomMargin: parent.bottomPadding
0181         Repeater {
0182             model: root.rightActions
0183             Kirigami.Icon {
0184                 implicitWidth: Kirigami.Units.iconSizes.sizeForLabels
0185                 implicitHeight: Kirigami.Units.iconSizes.sizeForLabels
0186 
0187                 anchors.verticalCenter: parent.verticalCenter
0188 
0189                 source: modelData.icon.name.length > 0 ? modelData.icon.name : modelData.icon.source
0190                 active: rightActionArea.containsPress || rightActionArea.activeFocus
0191                 visible: !(modelData instanceof Kirigami.Action) || modelData.visible
0192                 enabled: modelData.enabled
0193 
0194                 ActionIconMouseArea {
0195                     id: rightActionArea
0196                     Accessible.name: modelData.text
0197                     onClicked: mouse => modelData.trigger()
0198                 }
0199 
0200                 QQC2.ToolTip {
0201                     visible: (rightActionArea.containsMouse || rightActionArea.activeFocus) && (modelData.text.length > 0)
0202                     text: modelData.text
0203                 }
0204             }
0205         }
0206     }
0207 }