]> git.phdru.name Git - m_librarian.git/blobdiff - m_librarian/wx/ListBooks.py
Feat(wx/search): Search books
[m_librarian.git] / m_librarian / wx / ListBooks.py
index 955ef851782e3c40653b9d8fdacc0cf4edc79c20..4cfabcdb36026af7206ce0cff6f1c618047df1d0 100644 (file)
@@ -3,48 +3,24 @@
 import wx
 from ..compat import string_type, unicode_type
 from ..translations import translations
-from .AWindow import AWindow
+from .Grids import GridWindow, GridPanel
 
 
-class ListBooksWindow(AWindow):
-
-    session_config_section_name = 'list_books'
-    window_title = u"m_Librarian: Список книг"
-
-    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()
+class ListBooksPanel(GridPanel):
 
     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}
+        books_by_author = self.param['books_by_author']
+        columns = self.param['columns']
+        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)
         grid = self.grid
-        grid.CreateGrid(len(books) + len(series), len(columns))
+        grid.CreateGrid(total_rows, len(columns))
         grid.EnableEditing(False)
-        for row in range(len(books)):
+        for row in range(total_rows):
             grid.SetRowLabelValue(row, str(row))
             grid.AutoSizeRowLabelSize(row)
         for col, col_name in enumerate(columns):
@@ -55,25 +31,42 @@ class ListBooksPanel(wx.Panel):
                 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))
+        for author in sorted(books_by_author):
+            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
-                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()
+
+    def OnDClick(self, event):
+        pass
+
+    def OnKeyDown(self, event):
+        if event.GetKeyCode() == wx.WXK_ESCAPE:
+            self.Parent.Close()
+        else:
+            event.Skip()
+
+class ListBooksWindow(GridWindow):
+
+    session_config_section_name = 'list_books'
+    window_title = u"m_Librarian: Список книг"
+    GridPanelClass = ListBooksPanel