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 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
111 changes: 46 additions & 65 deletions GTG/core/sorters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,45 @@ def unwrap(row, expected_type):

return item

class ReversibleSorter(Gtk.Sorter):

class TaskTitleSorter(Gtk.Sorter):
__gtype_name__ = 'TaskTitleSorter'
def __init__(self) -> None:
self._reverse: bool = False
super().__init__()


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


@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."""

def __init__(self):
super(TaskTitleSorter, self).__init__()
if first == second:
return Gtk.Ordering.EQUAL

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

if self._reverse:
return Gtk.Ordering.SMALLER
else:
return Gtk.Ordering.LARGER


class TaskTitleSorter(ReversibleSorter):
__gtype_name__ = 'TaskTitleSorter'

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

a = unwrap(a, Task)
Expand All @@ -47,21 +78,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 +92,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 +106,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 +120,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 +149,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 +163,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)
89 changes: 46 additions & 43 deletions GTG/gtk/browser/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class MainWindow(Gtk.ApplicationWindow):
defer_menu_btn = Gtk.Template.Child()
defer_menu_days_section = Gtk.Template.Child()

sort_btn = Gtk.Template.Child('sort_menu_btn')

headerbar = Gtk.Template.Child('browser_headerbar')
main_menu_btn = Gtk.Template.Child()
main_menu = Gtk.Template.Child()
Expand Down Expand Up @@ -241,13 +243,6 @@ def _set_actions(self):
('recurring_month', self.on_set_recurring_every_month, None),
('recurring_year', self.on_set_recurring_every_year, None),
('recurring_toggle', self.on_toggle_recurring, None),
('sort_by_start', self.on_sort_start, None),
('sort_by_due', self.on_sort_due, None),
('sort_by_added', self.on_sort_added, None),
('sort_by_title', self.on_sort_title, None),
('sort_by_modified', self.on_sort_modified, None),
('sort_by_added', self.on_sort_added, None),
('sort_by_tags', self.on_sort_tags, None),
]

for action, callback, accel in action_entries:
Expand All @@ -260,6 +255,25 @@ def _set_actions(self):

if accel is not None:
self.app.set_accels_for_action(*accel)


# Stateful actions from now on
sort_variant = GLib.Variant.new_string('Title')
sort_action = Gio.SimpleAction.new_stateful('sort',
sort_variant.get_type(),
sort_variant)

sort_action.connect('change-state', self.on_sort)
self.add_action(sort_action)

order_variant = GLib.Variant.new_string('ASC')
order_action = Gio.SimpleAction.new_stateful('sort_order',
order_variant.get_type(),
order_variant)

order_action.connect('change-state', self.on_sort_order)
self.add_action(order_action)


def _init_icon_theme(self):
"""
Expand Down Expand Up @@ -1288,46 +1302,26 @@ def on_modify_tags(self, action, params):
self.modifytags_dialog.modify_tags(tasks)


def on_sort_start(self, action, params) -> None:
"""Callback when changing task sorting."""

self.set_sorter('Start')
self.store_sorting('start')


def on_sort_due(self, action, params) -> None:
"""Callback when changing task sorting."""

self.set_sorter('Due')
self.store_sorting('due')


def on_sort_added(self, action, params) -> None:
"""Callback when changing task sorting."""

self.set_sorter('Added')
self.store_sorting('added')
def on_sort(self, action, value) -> None:

action.set_state(value)
value_str = value.get_string()

self.set_sorter(value_str)
self.store_sorting(value_str.lower())

def on_sort_title(self, action, params) -> None:
"""Callback when changing task sorting."""

self.set_sorter('Title')
self.store_sorting('title')
def on_sort_order(self, action, value) -> None:

action.set_state(value)
value_str = value.get_string()


def on_sort_modified(self, action, params) -> None:
"""Callback when changing task sorting."""

self.set_sorter('Modified')
self.store_sorting('modified')


def on_sort_tags(self, action, params) -> None:
"""Callback when changing task sorting."""

self.set_sorter('Tags')
self.store_sorting('tags')
if value_str == 'ASC':
self.get_pane().set_sort_order(reverse=False)
self.change_sort_icon('ASC')
else:
self.get_pane().set_sort_order(reverse=True)
self.change_sort_icon('DESC')


def store_sorting(self, mode: str) -> None:
Expand All @@ -1348,6 +1342,15 @@ def set_sorter(self, value: str) -> None:
self.get_pane().set_sorter(value)


def change_sort_icon(self, order: str) -> None:
"""Change icon for sorting menu button."""

if order == 'ASC':
self.sort_btn.set_icon_name('view-sort-ascending-symbolic')
elif order == 'DESC':
self.sort_btn.set_icon_name('view-sort-descending-symbolic')


def close_all_task_editors(self, task_id):
""" Including editors of subtasks """
all_subtasks = []
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
Loading
Loading