Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Update ColumnView to use Expressions #191

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ Here is a compilation of resources to learn more about the GNOME platform.
- [Valadoc.org](https://valadoc.org)
- [Vala tutorial](https://wiki.gnome.org/Projects/Vala/Tutorial)
- [Vala examples](https://wiki.gnome.org/Projects/Vala/Examples)

### Python

- [PyGObject Docs](https://pygobject.gnome.org)
37 changes: 8 additions & 29 deletions src/Column View/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _on_factory_setup(_factory, list_item):

def _on_factory_bind(_factory, list_item, what):
label_widget = list_item.get_child()
book = list_item.get_item().get_item()
book = list_item.get_item()
label_widget.set_label(str(getattr(book, what)))


Expand All @@ -70,35 +70,14 @@ def _on_factory_bind(_factory, list_item, what):
col3.get_factory().connect("setup", _on_factory_setup)
col3.get_factory().connect("bind", _on_factory_bind, "year")


# Custom Sorter is required because PyGObject doesn't currently support
# Gtk.Expression: https://gitlab.gnome.org/GNOME/pygobject/-/issues/356


def model_func(_item):
pass


tree_model = Gtk.TreeListModel.new(data_model, False, True, model_func)
tree_sorter = Gtk.TreeListRowSorter.new(column_view.get_sorter())
sorter_model = Gtk.SortListModel(model=tree_model, sorter=tree_sorter)
sorter_model = Gtk.SortListModel.new(model=data_model, sorter=column_view.get_sorter())
selection = Gtk.SingleSelection.new(model=sorter_model)
column_view.set_model(model=selection)

col1_exp = Gtk.PropertyExpression.new(Book, None, "title")
col2_exp = Gtk.PropertyExpression.new(Book, None, "author")
col3_exp = Gtk.PropertyExpression.new(Book, None, "year")

def str_sorter(object_a, object_b, column) -> bool:
a = getattr(object_a, column).lower()
b = getattr(object_b, column).lower()
return (a > b) - (a < b)


def int_sorter(object_a, object_b, column) -> bool:
print(object_a)
a = getattr(object_a, column)
b = getattr(object_b, column)
return (a > b) - (a < b)


col1.set_sorter(Gtk.CustomSorter.new(str_sorter, "title"))
col2.set_sorter(Gtk.CustomSorter.new(str_sorter, "author"))
col3.set_sorter(Gtk.CustomSorter.new(int_sorter, "year"))
col1.set_sorter(Gtk.StringSorter.new(col1_exp))
col2.set_sorter(Gtk.StringSorter.new(col2_exp))
col3.set_sorter(Gtk.NumericSorter.new(col3_exp))
Loading