From 695ea3e9da446a97bc2cccf5dd08e3ee717b32ff Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 9 Jul 2024 16:33:42 +0300 Subject: [PATCH] Feat(wx): Sort authors by columns [skip ci] --- TODO | 3 --- m_librarian/wx/ListAuthors.py | 38 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/TODO b/TODO index 4e9904f..f1fa2d7 100644 --- a/TODO +++ b/TODO @@ -1,9 +1,6 @@ Next release ------------ -List of authors - sort by clicking on column titles. - - Installers. diff --git a/m_librarian/wx/ListAuthors.py b/m_librarian/wx/ListAuthors.py index b2aac25..c10f07b 100644 --- a/m_librarian/wx/ListAuthors.py +++ b/m_librarian/wx/ListAuthors.py @@ -14,6 +14,7 @@ class ListAuthorsPanel(GridPanel): _ = getattr(translations, 'ugettext', None) or translations.gettext authors = self.param['authors'] columns = self.param['columns'] + self.param['column_sort'] = [] grid = self.grid grid.CreateGrid(len(authors), len(columns)) grid.EnableEditing(False) @@ -21,12 +22,13 @@ class ListAuthorsPanel(GridPanel): grid.SetRowLabelValue(row, str(row)) grid.AutoSizeRowLabelSize(row) for col, col_name in enumerate(columns): - grid.SetColLabelValue(col, _(col_name)) + grid.SetColLabelValue(col, _(col_name) + u'↓') grid.AutoSizeColLabelSize(col) if col_name == 'count': cell_attr = wx.grid.GridCellAttr() cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_CENTRE) grid.SetColAttr(col, cell_attr) + self.param['column_sort'].append('+') for row, author in enumerate(authors): for col, col_name in enumerate(columns): value = getattr(author, col_name) @@ -35,6 +37,7 @@ class ListAuthorsPanel(GridPanel): grid.SetCellValue(row, col, value) grid.AutoSizeColumns() grid.AutoSizeRows() + grid.Bind(wx.grid.EVT_GRID_COL_SORT, self.OnSort) def listBooks(self, row): authors = self.param['authors'] @@ -55,6 +58,39 @@ class ListAuthorsPanel(GridPanel): else: event.Skip() + def OnSort(self, event): + authors = self.param['authors'] + columns = self.param['columns'] + column_sort = self.param['column_sort'] + sort_col = event.GetCol() + column_name = columns[sort_col] + sort_dir = column_sort[sort_col] + if sort_dir == '+': + reverse = True + sort_dir = '-' + sort_sign = u'↑' + elif sort_dir == '-': + reverse = False + sort_dir = '+' + sort_sign = u'↓' + else: + raise ValueError('Unknown sort direction "%s"' % sort_dir) + authors.sort( + key=lambda a, column_name=column_name: getattr(a, column_name), + reverse=reverse) + column_sort[sort_col] = sort_dir + _ = getattr(translations, 'ugettext', None) or translations.gettext + grid = self.grid + for col, col_name in enumerate(columns): + grid.SetColLabelValue(col, _(col_name) + u'↓') + grid.SetColLabelValue(sort_col, _(column_name) + sort_sign) + for row, author in enumerate(authors): + for col, col_name in enumerate(columns): + value = getattr(author, col_name) + if not isinstance(value, (string_type, unicode_type)): + value = str(value) + grid.SetCellValue(row, col, value) + class ListAuthorsWindow(GridWindow): -- 2.39.5