#!/usr/bin/python3 # -*- coding: utf-8 -*- # key-mapper - GUI for device specific keyboard mappings # Copyright (C) 2020 sezanzeb # # This file is part of key-mapper. # # key-mapper is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # key-mapper is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with key-mapper. If not, see . """User Interface.""" import os import sys from argparse import ArgumentParser import gi gi.require_version('Gtk', '3.0') gi.require_version('GLib', '2.0') from gi.repository import Gtk from keymapper.data import get_data_path from keymapper.profiles import find_devices, get_presets, get_mappings from keymapper.logger import logger, update_verbosity, log_info class SingleKeyMapping: """A single, configurable key mapping.""" box = None def __init__(self, delete_callback): """Construct a row and add it to the list in the GUI.""" self.delete_callback = delete_callback self.put_together() def get_widget(self): """Return the widget that wraps all the widgets of the row.""" return self.box def put_together(self): """Create all GTK widgets.""" delete_button = Gtk.Button() destroy_icon = Gtk.Image.new_from_icon_name( 'window-close', Gtk.IconSize.BUTTON ) delete_button.set_image(destroy_icon) delete_button.connect('clicked', self.on_delete_button_clicked) key_code = Gtk.Entry() key_code.set_width_chars(4) original_key = Gtk.Entry() original_key.set_width_chars(4) box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10) box.pack_start(delete_button, expand=False, fill=False, padding=0) box.pack_start(key_code, expand=True, fill=True, padding=0) box.pack_start(original_key, expand=True, fill=True, padding=0) box.set_margin_start(10) box.set_margin_end(10) box.show_all() self.box = box def on_delete_button_clicked(self, button): """Destroy the row and remove it from the config.""" self.box.destroy() self.delete_callback(self) class Window: """User Interface.""" def __init__(self): gladefile = os.path.join(get_data_path(), 'key-mapper.glade') builder = Gtk.Builder() builder.add_from_file(gladefile) builder.connect_signals(self) self.builder = builder window = builder.get_object('window') window.show() self.window = window self.populate_devices() def get(self, name): """Get a widget from the window""" return self.builder.get_object(name) def on_close(self, *_): """Safely close the application.""" Gtk.main_quit() def populate_devices(self): """Make the devices selectable.""" devices = find_devices() device_selection = self.get('device_selection') for (id, device) in devices: device_selection.append(device, device) def populate_profiles(self): """Show the available profiles for the selected device.""" def on_add_key_clicked(self, button): """Add a mapping to the list of mappings.""" single_key_mapping = SingleKeyMapping(self.on_row_removed) self.get('key_list').pack_start( single_key_mapping.get_widget(), expand=False, fill=False, padding=0 ) def on_row_removed(self, mapping): """Stuff to do when a row was removed Parameters ---------- mapping : SingleKeyMapping """ # shrink the window down as much as possible, otherwise it # will increase with each added mapping but won't go back when they # are removed. window = self.get('window') window.resize(window.get_size()[0], 1) if __name__ == '__main__': parser = ArgumentParser() parser.add_argument( '-d', '--debug', action='store_true', dest='debug', help='Displays additional debug information', default=False ) options = parser.parse_args(sys.argv[1:]) update_verbosity(options.debug) log_info() Window() Gtk.main()