# HG changeset patch # User Brad Greco # Date 1614734256 18000 # Node ID 091a1f59a79c4225dc296a1ef78d0fc232173885 # Parent b6d0a1e6ba3ad08359d18f7d16f697b74d41c976 Load profiles into list view diff -r b6d0a1e6ba3a -r 091a1f59a79c keyboard_colors/color_profile.py --- 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] diff -r b6d0a1e6ba3a -r 091a1f59a79c ui/MainWindow.glade --- 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 @@ - - - 1-2-3 - test name - - 600 @@ -94,7 +88,9 @@ False color_profile_list_store - + + + diff -r b6d0a1e6ba3a -r 091a1f59a79c ui/main_window.py --- 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):