creating and selecting presets - super buggy

xkb
sezanzeb 4 years ago committed by sezanzeb
parent 916ec6278a
commit ac1ba6dba3

@ -80,6 +80,7 @@ class Window:
"""User Interface.""" """User Interface."""
def __init__(self): def __init__(self):
self.rows = 0 self.rows = 0
self.selected_device = None
gladefile = os.path.join(get_data_path(), 'key-mapper.glade') gladefile = os.path.join(get_data_path(), 'key-mapper.glade')
builder = Gtk.Builder() builder = Gtk.Builder()
@ -94,8 +95,11 @@ class Window:
self.populate_devices() self.populate_devices()
# find an select the newest preset based on file modification dates # 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): def get(self, name):
"""Get a widget from the window""" """Get a widget from the window"""
@ -112,32 +116,49 @@ class Window:
for (id, device) in devices: for (id, device) in devices:
device_selection.append(device, device) device_selection.append(device, device)
def populate_presets(self, device): def populate_presets(self):
"""Show the available presets for the selected device.""" """Show the available presets for the selected device."""
presets = get_presets(device) presets = get_presets(self.selected_device)
preset_selection = self.get('preset_selection') preset_selection = self.get('preset_selection')
for preset in presets: for preset in presets:
preset_selection.append(preset) preset_selection.append(preset, preset)
def on_select_device(self, dropdown): def on_select_device(self, device):
"""List all presets, create one if none exist yet. """List all presets, create one if none exist yet."""
""" if isinstance(device, Gtk.ComboBoxText):
device = dropdown.get_active_text() preset = device.get_active_text()
device = device.get_active_text()
presets = get_presets(device) presets = get_presets(device)
if len(presets) == 0: if len(presets) == 0:
create_preset(device) 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): 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 # 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 correct column width, otherwise it may jump if the user adds
# the first row. # 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() self.on_add_key_clicked()
def on_add_key_clicked(self, button=None): def on_add_key_clicked(self, button=None):

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

@ -74,6 +74,23 @@ def _modify_config(config_contents, key, value):
return '\n'.join(split) 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: class Config:
"""Read and set config values.""" """Read and set config values."""
def __init__(self, device, preset, path=None): 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 In that directory, a folder for the device and a file for
the preset will be created. the preset will be created.
""" """
if path is None: path = get_config_path(device, preset, path)
path = os.path.expanduser('~/.config/key-mapper/')
path = os.path.join(path, device, preset)
logger.debug('Using config file at %s', path) logger.debug('Using config file at %s', path)
self.device = device self.device = device

@ -26,7 +26,7 @@ import os
import subprocess import subprocess
from keymapper.logger import logger 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): def get_xinput_list(type):
@ -64,8 +64,7 @@ def get_presets(device):
---------- ----------
device : string device : string
""" """
print('get_presets not yet implemented') return os.listdir(get_config_path(device))
return []
def create_preset(device, name=None): def create_preset(device, name=None):
@ -82,6 +81,7 @@ def create_preset(device, name=None):
# trigger the creation of a new config file: # trigger the creation of a new config file:
get_config(device, name) get_config(device, name)
return name
def get_mappings(device, preset): def get_mappings(device, preset):

@ -91,8 +91,15 @@ class Integration(unittest.TestCase):
self.assertTrue(self.window.window.get_visible()) self.assertTrue(self.window.window.get_visible())
def test_select_device_and_preset(self): def test_select_device_and_preset(self):
self.window.on_select_device('fakeDevice1') class FakeDropdown:
self.window.on_select_preset('fakePreset1') 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 # TODO test meaningful stuff here

Loading…
Cancel
Save