]> git.phdru.name Git - m_librarian.git/blob - m_librarian/wx/ListAuthors.py
Feat(wx): Catch `Escape` in the grid and close the window
[m_librarian.git] / m_librarian / wx / ListAuthors.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 ..search import books_by_author
6 from ..translations import translations
7 from .Grids import GridWindow, GridPanel
8 from .ListBooks import ListBooksWindow
9
10
11 class ListAuthorsPanel(GridPanel):
12
13     def InitGrid(self):
14         _ = getattr(translations, 'ugettext', None) or translations.gettext
15         authors = self.param['authors']
16         columns = self.param['columns']
17         grid = self.grid
18         grid.CreateGrid(len(authors), len(columns))
19         grid.EnableEditing(False)
20         for row in range(len(authors)):
21             grid.SetRowLabelValue(row, str(row))
22             grid.AutoSizeRowLabelSize(row)
23         for col, col_name in enumerate(columns):
24             grid.SetColLabelValue(col, _(col_name))
25             grid.AutoSizeColLabelSize(col)
26             if col_name == 'count':
27                 cell_attr = wx.grid.GridCellAttr()
28                 cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx. ALIGN_CENTRE)
29                 grid.SetColAttr(col, cell_attr)
30         for row, author in enumerate(authors):
31             for col, col_name in enumerate(columns):
32                 value = getattr(author, col_name)
33                 if not isinstance(value, (string_type, unicode_type)):
34                     value = str(value)
35                 grid.SetCellValue(row, col, value)
36         grid.AutoSizeColumns()
37         grid.AutoSizeRows()
38
39     def listBooks(self, row):
40         authors = self.param['authors']
41         author = authors[row]
42         _books_by_author = books_by_author(author.id)
43         ListBooksWindow(self, _books_by_author)
44
45     def OnDClick(self, event):
46         row = event.GetRow()
47         self.listBooks(row)
48
49     def OnKeyDown(self, event):
50         if event.GetKeyCode() == wx.WXK_ESCAPE:
51             self.Parent.Close()
52         elif event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER):
53             row = self.grid.GetGridCursorRow()
54             self.listBooks(row)
55         else:
56             event.Skip()
57
58
59 class ListAuthorsWindow(GridWindow):
60
61     session_config_section_name = 'list_authors'
62     window_title = u"m_Librarian: Список авторов"
63     GridPanelClass = ListAuthorsPanel