Configtool: introduce boolChoices.

This handles sets of booleans "magically" and is now used for
DISPLAY_BUS and DISPLAY_TYPE.
This commit is contained in:
Markus Hitter 2016-04-18 15:55:35 +02:00
parent a95a1c8855
commit 2541d7a82c
2 changed files with 51 additions and 39 deletions

View File

@ -9,55 +9,20 @@ class DisplayPage(wx.Panel, Page):
self.parent = parent
self.id = idPg
self.displayBusKeys = ['DISPLAY_BUS_4BIT', 'DISPLAY_BUS_8BIT',
'DISPLAY_BUS_I2C', 'DISPLAY_BUS_SPI']
self.displayTypeKeys = ['DISPLAY_TYPE_SSD1306', 'DISPLAY_TYPE_LCD1302']
self.labels = {'DISPLAY_BUS': "Display Bus:",
'DISPLAY_TYPE': "Display Type:"}
sz = wx.GridBagSizer()
sz.AddSpacer((20, 40), pos = (0, 0))
k = 'DISPLAY_BUS'
ch = self.addChoice(k, ['(disabled)'] + self.displayBusKeys, 0, 100,
self.onChoice, size = (140, -1))
ch = self.addBoolChoice('DISPLAY_BUS', True, 100, self.onChoice,
size = (140, -1))
sz.Add(ch, pos = (1, 1))
sz.AddSpacer((100, 10), pos = (1, 2))
k = 'DISPLAY_TYPE'
ch = self.addChoice(k, self.displayTypeKeys, 0, 100, self.onChoice,
size = (200, -1))
ch = self.addBoolChoice('DISPLAY_TYPE', False, 100, self.onChoice,
size = (200, -1))
sz.Add(ch, pos = (1, 3))
self.SetSizer(sz)
self.enableAll(False)
def insertValues(self, cfgValues):
Page.insertValues(self, cfgValues)
k = 'DISPLAY_BUS'
for tag in self.displayBusKeys:
if tag in cfgValues.keys() and cfgValues[tag]:
self.setChoice(k, cfgValues, tag)
break
k = 'DISPLAY_TYPE'
for tag in self.displayTypeKeys:
if tag in cfgValues.keys() and cfgValues[tag]:
self.setChoice(k, cfgValues, tag)
break
def getValues(self):
result = Page.getValues(self)
# Convert values to a set of booleans.
for k in self.choices:
del result[k]
choice = self.choices[k]
for i in range(choice.GetCount()):
result[choice.GetString(i)] = (i == choice.GetSelection())
return result

View File

@ -17,6 +17,7 @@ class Page:
self.radioButtonBoxes = {}
self.choices = {}
self.choicesOriginal = {}
self.boolChoices = {}
self.deco = Decoration()
self.font = font
@ -116,6 +117,30 @@ class Page:
return lsz
# addChoice handles #defines with value, this handles choices for
# sets of boolean #defines.
def addBoolChoice(self, name, allowBlank, labelWidth, validator,
size = (-1, -1)):
lsz = wx.BoxSizer(wx.HORIZONTAL)
st = wx.StaticText(self, wx.ID_ANY, self.labels[name],
size = (labelWidth, -1), style = wx.ALIGN_RIGHT)
st.SetFont(self.font)
lsz.Add(st, 1, wx.TOP, offsetChLabel)
ch = wx.Choice(self, wx.ID_ANY, size = size, name = name)
ch.SetBackgroundColour(self.deco.getBackgroundColour())
ch.SetFont(self.font)
ch.Bind(wx.EVT_CHOICE, validator)
if allowBlank:
ch.Append("(none)")
ch.SetSelection(0)
lsz.Add(ch)
self.boolChoices[name] = ch
return lsz
def setChoice(self, name, cfgValues, default):
if name in cfgValues.keys() and cfgValues[name][1] == True:
bv = cfgValues[name][0]
@ -235,6 +260,21 @@ class Page:
else:
print "Key " + k + " not found in config data."
for k in self.boolChoices.keys():
choice = self.boolChoices[k]
# Remove items left behind from the previous configuration.
while (choice.GetCount() and
not choice.GetString(choice.GetCount() - 1).startswith('(')):
choice.Delete(choice.GetCount() - 1)
# Add items found in this configuration.
for cfg in cfgValues.keys():
if cfg.startswith(k):
choice.Append(cfg)
if cfgValues[cfg]:
choice.SetSelection(choice.GetCount() - 1)
self.assertModified(False)
def getValues(self):
@ -262,6 +302,13 @@ class Page:
v = self.choices[k].GetSelection()
result[k] = self.choices[k].GetString(v), True
for k in self.boolChoices.keys():
choice = self.boolChoices[k]
for i in range(choice.GetCount()):
s = choice.GetString(i)
if not s.startswith('('):
result[s] = (i == choice.GetSelection())
return result
def assertModified(self, flag):