]> git.phdru.name Git - m_librarian.git/blob - m_librarian/wx/ListAuthors.py
Feat(wx/search): Catch Enter and double-click, open books 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 ..translations import translations
6 from .AWindow import AWindow
7 from .ListBooks import ListBooksWindow
8
9
10 class ListAuthorsWindow(AWindow):
11
12     session_config_section_name = 'list_authors'
13     window_title = u"m_Librarian: Список авторов"
14
15     def __init__(self, parent, search_authors_results):
16         self.search_authors_results = search_authors_results
17         AWindow.__init__(self, parent)
18
19     def OnInit(self):
20         AWindow.OnInit(self)
21         ListAuthorsPanel(self, self.search_authors_results)
22
23
24 class ListAuthorsPanel(wx.Panel):
25
26     def __init__(self, parent, search_authors_results):
27         wx.Panel.__init__(self, parent)
28         self.search_authors_results = search_authors_results
29
30         list_authors_sizer = wx.BoxSizer(wx.VERTICAL)
31         self.SetSizer(list_authors_sizer)
32
33         self.grid = grid = wx.grid.Grid(self)
34         list_authors_sizer.Add(grid, 0, wx.EXPAND, 0)
35
36         self.InitGrid()
37
38     def InitGrid(self):
39         _ = getattr(translations, 'ugettext', None) or translations.gettext
40         authors = self.search_authors_results['authors']
41         columns = self.search_authors_results['columns']
42         grid = self.grid
43         grid.CreateGrid(len(authors), len(columns))
44         grid.EnableEditing(False)
45         for row in range(len(authors)):
46             grid.SetRowLabelValue(row, str(row))
47             grid.AutoSizeRowLabelSize(row)
48         for col, col_name in enumerate(columns):
49             grid.SetColLabelValue(col, _(col_name))
50             grid.AutoSizeColLabelSize(col)
51             if col_name == 'count':
52                 cell_attr = wx.grid.GridCellAttr()
53                 cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx. ALIGN_CENTRE)
54                 grid.SetColAttr(col, cell_attr)
55         for row, author in enumerate(authors):
56             for col, col_name in enumerate(columns):
57                 value = getattr(author, col_name)
58                 if not isinstance(value, (string_type, unicode_type)):
59                     value = str(value)
60                 grid.SetCellValue(row, col, value)
61         grid.AutoSizeColumns()
62         grid.AutoSizeRows()
63
64         grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnDClick)
65         grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
66
67     def listBooks(self, row):
68         authors = self.search_authors_results['authors']
69         author = authors[row]
70         ListBooksWindow(self, author)
71
72     def OnDClick(self, event):
73         row = event.GetRow()
74         self.listBooks(row)
75
76     def OnKeyDown(self, event):
77         if event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER):
78             row = self.grid.GetGridCursorRow()
79             self.listBooks(row)
80         else:
81             event.Skip()