Skip to content

Commit

Permalink
Add Clear button to keyInputButton utility
Browse files Browse the repository at this point in the history
Fix #176
  • Loading branch information
Cuperino committed Sep 28, 2024
1 parent 6e82a5c commit 26aeb05
Showing 1 changed file with 75 additions and 60 deletions.
135 changes: 75 additions & 60 deletions src/kirigami_ui/KeyInputButton.qml
Original file line number Diff line number Diff line change
Expand Up @@ -25,77 +25,92 @@ import QtQuick.Layouts 1.12

import com.cuperino.qprompt 1.0

Button {
id: keyInputButton
RowLayout {
id: keyInput

property alias checked: keyInputButton.checked
property alias text: keyInputButton.text

signal toggleButtonsOff()
signal setKey(int key, int modifiers)

// Validate input
function isValidInput(key, modifiers) {
const actions = [
[prompter.keys.increaseVelocity, prompter.keys.increaseVelocityModifiers],
[prompter.keys.decreaseVelocity, prompter.keys.decreaseVelocityModifiers],
[prompter.keys.stop, prompter.keys.stopModifiers],
[prompter.keys.pause, prompter.keys.pauseModifiers],
[prompter.keys.reverse, prompter.keys.reverseModifiers],
[prompter.keys.rewind, prompter.keys.rewindModifiers],
[prompter.keys.fastForward, prompter.keys.fastForwardModifiers],
[prompter.keys.skipBackwards, prompter.keys.skipBackwardsModifiers],
[prompter.keys.skipForward, prompter.keys.skipForwardModifiers],
[prompter.keys.previousMarker, prompter.keys.previousMarkerModifiers],
[prompter.keys.nextMarker, prompter.keys.nextMarkerModifiers],
[prompter.keys.toggle, prompter.keys.toggleModifiers],
[prompter.keys.setVelocity0, prompter.keys.setVelocity0Modifiers],
[prompter.keys.setVelocity1, prompter.keys.setVelocity1Modifiers],
[prompter.keys.setVelocity2, prompter.keys.setVelocity2Modifiers],
[prompter.keys.setVelocity3, prompter.keys.setVelocity3Modifiers],
[prompter.keys.setVelocity4, prompter.keys.setVelocity4Modifiers],
[prompter.keys.setVelocity5, prompter.keys.setVelocity5Modifiers],
[prompter.keys.setVelocity6, prompter.keys.setVelocity6Modifiers],
[prompter.keys.setVelocity7, prompter.keys.setVelocity7Modifiers],
[prompter.keys.setVelocity8, prompter.keys.setVelocity8Modifiers],
[prompter.keys.setVelocity9, prompter.keys.setVelocity9Modifiers]
]
// Return invalid if key is in use
let flag = false;
for (let i=0; i<actions.length; i++) {
if ( actions[i][0]===key && actions[i][1]===modifiers ) {
flag = true;
break;
Button {
id: keyInputButton

checkable: true

onClicked: {
if (checked) {
keyInput.toggleButtonsOff()
checked = true
}
}
// The following inputs will not be considered valid on their own
return !flag && [Qt.Key_Escape, Qt.Key_Super_L, Qt.Key_Super_R, Qt.Key_Meta].indexOf(key)===-1
}

QmlUtil {
id: qmlutil
}

checkable: true
flat: true
// Validate input
function isValidInput(key, modifiers) {
const actions = [
[prompter.keys.increaseVelocity, prompter.keys.increaseVelocityModifiers],
[prompter.keys.decreaseVelocity, prompter.keys.decreaseVelocityModifiers],
[prompter.keys.stop, prompter.keys.stopModifiers],
[prompter.keys.pause, prompter.keys.pauseModifiers],
[prompter.keys.reverse, prompter.keys.reverseModifiers],
[prompter.keys.rewind, prompter.keys.rewindModifiers],
[prompter.keys.fastForward, prompter.keys.fastForwardModifiers],
[prompter.keys.skipBackwards, prompter.keys.skipBackwardsModifiers],
[prompter.keys.skipForward, prompter.keys.skipForwardModifiers],
[prompter.keys.previousMarker, prompter.keys.previousMarkerModifiers],
[prompter.keys.nextMarker, prompter.keys.nextMarkerModifiers],
[prompter.keys.toggle, prompter.keys.toggleModifiers],
[prompter.keys.setVelocity0, prompter.keys.setVelocity0Modifiers],
[prompter.keys.setVelocity1, prompter.keys.setVelocity1Modifiers],
[prompter.keys.setVelocity2, prompter.keys.setVelocity2Modifiers],
[prompter.keys.setVelocity3, prompter.keys.setVelocity3Modifiers],
[prompter.keys.setVelocity4, prompter.keys.setVelocity4Modifiers],
[prompter.keys.setVelocity5, prompter.keys.setVelocity5Modifiers],
[prompter.keys.setVelocity6, prompter.keys.setVelocity6Modifiers],
[prompter.keys.setVelocity7, prompter.keys.setVelocity7Modifiers],
[prompter.keys.setVelocity8, prompter.keys.setVelocity8Modifiers],
[prompter.keys.setVelocity9, prompter.keys.setVelocity9Modifiers]
]
// Return invalid if key is in use
let flag = false;
for (let i=0; i<actions.length; i++) {
if ( actions[i][0]===key && actions[i][1]===modifiers ) {
flag = true;
break;
}
}
// The following inputs will not be considered valid on their own
return !flag && [Qt.Key_Escape, Qt.Key_Super_L, Qt.Key_Super_R, Qt.Key_Meta].indexOf(key)===-1
}

onClicked: {
if (checked) {
keyInputButton.toggleButtonsOff()
checked = true
Layout.fillWidth: true
Keys.onShortcutOverride: (event) => {
if (event.key === Qt.Key_Escape)
event.accepted = true
}
Keys.onPressed: (event) => {
if (checked && [Qt.Key_Super_L, Qt.Key_Super_R, Qt.Key_Meta, Qt.Key_Control, Qt.Key_Shift, Qt.Key_Alt, Qt.Key_AltGr].indexOf(event.key)===-1) {
if (isValidInput(event.key, event.modifiers)) {
keyInput.setKey(event.key, event.modifiers);
text = qmlutil.keyToString(event.key, event.modifiers);
}
event.accepted = true;
keyInput.toggleButtonsOff();
}
}
}

Layout.fillWidth: true
Keys.onShortcutOverride: (event) => {
if (event.key === Qt.Key_Escape)
event.accepted = true
QmlUtil {
id: qmlutil
}
}
Keys.onPressed: (event) => {
if (checked && [Qt.Key_Super_L, Qt.Key_Super_R, Qt.Key_Meta, Qt.Key_Control, Qt.Key_Shift, Qt.Key_Alt, Qt.Key_AltGr].indexOf(event.key)===-1) {
if (isValidInput(event.key, event.modifiers)) {
keyInputButton.setKey(event.key, event.modifiers);
text = qmlutil.keyToString(event.key, event.modifiers);
}
event.accepted = true;
keyInputButton.toggleButtonsOff();
Button {
id: clearButton
text: i18nc("Button to remove a keyboard shortcut", "Clear")
enabled: keyInputButton.text !== ""
onClicked: {
keyInput.setKey(0, 0);
keyInputButton.text = "";
}
}
}

0 comments on commit 26aeb05

Please sign in to comment.