]> git.phdru.name Git - m_librarian.git/blob - m_librarian/wx/ListAuthors.py
15cb59cf3c45c6054a7d9234745c6631b02bf557
[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 .AWindow import AWindow
8 from .ListBooks import ListBooksWindow
9
10
11 class ListAuthorsWindow(AWindow):
12
13     session_config_section_name = 'list_authors'
14     window_title = u"m_Librarian: Список авторов"
15
16     def __init__(self, parent, search_authors_results):
17         self.search_authors_results = search_authors_results
18         AWindow.__init__(self, parent)
19
20     def OnInit(self):
21         AWindow.OnInit(self)
22         ListAuthorsPanel(self, self.search_authors_results)
23
24
25 class ListAuthorsPanel(wx.Panel):
26
27     def __init__(self, parent, search_authors_results):
28         wx.Panel.__init__(self, parent)
29         self.search_authors_results = search_authors_results
30
31         list_authors_sizer = wx.BoxSizer(wx.VERTICAL)
32         self.SetSizer(list_authors_sizer)
33
34         self.grid = grid = wx.grid.Grid(self)
35         list_authors_sizer.Add(grid, 0, wx.EXPAND, 0)
36
37         self.InitGrid()
38
39     def InitGrid(self):
40         _ = getattr(translations, 'ugettext', None) or translations.gettext
41         authors = self.search_authors_results['authors']
42         columns = self.search_authors_results['columns']
43         grid = self.grid
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)):
60                     value = str(value)
61                 grid.SetCellValue(row, col, value)
62         grid.AutoSizeColumns()
63         grid.AutoSizeRows()
64
65         grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnDClick)
66         grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
67
68     def listBooks(self, row):
69         authors = self.search_authors_results['authors']
70         author = authors[row]
71         _books_by_author = books_by_author(author.id)
72         ListBooksWindow(self, _books_by_author)
73
74     def OnDClick(self, event):
75         row = event.GetRow()
76         self.listBooks(row)
77
78     def OnKeyDown(self, event):
79         if event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER):
80             row = self.grid.GetGridCursorRow()
81             self.listBooks(row)
82         else:
83             event.Skip()