Coherence/src/core/widgets/template/dynamic_widget_template.py

128 lines
4.1 KiB
Python
Raw Normal View History

2023-04-03 04:42:09 +00:00
# Python imports
2023-04-09 22:33:07 +00:00
import os
2023-04-07 03:45:02 +00:00
import uuid
import pickle
2023-04-03 04:42:09 +00:00
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
2023-04-09 22:33:07 +00:00
from gi.repository import GLib
2023-04-03 04:42:09 +00:00
# Application imports
from .dynamic_header_widget import DynamicHeaderWidget
2023-04-05 22:35:23 +00:00
class DynamicWidgetException(Exception):
...
2023-04-03 04:42:09 +00:00
class DynamicWidget(Gtk.Box):
2023-04-08 03:39:49 +00:00
def __init__(self, move_callback, path = None, x = None, y = None):
2023-04-03 04:42:09 +00:00
super(DynamicWidget, self).__init__()
2023-04-07 03:45:02 +00:00
self.uuid = str(uuid.uuid4())
self._header_widget = DynamicHeaderWidget(move_callback, x, y)
self._body_widget = None
2023-04-09 22:33:07 +00:00
self.save_needed = False
self.is_not_stop_watcher = True
self.is_self_destroyed = False
2023-04-03 04:42:09 +00:00
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
2023-04-08 03:39:49 +00:00
self._load_widgets() if not path else self.load_from_path(path)
2023-04-03 04:42:09 +00:00
self.show_all()
2023-04-09 22:33:07 +00:00
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
2023-04-03 04:42:09 +00:00
def _setup_styling(self):
self.set_orientation(1)
2023-04-05 03:23:07 +00:00
self.set_size_request(256, 45)
2023-04-03 04:42:09 +00:00
ctx = self.get_style_context()
ctx.add_class("dynamic-widget")
def _setup_signals(self):
...
def _subscribe_to_events(self):
...
def _load_widgets(self):
2023-04-05 22:35:23 +00:00
try:
self._body_widget = event_system.emit_and_await("get_widget_type")
if not self._body_widget:
2023-04-05 22:35:23 +00:00
raise Exception("No widget detected... Will not insert anything.")
2023-04-08 03:39:49 +00:00
self.add(self._header_widget)
self.add(self._body_widget)
2023-04-05 22:35:23 +00:00
except DynamicWidgetException as e:
logger.debug(e)
2023-04-09 22:33:07 +00:00
raise Exception("No widget loaded!")
2023-04-07 03:45:02 +00:00
2023-04-08 03:39:49 +00:00
def get_header(self):
return self._header_widget
def get_body(self):
return self._body_widget
def load_from_path(self, path):
with open(path, "rb") as f:
save_collection = pickle.load(f)
event_system.emit("set_widget_type", save_collection["widget_type"])
self._load_widgets()
2023-04-09 22:33:07 +00:00
self._load_collection_data(save_collection)
2023-04-08 03:39:49 +00:00
2023-04-09 22:33:07 +00:00
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()
2023-04-08 03:39:49 +00:00
2023-04-07 03:45:02 +00:00
def save(self):
2023-04-09 22:33:07 +00:00
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}"
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)