From fe3f601a8a558c6c5ae65f0f0fa8b0cc6189ac5f Mon Sep 17 00:00:00 2001 From: Nokse22 <44558032+Nokse22@users.noreply.github.com> Date: Sun, 6 Oct 2024 18:37:01 +0200 Subject: [PATCH 1/4] added tree view demo --- src/List View with TreeListModel/main.blp | 35 +++++++++ src/List View with TreeListModel/main.json | 6 ++ src/List View with TreeListModel/main.py | 87 ++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/List View with TreeListModel/main.blp create mode 100644 src/List View with TreeListModel/main.json create mode 100644 src/List View with TreeListModel/main.py diff --git a/src/List View with TreeListModel/main.blp b/src/List View with TreeListModel/main.blp new file mode 100644 index 00000000..672ab4ef --- /dev/null +++ b/src/List View with TreeListModel/main.blp @@ -0,0 +1,35 @@ +using Gtk 4.0; +using Adw 1; + +Adw.StatusPage { + title: _("List View with TreeListModel"); + description: _("Arrange items in a tree like structure"); + valign: start; + + Adw.Clamp { + maximum-size: 360; + + Box { + orientation: vertical; + spacing: 18; + + Box { + halign: center; + + LinkButton { + label: _("API Reference"); + uri: "https://docs.gtk.org/gtk4/class.TreeListModel.html"; + } + + LinkButton { + label: _("Documentation"); + uri: "https://docs.gtk.org/gtk4/section-list-widget.html#displaying-trees"; + } + } + + ListView list_view { + factory: SignalListItemFactory factory {}; + } + } + } +} diff --git a/src/List View with TreeListModel/main.json b/src/List View with TreeListModel/main.json new file mode 100644 index 00000000..4de539d8 --- /dev/null +++ b/src/List View with TreeListModel/main.json @@ -0,0 +1,6 @@ +{ + "category": "layout", + "description": "Arrange items in a tree like structure", + "panels": ["code", "ui", "preview"], + "autorun": true +} diff --git a/src/List View with TreeListModel/main.py b/src/List View with TreeListModel/main.py new file mode 100644 index 00000000..a5e3fdf1 --- /dev/null +++ b/src/List View with TreeListModel/main.py @@ -0,0 +1,87 @@ +import gi + +gi.require_version("Gtk", "4.0") +gi.require_version("Adw", "1") +from gi.repository import Gtk, Adw, GObject, Gio +import workbench + +list_view = workbench.builder.get_object("list_view") +factory = workbench.builder.get_object("factory") + + +class TreeNode(GObject.Object): + def __init__(self, _title, children=None): + super().__init__() + self.children = children or [] + self.title = _title + + +class TreeWidget(Adw.Bin): + def __init__(self): + super().__init__() + + box = Gtk.Box( + spacing=6, margin_start=3, margin_end=10, margin_top=6, margin_bottom=6 + ) + + self.expander = Gtk.TreeExpander.new() + + self.label = Gtk.Label(xalign=0, ellipsize=3) + + box.append(self.expander) + box.append(self.label) + + self.set_child(box) + + def set_text(self, text): + self.label.set_text(text) + + +def create_model_func(item): + child_model = Gio.ListStore.new(TreeNode) + for child in item.children: + child_model.append(child) + return child_model + + +def on_setup(_, list_item): + list_item.set_child(TreeWidget()) + + +def on_bind(_, list_item): + item = list_item.get_item() + widget = list_item.get_child() + widget.expander.set_list_row(item) + + item = list_item.get_item().get_item() + + widget.set_text(item.title) + + +factory.connect("setup", on_setup) +factory.connect("bind", on_bind) + +root_model = TreeNode( + "Root", + [ + TreeNode("Child 1", [TreeNode("Child 1.1", []), TreeNode("Child 1.2", [])]), + TreeNode( + "Child 2", + [ + TreeNode("Child 2.1", []), + TreeNode("Child 2.2", []), + TreeNode("Child 2.3", [TreeNode("Child 3.1", [])]), + ], + ), + ], +) + +tree_model = Gio.ListStore.new(TreeNode) +tree_model.append(root_model) + +tree_list_model = Gtk.TreeListModel.new(tree_model, False, True, create_model_func) +tree_list_model.set_autoexpand(False) + +selection_model = Gtk.NoSelection(model=tree_list_model) + +list_view.set_model(selection_model) From d4a45b487633aaa5d153d8b74be4be13268268bb Mon Sep 17 00:00:00 2001 From: Nokse22 <44558032+Nokse22@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:51:15 +0200 Subject: [PATCH 2/4] simplified and renamed it Tree List Model --- .../main.blp | 9 +++-- .../main.json | 0 .../main.py | 33 ++++++++----------- 3 files changed, 20 insertions(+), 22 deletions(-) rename src/{List View with TreeListModel => Tree List Model}/main.blp (77%) rename src/{List View with TreeListModel => Tree List Model}/main.json (100%) rename src/{List View with TreeListModel => Tree List Model}/main.py (71%) diff --git a/src/List View with TreeListModel/main.blp b/src/Tree List Model/main.blp similarity index 77% rename from src/List View with TreeListModel/main.blp rename to src/Tree List Model/main.blp index 672ab4ef..fbf006f7 100644 --- a/src/List View with TreeListModel/main.blp +++ b/src/Tree List Model/main.blp @@ -27,8 +27,13 @@ Adw.StatusPage { } } - ListView list_view { - factory: SignalListItemFactory factory {}; + ScrolledWindow { + has-frame: true; + height-request: 320; + + child: ListView list_view { + factory: SignalListItemFactory factory {}; + }; } } } diff --git a/src/List View with TreeListModel/main.json b/src/Tree List Model/main.json similarity index 100% rename from src/List View with TreeListModel/main.json rename to src/Tree List Model/main.json diff --git a/src/List View with TreeListModel/main.py b/src/Tree List Model/main.py similarity index 71% rename from src/List View with TreeListModel/main.py rename to src/Tree List Model/main.py index a5e3fdf1..56457183 100644 --- a/src/List View with TreeListModel/main.py +++ b/src/Tree List Model/main.py @@ -2,7 +2,7 @@ gi.require_version("Gtk", "4.0") gi.require_version("Adw", "1") -from gi.repository import Gtk, Adw, GObject, Gio +from gi.repository import Gtk, GObject, Gio import workbench list_view = workbench.builder.get_object("list_view") @@ -10,31 +10,24 @@ class TreeNode(GObject.Object): - def __init__(self, _title, children=None): + def __init__(self, _title, _children=None): super().__init__() - self.children = children or [] + self.children = _children or [] self.title = _title -class TreeWidget(Adw.Bin): +class TreeWidget(Gtk.Box): def __init__(self): - super().__init__() - - box = Gtk.Box( - spacing=6, margin_start=3, margin_end=10, margin_top=6, margin_bottom=6 + super().__init__( + spacing=6, margin_start=6, margin_end=12, margin_top=6, margin_bottom=6 ) self.expander = Gtk.TreeExpander.new() self.label = Gtk.Label(xalign=0, ellipsize=3) - box.append(self.expander) - box.append(self.label) - - self.set_child(box) - - def set_text(self, text): - self.label.set_text(text) + self.append(self.expander) + self.append(self.label) def create_model_func(item): @@ -49,13 +42,12 @@ def on_setup(_, list_item): def on_bind(_, list_item): - item = list_item.get_item() + list_row = list_item.get_item() widget = list_item.get_child() - widget.expander.set_list_row(item) - - item = list_item.get_item().get_item() + item = list_row.get_item() - widget.set_text(item.title) + widget.expander.set_list_row(list_row) + widget.label.set_label(item.title) factory.connect("setup", on_setup) @@ -85,3 +77,4 @@ def on_bind(_, list_item): selection_model = Gtk.NoSelection(model=tree_list_model) list_view.set_model(selection_model) + From dcd29c1d7ae4c0ef7f527d00f7ac4b21f39a393d Mon Sep 17 00:00:00 2001 From: Nokse22 <44558032+Nokse22@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:55:19 +0200 Subject: [PATCH 3/4] formatting --- src/Tree List Model/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Tree List Model/main.py b/src/Tree List Model/main.py index 56457183..3599084f 100644 --- a/src/Tree List Model/main.py +++ b/src/Tree List Model/main.py @@ -77,4 +77,3 @@ def on_bind(_, list_item): selection_model = Gtk.NoSelection(model=tree_list_model) list_view.set_model(selection_model) - From 5386236bddc68ea34631b4be7c7c23e2cf649a23 Mon Sep 17 00:00:00 2001 From: Nokse22 <44558032+Nokse22@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:23:46 +0200 Subject: [PATCH 4/4] Renamed it 'List View with a Tree', moved link buttons, removed expanders for items without children --- .../main.blp | 20 +++++++++---------- .../main.json | 0 .../main.py | 2 ++ 3 files changed, 12 insertions(+), 10 deletions(-) rename src/{Tree List Model => List View with a Tree}/main.blp (94%) rename src/{Tree List Model => List View with a Tree}/main.json (100%) rename src/{Tree List Model => List View with a Tree}/main.py (97%) diff --git a/src/Tree List Model/main.blp b/src/List View with a Tree/main.blp similarity index 94% rename from src/Tree List Model/main.blp rename to src/List View with a Tree/main.blp index fbf006f7..27fab0a4 100644 --- a/src/Tree List Model/main.blp +++ b/src/List View with a Tree/main.blp @@ -2,7 +2,7 @@ using Gtk 4.0; using Adw 1; Adw.StatusPage { - title: _("List View with TreeListModel"); + title: _("List View with a Tree"); description: _("Arrange items in a tree like structure"); valign: start; @@ -13,6 +13,15 @@ Adw.StatusPage { orientation: vertical; spacing: 18; + ScrolledWindow { + has-frame: true; + height-request: 320; + + child: ListView list_view { + factory: SignalListItemFactory factory {}; + }; + } + Box { halign: center; @@ -26,15 +35,6 @@ Adw.StatusPage { uri: "https://docs.gtk.org/gtk4/section-list-widget.html#displaying-trees"; } } - - ScrolledWindow { - has-frame: true; - height-request: 320; - - child: ListView list_view { - factory: SignalListItemFactory factory {}; - }; - } } } } diff --git a/src/Tree List Model/main.json b/src/List View with a Tree/main.json similarity index 100% rename from src/Tree List Model/main.json rename to src/List View with a Tree/main.json diff --git a/src/Tree List Model/main.py b/src/List View with a Tree/main.py similarity index 97% rename from src/Tree List Model/main.py rename to src/List View with a Tree/main.py index 3599084f..a3cec7ad 100644 --- a/src/Tree List Model/main.py +++ b/src/List View with a Tree/main.py @@ -31,6 +31,8 @@ def __init__(self): def create_model_func(item): + if item.children == []: + return None child_model = Gio.ListStore.new(TreeNode) for child in item.children: child_model.append(child)