gui: add mouse bindings
This commit is contained in:
@@ -259,11 +259,72 @@ class KeyBindingDialog(QtWidgets.QDialog):
|
||||
self.accept()
|
||||
|
||||
|
||||
class KeyBindingsWidget(QtWidgets.QWidget):
|
||||
def __init__(self, key_bindings):
|
||||
class MouseBindingDialog(QtWidgets.QDialog):
|
||||
action_options = ([
|
||||
"Copy", "Paste", "PasteSelection", "IncreaseFontSize", "DecreaseFontSize",
|
||||
"ResetFontSize", "ScrollPageUp", "ScrollPageDown", "ScrollLineUp",
|
||||
"ScrollLineDown", "ScrollToTop", "ScrollToBottom", "ClearHistory", "Hide",
|
||||
"Quit", "ToggleFullscreen", "SpawnNewInstance", "ClearLogNotice", "None",
|
||||
] + (['ToggleSimpleFullscreen'] if platform.system() == 'Darwin' else []))
|
||||
|
||||
modifier_options = ["Command", "Control", "Option", "Super", "Shift", "Alt"]
|
||||
|
||||
mouse_options = [
|
||||
'Middle', 'Left', 'Right', '1', '2', '3', '4', '5', '6', '7', '8', '9'
|
||||
]
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.result = {}
|
||||
self.layout = QtWidgets.QFormLayout()
|
||||
self.layout.setFieldGrowthPolicy(
|
||||
QtWidgets.QFormLayout.AllNonFixedFieldsGrow
|
||||
)
|
||||
|
||||
self.mouse = QtWidgets.QComboBox()
|
||||
for option in self.mouse_options:
|
||||
self.mouse.addItem(option)
|
||||
self.layout.addRow(QtWidgets.QLabel('Mouse'), self.mouse)
|
||||
|
||||
self.mods = QtWidgets.QListWidget()
|
||||
for option in self.modifier_options:
|
||||
self.mods.addItem(option)
|
||||
self.mods.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
|
||||
self.layout.addRow(QtWidgets.QLabel('Mods'), self.mods)
|
||||
|
||||
self.action = QtWidgets.QComboBox()
|
||||
for option in self.action_options:
|
||||
self.action.addItem(option)
|
||||
self.layout.addRow(QtWidgets.QLabel('Action'), self.action)
|
||||
|
||||
buttons = QtWidgets.QDialogButtonBox()
|
||||
buttons.addButton(buttons.Ok)
|
||||
buttons.addButton(buttons.Cancel)
|
||||
buttons.accepted.connect(self.do_accept)
|
||||
buttons.rejected.connect(self.reject)
|
||||
self.layout.addWidget(buttons)
|
||||
|
||||
self.setLayout(self.layout)
|
||||
|
||||
def do_accept(self):
|
||||
mouse = self.mouse.currentText()
|
||||
# TODO: this is an ugly heuristic. should we parse yaml here?
|
||||
self.result['mouse'] = int(mouse) if mouse.isdigit() else mouse
|
||||
|
||||
mods = self.mods.selectedItems()
|
||||
if len(mods):
|
||||
self.result['mods'] = '|'.join(mod.text() for mod in mods)
|
||||
|
||||
self.result['action'] = self.action.currentText()
|
||||
|
||||
self.accept()
|
||||
|
||||
|
||||
class MultiListWidget(QtWidgets.QWidget):
|
||||
def __init__(self, elems):
|
||||
super().__init__()
|
||||
self.key_bindings = key_bindings
|
||||
self.lst = None
|
||||
self.elems = elems
|
||||
self.init()
|
||||
|
||||
def init(self):
|
||||
@@ -286,7 +347,7 @@ class KeyBindingsWidget(QtWidgets.QWidget):
|
||||
def update(self):
|
||||
if self.lst:
|
||||
self.lst.setParent(None)
|
||||
self.lst = QtWidgets.QTableWidget(len(self.key_bindings), 2)
|
||||
self.lst = QtWidgets.QTableWidget(len(self.elems), 2)
|
||||
self.lst.setShowGrid(False)
|
||||
horizontal_header = self.lst.horizontalHeader()
|
||||
horizontal_header.setVisible(False)
|
||||
@@ -296,18 +357,18 @@ class KeyBindingsWidget(QtWidgets.QWidget):
|
||||
self.lst.verticalHeader().setVisible(False)
|
||||
self.lst.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
|
||||
self.lst.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
|
||||
for i, binding in enumerate(self.key_bindings):
|
||||
for i, binding in enumerate(self.elems):
|
||||
key, action = self.itemize(binding)
|
||||
self.lst.setItem(i, 0, QtWidgets.QTableWidgetItem(key))
|
||||
self.lst.setItem(i, 1, QtWidgets.QTableWidgetItem(action))
|
||||
self.layout.insertWidget(0, self.lst)
|
||||
|
||||
def open_dialog(self):
|
||||
dialog = KeyBindingDialog()
|
||||
dialog = self.dialog()
|
||||
|
||||
if dialog.exec() == QtWidgets.QDialog.Accepted:
|
||||
key_binding = dialog.result
|
||||
self.key_bindings.append(key_binding)
|
||||
self.elems.append(key_binding)
|
||||
self.update()
|
||||
|
||||
def delete_selected(self):
|
||||
@@ -316,9 +377,18 @@ class KeyBindingsWidget(QtWidgets.QWidget):
|
||||
to_delete.add(i.row())
|
||||
|
||||
for i in sorted(to_delete, reverse=True):
|
||||
del self.key_bindings[i]
|
||||
del self.elems[i]
|
||||
self.update()
|
||||
|
||||
def value(self):
|
||||
return self.elems
|
||||
|
||||
|
||||
class KeyBindingsWidget(MultiListWidget):
|
||||
def __init__(self, *args):
|
||||
super().__init__(*args)
|
||||
self.dialog = KeyBindingDialog
|
||||
|
||||
def itemize(self, binding):
|
||||
key = binding['key']
|
||||
|
||||
@@ -337,8 +407,22 @@ class KeyBindingsWidget(QtWidgets.QWidget):
|
||||
action = 'chars({})'.format(chars)
|
||||
return key, action
|
||||
|
||||
def value(self):
|
||||
return self.key_bindings
|
||||
|
||||
class MouseBindingsWidget(MultiListWidget):
|
||||
def __init__(self, *args):
|
||||
super().__init__(*args)
|
||||
self.dialog = MouseBindingDialog
|
||||
|
||||
def itemize(self, binding):
|
||||
key = binding['mouse']
|
||||
|
||||
mods = binding.get('mods')
|
||||
if mods:
|
||||
key = '{} {}'.format(' '.join(mods.split('|')), key)
|
||||
|
||||
action = binding.get('action')
|
||||
|
||||
return key, action
|
||||
|
||||
|
||||
class ConfigWidget(QtWidgets.QWidget):
|
||||
@@ -699,10 +783,23 @@ class KeyBindings(ConfigWidget):
|
||||
def __init__(self, config):
|
||||
super().__init__()
|
||||
|
||||
keybindings = KeyBindingsWidget(config)
|
||||
key_bindings = KeyBindingsWidget(config)
|
||||
|
||||
self.widgets = {
|
||||
'': keybindings,
|
||||
'': key_bindings,
|
||||
}
|
||||
|
||||
self.render_state()
|
||||
|
||||
|
||||
class MouseBindings(ConfigWidget):
|
||||
def __init__(self, config):
|
||||
super().__init__()
|
||||
|
||||
mouse_bindings = MouseBindingsWidget(config)
|
||||
|
||||
self.widgets = {
|
||||
'': mouse_bindings,
|
||||
}
|
||||
|
||||
self.render_state()
|
||||
@@ -734,6 +831,9 @@ class Config(QtWidgets.QWidget):
|
||||
self.tabs.addTab(
|
||||
KeyBindings(self.config.get('key_bindings', [])), "Key Bindings"
|
||||
)
|
||||
self.tabs.addTab(
|
||||
MouseBindings(self.config.get('mouse_bindings', [])), "Mouse Bindings"
|
||||
)
|
||||
|
||||
self.layout.addWidget(self.tabs)
|
||||
|
||||
|
Reference in New Issue
Block a user