generated from itdominator/Python-With-Gtk-Template
Further loading/saving work
This commit is contained in:
parent
6daeabe66e
commit
fd882743b8
|
@ -43,22 +43,12 @@ class DragArea(Gtk.Fixed):
|
||||||
|
|
||||||
def wrap_widget_in_trap(self, widget):
|
def wrap_widget_in_trap(self, widget):
|
||||||
eve_trap_box = Gtk.EventBox()
|
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.connect("button-release-event", self._release_event)
|
||||||
|
|
||||||
eve_trap_box.add(widget)
|
eve_trap_box.add(widget)
|
||||||
eve_trap_box.show()
|
eve_trap_box.show()
|
||||||
|
|
||||||
return eve_trap_box
|
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):
|
def _release_event(self, widget = None, eve = None):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -59,13 +59,18 @@ class DynamicHeaderWidget(Gtk.Box):
|
||||||
drag_eve.connect("button-press-event", self._press_event)
|
drag_eve.connect("button-press-event", self._press_event)
|
||||||
drag_eve.connect("motion-notify-event", self._motion_event)
|
drag_eve.connect("motion-notify-event", self._motion_event)
|
||||||
drag_eve.connect("button-release-event", self._release_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(drag_eve)
|
||||||
self.add(hresize_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):
|
def _press_event(self, widget = None, eve = None):
|
||||||
window = settings.get_main_window()
|
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)
|
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)
|
window.get_window().set_cursor(cursor)
|
||||||
|
@ -81,6 +86,7 @@ class DynamicHeaderWidget(Gtk.Box):
|
||||||
self._current_w = -1
|
self._current_w = -1
|
||||||
|
|
||||||
self.set_size_request(self._current_w, self._current_h)
|
self.set_size_request(self._current_w, self._current_h)
|
||||||
|
self.get_parent().save_needed = True
|
||||||
|
|
||||||
def _motion_event(self, widget = None, eve = None):
|
def _motion_event(self, widget = None, eve = None):
|
||||||
if self._update_block:
|
if self._update_block:
|
||||||
|
@ -97,6 +103,7 @@ class DynamicHeaderWidget(Gtk.Box):
|
||||||
|
|
||||||
self._update_block = True
|
self._update_block = True
|
||||||
self._move_callback(self.get_parent(), self._current_x, self._current_y)
|
self._move_callback(self.get_parent(), self._current_x, self._current_y)
|
||||||
|
self.get_parent().save_needed = True
|
||||||
self._update_block = False
|
self._update_block = False
|
||||||
|
|
||||||
def _release_event(self, widget = None, eve = None):
|
def _release_event(self, widget = None, eve = None):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# Python imports
|
# Python imports
|
||||||
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
|
@ -6,6 +7,7 @@ import pickle
|
||||||
import gi
|
import gi
|
||||||
gi.require_version('Gtk', '3.0')
|
gi.require_version('Gtk', '3.0')
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
|
from gi.repository import GLib
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
from .dynamic_header_widget import DynamicHeaderWidget
|
from .dynamic_header_widget import DynamicHeaderWidget
|
||||||
|
@ -23,15 +25,25 @@ class DynamicWidget(Gtk.Box):
|
||||||
self.uuid = str(uuid.uuid4())
|
self.uuid = str(uuid.uuid4())
|
||||||
self._header_widget = DynamicHeaderWidget(move_callback, x, y)
|
self._header_widget = DynamicHeaderWidget(move_callback, x, y)
|
||||||
self._body_widget = None
|
self._body_widget = None
|
||||||
|
self.save_needed = False
|
||||||
|
self.is_not_stop_watcher = True
|
||||||
|
self.is_self_destroyed = False
|
||||||
|
|
||||||
self._setup_styling()
|
self._setup_styling()
|
||||||
self._setup_signals()
|
self._setup_signals()
|
||||||
self._subscribe_to_events()
|
self._subscribe_to_events()
|
||||||
|
|
||||||
self._load_widgets() if not path else self.load_from_path(path)
|
self._load_widgets() if not path else self.load_from_path(path)
|
||||||
|
|
||||||
self.show_all()
|
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):
|
def _setup_styling(self):
|
||||||
self.set_orientation(1)
|
self.set_orientation(1)
|
||||||
|
@ -55,6 +67,7 @@ class DynamicWidget(Gtk.Box):
|
||||||
self.add(self._body_widget)
|
self.add(self._body_widget)
|
||||||
except DynamicWidgetException as e:
|
except DynamicWidgetException as e:
|
||||||
logger.debug(e)
|
logger.debug(e)
|
||||||
|
raise Exception("No widget loaded!")
|
||||||
|
|
||||||
def get_header(self):
|
def get_header(self):
|
||||||
return self._header_widget
|
return self._header_widget
|
||||||
|
@ -68,24 +81,47 @@ class DynamicWidget(Gtk.Box):
|
||||||
event_system.emit("set_widget_type", save_collection["widget_type"])
|
event_system.emit("set_widget_type", save_collection["widget_type"])
|
||||||
|
|
||||||
self._load_widgets()
|
self._load_widgets()
|
||||||
|
self._load_collection_data(save_collection)
|
||||||
|
|
||||||
if not self._body_widget:
|
def _load_collection_data(self, save_collection):
|
||||||
raise Exception("No widget loaded from path!")
|
self.uuid = save_collection["uuid"]
|
||||||
else:
|
self._header_widget._current_w = save_collection["w"]
|
||||||
self._header_widget._current_w = save_collection["w"]
|
self._header_widget._current_h = save_collection["h"]
|
||||||
self._header_widget._current_h = save_collection["h"]
|
self._header_widget._current_x = save_collection["x"]
|
||||||
self._header_widget._current_x = save_collection["x"]
|
self._header_widget._current_y = save_collection["y"]
|
||||||
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()
|
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):
|
def save(self):
|
||||||
save_collection = self._body_widget.get_saveable_data()
|
if not self.is_self_destroyed:
|
||||||
save_collection["w"] = self._header_widget._current_w
|
save_collection = self._body_widget.get_saveable_data()
|
||||||
save_collection["h"] = self._header_widget._current_h
|
save_collection["uuid"] = self.uuid
|
||||||
save_collection["x"] = self._header_widget._current_x
|
save_collection["w"] = self._header_widget._current_w
|
||||||
save_collection["y"] = self._header_widget._current_y
|
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}"
|
path = f"{settings.get_active_page()}{self.uuid}"
|
||||||
with open(path, "wb") as f:
|
logger.debug(f"Saving: {path}")
|
||||||
pickle.dump(save_collection, f, protocol = pickle.HIGHEST_PROTOCOL, fix_imports = True, buffer_callback = None)
|
with open(path, "wb") as f:
|
||||||
|
pickle.dump(save_collection, f, protocol = pickle.HIGHEST_PROTOCOL, fix_imports = True, buffer_callback = None)
|
||||||
|
|
|
@ -70,7 +70,7 @@ class TabHeaderTemplate(Gtk.Box):
|
||||||
self.add(close)
|
self.add(close)
|
||||||
|
|
||||||
def set_tab_label(self, label = "untitled"):
|
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):
|
def get_tab_label(self):
|
||||||
return self.get_children()[0].get_text()
|
return self.get_children()[0].get_children()[0].get_text()
|
||||||
|
|
|
@ -3,13 +3,15 @@
|
||||||
# Lib imports
|
# Lib imports
|
||||||
import gi
|
import gi
|
||||||
gi.require_version('Gtk', '3.0')
|
gi.require_version('Gtk', '3.0')
|
||||||
|
gi.require_version('GtkSource', '4')
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
|
from gi.repository import GtkSource
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
from utils.widget_save_load_controller import WidgetSaveLoadController
|
from utils.widget_save_load_controller import WidgetSaveLoadController
|
||||||
|
|
||||||
|
|
||||||
class TextAreaWidget(WidgetSaveLoadController, Gtk.TextView):
|
class TextAreaWidget(WidgetSaveLoadController, GtkSource.View):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(TextAreaWidget, self).__init__()
|
super(TextAreaWidget, self).__init__()
|
||||||
|
|
||||||
|
@ -27,7 +29,7 @@ class TextAreaWidget(WidgetSaveLoadController, Gtk.TextView):
|
||||||
...
|
...
|
||||||
|
|
||||||
def _setup_signals(self):
|
def _setup_signals(self):
|
||||||
...
|
self.connect("key-release-event", self._key_released)
|
||||||
|
|
||||||
def _subscribe_to_events(self):
|
def _subscribe_to_events(self):
|
||||||
...
|
...
|
||||||
|
@ -35,6 +37,9 @@ class TextAreaWidget(WidgetSaveLoadController, Gtk.TextView):
|
||||||
def _load_widgets(self):
|
def _load_widgets(self):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
def _key_released(self, widget = None, eve = None):
|
||||||
|
self.get_parent().save_needed = True
|
||||||
|
|
||||||
def get_buffer_text(self):
|
def get_buffer_text(self):
|
||||||
start, end = self.buffer.get_start_iter(), self.buffer.get_end_iter()
|
start, end = self.buffer.get_start_iter(), self.buffer.get_end_iter()
|
||||||
return self.buffer.get_slice(start, end, True)
|
return self.buffer.get_slice(start, end, True)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
# Python imports
|
# Python imports
|
||||||
|
|
||||||
# Lib imports
|
# Lib imports
|
||||||
|
import gi
|
||||||
|
from gi.repository import GLib
|
||||||
|
|
||||||
# Application imports
|
# Application imports
|
||||||
|
|
||||||
|
@ -15,6 +17,7 @@ class WidgetSaveLoadController:
|
||||||
super(WidgetSaveLoadController, self).__init__()
|
super(WidgetSaveLoadController, self).__init__()
|
||||||
self.saveable_data = []
|
self.saveable_data = []
|
||||||
self.save_collection = {
|
self.save_collection = {
|
||||||
|
"uuid": "",
|
||||||
"width": -1,
|
"width": -1,
|
||||||
"height": -1,
|
"height": -1,
|
||||||
"x": 0,
|
"x": 0,
|
||||||
|
|
Loading…
Reference in New Issue