]> git.phdru.name Git - m_librarian.git/blob - m_librarian/wx/ListBooks.py
4cfabcdb36026af7206ce0cff6f1c618047df1d0
[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 .Grids import GridWindow, GridPanel
7
8
9 class ListBooksPanel(GridPanel):
10
11     def InitGrid(self):
12         _ = getattr(translations, 'ugettext', None) or translations.gettext
13         books_by_author = self.param['books_by_author']
14         columns = self.param['columns']
15         total_rows = 0
16         for author in books_by_author:
17             books = books_by_author[author]
18             series = {book.series for book in books}
19             total_rows += len(books) + len(series)
20         grid = self.grid
21         grid.CreateGrid(total_rows, len(columns))
22         grid.EnableEditing(False)
23         for row in range(total_rows):
24             grid.SetRowLabelValue(row, str(row))
25             grid.AutoSizeRowLabelSize(row)
26         for col, col_name in enumerate(columns):
27             grid.SetColLabelValue(col, _(col_name))
28             grid.AutoSizeColLabelSize(col)
29             if col_name in ('ser_no', 'size'):
30                 cell_attr = wx.grid.GridCellAttr()
31                 cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx. ALIGN_CENTRE)
32                 grid.SetColAttr(col, cell_attr)
33         row = 0
34         for author in sorted(books_by_author):
35             books = books_by_author[author]
36             series = None
37             for book in books:
38                 if book.series != series:
39                     if book.series:
40                         value = book.series
41                     else:
42                         value = u'Вне серий'
43                     grid.SetCellAlignment(row, 0, wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
44                     grid.SetCellSize(row, 0, 1, len(columns))
45                     grid.SetCellValue(row, 0, u'%s — %s' % (book.author1, value))
46                     row += 1
47                     series = book.series
48                 for col, col_name in enumerate(columns):
49                     value = getattr(book, col_name)
50                     if value is None:
51                         value = u''
52                     elif not isinstance(value, (string_type, unicode_type)):
53                         value = str(value)
54                     grid.SetCellValue(row, col, value)
55                 row += 1
56         grid.AutoSizeColumns()
57         grid.AutoSizeRows()
58
59     def OnDClick(self, event):
60         pass
61
62     def OnKeyDown(self, event):
63         if event.GetKeyCode() == wx.WXK_ESCAPE:
64             self.Parent.Close()
65         else:
66             event.Skip()
67
68 class ListBooksWindow(GridWindow):
69
70     session_config_section_name = 'list_books'
71     window_title = u"m_Librarian: Список книг"
72     GridPanelClass = ListBooksPanel