From fd882743b871732df2b04ffc191a16828f3f88bd Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sun, 9 Apr 2023 17:33:07 -0500 Subject: [PATCH] Further loading/saving work --- src/core/widgets/drag_area_widget.py | 10 --- .../widgets/template/dynamic_header_widget.py | 9 ++- .../template/dynamic_widget_template.py | 72 ++++++++++++++----- .../widgets/template/tab_header_template.py | 4 +- .../widget_selector/widgets/text_area.py | 9 ++- src/utils/widget_save_load_controller.py | 3 + 6 files changed, 74 insertions(+), 33 deletions(-) diff --git a/src/core/widgets/drag_area_widget.py b/src/core/widgets/drag_area_widget.py index 4c91329..af52805 100644 --- a/src/core/widgets/drag_area_widget.py +++ b/src/core/widgets/drag_area_widget.py @@ -43,22 +43,12 @@ class DragArea(Gtk.Fixed): def wrap_widget_in_trap(self, widget): eve_trap_box = Gtk.EventBox() - - eve_trap_box.connect("enter-notify-event", self._enter_event) - eve_trap_box.connect("motion-notify-event", self._motion_event) eve_trap_box.connect("button-release-event", self._release_event) - eve_trap_box.add(widget) eve_trap_box.show() return eve_trap_box - def _enter_event(self, widget = None, eve = None): - ... - - def _motion_event(self, widget = None, eve = None): - ... - def _release_event(self, widget = None, eve = None): return True diff --git a/src/core/widgets/template/dynamic_header_widget.py b/src/core/widgets/template/dynamic_header_widget.py index 52e426b..698c906 100644 --- a/src/core/widgets/template/dynamic_header_widget.py +++ b/src/core/widgets/template/dynamic_header_widget.py @@ -59,13 +59,18 @@ class DynamicHeaderWidget(Gtk.Box): drag_eve.connect("button-press-event", self._press_event) drag_eve.connect("motion-notify-event", self._motion_event) drag_eve.connect("button-release-event", self._release_event) + drag_eve.connect("key-release-event", self._key_release_event) self.add(drag_eve) self.add(hresize_eve) + def _key_release_event(self, widget = None, eve = None): + if eve.hardware_keycode == 119: # NOTE: If Delete key + self.get_parent().delete() + + def _press_event(self, widget = None, eve = None): window = settings.get_main_window() - cursor = None cursor = Gdk.Cursor(Gdk.CursorType.CROSSHAIR) if widget.get_label() == "..." else Gdk.Cursor(Gdk.CursorType.SB_H_DOUBLE_ARROW) window.get_window().set_cursor(cursor) @@ -81,6 +86,7 @@ class DynamicHeaderWidget(Gtk.Box): self._current_w = -1 self.set_size_request(self._current_w, self._current_h) + self.get_parent().save_needed = True def _motion_event(self, widget = None, eve = None): if self._update_block: @@ -97,6 +103,7 @@ class DynamicHeaderWidget(Gtk.Box): self._update_block = True self._move_callback(self.get_parent(), self._current_x, self._current_y) + self.get_parent().save_needed = True self._update_block = False def _release_event(self, widget = None, eve = None): diff --git a/src/core/widgets/template/dynamic_widget_template.py b/src/core/widgets/template/dynamic_widget_template.py index 4e2510d..329a1b9 100644 --- a/src/core/widgets/template/dynamic_widget_template.py +++ b/src/core/widgets/template/dynamic_widget_template.py @@ -1,4 +1,5 @@ # Python imports +import os import uuid import pickle @@ -6,6 +7,7 @@ import pickle import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk +from gi.repository import GLib # Application imports from .dynamic_header_widget import DynamicHeaderWidget @@ -23,15 +25,25 @@ class DynamicWidget(Gtk.Box): self.uuid = str(uuid.uuid4()) self._header_widget = DynamicHeaderWidget(move_callback, x, y) self._body_widget = None + self.save_needed = False + self.is_not_stop_watcher = True + self.is_self_destroyed = False self._setup_styling() self._setup_signals() self._subscribe_to_events() self._load_widgets() if not path else self.load_from_path(path) - self.show_all() + GLib.timeout_add(5000, self._save_watcher, GLib.PRIORITY_LOW) + + def _save_watcher(self, widget = None, eve = None): + if self.save_needed: + self.save() + self.save_needed = False + + return self.is_not_stop_watcher # NOTE: Must be true to continue idle timeout calls def _setup_styling(self): self.set_orientation(1) @@ -55,6 +67,7 @@ class DynamicWidget(Gtk.Box): self.add(self._body_widget) except DynamicWidgetException as e: logger.debug(e) + raise Exception("No widget loaded!") def get_header(self): return self._header_widget @@ -68,24 +81,47 @@ class DynamicWidget(Gtk.Box): event_system.emit("set_widget_type", save_collection["widget_type"]) self._load_widgets() + self._load_collection_data(save_collection) - if not self._body_widget: - raise Exception("No widget loaded from path!") - else: - self._header_widget._current_w = save_collection["w"] - self._header_widget._current_h = save_collection["h"] - self._header_widget._current_x = save_collection["x"] - self._header_widget._current_y = save_collection["y"] - self._body_widget.set_saveable_data(save_collection) - self._body_widget.load_saveable_data() + def _load_collection_data(self, save_collection): + self.uuid = save_collection["uuid"] + self._header_widget._current_w = save_collection["w"] + self._header_widget._current_h = save_collection["h"] + self._header_widget._current_x = save_collection["x"] + self._header_widget._current_y = save_collection["y"] + self._header_widget.set_size_request(self._header_widget._current_w, self._header_widget._current_h) + + self._body_widget.set_saveable_data(save_collection) + self._body_widget.load_saveable_data() + + def delete(self): + path = f"{settings.get_active_page()}{self.uuid}" + logger.debug(f"Deleteing: {path}") + + try: + if os.path.exists(path): + if os.path.isfile(path): + os.remove(path) + except Exception as e: + logger.debug(f"Could not delete: {path}\n{e}") + return + + self.is_self_destroyed = True + self.is_not_stop_watcher = False + self._header_widget.destroy() + self._body_widget.destroy() + self.destroy() def save(self): - save_collection = self._body_widget.get_saveable_data() - save_collection["w"] = self._header_widget._current_w - save_collection["h"] = self._header_widget._current_h - save_collection["x"] = self._header_widget._current_x - save_collection["y"] = self._header_widget._current_y + if not self.is_self_destroyed: + save_collection = self._body_widget.get_saveable_data() + save_collection["uuid"] = self.uuid + save_collection["w"] = self._header_widget._current_w + save_collection["h"] = self._header_widget._current_h + save_collection["x"] = self._header_widget._current_x + save_collection["y"] = self._header_widget._current_y - path = f"{settings.get_active_page()}/{self.uuid}" - with open(path, "wb") as f: - pickle.dump(save_collection, f, protocol = pickle.HIGHEST_PROTOCOL, fix_imports = True, buffer_callback = None) + path = f"{settings.get_active_page()}{self.uuid}" + logger.debug(f"Saving: {path}") + with open(path, "wb") as f: + pickle.dump(save_collection, f, protocol = pickle.HIGHEST_PROTOCOL, fix_imports = True, buffer_callback = None) diff --git a/src/core/widgets/template/tab_header_template.py b/src/core/widgets/template/tab_header_template.py index accfabc..712bab9 100644 --- a/src/core/widgets/template/tab_header_template.py +++ b/src/core/widgets/template/tab_header_template.py @@ -70,7 +70,7 @@ class TabHeaderTemplate(Gtk.Box): self.add(close) def set_tab_label(self, label = "untitled"): - self.get_children()[0].set_label(label) + self.get_children()[0].get_children()[0].set_label(label) def get_tab_label(self): - return self.get_children()[0].get_text() + return self.get_children()[0].get_children()[0].get_text() diff --git a/src/core/widgets/widget_selector/widgets/text_area.py b/src/core/widgets/widget_selector/widgets/text_area.py index e4cf136..c80bf0e 100644 --- a/src/core/widgets/widget_selector/widgets/text_area.py +++ b/src/core/widgets/widget_selector/widgets/text_area.py @@ -3,13 +3,15 @@ # Lib imports import gi gi.require_version('Gtk', '3.0') +gi.require_version('GtkSource', '4') from gi.repository import Gtk +from gi.repository import GtkSource # Application imports from utils.widget_save_load_controller import WidgetSaveLoadController -class TextAreaWidget(WidgetSaveLoadController, Gtk.TextView): +class TextAreaWidget(WidgetSaveLoadController, GtkSource.View): def __init__(self): super(TextAreaWidget, self).__init__() @@ -27,7 +29,7 @@ class TextAreaWidget(WidgetSaveLoadController, Gtk.TextView): ... def _setup_signals(self): - ... + self.connect("key-release-event", self._key_released) def _subscribe_to_events(self): ... @@ -35,6 +37,9 @@ class TextAreaWidget(WidgetSaveLoadController, Gtk.TextView): def _load_widgets(self): ... + def _key_released(self, widget = None, eve = None): + self.get_parent().save_needed = True + def get_buffer_text(self): start, end = self.buffer.get_start_iter(), self.buffer.get_end_iter() return self.buffer.get_slice(start, end, True) diff --git a/src/utils/widget_save_load_controller.py b/src/utils/widget_save_load_controller.py index 506e86d..823d933 100644 --- a/src/utils/widget_save_load_controller.py +++ b/src/utils/widget_save_load_controller.py @@ -1,6 +1,8 @@ # Python imports # Lib imports +import gi +from gi.repository import GLib # Application imports @@ -15,6 +17,7 @@ class WidgetSaveLoadController: super(WidgetSaveLoadController, self).__init__() self.saveable_data = [] self.save_collection = { + "uuid": "", "width": -1, "height": -1, "x": 0,