diff --git a/examples/parametertree.py b/examples/parametertree.py index c4aca740..7f8f7039 100644 --- a/examples/parametertree.py +++ b/examples/parametertree.py @@ -81,7 +81,6 @@ class TextParameterItem(pTypes.WidgetParameterItem): return self.textBox class TextParameter(Parameter): - type = 'text' itemClass = TextParameterItem registerParameterType('text', TextParameter) @@ -151,7 +150,9 @@ t2.setParameters(p, showTop=False) t2.show() t2.resize(400,600) - +## test save/restore +s = p.saveState() +p.restoreState(s) ## Start Qt event loop unless running in interactive mode or using pyside. diff --git a/parametertree/Parameter.py b/parametertree/Parameter.py index ebf9be58..6c264693 100644 --- a/parametertree/Parameter.py +++ b/parametertree/Parameter.py @@ -70,6 +70,7 @@ class Parameter(QtCore.QObject): QtCore.QObject.__init__(self) self.opts = { + 'type': None, 'readonly': False, 'visible': True, 'enabled': True, @@ -130,6 +131,9 @@ class Parameter(QtCore.QObject): self.sigNameChanged.emit(self, name) return name + def type(self): + return self.opts['type'] + def childPath(self, child): """ Return the path of parameter names from self to child. @@ -171,15 +175,80 @@ class Parameter(QtCore.QObject): return vals def saveState(self): - """Return a structure representing the entire state of the parameter tree.""" + """ + Return a structure representing the entire state of the parameter tree. + The tree state may be restored from this structure using restoreState() + """ state = self.opts.copy() - state['children'] = {ch.name(): ch.saveState() for ch in self} + state['children'] = [ch.saveState() for ch in self] return state + def restoreState(self, state, recursive=True, addChildren=True, removeChildren=True): + """ + Restore the state of this parameter and its children from a structure generated using saveState() + If recursive is True, then attempt to restore the state of child parameters as well. + If addChildren is True, then any children which are referenced in the state object will be + created if they do not already exist. + If removeChildren is True, then any children which are not referenced in the state object will + be removed. + """ + childState = state.get('children', []) + self.setOpts(**state) + + if not recursive: + return + + ptr = 0 ## pointer to first child that has not been restored yet + foundChilds = set() + #print "==============", self.name() + for ch in childState: + name = ch['name'] + typ = ch['type'] + #print('child: %s, %s' % (self.name()+'.'+name, typ)) + + ## First, see if there is already a child with this name and type + gotChild = False + for i, ch2 in enumerate(self.childs[ptr:]): + #print ch2, ch2.name, ch2.type + if ch2.name() != name or ch2.type() != typ: + continue + gotChild = True + #print " found it" + if i != 0: ## move parameter to next position + self.removeChild(ch2) + self.insertChild(ptr, ch2) + #print " moved to position", ptr + ch2.restoreState(ch, recursive=recursive, addChildren=addChildren, removeChildren=removeChildren) + foundChilds.add(ch2) + + break + + if not gotChild: + if not addChildren: + #print " ignored child" + continue + #print " created new" + ch2 = Parameter.create(**ch) + self.insertChild(ptr, ch2) + foundChilds.add(ch2) + + ptr += 1 + + if removeChildren: + for ch in self: + if ch not in foundChilds: + #print " remove:", ch + self.removeChild(ch) + + + + def defaultValue(self): return self.opts['default'] def setDefault(self, val): + if self.opts['default'] == val: + return self.opts['default'] = val self.sigDefaultChanged.emit(self, val) diff --git a/parametertree/parameterTypes.py b/parametertree/parameterTypes.py index 30f714c1..b1af7b91 100644 --- a/parametertree/parameterTypes.py +++ b/parametertree/parameterTypes.py @@ -366,7 +366,6 @@ class GroupParameter(Parameter): of child parameters. It also provides a simple mechanism for displaying a button or combo that can be used to add new parameters to the group. """ - type = 'group' itemClass = GroupParameterItem def addNew(self, typ=None): @@ -457,7 +456,6 @@ class ListParameterItem(WidgetParameterItem): class ListParameter(Parameter): - type = 'list' itemClass = ListParameterItem def __init__(self, **opts):