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
9 _ = getattr(translations, 'ugettext', None) or translations.gettext
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
18 for row in range(rows_count):
20 self.data.append(row_data)
21 for col in range(len(column_names)):
24 # required methods for the wxPyGridTableBase interface
26 def GetNumberRows(self):
27 return self.rows_count
29 def GetNumberCols(self):
30 return len(self.column_names)
32 def IsEmptyCell(self, row, col):
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
39 def GetValue(self, row, col):
40 return self.data[row][col]
42 def SetValue(self, row, col, value):
43 self.data[row][col] = value
47 # Called when the grid needs to display labels
48 def GetRowLabelValue(self, row):
51 def GetColLabelValue(self, col):
52 return _(self.column_names[col])
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):
59 return wx.grid.GRID_VALUE_BOOL
61 return wx.grid.GRID_VALUE_STRING
63 # Called to determine how the data can be fetched and stored by the
64 # editor and renderer. This allows you to enforce some type-safety
66 def CanGetValueAs(self, row, col, typeName):
67 colType = self.GetTypeName(row, col)
68 if typeName == colType:
73 def CanSetValueAs(self, row, col, typeName):
74 return self.CanGetValueAs(row, col, typeName)
77 class ListBooksPanel(GridPanel):
80 books_by_author = self.param['books_by_author']
81 columns = self.param['columns']
82 columns.insert(0, u'Выбрать')
84 for author in books_by_author:
85 books = books_by_author[author]
86 series = {book.series for book in books}
87 total_rows += len(books) + len(series) + 1
89 grid.SetTable(BooksDataTable(total_rows, columns), takeOwnership=True)
90 grid.EnableEditing(False)
91 for col, col_name in enumerate(columns):
92 grid.AutoSizeColLabelSize(col)
94 cell_attr = wx.grid.GridCellAttr()
95 cell_attr.SetAlignment(wx.ALIGN_CENTRE, wx. ALIGN_CENTRE)
96 grid.SetColAttr(col, cell_attr)
97 elif col_name in ('ser_no', 'size'):
98 cell_attr = wx.grid.GridCellAttr()
99 cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx. ALIGN_CENTRE)
100 grid.SetColAttr(col, cell_attr)
102 for author in sorted(books_by_author):
103 grid.SetCellAlignment(row, 1, wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
104 grid.SetCellSize(row, 1, 1, len(columns)-1)
105 grid.SetCellValue(row, 1, u'%s' % (author,))
107 books = books_by_author[author]
110 if book.series != series:
115 grid.SetCellAlignment(row, 1,
116 wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
117 grid.SetCellSize(row, 1, 1, len(columns)-1)
118 grid.SetCellValue(row, 1,
119 u'%s — %s' % (book.author1, value))
122 for col, col_name in enumerate(columns[1:]):
123 value = getattr(book, col_name)
126 elif not isinstance(value, (string_type, unicode_type)):
128 grid.SetCellValue(row, col+1, value)
130 grid.AutoSizeColumns()
133 def OnDClick(self, event):
136 def OnKeyDown(self, event):
137 if event.GetKeyCode() == wx.WXK_ESCAPE:
143 class ListBooksWindow(GridWindow):
145 session_config_section_name = 'list_books'
146 window_title = u"m_Librarian: Список книг"
147 GridPanelClass = ListBooksPanel