Skip to content

Commit

Permalink
Some cleanups to new-profile and load-profile handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
emersonrp committed Jun 3, 2024
1 parent 3db8c50 commit 0b70381
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
3 changes: 2 additions & 1 deletion BindControl.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ 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:
self.Layout()
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)
Expand Down
3 changes: 2 additions & 1 deletion Page/CustomBinds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 22 additions & 15 deletions Profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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")
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 0b70381

Please sign in to comment.