]> git.phdru.name Git - m_librarian.git/blob - m_librarian/wx/ListBooks.py
Feat(wx/books): List books for an author
[m_librarian.git] / m_librarian / wx / ListBooks.py
1 # coding: utf-8
2
3 import wx
4 from ..compat import string_type, unicode_type
5 from ..translations import translations
6 from .AWindow import AWindow
7
8
9 class ListBooksWindow(AWindow):
10
11     session_config_section_name = 'list_books'
12     window_title = u"m_Librarian: Список книг"
13
14     def __init__(self, parent, books_by_author):
15         self.books_by_author = books_by_author
16         AWindow.__init__(self, parent)
17
18     def OnInit(self):
19         AWindow.OnInit(self)
20         ListBooksPanel(self, self.books_by_author)
21
22
23 class ListBooksPanel(wx.Panel):
24
25     def __init__(self, parent, books_by_author):
26         wx.Panel.__init__(self, parent)
27         self.books_by_author = books_by_author
28
29         list_books_sizer = wx.BoxSizer(wx.VERTICAL)
30         self.SetSizer(list_books_sizer)
31
32         self.grid = grid = wx.grid.Grid(self)
33         list_books_sizer.Add(grid, 0, wx.EXPAND, 0)
34
35         self.InitGrid()
36
37     def InitGrid(self):
38         _ = getattr(translations, 'ugettext', None) or translations.gettext
39         books_by_author = self.books_by_author['books_by_author']
40         columns = self.books_by_author['columns']
41         author = next(iter(books_by_author))
42         books = books_by_author[author]
43         grid = self.grid
44         grid.CreateGrid(len(books), len(columns))
45         grid.EnableEditing(False)
46         for row in range(len(books)):
47             grid.SetRowLabelValue(row, str(row))
48             grid.AutoSizeRowLabelSize(row)
49         for col, col_name in enumerate(columns):
50             grid.SetColLabelValue(col, _(col_name))
51             grid.AutoSizeColLabelSize(col)
52             if col_name in ('ser_no', 'size'):
53                 cell_attr = wx.grid.GridCellAttr()
54                 cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx. ALIGN_CENTRE)
55                 grid.SetColAttr(col, cell_attr)
56         for row, book in enumerate(books):
57             for col, col_name in enumerate(columns):
58                 value = getattr(book, col_name)
59                 if value is None:
60                     value = u''
61                 elif not isinstance(value, (string_type, unicode_type)):
62                     value = str(value)
63                 grid.SetCellValue(row, col, value)
64         grid.AutoSizeColumns()
65         grid.AutoSizeRows()