]> git.phdru.name Git - m_librarian.git/blobdiff - m_librarian/wx/ListBooks.py
Feat(wx/books): Set custom table
[m_librarian.git] / m_librarian / wx / ListBooks.py
index b1f84a11345449e50da3e4f06d0ea84ead2894e2..514301bcee967fe32bc3a5089077bd7223b71ea4 100644 (file)
 # coding: utf-8
 
-import wx
+import wx, wx.grid  # noqa: E401 multiple imports on one line
 from ..compat import string_type, unicode_type
 from ..translations import translations
 from .Grids import GridWindow, GridPanel
 
 
+_ = getattr(translations, 'ugettext', None) or translations.gettext
+
+
+class BooksDataTable(wx.grid.GridTableBase):
+    def __init__(self, rows_count, column_names):
+        wx.grid.GridTableBase.__init__(self)
+        self.rows_count = rows_count
+        self.column_names = column_names
+        self.data = []
+        for row in range(rows_count):
+            row_data = []
+            self.data.append(row_data)
+            for col in range(len(column_names)):
+                row_data.append('')
+
+    # required methods for the wxPyGridTableBase interface
+
+    def GetNumberRows(self):
+        return self.rows_count
+
+    def GetNumberCols(self):
+        return len(self.column_names)
+
+    def IsEmptyCell(self, row, col):
+        return False
+
+    # Get/Set values in the table.  The Python version of these
+    # methods can handle any data-type, (as long as the Editor and
+    # Renderer understands the type too,) not just strings as in the
+    # C++ version.
+    def GetValue(self, row, col):
+        return self.data[row][col]
+
+    def SetValue(self, row, col, value):
+        self.data[row][col] = value
+
+    # Optional methods
+
+    # Called when the grid needs to display labels
+    def GetRowLabelValue(self, row):
+        return str(row)
+
+    def GetColLabelValue(self, col):
+        return _(self.column_names[col])
+
+    # Called to determine the kind of editor/renderer to use by
+    # default, doesn't necessarily have to be the same type used
+    # natively by the editor/renderer if they know how to convert.
+    def GetTypeName(self, row, col):
+        return wx.grid.GRID_VALUE_STRING
+
+
 class ListBooksPanel(GridPanel):
 
     def InitGrid(self):
-        _ = getattr(translations, 'ugettext', None) or translations.gettext
         books_by_author = self.param['books_by_author']
         columns = self.param['columns']
-        author = next(iter(books_by_author))
-        books = books_by_author[author]
-        series = {book.series for book in books}
+        total_rows = 0
+        for author in books_by_author:
+            books = books_by_author[author]
+            series = {book.series for book in books}
+            total_rows += len(books) + len(series) + 1
         grid = self.grid
-        grid.CreateGrid(len(books) + len(series), len(columns))
+        grid.SetTable(BooksDataTable(total_rows, columns), takeOwnership=True)
         grid.EnableEditing(False)
-        for row in range(len(books)):
-            grid.SetRowLabelValue(row, str(row))
-            grid.AutoSizeRowLabelSize(row)
         for col, col_name in enumerate(columns):
-            grid.SetColLabelValue(col, _(col_name))
             grid.AutoSizeColLabelSize(col)
             if col_name in ('ser_no', 'size'):
                 cell_attr = wx.grid.GridCellAttr()
                 cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx. ALIGN_CENTRE)
                 grid.SetColAttr(col, cell_attr)
         row = 0
-        series = None
-        for book in books:
-            if book.series != series:
-                if book.series:
-                    value = book.series
-                else:
-                    value = u'Вне серий'
-                grid.SetCellAlignment(row, 0, wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
-                grid.SetCellSize(row, 0, 1, len(columns))
-                grid.SetCellValue(row, 0, u'%s — %s' % (book.author1, value))
-                row += 1
-                series = book.series
-            for col, col_name in enumerate(columns):
-                value = getattr(book, col_name)
-                if value is None:
-                    value = u''
-                elif not isinstance(value, (string_type, unicode_type)):
-                    value = str(value)
-                grid.SetCellValue(row, col, value)
+        for author in sorted(books_by_author):
+            grid.SetCellAlignment(row, 0, wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
+            grid.SetCellSize(row, 0, 1, len(columns))
+            grid.SetCellValue(row, 0, u'%s' % (author,))
             row += 1
+            books = books_by_author[author]
+            series = None
+            for book in books:
+                if book.series != series:
+                    if book.series:
+                        value = book.series
+                    else:
+                        value = u'Вне серий'
+                    grid.SetCellAlignment(row, 0,
+                                          wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
+                    grid.SetCellSize(row, 0, 1, len(columns))
+                    grid.SetCellValue(row, 0,
+                                      u'%s — %s' % (book.author1, value))
+                    row += 1
+                    series = book.series
+                for col, col_name in enumerate(columns):
+                    value = getattr(book, col_name)
+                    if value is None:
+                        value = u''
+                    elif not isinstance(value, (string_type, unicode_type)):
+                        value = str(value)
+                    grid.SetCellValue(row, col, value)
+                row += 1
         grid.AutoSizeColumns()
         grid.AutoSizeRows()
 
@@ -61,6 +118,7 @@ class ListBooksPanel(GridPanel):
         else:
             event.Skip()
 
+
 class ListBooksWindow(GridWindow):
 
     session_config_section_name = 'list_books'