creating and selecting presets - super buggy

This commit is contained in:
sezanzeb 2020-10-31 19:10:40 +01:00
parent 5932d0f36c
commit 94335d1d68
5 changed files with 68 additions and 24 deletions

View File

@ -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):

View File

@ -103,12 +103,13 @@
</packing>
</child>
<child>
<object class="GtkButton" id="add_key1">
<object class="GtkButton" id="create_preset">
<property name="label" translatable="yes">Create</property>
<property name="width_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_create_preset_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>

View File

@ -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

View File

@ -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):

View File

@ -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