Warning, /frameworks/kdeclarative/src/qmlcontrols/kquickcontrols/KeySequenceItem.qml is written in an unsupported language. File is not indexed.

0001 import QtQuick 2.15
0002 import QtQuick.Controls 2.15
0003 import QtQuick.Layouts 1.15
0004 import QtQuick.Window 2.15
0005 
0006 import org.kde.private.kquickcontrols 2.0 as KQuickControlsPrivate
0007 
0008 RowLayout {
0009     id: root
0010 
0011     property bool showClearButton: true
0012     property bool showCancelButton: false /// TODO KF6 default to true
0013     property alias modifierOnlyAllowed: helper.modifierOnlyAllowed
0014     property alias modifierlessAllowed: helper.modifierlessAllowed
0015     property alias multiKeyShortcutsAllowed: helper.multiKeyShortcutsAllowed
0016     property alias keySequence: helper.currentKeySequence
0017 
0018     /**
0019      * This property controls which types of shortcuts are checked for conflicts when the keySequence
0020      * is set. If a conflict is detected, a messagebox will be shown asking the user to confirm their
0021      * input. Valid values are combinations of the following flags:
0022      *   - @p ShortcutType.None Do not check for conflicts.
0023      *   - @p ShortcutType.StandardShortcuts Check against standard shortcuts. @see KStandardshortcut
0024      *   - @p ShortcutType.GlobalShortcuts Check against global shortcuts. @see KGlobalAccel
0025      *
0026      * The default is `ShortcutType.GlobalShortcuts | ShortcutType.StandardShortcut`
0027      * @since 5.74
0028      */
0029     property alias checkForConflictsAgainst: helper.checkAgainstShortcutTypes
0030 
0031     /**
0032      * This signal is emitted after the user introduces a new key sequence
0033      *
0034      * @since 5.68
0035      * @deprecated Use keySequenceModified()
0036      */
0037     signal captureFinished()
0038 
0039     /***
0040      * Emitted whenever the key sequence is modified by the user, interacting with the component
0041      *
0042      * Either by interacting capturing a key sequence or pressing the clear button.
0043      *
0044      * @since 5.99
0045      */
0046     signal keySequenceModified()
0047 
0048     /**
0049      * Start capturing a key sequence. This equivalent to the user clicking on the main button of the item
0050      * @since 5.70
0051      */
0052     function startCapturing() {
0053         mainButton.checked = true
0054     }
0055 
0056     KQuickControlsPrivate.KeySequenceHelper {
0057         id: helper
0058         onGotKeySequence: keySequence => {
0059             if (isKeySequenceAvailable(keySequence)) {
0060                 root.keySequence = keySequence;
0061             } else {
0062                 root.keySequence = mainButton.previousSequence
0063             }
0064             mainButton.checked = false;
0065             root.captureFinished();
0066             root.keySequenceModified();
0067         }
0068     }
0069 
0070     KQuickControlsPrivate.TranslationContext {
0071         id: _tr
0072         domain: "kdeclarative5"
0073     }
0074 
0075     Button {
0076         id: mainButton
0077 
0078         icon.name: "configure"
0079 
0080         checkable: true
0081         focus: checked
0082 
0083         hoverEnabled: true
0084         property var previousSequence: ""
0085 
0086         text: {
0087             const keys = helper.isRecording ? helper.currentKeySequence : root.keySequence
0088             const text = helper.keySequenceIsEmpty(keys)
0089                 ? (helper.isRecording
0090                     ? _tr.i18nc("What the user inputs now will be taken as the new shortcut", "Input")
0091                     : _tr.i18nc("No shortcut defined", "None"))
0092                 // Single ampersand gets interpreted by the button as a mnemonic
0093                 // and removed; replace it with a double ampersand so that it
0094                 // will be displayed by the button as a single ampersand, or
0095                 // else shortcuts with the actual ampersand character will
0096                 // appear to be partially empty.
0097                 : helper.keySequenceNativeText(keys).replace('&', '&&');
0098             // These spaces are intentional
0099             return " " + text + (helper.isRecording ? " ... " : " ")
0100         }
0101 
0102         Accessible.description: _tr.i18n("Click on the button, then enter the shortcut like you would in the program.\nExample for Ctrl+A: hold the Ctrl key and press A.")
0103 
0104         ToolTip {
0105             visible: mainButton.hovered
0106             text: mainButton.Accessible.description
0107         }
0108 
0109         onCheckedChanged: {
0110             if (checked) {
0111                 previousSequence = helper.keySequenceNativeText(root.keySequence)
0112                 helper.window = helper.renderWindow(parent.Window.window)
0113                 mainButton.forceActiveFocus()
0114                 helper.startRecording()
0115             } else if (helper.isRecording) {
0116                 helper.cancelRecording()
0117             }
0118         }
0119 
0120         onFocusChanged: {
0121             if (!focus) {
0122                 mainButton.checked = false
0123             }
0124         }
0125     }
0126 
0127     Button {
0128         id: clearButton
0129         Layout.fillHeight: true
0130         Layout.preferredWidth: height
0131         visible: root.showClearButton && !helper.isRecording
0132         onClicked: {
0133             root.keySequence = helper.fromString()
0134             root.keySequenceModified();
0135             root.captureFinished(); // Not really capturing, but otherwise we cannot track this state, hence apps should use keySequenceModified
0136         }
0137 
0138         // Just a helper object
0139         Text {
0140             id: theText
0141             visible: false
0142             text: root.keySequence
0143         }
0144         enabled: theText.text.length > 0
0145 
0146         hoverEnabled: true
0147         // icon name determines the direction of the arrow, NOT the direction of the app layout
0148         icon.name: Qt.application.layoutDirection === Qt.LeftToRight ? "edit-clear-locationbar-rtl" : "edit-clear-locationbar-ltr"
0149 
0150         Accessible.name: _tr.i18nc("@info:tooltip", "Clear Key Sequence")
0151 
0152         ToolTip {
0153             visible: clearButton.hovered
0154             text: clearButton.Accessible.name
0155         }
0156     }
0157 
0158     Button {
0159         Layout.fillHeight: true
0160         Layout.preferredWidth: height
0161         onClicked: helper.cancelRecording()
0162         visible: root.showCancelButton && helper.isRecording
0163 
0164         icon.name: "dialog-cancel"
0165 
0166         Accessible.name: _tr.i18nc("@info:tooltip", "Cancel Key Sequence Recording")
0167 
0168         ToolTip {
0169             visible: parent.hovered
0170             text: parent.Accessible.name
0171         }
0172     }
0173 }