Dynamic header and widget support code

This commit is contained in:
itdominator 2023-04-02 23:42:09 -05:00
parent 60de4955f7
commit 449f8b54ce
5 changed files with 174 additions and 20 deletions

View File

@ -3,16 +3,22 @@
# 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 .dynamic_widget import DynamicWidget
class DragArea(Gtk.Fixed):
class DragArea(GtkSource.View):
def __init__(self):
super(DragArea, self).__init__()
self.buffer = self.get_buffer()
self._children_content = []
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
@ -20,7 +26,8 @@ class DragArea(Gtk.Fixed):
def _setup_styling(self):
...
self.set_size_request(1800, 1800)
self.set_cursor_visible(False)
def _setup_signals(self):
self.connect("button-release-event", self.add_or_select_widget)
@ -31,5 +38,34 @@ class DragArea(Gtk.Fixed):
def _load_widgets(self):
...
def add_fixed_base_widgets(self, name = None, date = None, update_header_callback = None):
entry = Gtk.Entry(text = name)
date_label = Gtk.Label(label = date)
entry.connect("changed", update_header_callback)
entry.set_width_chars(25)
entry.set_max_length(25)
# PRIVATE = 0, WIDGET = 1, TEXT = 2, LEFT = 3, RIGHT = 4, TOP = 5, BOTTOM = 6
self.add_child_in_window(entry, Gtk.TextWindowType.TEXT, 30, 30)
self.add_child_in_window(date_label, Gtk.TextWindowType.TEXT, 30, 65)
def add_or_select_widget(self, widget = None, eve = None):
logger.debug(f"Add or selecting widget...")
dynamic_widget = DynamicWidget(self._move_callback, eve.x, eve.y)
self._children_content.append(dynamic_widget)
self.add_child_in_window(dynamic_widget, Gtk.TextWindowType.TEXT, eve.x, eve.y)
self.get_page_content()
def get_page_content(self, widget = None, eve = None):
# PRIVATE = 0, WIDGET = 1, TEXT = 2, LEFT = 3, RIGHT = 4, TOP = 5, BOTTOM = 6
text_window = self.get_window(Gtk.TextWindowType.TEXT)
logger.info(text_window)
# NOTE: Probably should use something similar to pass to custom widget content container
def _move_callback(self, widget = None, x = None, y = None, call_back = None):
logger.info(widget)
logger.info(x)
logger.info(y)
logger.info(call_back)
# self.move_child(widget, x, y)

View File

@ -0,0 +1,84 @@
# Python imports
# Lib imports
# Application imports
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
class DynamicHeaderWidget(Gtk.Box):
def __init__(self, move_callback, x, y):
super(DynamicHeaderWidget, self).__init__()
self._move_callback = move_callback
self._current_x = x
self._current_y = y
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets()
self.show_all()
def _setup_styling(self):
self.set_orientation(0)
def _setup_signals(self):
...
def _subscribe_to_events(self):
...
def _load_widgets(self):
drag_eve = Gtk.EventBox()
hresize_eve = Gtk.EventBox()
self.is_dragging = False
label1 = Gtk.Label(label="")
label2 = Gtk.Label(label="....")
label3 = Gtk.Label(label="< >")
drag_eve.set_above_child(True)
hresize_eve.set_above_child(True)
label1.set_hexpand(True)
label2.set_hexpand(True)
label3.set_hexpand(True)
label2.set_yalign(0.5)
label3.set_yalign(0.5)
label3.set_xalign(1.0)
drag_eve.add(label2)
hresize_eve.add(label3)
URI_TARGET_TYPE = 80
uri_target = Gtk.TargetEntry.new('text/uri-list', Gtk.TargetFlags(0), URI_TARGET_TYPE)
targets = [ uri_target ]
flags = Gtk.DestDefaults.ALL
action = Gdk.DragAction.COPY
drag_eve.drag_dest_set(flags, targets, action)
drag_eve.connect("button-press-event", self._toggle_drag_start)
drag_eve.connect("motion-notify-event", self._do_drag_motion_call)
drag_eve.connect("button-release-event", self._toggle_drag_start)
self.add(label1)
self.add(drag_eve)
self.add(hresize_eve)
def _toggle_drag_start(self, widget = None, eve = None):
self.is_dragging = not self.is_dragging
def _do_drag_motion_call(self, widget = None, eve = None):
if self.is_dragging:
self._move_callback(self.get_parent(), self._current_x, self._current_y, self.set_current_xy)
def set_current_xy(self, x, y):
self._current_x = x
self._current_y = y

View File

@ -0,0 +1,42 @@
# Python imports
# Lib imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Application imports
from .dynamic_header_widget import DynamicHeaderWidget
class DynamicWidget(Gtk.Box):
def __init__(self, move_callback, x, y):
super(DynamicWidget, self).__init__()
self._header_widget = DynamicHeaderWidget(move_callback, x, y)
self._setup_styling()
self._setup_signals()
self._subscribe_to_events()
self._load_widgets()
self.show_all()
def _setup_styling(self):
self.set_orientation(1)
self.set_size_request(120, 45)
ctx = self.get_style_context()
ctx.add_class("dynamic-widget")
...
def _setup_signals(self):
...
def _subscribe_to_events(self):
...
def _load_widgets(self):
label = Gtk.Label(label="Test")
self.add(self._header_widget)
self.add(label)

View File

@ -11,13 +11,15 @@ from .pages_tab_widget import PagesTabWidget
# class Page(Gtk.Box):
class Page(Gtk.ScrolledWindow):
def __init__(self, close_tab, section_pth, manifest_pth):
super(Page, self).__init__()
self._manifest_pth = settings.generate_page_manifest(section_pth) if not manifest_pth else manifest_pth
self._close_tab = close_tab
self._drag_area = None
self._drag_area = None
self._name = None
self._date = None
@ -27,10 +29,13 @@ class Page(Gtk.ScrolledWindow):
self._load_widgets()
self._load_page_manifest()
self._add_fixed_base_widgets()
self._tab_widget = PagesTabWidget(self, self._close_tab, self._name)
def _setup_styling(self):
# ctx = self.get_style_context()
# ctx.add_class("page-bg")
...
def _setup_signals(self):
@ -40,15 +45,10 @@ class Page(Gtk.ScrolledWindow):
...
def _load_widgets(self):
viewport = Gtk.Viewport()
self._drag_area = DragArea()
viewport.set_vexpand(True)
viewport.set_hexpand(True)
viewport = Gtk.Viewport()
viewport.add(self._drag_area)
viewport.show_all()
self.add(viewport)
def get_tab_widget(self):
@ -71,16 +71,9 @@ class Page(Gtk.ScrolledWindow):
self._date = parts[1].strip()
def add_fixed_base_widgets(self):
entry = Gtk.Entry(text = self._name)
date_label = Gtk.Label(label = self._date)
def _add_fixed_base_widgets(self):
self._drag_area.add_fixed_base_widgets(self._name, self._date, self._update_header)
entry.connect("changed", self._update_header)
entry.set_width_chars(25)
entry.set_max_length(25)
self._drag_area.put(entry, 30, 30)
self._drag_area.put(date_label, 30, 65)
def _update_header(self, widget = None, eve = None):
self._name = widget.get_text()

View File

@ -79,7 +79,6 @@ class Pages(Gtk.Notebook):
self.set_tab_detachable(page, False)
self.set_tab_reorderable(page, True)
page.add_fixed_base_widgets()
page.show_all()
self.set_current_page(page_num)