# HG changeset patch # User Brad Greco # Date 1611799350 18000 # Node ID 11a9d346aa7a9621036d0cd1e7cc259e7b1a98c5 Sort of working model diff -r 000000000000 -r 11a9d346aa7a .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Wed Jan 27 21:02:30 2021 -0500 @@ -0,0 +1,5 @@ +syntax:glob + +*.pyc +.vscode +*~ diff -r 000000000000 -r 11a9d346aa7a keyboard_colors/__init__.py diff -r 000000000000 -r 11a9d346aa7a keyboard_colors/color_profile.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keyboard_colors/color_profile.py Wed Jan 27 21:02:30 2021 -0500 @@ -0,0 +1,65 @@ +import gi +import importlib +import json +import os +import pathlib +import sys +import uuid +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk +from gi.repository import GLib + +class ColorProfileBase: + + type_name = '(no name)' + + def __init__(self): + self.name = '' + self.id = uuid.uuid4().hex + + def build_settings_ui(self): + glade_file = sys.modules[self.__module__].__file__.replace('.py', '.glade') + builder = Gtk.Builder() + builder.add_from_file(glade_file) + return builder.get_object('edit_profile_container') + +class ProfileManager: + + def __init__(self): + self.profiles = self.load_profiles() + self.save_profiles() + + def get_types(self): + types = [] + profile_directory = os.path.dirname(os.path.realpath(__file__)) + (_, dirnames, _) = next(os.walk(profile_directory)) + for dirname in dirnames: + module_file = os.path.join(profile_directory, dirname, dirname + '.py') + if os.path.isfile(module_file): + profile_module = importlib.import_module('keyboard_colors.' + dirname + '.' + dirname) + profile_class = getattr(profile_module, 'ColorProfile') + types.append(profile_class) + return types + + def config_path(self): + dir = GLib.get_user_config_dir() + if not os.path.isdir(dir): + os.mkdir(dir) + return os.path.join(dir, 'keyboard-color-profiles.conf') + + def load_profiles(self): + 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: SimpleNamespace(**d)) + except json.decoder.JSONDecodeError: + return [] + else: + return [] + + def save_profiles(self): + data = json.dumps(self.profiles, default=lambda o: o.__dict__) + pathlib.Path(self.config_path()).write_text(data, 'utf-8') + + def get_profiles(self): + return self.profiles \ No newline at end of file diff -r 000000000000 -r 11a9d346aa7a keyboard_colors/custom/__init__.py diff -r 000000000000 -r 11a9d346aa7a keyboard_colors/custom/custom.glade --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keyboard_colors/custom/custom.glade Wed Jan 27 21:02:30 2021 -0500 @@ -0,0 +1,28 @@ + + + + + + True + False + vertical + + + True + False + label + + + False + True + 0 + + + + + + + + + + diff -r 000000000000 -r 11a9d346aa7a keyboard_colors/custom/custom.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/keyboard_colors/custom/custom.py Wed Jan 27 21:02:30 2021 -0500 @@ -0,0 +1,8 @@ +from keyboard_colors.color_profile import ColorProfileBase + +class ColorProfile(ColorProfileBase): + + type_name = 'Custom colors' + + def __init__(self): + super().__init__() \ No newline at end of file diff -r 000000000000 -r 11a9d346aa7a main.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.py Wed Jan 27 21:02:30 2021 -0500 @@ -0,0 +1,7 @@ +from ui.main_window import MainWindow + +def main(): + MainWindow().init() + +if __name__ == "__main__": + main() diff -r 000000000000 -r 11a9d346aa7a ui/MainWindow.glade --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/MainWindow.glade Wed Jan 27 21:02:30 2021 -0500 @@ -0,0 +1,270 @@ + + + + + + True + False + 7 + + + True + False + end + gtk-add + + + True + True + 0 + + + + + True + False + start + Add color profile + + + True + True + end + 1 + + + + + + + + + + + + + 1-2-3 + test name + + + + + 600 + 400 + False + True + + + + + + True + False + vertical + + + True + False + 5 + True + + + + + + False + True + 0 + + + + + True + True + + + True + False + vertical + + + True + True + False + False + color_profile_list_store + + + + + + Color profile + + + + 1 + + + + + + + False + True + 0 + + + + + + + + False + True + + + + + True + False + 0 + in + + + False + True + 10 + 10 + 10 + 10 + vertical + + + True + False + center + 9 + True + True + + + True + False + end + 1 + Type + + + 0 + 0 + + + + + True + False + end + Name + + + 0 + 1 + + + + + True + False + start + + + 1 + 0 + + + + + True + True + + + + + 1 + 1 + + + + + False + True + 0 + + + + + True + False + vertical + + + + + + True + True + 1 + + + + + True + False + True + start + + + gtk-delete + True + True + True + True + + + + True + True + 0 + + + + + False + True + 2 + + + + + + + + + + True + True + + + + + True + True + 1 + + + + + + diff -r 000000000000 -r 11a9d346aa7a ui/__init__.py diff -r 000000000000 -r 11a9d346aa7a ui/main_window.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/main_window.py Wed Jan 27 21:02:30 2021 -0500 @@ -0,0 +1,91 @@ +import gi +import os +gi.require_version("Gtk", "3.0") +from gi.repository import Gtk +from keyboard_colors.color_profile import ProfileManager +# from ui.keyboard_section import KeyboardSection + +class MainWindow: + + def init(self): + self.profile_manager = ProfileManager() + + self.builder = Gtk.Builder() + dirname = os.path.dirname(os.path.realpath(__file__)) + self.builder.add_from_file(os.path.join(dirname, 'MainWindow.glade')) + 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"]) + + 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(): + + menu = Gtk.Menu() + menu.set_halign(Gtk.Align.CENTER) + for profile_type in self.profile_manager.get_types(): + item = Gtk.MenuItem.new_with_label(profile_type.type_name) + item.connect('activate', self.create_profile, profile_type) + item.show() + menu.append(item) + add_button = Gtk.MenuButton() + add_button.set_popup(menu) + add_profile_button_contents = self.builder.get_object('add_profile_button_contents') + add_button.add(add_profile_button_contents) + + profile_pane = self.builder.get_object('profile_pane') + profile_pane.add(add_button) + + window.show_all() + Gtk.main() + + def create_profile(self, widget, profile_type): + 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.edit_profile(profile) + self.builder.get_object('edit_profile_name_entry').grab_focus() + + def edit_profile(self, profile): + self.builder.get_object('edit_profile_box').show() + + self.builder.get_object('edit_profile_type_name_label').set_text(profile.type_name) + self.builder.get_object('edit_profile_name_entry').set_text(profile.name) + + edit_profile_details_box = self.builder.get_object('edit_profile_details_box') + for child in edit_profile_details_box.get_children(): + edit_profile_details_box.remove(child) + edit_profile_details_box.add(profile.build_settings_ui()) + + 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) + + def edit_profile_name_entry_focus_out(self, entry, event): + pass + +class KeyboardSection(Gtk.Grid): + + def __init__(self, model): + super().__init__() + + profile_label = Gtk.Label('Color profile') + # profile_combo_box = Gtk.ComboBox(model) + profile_combo_box = Gtk.ComboBox.new_with_model(model) + # profile_combo_box.set_entry_text_column(1) + # profile_combo_box.append_text('asdf') + renderer_text = Gtk.CellRendererText() + profile_combo_box.pack_start(renderer_text, True) + profile_combo_box.add_attribute(renderer_text, 'text', 1) + + self.add(profile_label) + self.add(profile_combo_box)