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

Sort Order #1135

Merged
merged 7 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
112 changes: 47 additions & 65 deletions GTG/core/sorters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,46 @@ def unwrap(row, expected_type):

return item

class ReversibleSorter(Gtk.Sorter):

def __init__(self) -> None:
self._reverse: bool = False
super().__init__()


@property
def reverse(self) -> bool:
return self._reverse

class TaskTitleSorter(Gtk.Sorter):
__gtype_name__ = 'TaskTitleSorter'

def __init__(self):
super(TaskTitleSorter, self).__init__()
@reverse.setter
def reverse(self, value: bool) -> None:
self._reverse = value
self.changed(Gtk.SorterChange.INVERTED)


def reversible_compare(self, first, second) -> Gtk.Ordering:
diegogangl marked this conversation as resolved.
Show resolved Hide resolved
"""Compare for reversible sorters."""

if self._reverse:
if first < second:
return Gtk.Ordering.LARGER
elif first > second:
return Gtk.Ordering.SMALLER
else:
return Gtk.Ordering.EQUAL
else:
if first > second:
return Gtk.Ordering.LARGER
elif first < second:
return Gtk.Ordering.SMALLER
else:
return Gtk.Ordering.EQUAL


class TaskTitleSorter(ReversibleSorter):
__gtype_name__ = 'TaskTitleSorter'

def do_compare(self, a, b) -> Gtk.Ordering:

a = unwrap(a, Task)
Expand All @@ -47,21 +79,12 @@ def do_compare(self, a, b) -> Gtk.Ordering:
first = a.title[0]
second = b.title[0]

if first > second:
return Gtk.Ordering.LARGER
elif first < second:
return Gtk.Ordering.SMALLER
else:
return Gtk.Ordering.EQUAL
return self.reversible_compare(first, second)


class TaskDueSorter(Gtk.Sorter):
class TaskDueSorter(ReversibleSorter):
__gtype_name__ = 'DueSorter'

def __init__(self):
super(TaskDueSorter, self).__init__()


def do_compare(self, a, b) -> Gtk.Ordering:

a = unwrap(a, Task)
Expand All @@ -70,21 +93,12 @@ def do_compare(self, a, b) -> Gtk.Ordering:
first = a.date_due
second = b.date_due

if first > second:
return Gtk.Ordering.LARGER
elif first < second:
return Gtk.Ordering.SMALLER
else:
return Gtk.Ordering.EQUAL
return self.reversible_compare(first, second)


class TaskStartSorter(Gtk.Sorter):
class TaskStartSorter(ReversibleSorter):
__gtype_name__ = 'StartSorter'

def __init__(self):
super(TaskStartSorter, self).__init__()


def do_compare(self, a, b) -> Gtk.Ordering:

a = unwrap(a, Task)
Expand All @@ -93,21 +107,12 @@ def do_compare(self, a, b) -> Gtk.Ordering:
first = a.date_start
second = b.date_start

if first > second:
return Gtk.Ordering.LARGER
elif first < second:
return Gtk.Ordering.SMALLER
else:
return Gtk.Ordering.EQUAL
return self.reversible_compare(first, second)


class TaskModifiedSorter(Gtk.Sorter):
class TaskModifiedSorter(ReversibleSorter):
__gtype_name__ = 'ModifiedSorter'

def __init__(self):
super(TaskModifiedSorter, self).__init__()


def do_compare(self, a, b) -> Gtk.Ordering:

a = unwrap(a, Task)
Expand All @@ -116,21 +121,12 @@ def do_compare(self, a, b) -> Gtk.Ordering:
first = a.date_modified
second = b.date_modified

if first > second:
return Gtk.Ordering.LARGER
elif first < second:
return Gtk.Ordering.SMALLER
else:
return Gtk.Ordering.EQUAL
return self.reversible_compare(first, second)


class TaskTagSorter(Gtk.Sorter):
class TaskTagSorter(ReversibleSorter):
__gtype_name__ = 'TagSorter'

def __init__(self):
super(TaskTagSorter, self).__init__()


def get_first_letter(self, tags) -> str:
"""Get first letter of the first tag in a set of tags."""

Expand All @@ -154,21 +150,12 @@ def do_compare(self, a, b) -> Gtk.Ordering:
else:
second = 'zzzzzzz'

if first > second:
return Gtk.Ordering.LARGER
elif first < second:
return Gtk.Ordering.SMALLER
else:
return Gtk.Ordering.EQUAL
return self.reversible_compare(first, second)


