]> git.phdru.name Git - m_librarian.git/commitdiff
Feat(wx): Sort authors by columns
authorOleg Broytman <phd@phdru.name>
Tue, 9 Jul 2024 13:33:42 +0000 (16:33 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 9 Jul 2024 13:34:52 +0000 (16:34 +0300)
[skip ci]

TODO
m_librarian/wx/ListAuthors.py

diff --git a/TODO b/TODO
index 4e9904f569856f1a997bf21e15aa0ef4c2d1b03f..f1fa2d7ad059070dd5ab322a48664584fe4b6d46 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,9 +1,6 @@
 Next release
 ------------
 
-List of authors - sort by clicking on column titles.
-
-
 Installers.
 
 
index b2aac2501dd75f1c38d0134609549157b103d9bf..c10f07ba8f403642a0f0a3043d86b8eff3057b3b 100644 (file)
@@ -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):