]> git.phdru.name Git - m_librarian.git/commitdiff
Refactor(wx): Split `MainWindow` to `AWindow`
authorOleg Broytman <phd@phdru.name>
Fri, 5 Jan 2024 19:10:33 +0000 (22:10 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 5 Jan 2024 19:19:27 +0000 (22:19 +0300)
[skip ci]

m_librarian/wx/AWindow.py [new file with mode: 0644]
m_librarian/wx/Application.py

diff --git a/m_librarian/wx/AWindow.py b/m_librarian/wx/AWindow.py
new file mode 100644 (file)
index 0000000..4621fd4
--- /dev/null
@@ -0,0 +1,84 @@
+# coding: utf-8
+
+import wx, wx.adv  # noqa: E401 multiple imports on one line
+from ..__version__ import __version__
+from .session_config import get_session_config
+
+
+class AWindow(wx.Frame):
+
+    '''
+    A universal parent class for all top-level application windows
+
+    Standard menu and ability to save/restore window size.
+    '''
+
+    # Subclasses should override these
+    session_config_section_name = None
+    window_title = None
+
+    def __init__(self, parent=None):
+        if self.session_config_section_name:
+            session_config = get_session_config()
+            width = session_config.getint(
+                self.session_config_section_name, 'width', 600)
+            height = session_config.getint(
+                self.session_config_section_name, 'height', 400)
+        else:
+            width = 600
+            height = 400
+        wx.Frame.__init__(
+            self,
+            parent=parent, id=-1, title=self.window_title,
+            size=wx.Size(width=width, height=height),
+        )
+        self.OnInit()
+        self.Show(True)
+
+    def OnInit(self):
+        self.InitMenu()
+        if self.session_config_section_name:
+            self.Bind(wx.EVT_SIZE, self.OnSize)
+
+    def InitMenu(self):
+        MenuBar = wx.MenuBar()
+        self.SetMenuBar(MenuBar)
+
+        file_menu = wx.Menu()
+        exit = file_menu.Append(wx.ID_EXIT, u"&Выход", u"Выйти из программы")
+        self.Bind(wx.EVT_MENU, self.OnQuit, exit)
+        MenuBar.Append(file_menu, u"&Файл")
+
+        about_menu = wx.Menu()
+        about = about_menu.Append(wx.ID_ABOUT,
+                                  u"&О m_Librarian", u"О m_Librarian")
+        self.Bind(wx.EVT_MENU, self.OnAbout, about)
+        MenuBar.Append(about_menu, u"&О программе")
+
+    def OnQuit(self, event):
+        self.Close(True)
+
+    def OnAbout(self, event):
+        aboutInfo = wx.adv.AboutDialogInfo()
+        aboutInfo.SetName(u'm_Librarian')
+        aboutInfo.SetVersion(__version__)
+        aboutInfo.SetDescription(
+            u'Библиотекарь для библиотек LibRusEc/Flibusta')
+        aboutInfo.AddDeveloper(u'Олег Бройтман')
+        aboutInfo.SetWebSite(
+            u'https://phdru.name/Software/Python/m_librarian/')
+        aboutInfo.SetCopyright(u'(C) 2023, 2024 Олег Бройтман')
+        aboutInfo.SetLicense(u'GPL')
+        wx.adv.AboutBox(aboutInfo)
+
+    def OnSize(self, event):
+        """Save window size in the session config"""
+        if self.session_config_section_name:
+            size = event.GetSize()
+            session_config = get_session_config()
+            session_config.set(
+                self.session_config_section_name, 'width', str(size.width))
+            session_config.set(
+                self.session_config_section_name, 'height', str(size.height))
+            session_config.save()
+        event.Skip()  # Call other handlers
index 489924ddcd360083f13b534ea355a2ffdab883f5..b7ad92dacb05bceebfc63e7b8d1ae02067625e33 100644 (file)
@@ -1,65 +1,16 @@
-# coding: utf-8
-
-import wx, wx.adv  # noqa: E401 multiple imports on one line
-from ..__version__ import __version__
+import wx
+from .AWindow import AWindow
 from .SearchPanel import SearchPanel
-from .session_config import get_session_config
-
-
-class MainWindow(wx.Frame):
-
-    def __init__(self):
-        session_config = get_session_config()
-        width = session_config.getint('main_window', 'width', 600)
-        height = session_config.getint('main_window', 'height', 400)
-        super(wx.Frame, self).__init__(
-            parent=None, id=-1, title=u"m_Librarian",
-            size=wx.Size(width=width, height=height),
-        )
-        self.InitMenu()
-        self.search_panel = search_panel = SearchPanel(self)
-        self.Show(True)
-        self.Bind(wx.EVT_SIZE, self.OnSize)
 
-    def InitMenu(self):
-        MenuBar = wx.MenuBar()
-        self.SetMenuBar(MenuBar)
 
-        file_menu = wx.Menu()
-        exit = file_menu.Append(wx.ID_EXIT, u"&Выход", u"Выйти из программы")
-        self.Bind(wx.EVT_MENU, self.OnQuit, exit)
-        MenuBar.Append(file_menu, u"&Файл")
+class MainWindow(AWindow):
 
-        about_menu = wx.Menu()
-        about = about_menu.Append(wx.ID_ABOUT,
-                                  u"&О m_Librarian", u"О m_Librarian")
-        self.Bind(wx.EVT_MENU, self.OnAbout, about)
-        MenuBar.Append(about_menu, u"&О программе")
+    session_config_section_name = 'main_window'
+    window_title = u"m_Librarian"
 
-    def OnQuit(self, event):
-        self.Close(True)
-
-    def OnAbout(self, event):
-        aboutInfo = wx.adv.AboutDialogInfo()
-        aboutInfo.SetName(u'm_Librarian')
-        aboutInfo.SetVersion(__version__)
-        aboutInfo.SetDescription(
-            u'Библиотекарь для библиотек LibRusEc/Flibusta')
-        aboutInfo.AddDeveloper(u'Олег Бройтман')
-        aboutInfo.SetWebSite(
-            u'https://phdru.name/Software/Python/m_librarian/')
-        aboutInfo.SetCopyright(u'(C) 2023, 2024 Олег Бройтман')
-        aboutInfo.SetLicense(u'GPL')
-        wx.adv.AboutBox(aboutInfo)
-
-    def OnSize(self, event):
-        """Save window size in the session config"""
-        size = event.GetSize()
-        session_config = get_session_config()
-        session_config.set('main_window', 'width', str(size.width))
-        session_config.set('main_window', 'height', str(size.height))
-        session_config.save()
-        event.Skip()  # Call other handlers
+    def OnInit(self):
+        AWindow.OnInit(self)
+        self.search_panel = SearchPanel(self)
 
 
 class Application(wx.App):