Mercurial > system76-keyboard-colors
changeset 2:091a1f59a79c
Load profiles into list view
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Tue, 02 Mar 2021 20:17:36 -0500 |
parents | b6d0a1e6ba3a |
children | eb2aa09653bd |
files | keyboard_colors/color_profile.py ui/MainWindow.glade ui/main_window.py |
diffstat | 3 files changed, 51 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/keyboard_colors/color_profile.py Tue Feb 02 19:46:01 2021 -0500 +++ b/keyboard_colors/color_profile.py Tue Mar 02 20:17:36 2021 -0500 @@ -6,6 +6,7 @@ import sys import types import uuid +from types import SimpleNamespace gi.require_version("Gtk", "3.0") from gi.repository import Gtk # noqa: E402 from gi.repository import GLib # noqa: E402 @@ -17,7 +18,7 @@ def __init__(self): self.name = '' - self.id = uuid.uuid4().hex + self.id = str(uuid.uuid4()) def build_settings_ui(self): glade_file = sys.modules[self.__module__].__file__.replace('.py', '.glade') @@ -25,12 +26,25 @@ builder.add_from_file(glade_file) return builder.get_object('edit_profile_container') + def serialize(self): + properties = self.__dict__ + properties['module'] = self.__class__.__module__ + return properties + + @staticmethod + def unserialize(data): + profile_module = importlib.import_module(data['module']) + profile_class = getattr(profile_module, 'ColorProfile') + profile = profile_class() + profile.__dict__.update(data) + return profile + class ProfileManager: def __init__(self): - self.profiles = self.load_profiles() - self.save_profiles() + profile_list = self.load_profiles() + self.profiles = {p.id: p for p in profile_list} def get_types(self): types = [] @@ -55,15 +69,18 @@ if os.path.isfile(self.config_path()): data = pathlib.Path(self.config_path()).read_text('utf-8') try: - return json.loads(data, object_hook=lambda d: types.SimpleNamespace(**d)) + return json.loads(data, object_hook=lambda d: ColorProfileBase.unserialize(d)) except json.decoder.JSONDecodeError: return [] else: return [] def save_profiles(self): - data = json.dumps(self.profiles, default=lambda o: o.__dict__) + data = json.dumps(self.profiles, default=lambda o: o.serialize()) pathlib.Path(self.config_path()).write_text(data, 'utf-8') def get_profiles(self): return self.profiles + + def get_profile(self, profile_id): + return self.get_profiles()[profile_id]
--- a/ui/MainWindow.glade Tue Feb 02 19:46:01 2021 -0500 +++ b/ui/MainWindow.glade Tue Mar 02 20:17:36 2021 -0500 @@ -41,12 +41,6 @@ <!-- column-name name --> <column type="gchararray"/> </columns> - <data> - <row> - <col id="0" translatable="yes">1-2-3</col> - <col id="1" translatable="yes">test name</col> - </row> - </data> </object> <object class="GtkApplicationWindow" id="main_window"> <property name="width_request">600</property> @@ -94,7 +88,9 @@ <property name="vexpand">False</property> <property name="model">color_profile_list_store</property> <child internal-child="selection"> - <object class="GtkTreeSelection"/> + <object class="GtkTreeSelection" id="profile_tree_view_selection"> + <signal name="changed" handler="profile_tree_view_selection_changed" swapped="no"/> + </object> </child> <child> <object class="GtkTreeViewColumn">
--- a/ui/main_window.py Tue Feb 02 19:46:01 2021 -0500 +++ b/ui/main_window.py Tue Mar 02 20:17:36 2021 -0500 @@ -1,8 +1,8 @@ import gi import os -from gi.repository import Gtk from keyboard_colors.color_profile import ProfileManager gi.require_version("Gtk", "3.0") +from gi.repository import Gtk # from ui.keyboard_section import KeyboardSection @@ -10,6 +10,7 @@ def init(self): self.profile_manager = ProfileManager() + # self.profile_manager.save_profiles() self.builder = Gtk.Builder() dirname = os.path.dirname(os.path.realpath(__file__)) @@ -17,15 +18,19 @@ self.builder.connect_signals(self) window = self.builder.get_object('main_window') - color_profile_list_store = self.builder.get_object('color_profile_list_store') - # color_profile_list_store.append(["1", "test"]) + self.color_profile_list_store = self.builder.get_object('color_profile_list_store') + self.load_color_profiles() keyboard_section = self.builder.get_object('keyboard_section') - keyboard_section.pack_start(KeyboardSection(color_profile_list_store), False, False, 0) - keyboard_section.pack_start(KeyboardSection(color_profile_list_store), False, False, 0) - keyboard_section.pack_start(KeyboardSection(color_profile_list_store), False, False, 0) - - # for profile_id, profile_type in self.profile_manager.get_types().items(): + keyboard_section.pack_start( + KeyboardSection(self.color_profile_list_store), False, False, 0 + ) + keyboard_section.pack_start( + KeyboardSection(self.color_profile_list_store), False, False, 0 + ) + keyboard_section.pack_start( + KeyboardSection(self.color_profile_list_store), False, False, 0 + ) menu = Gtk.Menu() menu.set_halign(Gtk.Align.CENTER) @@ -46,10 +51,12 @@ Gtk.main() def create_profile(self, widget, profile_type): + # todo move to profile manager profile = profile_type() - color_profile_list_store = self.builder.get_object('color_profile_list_store') - color_profile_list_store.append([profile.id, 'New ' + profile.type_name]) - self.builder.get_object('profile_tree_view').set_cursor(len(color_profile_list_store) - 1) + self.color_profile_list_store.append([profile.id, 'New ' + profile.type_name]) + self.builder.get_object('profile_tree_view').set_cursor( + len(self.color_profile_list_store) - 1 + ) self.edit_profile(profile) self.builder.get_object('edit_profile_name_entry').grab_focus() @@ -64,9 +71,13 @@ edit_profile_details_box.remove(child) edit_profile_details_box.add(profile.build_settings_ui()) + def profile_tree_view_selection_changed(self, selection): + (model, tree_iter) = selection.get_selected() + profile_id = model.get_value(tree_iter, 0) + self.edit_profile(self.profile_manager.get_profile(profile_id)) + def edit_profile_name_changed(self, entry): profile_name = entry.get_text() - color_profile_list_store = self.builder.get_object('color_profile_list_store') selection = self.builder.get_object('profile_tree_view').get_selection() (model, tree_iter) = selection.get_selected() model.set_value(tree_iter, 1, profile_name) @@ -74,6 +85,9 @@ def edit_profile_name_entry_focus_out(self, entry, event): pass + def load_color_profiles(self): + for profile in self.profile_manager.get_profiles().values(): + self.color_profile_list_store.append([profile.id, profile.name]) class KeyboardSection(Gtk.Grid):