From 0b70381818d404239f24a06e74ab45af57e63476 Mon Sep 17 00:00:00 2001 From: Russell Pickett Date: Mon, 3 Jun 2024 12:37:57 -0400 Subject: [PATCH] Some cleanups to new-profile and load-profile handling This commit now doesn't "new profile" just before load profile, in case the user changes their mind and clicks "cancel." Also loading a profile is more thorough in scrubbing controls to a neutral state while loading. Finally, it fixes a couple bugs triggered by adding a new sizer for the "rename bind" button. --- BindControl.py | 3 ++- Page/CustomBinds.py | 3 ++- Profile.py | 37 ++++++++++++++++++++++--------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/BindControl.py b/BindControl.py index 5ca09628..5a01d3bc 100755 --- a/BindControl.py +++ b/BindControl.py @@ -173,6 +173,8 @@ def OnNewProfile(self, _): defaultProfile = Path(self.Profile.ProfilePath() / 'Default.bcp') self.Profile.doLoadFromFile(defaultProfile) + self.Profile.General.SetState('Name', '') + self.Profile.Filename = None except Exception as e: wx.LogError(f"Something broke in new profile: {e}") finally: @@ -180,7 +182,6 @@ def OnNewProfile(self, _): self.Thaw() def OnProfileLoad(self, evt) : - self.OnNewProfile(evt) self.Profile.LoadFromFile(evt) def OnProfileSave(self, evt) : self.Profile.doSaveToFile(evt) def OnProfileSaveAs(self, evt) : self.Profile.SaveToFile(evt) diff --git a/Page/CustomBinds.py b/Page/CustomBinds.py index 8cf54904..f747e5f5 100644 --- a/Page/CustomBinds.py +++ b/Page/CustomBinds.py @@ -84,6 +84,7 @@ def AddBindToPage(self, bindpane = None): deleteButton = wx.Button(self.scrolledPane, -1, "X", size = [40, -1]) deleteButton.SetForegroundColour(wx.RED) setattr(deleteButton, "BindPane", bindpane) + setattr(deleteButton, "BindSizer", bindSizer) setattr(bindpane, "DelButton", deleteButton) deleteButton.SetToolTip(f'Delete bind "{bindpane.Title}"') deleteButton.Bind(wx.EVT_BUTTON, self.OnDeleteButton) @@ -143,7 +144,7 @@ def OnDeleteButton(self, evt): delButton = evt.EventObject bindname = delButton.BindPane.Title if wx.MessageBox(f'Delete Bind "{bindname}"?', 'Delete Bind', wx.YES_NO) == wx.NO: return - sizer = delButton.GetContainingSizer() + sizer = delButton.BindSizer self.PaneSizer.Hide(sizer) self.PaneSizer.Remove(sizer) self.Panes.remove(delButton.BindPane) diff --git a/Profile.py b/Profile.py index b94c0c3d..d7382bf7 100644 --- a/Profile.py +++ b/Profile.py @@ -16,7 +16,7 @@ from Page.CustomBinds import CustomBinds import UI -from UI.ControlGroup import bcKeyButton +from UI.ControlGroup import bcKeyButton, cgStaticText, cgSpinCtrl, cgSpinCtrlDouble from UI.SimpleBindPane import SimpleBindPane from UI.BufferBindPane import BufferBindPane from UI.ComplexBindPane import ComplexBindPane @@ -111,7 +111,7 @@ def SaveAsDefault(self, _ = None): self.Filename = Path(self.ProfilePath() / 'Default.bcp') if self.Filename.exists(): - result = wx.MessageBox("Overwrite Default Profile?", "Default Profile Exists", wx.YES_NO) + result = wx.MessageBox("Overwrite Default Profile?", "Profile Exists", wx.YES_NO) if result == wx.NO: return self.doSaveToFile() @@ -198,7 +198,8 @@ def doSaveToFile(self, _ = None): bindui = pane.GetSizer().GetChildren() if bindui: bindpane = bindui[0].GetWindow() - savedata['CustomBinds'].append(bindpane.Serialize()) + if bindpane: + savedata['CustomBinds'].append(bindpane.Serialize()) dumpstring = json.dumps(savedata, indent=2) try: @@ -211,11 +212,10 @@ def doSaveToFile(self, _ = None): wx.LogError(f"Problem saving to profile '{savefile}': {e}") def LoadFromFile(self, _): - with wx.FileDialog(self, "Open Profile file", - wildcard="Bindcontrol Profiles (*.bcp)|*.bcp|All Files (*.*)|*.*", + wildcard = "Bindcontrol Profiles (*.bcp)|*.bcp|All Files (*.*)|*.*", defaultDir = str(self.ProfilePath()), - style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog: + style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog: if fileDialog.ShowModal() == wx.ID_CANCEL: wx.LogMessage("User canceled loading profile") @@ -246,16 +246,15 @@ def doLoadFromFile(self, pathname): value = None if pagename in data: value = data[pagename].get(controlname, None) - if value is None: continue # look up what type of control it is to know how to update its value if isinstance(control, wx.DirPickerCtrl): - control.SetPath(value) + control.SetPath(value if value else '') elif isinstance(control, bcKeyButton): - control.SetLabel(value) - control.Key = value + control.SetLabel(value if value else '') + control.Key = value if value else '' elif isinstance(control, wx.Button): - control.SetLabel(value) + control.SetLabel(value if value else '') elif isinstance(control, wx.ColourPickerCtrl) or isinstance(control, csel.ColourSelect): control.SetColour(value) if isinstance(control, csel.ColourSelect): @@ -267,9 +266,15 @@ def doLoadFromFile(self, pathname): if isinstance(value, str): value = control.FindString(value) if value == wx.NOT_FOUND: value = 0 - control.SetSelection(value) + control.SetSelection(value if value else 0) + elif isinstance(control, wx.CheckBox): + control.SetValue(value if value else False) + elif isinstance(control, cgStaticText): + continue + elif isinstance(control, cgSpinCtrl) or isinstance(control, cgSpinCtrlDouble): + control.SetValue(value if value else page.Init.get(controlname, 0)) else: - control.SetValue(value) + control.SetValue(value if value else '') if pagename == 'General': page.IncarnateBox.FillWith(data) @@ -296,14 +301,16 @@ def doLoadFromFile(self, pathname): cbpage.scrolledPane.DestroyChildren() for custombind in data['CustomBinds']: if not custombind: continue + + bindpane = None if custombind['Type'] == "SimpleBind": bindpane = SimpleBindPane(cbpage, init = custombind) - cbpage.AddBindToPage(bindpane = bindpane) elif custombind['Type'] == "BufferBind": bindpane = BufferBindPane(cbpage, init = custombind) - cbpage.AddBindToPage(bindpane = bindpane) elif custombind['Type'] == "ComplexBind": bindpane = ComplexBindPane(cbpage, init = custombind) + + if bindpane: cbpage.AddBindToPage(bindpane = bindpane) self.Filename = Path(pathname)