From: Oleg Broytman Date: Sun, 7 Jan 2024 21:26:08 +0000 (+0300) Subject: Refactor(wx): Refactor common code X-Git-Tag: 0.3.0~9^2~11 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=80ba14a7bd17c4ddf1bd7c193e0bba6aa4d0c907;p=m_librarian.git Refactor(wx): Refactor common code [skip ci] --- diff --git a/m_librarian/wx/Grids.py b/m_librarian/wx/Grids.py new file mode 100644 index 0000000..415f684 --- /dev/null +++ b/m_librarian/wx/Grids.py @@ -0,0 +1,36 @@ +import wx, wx.grid # noqa: E401 multiple imports on one line +from .AWindow import AWindow + + +class GridPanel(wx.Panel): + + def __init__(self, parent, param): + wx.Panel.__init__(self, parent) + self.param = param + + vsizer = wx.BoxSizer(wx.VERTICAL) + self.SetSizer(vsizer) + + self.grid = grid = wx.grid.Grid(self) + vsizer.Add(grid, 0, wx.EXPAND, 0) + + self.InitGrid() + + def InitGrid(self): + raise NotImplementedError + + +class GridWindow(AWindow): + + # Subclasses must override these + session_config_section_name = None + window_title = None + GridPanelClass = GridPanel + + def __init__(self, parent, param): + self.param = param + AWindow.__init__(self, parent) + + def OnInit(self): + AWindow.OnInit(self) + self.GridPanelClass(self, self.param) diff --git a/m_librarian/wx/ListAuthors.py b/m_librarian/wx/ListAuthors.py index 15cb59c..3d73a28 100644 --- a/m_librarian/wx/ListAuthors.py +++ b/m_librarian/wx/ListAuthors.py @@ -4,42 +4,16 @@ 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 .Grids import GridWindow, GridPanel from .ListBooks import ListBooksWindow -class ListAuthorsWindow(AWindow): - - session_config_section_name = 'list_authors' - window_title = u"m_Librarian: Список авторов" - - def __init__(self, parent, search_authors_results): - self.search_authors_results = search_authors_results - AWindow.__init__(self, parent) - - def OnInit(self): - AWindow.OnInit(self) - ListAuthorsPanel(self, self.search_authors_results) - - -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) - - self.grid = grid = wx.grid.Grid(self) - list_authors_sizer.Add(grid, 0, wx.EXPAND, 0) - - self.InitGrid() +class ListAuthorsPanel(GridPanel): def InitGrid(self): _ = getattr(translations, 'ugettext', None) or translations.gettext - authors = self.search_authors_results['authors'] - columns = self.search_authors_results['columns'] + authors = self.param['authors'] + columns = self.param['columns'] grid = self.grid grid.CreateGrid(len(authors), len(columns)) grid.EnableEditing(False) @@ -66,7 +40,7 @@ class ListAuthorsPanel(wx.Panel): grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) def listBooks(self, row): - authors = self.search_authors_results['authors'] + authors = self.param['authors'] author = authors[row] _books_by_author = books_by_author(author.id) ListBooksWindow(self, _books_by_author) @@ -81,3 +55,10 @@ class ListAuthorsPanel(wx.Panel): self.listBooks(row) else: event.Skip() + + +class ListAuthorsWindow(GridWindow): + + session_config_section_name = 'list_authors' + window_title = u"m_Librarian: Список авторов" + GridPanelClass = ListAuthorsPanel diff --git a/m_librarian/wx/ListBooks.py b/m_librarian/wx/ListBooks.py index 955ef85..3bcd331 100644 --- a/m_librarian/wx/ListBooks.py +++ b/m_librarian/wx/ListBooks.py @@ -3,41 +3,15 @@ import wx from ..compat import string_type, unicode_type from ..translations import translations -from .AWindow import AWindow +from .Grids import GridWindow, GridPanel -class ListBooksWindow(AWindow): - - session_config_section_name = 'list_books' - window_title = u"m_Librarian: Список книг" - - def __init__(self, parent, books_by_author): - self.books_by_author = books_by_author - AWindow.__init__(self, parent) - - def OnInit(self): - AWindow.OnInit(self) - ListBooksPanel(self, self.books_by_author) - - -class ListBooksPanel(wx.Panel): - - def __init__(self, parent, books_by_author): - wx.Panel.__init__(self, parent) - self.books_by_author = books_by_author - - list_books_sizer = wx.BoxSizer(wx.VERTICAL) - self.SetSizer(list_books_sizer) - - self.grid = grid = wx.grid.Grid(self) - list_books_sizer.Add(grid, 0, wx.EXPAND, 0) - - self.InitGrid() +class ListBooksPanel(GridPanel): def InitGrid(self): _ = getattr(translations, 'ugettext', None) or translations.gettext - books_by_author = self.books_by_author['books_by_author'] - columns = self.books_by_author['columns'] + books_by_author = self.param['books_by_author'] + columns = self.param['columns'] author = next(iter(books_by_author)) books = books_by_author[author] series = {book.series for book in books} @@ -77,3 +51,10 @@ class ListBooksPanel(wx.Panel): row += 1 grid.AutoSizeColumns() grid.AutoSizeRows() + + +class ListBooksWindow(GridWindow): + + session_config_section_name = 'list_books' + window_title = u"m_Librarian: Список книг" + GridPanelClass = ListBooksPanel