3 import wx, wx.grid # noqa: E401 multiple imports on one line
4 from ..compat import string_type, unicode_type
5 from ..search import books_by_author
6 from ..translations import translations
7 from .AWindow import AWindow
8 from .ListBooks import ListBooksWindow
11 class ListAuthorsWindow(AWindow):
13 session_config_section_name = 'list_authors'
14 window_title = u"m_Librarian: Список авторов"
16 def __init__(self, parent, search_authors_results):
17 self.search_authors_results = search_authors_results
18 AWindow.__init__(self, parent)
22 ListAuthorsPanel(self, self.search_authors_results)
25 class ListAuthorsPanel(wx.Panel):
27 def __init__(self, parent, search_authors_results):
28 wx.Panel.__init__(self, parent)
29 self.search_authors_results = search_authors_results
31 list_authors_sizer = wx.BoxSizer(wx.VERTICAL)
32 self.SetSizer(list_authors_sizer)
34 self.grid = grid = wx.grid.Grid(self)
35 list_authors_sizer.Add(grid, 0, wx.EXPAND, 0)
40 _ = getattr(translations, 'ugettext', None) or translations.gettext
41 authors = self.search_authors_results['authors']
42 columns = self.search_authors_results['columns']
44 grid.CreateGrid(len(authors), len(columns))
45 grid.EnableEditing(False)
46 for row in range(len(authors)):
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 == 'count':
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, author in enumerate(authors):
57 for col, col_name in enumerate(columns):
58 value = getattr(author, col_name)
59 if not isinstance(value, (string_type, unicode_type)):
61 grid.SetCellValue(row, col, value)
62 grid.AutoSizeColumns()
65 grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnDClick)
66 grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
68 def listBooks(self, row):
69 authors = self.search_authors_results['authors']
71 _books_by_author = books_by_author(author.id)
72 ListBooksWindow(self, _books_by_author)
74 def OnDClick(self, event):
78 def OnKeyDown(self, event):
79 if event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER):
80 row = self.grid.GetGridCursorRow()