]> git.phdru.name Git - m_librarian.git/blob - m_librarian/wx/ListBooks.py
b027de006e89282b5bf6821979c34b598221c05d
[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) + 1
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             grid.SetCellAlignment(row, 0, wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
36             grid.SetCellSize(row, 0, 1, len(columns))
37             grid.SetCellValue(row, 0, u'%s' % (author,))
38             row += 1
39             books = books_by_author[author]
40             series = None
41             for book in books:
42                 if book.series != series:
43                     if book.series:
44                         value = book.series
45                     else:
46                         value = u'Вне серий'
47                     grid.SetCellAlignment(row, 0,
48                                           wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
49                     grid.SetCellSize(row, 0, 1, len(columns))
50                     grid.SetCellValue(row, 0,
51                                       u'%s — %s' % (book.author1, value))
52                     row += 1
53                     series = book.series
54                 for col, col_name in enumerate(columns):
55                     value = getattr(book, col_name)
56                     if value is None:
57                         value = u''
58                     elif not isinstance(value, (string_type, unicode_type)):
59                         value = str(value)
60                     grid.SetCellValue(row, col, value)
61                 row += 1
62         grid.AutoSizeColumns()
63         grid.AutoSizeRows()
64
65     def OnDClick(self, event):
66         pass
67
68     def OnKeyDown(self, event):
69         if event.GetKeyCode() == wx.WXK_ESCAPE:
70             self.Parent.Close()
71         else:
72             event.Skip()
73
74
75 class ListBooksWindow(GridWindow):
76
77     session_config_section_name = 'list_books'
78     window_title = u"m_Librarian: Список книг"
79     GridPanelClass = ListBooksPanel