From 19d77470ce619cddaaf0862144b300ec092c14c6 Mon Sep 17 00:00:00 2001 From: Jamie Cash Date: Wed, 3 Mar 2021 14:41:36 +0000 Subject: [PATCH] Settings dialog saves size and position --- config.yaml | 6 +++++ mt5_correlation/config.py | 48 +++++++++++++++++++++++++++++++++------ mt5_correlation/gui.py | 2 +- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/config.yaml b/config.yaml index fa1d7a2..9c47a21 100644 --- a/config.yaml +++ b/config.yaml @@ -63,4 +63,10 @@ window: width: 1459 height: 894 style: 541072960 +settings_window: + x: 42 + y: 132 + width: 1459 + height: 894 + style: 524352 ... \ No newline at end of file diff --git a/mt5_correlation/config.py b/mt5_correlation/config.py index e8f3ddc..718c37f 100644 --- a/mt5_correlation/config.py +++ b/mt5_correlation/config.py @@ -101,9 +101,20 @@ class SettingsDialog(wx.Dialog): # Store any settings that have changed changed_settings = {} - def __init__(self, *args, **kwargs): + def __init__(self, parent, exclude=None): + """ + Open the settings dialog + :param parent: The parent frame for this dialog + :param exclude: List of settings root nodes to exclude from this dialog + """ # Super Constructor - wx.Dialog.__init__(self, *args, **kwargs) + wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title="Settings", + pos=wx.Point(x=Config().get('settings_window.x'), + y=Config().get('settings_window.y')), + size=wx.Size(width=Config().get('settings_window.width'), + height=Config().get('settings_window.height')), + style=Config().get('settings_window.style')) + self.SetTitle("Settings") # Create logger and get config @@ -116,6 +127,11 @@ class SettingsDialog(wx.Dialog): # Dialog should be resizable self.SetWindowStyle(wx.RESIZE_BORDER) + # Settings to exclude. Just settings_window if None. Add settings_window if not specified. + exclude = ['settings_window'] if exclude is None else exclude + if 'settings_window' not in exclude: + exclude.append('settings_window') + # We want 2 vertical sections, the tabbed notebook and the buttons. The buttons sizer will have 2 horizontal # sections, one for each button. main_sizer = wx.BoxSizer(wx.VERTICAL) # Notebook panel @@ -129,11 +145,13 @@ class SettingsDialog(wx.Dialog): root_nodes = self.__settings.get_root_nodes() self.__tabs = [] for node in root_nodes: - # Create new tab - self.__tabs.append(SettingsTab(self, self.__notebook, node)) + # Exclude? + if node not in exclude: + # Create new tab + self.__tabs.append(SettingsTab(self, self.__notebook, node)) - # Add tab to notebook - self.__notebook.AddPage(self.__tabs[-1], node) + # Add tab to notebook + self.__notebook.AddPage(self.__tabs[-1], node) # Buttons button_ok = wx.Button(self, label="Update") @@ -151,6 +169,9 @@ class SettingsDialog(wx.Dialog): button_cancel.Bind(wx.EVT_BUTTON, self.__on_cancel) self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.__on_page_select) + # Bind window close event + self.Bind(wx.EVT_CLOSE, self.__on_close, self) + # Call on_page_select to select the first page self.__on_page_select(event=None) @@ -163,7 +184,7 @@ class SettingsDialog(wx.Dialog): # Clear changed settings and close self.changed_settings = {} self.EndModal(wx.ID_CANCEL) - self.Destroy() + self.Close() def __on_ok(self, event): # Update settings and save @@ -198,6 +219,19 @@ class SettingsDialog(wx.Dialog): self.EndModal(wx.ID_OK) self.Destroy() + def __on_close(self, event): + # Save pos and size + x, y = self.GetPosition() + width, height = self.GetSize() + self.__settings.set('settings_window.x', x) + self.__settings.set('settings_window.y', y) + self.__settings.set('settings_window.width', width) + self.__settings.set('settings_window.height', height) + + # Style + style = self.GetWindowStyle() + self.__settings.set('settings_window.style', style) + class SettingsTab(wx.Panel): """ diff --git a/mt5_correlation/gui.py b/mt5_correlation/gui.py index d3a6717..6e505a2 100644 --- a/mt5_correlation/gui.py +++ b/mt5_correlation/gui.py @@ -305,7 +305,7 @@ class MonitorFrame(wx.Frame): Opens the settings dialog :return: """ - settings_dialog = SettingsDialog(self, size=(500, 250)) + settings_dialog = SettingsDialog(parent=self, exclude=['window']) res = settings_dialog.ShowModal() if res == wx.ID_OK: # Reload relevant parts of app