From 94335d1d68a5c36d3b3fc2d6506c7f48c93446de Mon Sep 17 00:00:00 2001 From: sezanzeb Date: Sat, 31 Oct 2020 19:10:40 +0100 Subject: [PATCH] creating and selecting presets - super buggy --- bin/key-mapper-gtk | 51 ++++++++++++++++++++++++---------- data/key-mapper.glade | 3 +- keymapper/config.py | 21 ++++++++++++-- keymapper/presets.py | 6 ++-- tests/testcases/integration.py | 11 ++++++-- 5 files changed, 68 insertions(+), 24 deletions(-) diff --git a/bin/key-mapper-gtk b/bin/key-mapper-gtk index 56ec8467..090eaba5 100755 --- a/bin/key-mapper-gtk +++ b/bin/key-mapper-gtk @@ -80,6 +80,7 @@ class Window: """User Interface.""" def __init__(self): self.rows = 0 + self.selected_device = None gladefile = os.path.join(get_data_path(), 'key-mapper.glade') builder = Gtk.Builder() @@ -94,8 +95,11 @@ class Window: self.populate_devices() # find an select the newest preset based on file modification dates - self.on_select_preset(find_newest_preset()) - + device, preset = find_newest_preset() + if device: + self.on_select_device(device) + if preset: + self.on_select_preset(preset) def get(self, name): """Get a widget from the window""" @@ -112,32 +116,49 @@ class Window: for (id, device) in devices: device_selection.append(device, device) - def populate_presets(self, device): + def populate_presets(self): """Show the available presets for the selected device.""" - presets = get_presets(device) + presets = get_presets(self.selected_device) preset_selection = self.get('preset_selection') for preset in presets: - preset_selection.append(preset) + preset_selection.append(preset, preset) - def on_select_device(self, dropdown): - """List all presets, create one if none exist yet. - """ - device = dropdown.get_active_text() + def on_select_device(self, device): + """List all presets, create one if none exist yet.""" + if isinstance(device, Gtk.ComboBoxText): + preset = device.get_active_text() + + device = device.get_active_text() presets = get_presets(device) if len(presets) == 0: create_preset(device) - self.populate_presets(device) + self.selected_device = device + self.populate_presets() + + def on_create_preset_clicked(self, button): + """Create a new preset and select it.""" + new_preset = create_preset(self.selected_device) + self.get('preset_selection').append(new_preset, new_preset) + self.on_select_preset(new_preset) def on_select_preset(self, preset): - """Show the mappings of the preset + """Show the mappings of the preset""" + if isinstance(preset, Gtk.ComboBoxText): + preset = preset.get_active_text() - Parameters - ---------- - preset : string - """ # prepare one empty input to add stuff, and to get the grid to # the correct column width, otherwise it may jump if the user adds # the first row. + key_list = self.get('key_list') + for i in range(self.rows): + # don't remove the header + key_list.remove_row(i + 1) + self.rows = 0 + + self.get('preset_selection').set_active_id(preset) + + # TODO show all mapped keys from config + self.on_add_key_clicked() def on_add_key_clicked(self, button=None): diff --git a/data/key-mapper.glade b/data/key-mapper.glade index 55126c2e..16f13ef3 100644 --- a/data/key-mapper.glade +++ b/data/key-mapper.glade @@ -103,12 +103,13 @@ - + Create 80 True True True + False diff --git a/keymapper/config.py b/keymapper/config.py index 991f4785..ffc872f9 100644 --- a/keymapper/config.py +++ b/keymapper/config.py @@ -74,6 +74,23 @@ def _modify_config(config_contents, key, value): return '\n'.join(split) +def get_config_path(device, preset=None, path=None): + """Get the path that leads to the coniguration of that preset. + + Parameters + ---------- + device : string + preset : string or None + If none, will return the folder of the device + path : string or None + If none, will default to '~/.config/key-mapper/'. + In that directory, a folder for the device and a file for + the preset will be created. + """ + path = path or os.path.expanduser('~/.config/key-mapper/') + return os.path.join(path, device, preset or '') + + class Config: """Read and set config values.""" def __init__(self, device, preset, path=None): @@ -88,9 +105,7 @@ class Config: In that directory, a folder for the device and a file for the preset will be created. """ - if path is None: - path = os.path.expanduser('~/.config/key-mapper/') - path = os.path.join(path, device, preset) + path = get_config_path(device, preset, path) logger.debug('Using config file at %s', path) self.device = device diff --git a/keymapper/presets.py b/keymapper/presets.py index b92616e5..455b73be 100644 --- a/keymapper/presets.py +++ b/keymapper/presets.py @@ -26,7 +26,7 @@ import os import subprocess from keymapper.logger import logger -from keymapper.config import get_config +from keymapper.config import get_config, get_config_path def get_xinput_list(type): @@ -64,8 +64,7 @@ def get_presets(device): ---------- device : string """ - print('get_presets not yet implemented') - return [] + return os.listdir(get_config_path(device)) def create_preset(device, name=None): @@ -82,6 +81,7 @@ def create_preset(device, name=None): # trigger the creation of a new config file: get_config(device, name) + return name def get_mappings(device, preset): diff --git a/tests/testcases/integration.py b/tests/testcases/integration.py index b01af4d8..decd190f 100644 --- a/tests/testcases/integration.py +++ b/tests/testcases/integration.py @@ -91,8 +91,15 @@ class Integration(unittest.TestCase): self.assertTrue(self.window.window.get_visible()) def test_select_device_and_preset(self): - self.window.on_select_device('fakeDevice1') - self.window.on_select_preset('fakePreset1') + class FakeDropdown: + def __init__(self, name): + self.name = name + + def get_active_text(self): + return self.name + + self.window.on_select_device(FakeDropdown('fakeDevice1')) + self.window.on_select_preset(FakeDropdown('fakePreset1')) # TODO test meaningful stuff here