]> git.phdru.name Git - m_librarian.git/blob - m_librarian/wx/ListBooks.py
514301bcee967fe32bc3a5089077bd7223b71ea4
[m_librarian.git] / m_librarian / wx / ListBooks.py
1 # coding: utf-8
2
3 import wx, wx.grid  # noqa: E401 multiple imports on one line
4 from ..compat import string_type, unicode_type
5 from ..translations import translations
6 from .Grids import GridWindow, GridPanel
7
8
9 _ = getattr(translations, 'ugettext', None) or translations.gettext
10
11
12 class BooksDataTable(wx.grid.GridTableBase):
13     def __init__(self, rows_count, column_names):
14         wx.grid.GridTableBase.__init__(self)
15         self.rows_count = rows_count
16         self.column_names = column_names
17         self.data = []
18         for row in range(rows_count):
19             row_data = []
20             self.data.append(row_data)
21             for col in range(len(column_names)):
22                 row_data.append('')
23
24     # required methods for the wxPyGridTableBase interface
25
26     def GetNumberRows(self):
27         return self.rows_count
28
29     def GetNumberCols(self):
30         return len(self.column_names)
31
32     def IsEmptyCell(self, row, col):
33         return False
34
35     # Get/Set values in the table.  The Python version of these
36     # methods can handle any data-type, (as long as the Editor and
37     # Renderer understands the type too,) not just strings as in the
38     # C++ version.
39     def GetValue(self, row, col):
40         return self.data[row][col]
41
42     def SetValue(self, row, col, value):
43         self.data[row][col] = value
44
45     # Optional methods
46
47     # Called when the grid needs to display labels
48     def GetRowLabelValue(self, row):
49         return str(row)
50
51     def GetColLabelValue(self, col):
52         return _(self.column_names[col])
53
54     # Called to determine the kind of editor/renderer to use by
55     # default, doesn't necessarily have to be the same type used
56     # natively by the editor/renderer if they know how to convert.
57     def GetTypeName(self, row, col):
58         return wx.grid.GRID_VALUE_STRING
59
60
61 class ListBooksPanel(GridPanel):
62
63     def InitGrid(self):
64         books_by_author = self.param['books_by_author']
65         columns = self.param['columns']
66         total_rows = 0
67         for author in books_by_author:
68             books = books_by_author[author]
69             series = {book.series for book in books}
70             total_rows += len(books) + len(series) + 1
71         grid = self.grid
72         grid.SetTable(BooksDataTable(total_rows, columns), takeOwnership=True)
73         grid.EnableEditing(False)
74         for col, col_name in enumerate(columns):
75             grid.AutoSizeColLabelSize(col)
76             if col_name in ('ser_no', 'size'):
77                 cell_attr = wx.grid.GridCellAttr()
78                 cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx. ALIGN_CENTRE)
79                 grid.SetColAttr(col, cell_attr)
80         row = 0
81         for author in sorted(books_by_author):
82             grid.SetCellAlignment(row, 0, wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
83             grid.SetCellSize(row, 0, 1, len(columns))
84             grid.SetCellValue(row, 0, u'%s' % (author,))
85             row += 1
86             books = books_by_author[author]
87             series = None
88             for book in books:
89                 if book.series != series:
90                     if book.series:
91                         value = book.series
92                     else:
93                         value = u'Вне серий'
94                     grid.SetCellAlignment(row, 0,
95                                           wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
96                     grid.SetCellSize(row, 0, 1, len(columns))
97                     grid.SetCellValue(row, 0,
98                                       u'%s — %s' % (book.author1, value))
99                     row += 1
100                     series = book.series
101                 for col, col_name in enumerate(columns):
102                     value = getattr(book, col_name)
103                     if value is None:
104                         value = u''
105                     elif not isinstance(value, (string_type, unicode_type)):
106                         value = str(value)
107                     grid.SetCellValue(row, col, value)
108                 row += 1
109         grid.AutoSizeColumns()
110         grid.AutoSizeRows()
111
112     def OnDClick(self, event):
113         pass
114
115     def OnKeyDown(self, event):
116         if event.GetKeyCode() == wx.WXK_ESCAPE:
117             self.Parent.Close()
118         else:
119             event.Skip()
120
121
122 class ListBooksWindow(GridWindow):
123
124     session_config_section_name = 'list_books'
125     window_title = u"m_Librarian: Список книг"
126     GridPanelClass = ListBooksPanel