Commit beb9e412 authored by Taylor Hanayik's avatar Taylor Hanayik
Browse files

more modular layout functions

parent e955cb66
......@@ -14,73 +14,57 @@ import fsl.gui.exceptions as fslerrs
import fsl.gui.views as fslviews
import fsl.gui.widgets as fslwidgets
# keyToWidgetMap = {
# 'column' : fslwidgets.column,
# 'row' : fslwidgets.row,
# 'group' : fslwidgets.group,
# 'filepath' : fslwidgets.filepath,
# 'checkbox' : fslwidgets.checkbox,
# 'choice' : fslwidgets.choice,
# 'number' : fslwidgets.number,
# 'button' : fslwidgets.button,
# }
# allowedWidgets = (keyToWidgetMap.keys())
allowedContainerWidgets = (
'column', 'row', 'group',
)
allowedWidgets = (
'column', 'row', 'group', 'filepath',
'checkbox', 'choice', 'number', 'button'
*allowedContainerWidgets,
'filepath', 'checkbox', 'choice',
'number', 'button',
)
allowedContainerWidgets = (
'column', 'row', 'group',
)
allowedConfigKeys = (
'appName', 'windowSize', 'width', 'height',
'layout',
)
'appName', 'windowSize', 'width',
'height', 'layout',
)
allowedKeys = (
*allowedConfigKeys,
*allowedWidgets,
)
*allowedConfigKeys,
*allowedWidgets,
)
def isGroup(key):
parts = key.split(sep='_')
g = parts[0]
if g == "group":
"""
returns True is key == "group"
returns False otherwise
"""
if key == "group":
return True
else:
return False
def isContainer(key):
if isGroup(key):
key = "group"
"""
returns True if this key is associated with a container widget
from allowedContainerWidgets.
returns False otherwise
a container widget is not really intereactive for the user.
it holds other widgets as children
"""
if key in allowedContainerWidgets:
return True
else:
return False
def getGroupName(key):
if not isGroup(key):
raise Exception("group must be in the forms 'group' or 'group_name'")
parts = key.split(sep="_")
if len(parts) == 2:
n = parts[-1]
elif len(parts) > 2:
raise Exception("group must be in the forms 'group' or 'group_name'")
else:
n = ""
return n
def load_spec(specFile):
def loadSpec(specFile):
"""
return the loaded yaml data as a dict
"""
with open(specFile) as sf:
spec = yaml.load(sf, Loader=yaml.FullLoader)
return spec
......@@ -94,26 +78,15 @@ def checkSpec(buildSpec):
for entry in d:
print(entry)
for k, v in entry.items():
if (k not in allowedKeys) & (not isGroup(k)):
k, _ = parseWidgetKey(k)
if k not in allowedKeys:
raise fslerrs.NotAValidKey("{} is not an allowed buildSpec key".format(k))
if type(v) is dict:
dictCheck(v)
dictCheck(buildSpec)
def widgetFromKey(key):
"""
return an appropriate widget for a given key
"""
if isGroup(key):
key = "group"
if key in keyToWidgetMap:
return keyToWidgetMap[key]
else:
raise fslerrs.NotAValidKey("{} is not an allowed buildSpec key".format(key))
def _layout_from(widget):
def layoutFrom(widget):
"""
redo layout of all widgets up the parent tree from this widget.
Stop when we get to a frame. This was taken from a wx wiki post
......@@ -125,34 +98,62 @@ def _layout_from(widget):
if widget.IsTopLevel():
break
def parseWidgetKey(key):
"""
returns string tuple (key, tag)
the string "key" must not contain more than one underscore
"""
parts = key.split(sep="_")
t = ""
if len(parts) == 1:
n = parts[0]
elif len(parts) == 2:
n = parts[0]
t = parts[-1]
elif len(parts) > 2:
raise Exception("keys must be in the forms 'key' or 'key_name'")
else:
n = ""
return n, t
def makeWidget(parent, propObj, key, tag, value):
"""
returns the appropriate widget from a key string
key: str parsed from the form "key_tag" or "key"
tag: str parsed from the form "key_tag"
value: the dict value for this key. Only value==dict is used here
"""
if key in allowedWidgets:
wid = getattr(fslwidgets, key)
if isinstance(value, dict):
w = wid(parent, propObj, **value)
else:
if isGroup(key):
w = wid(parent, tag)
else:
if isinstance(parent.GetSizer(), wx.StaticBoxSizer):
w = wid(parent.GetSizer().GetStaticBox())
else:
w = wid(parent)
return w
def layout(parent, buildSpec, propObj):
"""
layout all allowed widgets in a buildSpec
"""
for entry in buildSpec:
for k, v in entry.items():
if (k in allowedWidgets) or (isGroup(k)):
# wid = widgetFromKey(k)
if isGroup(k):
wid = getattr(fslwidgets, 'group')
else:
wid = getattr(fslwidgets, k)
if isinstance(v, dict):
w = wid(parent, propObj, **v)
else:
if isGroup(k):
w = wid(parent, getGroupName(k))
else:
if isinstance(parent.GetSizer(), wx.StaticBoxSizer):
w = wid(parent.GetSizer().GetStaticBox())
else:
w = wid(parent)
parent.GetSizer().Add(w, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
_layout_from(w)
if isContainer(k):
if type(v) is list:
layout(w, v, propObj)
k, t = parseWidgetKey(k)
w = makeWidget(parent, propObj, k, t, v,)
parent.GetSizer().Add(w, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
layoutFrom(w)
if isContainer(k):
if type(v) is list:
layout(w, v, propObj)
parent.Layout()
return parent
......
......@@ -90,7 +90,7 @@ def main():
# get an app instance
app = wx.App()
pnm = Pnm()
pnmView = core.load_spec(fslViews.pnm)
pnmView = core.loadSpec(fslViews.pnm)
gui = core.buildGUI(pnmView, pnm)
app.MainLoop()
if __name__ == "__main__":
......
......@@ -174,7 +174,7 @@ def _embed(parent=None, make_fsleyesframe=True, **kwargs):
raise RuntimeError('Failed to start FSLeyes')
return ret[0]
def layout_from(widget):
def layout_from_deprecated(widget):
"""
redo layout of all widgets up the parent tree from this widget.
Stop when we get to a frame. This was taken from a wx wiki post
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment