]> git.phdru.name Git - m_librarian.git/blob - m_librarian/wx/ListAuthors.py
3d73a2827c7d3949707afe07298b43ffe930b15e
[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         grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnDClick)
40         grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
41
42     def listBooks(self, row):
43         authors = self.param['authors']
44         author = authors[row]
45         _books_by_author = books_by_author(author.id)
46         ListBooksWindow(self, _books_by_author)
47
48     def OnDClick(self, event):
49         row = event.GetRow()
50         self.listBooks(row)
51
52     def OnKeyDown(self, event):
53         if event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER):
54             row = self.grid.GetGridCursorRow()
55             self.listBooks(row)
56         else:
57             event.Skip()
58
59
60 class ListAuthorsWindow(GridWindow):
61
62     session_config_section_name = 'list_authors'
63     window_title = u"m_Librarian: Список авторов"
64     GridPanelClass = ListAuthorsPanel