generated from itdominator/Python-With-Gtk-Template
WIP loading page data
This commit is contained in:
parent
8ccb8f0709
commit
c2655dd4b3
@ -41,34 +41,6 @@ class DragArea(GtkSource.View):
|
||||
def _load_widgets(self):
|
||||
...
|
||||
|
||||
def add_fixed_base_widgets(self, name = None, date = None, update_header_callback = None):
|
||||
entry = Gtk.Entry(text = name)
|
||||
wrapped_entry = self.wrap_widget_in_trap( entry )
|
||||
date_label = Gtk.Label(label = date)
|
||||
wrapped_date = self.wrap_widget_in_trap( date_label )
|
||||
|
||||
ctx = entry.get_style_context()
|
||||
ctx.add_class("drag-area-title")
|
||||
|
||||
entry.connect("changed", update_header_callback)
|
||||
entry.set_width_chars(25)
|
||||
entry.set_max_length(25)
|
||||
|
||||
# self.put(wrapped_entry, 30, 30)
|
||||
# self.put(wrapped_date, 30, 65)
|
||||
self.add_child_in_window(wrapped_entry, Gtk.TextWindowType.WIDGET, 30, 30)
|
||||
self.add_child_in_window(wrapped_date, Gtk.TextWindowType.WIDGET, 30, 65)
|
||||
|
||||
def add_or_select_widget(self, widget = None, eve = None):
|
||||
dynamic_widget = DynamicWidget(self._move_callback, eve.x, eve.y)
|
||||
dynamic_widget_wrapped = self.wrap_widget_in_trap(dynamic_widget)
|
||||
|
||||
self._children_content.append(dynamic_widget)
|
||||
# self.put(dynamic_widget, eve.x, eve.y)
|
||||
self.add_child_in_window(dynamic_widget_wrapped, Gtk.TextWindowType.WIDGET, eve.x, eve.y)
|
||||
|
||||
dynamic_widget.save()
|
||||
|
||||
def _move_callback(self, widget = None, x = None, y = None):
|
||||
# self.move(widget.get_parent(), x, y)
|
||||
self.move_child(widget.get_parent(), x, y)
|
||||
@ -94,3 +66,40 @@ class DragArea(GtkSource.View):
|
||||
|
||||
def _release_event(self, widget = None, eve = None):
|
||||
return True
|
||||
|
||||
def _handle_add(self, widget, x, y):
|
||||
widget_wrapped = self.wrap_widget_in_trap(widget)
|
||||
|
||||
self._children_content.append(widget)
|
||||
# self.put(widget_wrapped, x, y)
|
||||
self.add_child_in_window(widget_wrapped, Gtk.TextWindowType.WIDGET, x, y)
|
||||
|
||||
|
||||
def add_fixed_base_widgets(self, name = None, date = None, update_header_callback = None):
|
||||
entry = Gtk.Entry(text = name)
|
||||
wrapped_entry = self.wrap_widget_in_trap( entry )
|
||||
date_label = Gtk.Label(label = date)
|
||||
wrapped_date = self.wrap_widget_in_trap( date_label )
|
||||
|
||||
ctx = entry.get_style_context()
|
||||
ctx.add_class("drag-area-title")
|
||||
|
||||
entry.connect("changed", update_header_callback)
|
||||
entry.set_width_chars(25)
|
||||
entry.set_max_length(25)
|
||||
|
||||
# self.put(wrapped_entry, 30, 30)
|
||||
# self.put(wrapped_date, 30, 65)
|
||||
self.add_child_in_window(wrapped_entry, Gtk.TextWindowType.WIDGET, 30, 30)
|
||||
self.add_child_in_window(wrapped_date, Gtk.TextWindowType.WIDGET, 30, 65)
|
||||
|
||||
def add_or_select_widget(self, widget = None, eve = None):
|
||||
dynamic_widget = DynamicWidget(self._move_callback, None, eve.x, eve.y)
|
||||
self._handle_add(dynamic_widget, eve.x, eve.y)
|
||||
dynamic_widget.save()
|
||||
|
||||
def load_path_to_widget(self, path):
|
||||
dynamic_widget = DynamicWidget(self._move_callback, path)
|
||||
x = dynamic_widget.get_header()._current_x
|
||||
y = dynamic_widget.get_header()._current_y
|
||||
self._handle_add(dynamic_widget, x, y)
|
||||
|
@ -1,4 +1,5 @@
|
||||
# Python imports
|
||||
import os
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
@ -31,7 +32,9 @@ class Page(Gtk.ScrolledWindow):
|
||||
self._load_widgets()
|
||||
|
||||
self._load_page_manifest()
|
||||
self._load_widgets_to_page()
|
||||
self._add_fixed_base_widgets()
|
||||
|
||||
self._tab_widget = PagesTabWidget(self, self._close_tab, self._name)
|
||||
|
||||
|
||||
@ -79,6 +82,13 @@ class Page(Gtk.ScrolledWindow):
|
||||
def _add_fixed_base_widgets(self):
|
||||
self._drag_area.add_fixed_base_widgets(self._name, self._date, self._update_header)
|
||||
|
||||
def _load_widgets_to_page(self):
|
||||
path = self._manifest_pth.replace("MANIFEST", "")
|
||||
files = settings.clean_pages_list( os.listdir( path ) )
|
||||
for file in files:
|
||||
fpath = os.path.join(path, file)
|
||||
self._drag_area.load_path_to_widget(fpath)
|
||||
|
||||
def _update_header(self, widget = None, eve = None):
|
||||
self._name = widget.get_text()
|
||||
self._tab_widget.set_tab_label(self._name)
|
||||
|
@ -17,7 +17,7 @@ class DynamicWidgetException(Exception):
|
||||
|
||||
|
||||
class DynamicWidget(Gtk.Box):
|
||||
def __init__(self, move_callback, x, y):
|
||||
def __init__(self, move_callback, path = None, x = None, y = None):
|
||||
super(DynamicWidget, self).__init__()
|
||||
|
||||
self.uuid = str(uuid.uuid4())
|
||||
@ -27,7 +27,8 @@ class DynamicWidget(Gtk.Box):
|
||||
self._setup_styling()
|
||||
self._setup_signals()
|
||||
self._subscribe_to_events()
|
||||
self._load_widgets()
|
||||
|
||||
self._load_widgets() if not path else self.load_from_path(path)
|
||||
|
||||
self.show_all()
|
||||
|
||||
@ -46,15 +47,38 @@ class DynamicWidget(Gtk.Box):
|
||||
|
||||
def _load_widgets(self):
|
||||
try:
|
||||
self.add(self._header_widget)
|
||||
self._body_widget = event_system.emit_and_await("get_widget_type")
|
||||
if not self._body_widget:
|
||||
raise Exception("No widget detected... Will not insert anything.")
|
||||
|
||||
self.add(self._header_widget)
|
||||
self.add(self._body_widget)
|
||||
except DynamicWidgetException as e:
|
||||
logger.debug(e)
|
||||
|
||||
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()
|
||||
|
||||
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 save(self):
|
||||
save_collection = self._body_widget.get_saveable_data()
|
||||
save_collection["w"] = self._header_widget._current_w
|
||||
@ -64,4 +88,4 @@ class DynamicWidget(Gtk.Box):
|
||||
|
||||
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)
|
||||
pickle.dump(save_collection, f, protocol = pickle.HIGHEST_PROTOCOL, fix_imports = True, buffer_callback = None)
|
||||
|
@ -68,13 +68,17 @@ class WidgetSelectorGrid(Gtk.Grid):
|
||||
|
||||
def _get_widget_type(self):
|
||||
logger.debug(self._seleced_widget_type)
|
||||
return self._seleced_widget_type.new()
|
||||
try:
|
||||
return self._seleced_widget_type().new()
|
||||
except Exception:
|
||||
return self._seleced_widget_type.new()
|
||||
|
||||
def _set_widget_type_eve(self, widget = None, eve = None):
|
||||
self._seleced_widget_type = widget.get_children()[0]
|
||||
|
||||
def _set_widget_type(self, type):
|
||||
self._seleced_widget_type = type
|
||||
widget = globals()[type] if isinstance(type, str) else type
|
||||
self._seleced_widget_type = widget
|
||||
|
||||
|
||||
class WidgetSelector(Gtk.Popover):
|
||||
|
@ -39,15 +39,18 @@ class TextAreaWidget(WidgetSaveLoadController, Gtk.TextView):
|
||||
start, end = self.buffer.get_start_iter(), self.buffer.get_end_iter()
|
||||
return self.buffer.get_slice(start, end, True)
|
||||
|
||||
def load_saveable_data(self, data):
|
||||
self.save_collection = data
|
||||
self.buffer.set_text(self.save_collection["data"])
|
||||
|
||||
def get_saveable_data(self):
|
||||
self.save_collection["data"] = self.get_buffer_text()
|
||||
self.save_collection["widget_type"] = str(type(self).__name__)
|
||||
return self.save_collection
|
||||
|
||||
def set_saveable_data(self, data):
|
||||
self.save_collection = data
|
||||
self.load_saveable_data()
|
||||
|
||||
def load_saveable_data(self):
|
||||
self.buffer.set_text(self.save_collection["data"])
|
||||
|
||||
def new(self):
|
||||
widget = TextAreaWidget()
|
||||
widget.buffer.set_text("Lorem ipsum dolor")
|
||||
|
@ -158,7 +158,7 @@ class Settings(StartCheckMixin):
|
||||
|
||||
return manifest
|
||||
|
||||
def clean_pages_list(self, pages):
|
||||
def clean_pages_list(self, pages = []):
|
||||
for i, page in enumerate(pages):
|
||||
if page == "MANIFEST":
|
||||
del pages[i]
|
||||
|
@ -26,8 +26,11 @@ class WidgetSaveLoadController:
|
||||
def register_saveable_data(self, data):
|
||||
self.saveable_data.append(data)
|
||||
|
||||
def load_saveable_data(self, data):
|
||||
def set_saveable_data(self, data):
|
||||
self.save_collection = data
|
||||
|
||||
def load_saveable_data(self):
|
||||
raise SaveLoadControllerException("WidgetSaveLoadController: load_saveable_data needs to be overridden...")
|
||||
|
||||
def get_saveable_data(self):
|
||||
return self.save_collection
|
||||
|
Loading…
Reference in New Issue
Block a user