]> git.phdru.name Git - m_librarian.git/blobdiff - m_librarian/wx/ListBooks.py
Feat(wx/books): Show series
[m_librarian.git] / m_librarian / wx / ListBooks.py
index 97f88268d760a46c09b43e5eb7e4f466d2559554..955ef851782e3c40653b9d8fdacc0cf4edc79c20 100644 (file)
@@ -1,6 +1,8 @@
 # coding: utf-8
 
 import wx
+from ..compat import string_type, unicode_type
+from ..translations import translations
 from .AWindow import AWindow
 
 
@@ -9,6 +11,69 @@ class ListBooksWindow(AWindow):
     session_config_section_name = 'list_books'
     window_title = u"m_Librarian: Список книг"
 
-    def __init__(self, parent, author):
-        self.author = author
+    def __init__(self, parent, books_by_author):
+        self.books_by_author = books_by_author
         AWindow.__init__(self, parent)
+
+    def OnInit(self):
+        AWindow.OnInit(self)
+        ListBooksPanel(self, self.books_by_author)
+
+
+class ListBooksPanel(wx.Panel):
+
+    def __init__(self, parent, books_by_author):
+        wx.Panel.__init__(self, parent)
+        self.books_by_author = books_by_author
+
+        list_books_sizer = wx.BoxSizer(wx.VERTICAL)
+        self.SetSizer(list_books_sizer)
+
+        self.grid = grid = wx.grid.Grid(self)
+        list_books_sizer.Add(grid, 0, wx.EXPAND, 0)
+
+        self.InitGrid()
+
+    def InitGrid(self):
+        _ = getattr(translations, 'ugettext', None) or translations.gettext
+        books_by_author = self.books_by_author['books_by_author']
+        columns = self.books_by_author['columns']
+        author = next(iter(books_by_author))
+        books = books_by_author[author]
+        series = {book.series for book in books}
+        grid = self.grid
+        grid.CreateGrid(len(books) + len(series), len(columns))
+        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)
+            row += 1
+        grid.AutoSizeColumns()
+        grid.AutoSizeRows()