class TaskAddedSorter(Gtk.Sorter):
class TaskAddedSorter(ReversibleSorter):
__gtype_name__ = 'AddedSorter'

def __init__(self):
super(TaskAddedSorter, self).__init__()


def do_compare(self, a, b) -> Gtk.Ordering:

a = unwrap(a, Task)
Expand All @@ -177,9 +164,4 @@ def do_compare(self, a, b) -> Gtk.Ordering:
first = a.date_added
second = b.date_added

if first > second:
return Gtk.Ordering.LARGER
elif first < second:
return Gtk.Ordering.SMALLER
else:
return Gtk.Ordering.EQUAL
return self.reversible_compare(first, second)
14 changes: 14 additions & 0 deletions GTG/gtk/browser/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ def _set_actions(self):
('sort_by_modified', self.on_sort_modified, None),
('sort_by_added', self.on_sort_added, None),
('sort_by_tags', self.on_sort_tags, None),
('sort_asc', self.on_sort_order_asc, None),
('sort_desc', self.on_sort_order_desc, None),
]

for action, callback, accel in action_entries:
Expand Down Expand Up @@ -1342,6 +1344,18 @@ def store_sorting(self, mode: str) -> None:
self.config.set('sort_mode_open', mode)


def on_sort_order_asc(self, action, params) -> None:
"""Set ascending order for current sorter."""

self.get_pane().set_sort_order(reverse=False)


def on_sort_order_desc(self, action, params) -> None:
"""Set descending order for current sorter."""

self.get_pane().set_sort_order(reverse=True)


def set_sorter(self, value: str) -> None:
"""Set sorter for current task pane."""

Expand Down
7 changes: 7 additions & 0 deletions GTG/gtk/browser/task_pane.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def __init__(self, browser, pane):
self.filtered.set_filter(self.task_filter)

self.sort_model = Gtk.TreeListRowSorter()
self.sort_model.set_sorter(TaskTitleSorter())

self.main_sorter = Gtk.SortListModel()
self.main_sorter.set_model(self.filtered)
Expand Down Expand Up @@ -305,6 +306,12 @@ def set_sorter(self, method=None) -> None:
self.sort_model.set_sorter(sorter)


def set_sort_order(self, reverse: bool) -> None:
"""Set order for the sorter."""

self.sort_model.get_sorter().reverse = reverse


def on_listview_activated(self, listview, position, user_data = None):
"""Callback when double clicking on a row."""

Expand Down
61 changes: 37 additions & 24 deletions GTG/gtk/data/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -197,35 +197,48 @@
</menu>

<menu id="sort_menu">
<item>
<attribute name="label" translatable="yes">Sort by Title</attribute>
<attribute name="action">win.sort_by_title</attribute>
</item>
<section>
<item>
<attribute name="label" translatable="yes">Sort by Title</attribute>
<attribute name="action">win.sort_by_title</attribute>
</item>

<item>
<attribute name="label" translatable="yes">Sort by Start Date</attribute>
<attribute name="action">win.sort_by_start</attribute>
</item>

<item>
<attribute name="label" translatable="yes">Sort by Start Date</attribute>
<attribute name="action">win.sort_by_start</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Sort by Due Date</attribute>
<attribute name="action">win.sort_by_due</attribute>
</item>

<item>
<attribute name="label" translatable="yes">Sort by Due Date</attribute>
<attribute name="action">win.sort_by_due</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Sort by Added Date</attribute>
<attribute name="action">win.sort_by_added</attribute>
</item>

<item>
<attribute name="label" translatable="yes">Sort by Added Date</attribute>
<attribute name="action">win.sort_by_added</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Sort by Modified Date</attribute>
<attribute name="action">win.sort_by_modified</attribute>
</item>

<item>
<attribute name="label" translatable="yes">Sort by Modified Date</attribute>
<attribute name="action">win.sort_by_modified</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Sort by Tags</attribute>
<attribute name="action">win.sort_by_tags</attribute>
</item>
</section>

<item>
<attribute name="label" translatable="yes">Sort by Tags</attribute>
<attribute name="action">win.sort_by_tags</attribute>
</item>
<section>
<item>
<attribute name="label" translatable="yes">Ascending Order</attribute>
<attribute name="action">win.sort_asc</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Descending Order</attribute>
<attribute name="action">win.sort_desc</attribute>
</item>
</section>
</menu>


Expand Down