2015-02-26 10:09:13 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import widgetUtils
|
|
|
|
import config
|
2018-11-22 13:35:19 -06:00
|
|
|
from . import wx_ui
|
2022-12-20 13:02:27 -06:00
|
|
|
from . import actions
|
2015-08-21 09:51:01 -05:00
|
|
|
from pubsub import pub
|
2015-02-26 10:09:13 -06:00
|
|
|
|
|
|
|
class KeystrokeEditor(object):
|
2022-12-20 13:02:27 -06:00
|
|
|
def __init__(self, session_type="twitter"):
|
2021-06-16 16:18:41 -05:00
|
|
|
super(KeystrokeEditor, self).__init__()
|
2022-12-20 13:02:27 -06:00
|
|
|
self.actions = getattr(actions, session_type).actions
|
2021-06-16 16:18:41 -05:00
|
|
|
self.changed = False # Change it if the keyboard shorcuts are reassigned.
|
|
|
|
self.dialog = wx_ui.keystrokeEditorDialog()
|
|
|
|
self.map = config.keymap["keymap"]
|
|
|
|
# we need to copy the keymap before modify it, for unregistering the old keystrokes if is needed.
|
|
|
|
self.hold_map = self.map.copy()
|
2022-12-20 13:02:27 -06:00
|
|
|
self.dialog.put_keystrokes(self.actions, self.map)
|
2021-06-16 16:18:41 -05:00
|
|
|
widgetUtils.connect_event(self.dialog.edit, widgetUtils.BUTTON_PRESSED, self.edit_keystroke)
|
2021-07-13 17:22:20 -05:00
|
|
|
widgetUtils.connect_event(self.dialog.undefine, widgetUtils.BUTTON_PRESSED, self.undefine_keystroke)
|
2021-06-16 16:18:41 -05:00
|
|
|
widgetUtils.connect_event(self.dialog.execute, widgetUtils.BUTTON_PRESSED, self.execute_action)
|
|
|
|
self.dialog.get_response()
|
2015-02-26 10:09:13 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def edit_keystroke(self, *args, **kwargs):
|
|
|
|
action = self.dialog.actions[self.dialog.get_action()]
|
|
|
|
edit_dialog = wx_ui.editKeystrokeDialog()
|
|
|
|
self.set_keystroke(self.map[action], edit_dialog)
|
|
|
|
answer = edit_dialog.get_response()
|
|
|
|
if answer == widgetUtils.OK:
|
|
|
|
new_keystroke = self.get_edited_keystroke(edit_dialog)
|
|
|
|
if new_keystroke != self.map[action]:
|
|
|
|
self.changed = True
|
|
|
|
self.map[action] = new_keystroke
|
2022-12-20 13:02:27 -06:00
|
|
|
self.dialog.put_keystrokes(self.actions, self.map)
|
2015-02-26 10:09:13 -06:00
|
|
|
|
2021-07-13 17:22:20 -05:00
|
|
|
def undefine_keystroke(self, *args, **kwargs):
|
|
|
|
action = self.dialog.actions[self.dialog.get_action()]
|
|
|
|
keystroke = self.map.get(action)
|
|
|
|
if keystroke == None:
|
|
|
|
return
|
|
|
|
answer = self.dialog.undefine_keystroke_confirmation()
|
|
|
|
if answer == widgetUtils.YES:
|
|
|
|
self.map[action] = ""
|
|
|
|
self.changed = True
|
2022-12-20 13:02:27 -06:00
|
|
|
self.dialog.put_keystrokes(self.actions, self.map)
|
2021-07-13 17:22:20 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def set_keystroke(self, keystroke, dialog):
|
|
|
|
for i in keystroke.split("+"):
|
|
|
|
if hasattr(dialog, i):
|
|
|
|
dialog.set(i, True)
|
|
|
|
dialog.set("key", keystroke.split("+")[-1])
|
2015-02-26 10:09:13 -06:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def get_edited_keystroke(self, dialog):
|
|
|
|
keys = []
|
|
|
|
if dialog.get("control") == True:
|
|
|
|
keys.append("control")
|
|
|
|
if dialog.get("win") == True:
|
|
|
|
keys.append("win")
|
|
|
|
if dialog.get("alt") == True:
|
|
|
|
keys.append("alt")
|
|
|
|
if dialog.get("shift") == True:
|
|
|
|
keys.append("shift")
|
|
|
|
if dialog.get("key") != "":
|
|
|
|
keys.append(dialog.get("key"))
|
|
|
|
else:
|
|
|
|
wx_ui.no_key()
|
|
|
|
return
|
|
|
|
return "+".join(keys)
|
2015-08-21 09:51:01 -05:00
|
|
|
|
2021-06-16 16:18:41 -05:00
|
|
|
def execute_action(self, *args, **kwargs):
|
|
|
|
action = self.dialog.actions[self.dialog.get_action()]
|
|
|
|
pub.sendMessage("execute-action", action=action)
|