]> git.phdru.name Git - m_librarian.git/blobdiff - m_librarian/wx/ListAuthors.py
Feat(wx/books): List books for an author
[m_librarian.git] / m_librarian / wx / ListAuthors.py
index 89c294529da74668e26af471e272c5f96f9a962f..15cb59cf3c45c6054a7d9234745c6631b02bf557 100644 (file)
@@ -2,8 +2,10 @@
 
 import wx, wx.grid  # noqa: E401 multiple imports on one line
 from ..compat import string_type, unicode_type
+from ..search import books_by_author
 from ..translations import translations
 from .AWindow import AWindow
+from .ListBooks import ListBooksWindow
 
 
 class ListAuthorsWindow(AWindow):
@@ -24,18 +26,21 @@ class ListAuthorsPanel(wx.Panel):
 
     def __init__(self, parent, search_authors_results):
         wx.Panel.__init__(self, parent)
+        self.search_authors_results = search_authors_results
+
         list_authors_sizer = wx.BoxSizer(wx.VERTICAL)
         self.SetSizer(list_authors_sizer)
 
-        grid = wx.grid.Grid(self)
+        self.grid = grid = wx.grid.Grid(self)
         list_authors_sizer.Add(grid, 0, wx.EXPAND, 0)
 
-        self.InitGrid(grid, search_authors_results)
+        self.InitGrid()
 
-    def InitGrid(self, grid, search_authors_results):
+    def InitGrid(self):
         _ = getattr(translations, 'ugettext', None) or translations.gettext
-        authors = search_authors_results['authors']
-        columns = search_authors_results['columns']
+        authors = self.search_authors_results['authors']
+        columns = self.search_authors_results['columns']
+        grid = self.grid
         grid.CreateGrid(len(authors), len(columns))
         grid.EnableEditing(False)
         for row in range(len(authors)):
@@ -56,3 +61,23 @@ class ListAuthorsPanel(wx.Panel):
                 grid.SetCellValue(row, col, value)
         grid.AutoSizeColumns()
         grid.AutoSizeRows()
+
+        grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnDClick)
+        grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
+
+    def listBooks(self, row):
+        authors = self.search_authors_results['authors']
+        author = authors[row]
+        _books_by_author = books_by_author(author.id)
+        ListBooksWindow(self, _books_by_author)
+
+    def OnDClick(self, event):
+        row = event.GetRow()
+        self.listBooks(row)
+
+    def OnKeyDown(self, event):
+        if event.GetKeyCode() in (wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER):
+            row = self.grid.GetGridCursorRow()
+            self.listBooks(row)
+        else:
+            event.Skip()