- m_pState->DoSortChildren(p, recursively);
- }
-
-#ifdef SWIG
- %pythoncode {
- def MapType(class_,factory):
- "Registers Python type/class to property mapping.\n\nfactory: Property builder function/class."
- global _type2property
- try:
- mappings = _type2property
- except NameError:
- raise AssertionError("call only after a propertygrid or manager instance constructed")
-
- mappings[class_] = factory
-
-
- def DoDefaultTypeMappings(self):
- "Map built-in properties."
- global _type2property
- try:
- mappings = _type2property
-
- return
- except NameError:
- mappings = {}
- _type2property = mappings
-
- mappings[str] = StringProperty
- mappings[unicode] = StringProperty
- mappings[int] = IntProperty
- mappings[float] = FloatProperty
- mappings[bool] = BoolProperty
- mappings[list] = ArrayStringProperty
- mappings[tuple] = ArrayStringProperty
- mappings[wx.Font] = FontProperty
- mappings[wx.Colour] = ColourProperty
- "mappings[wx.Size] = SizeProperty"
- "mappings[wx.Point] = PointProperty"
- "mappings[wx.FontData] = FontDataProperty"
-
- def DoDefaultValueTypeMappings(self):
- "Map pg value type ids to getter methods."
- global _vt2getter
- try:
- vt2getter = _vt2getter
-
- return
- except NameError:
- vt2getter = {}
- _vt2getter = vt2getter
-
- def GetPropertyValues(self,dict_=None, as_strings=False, inc_attributes=False):
- "Returns values in the grid."
- ""
- "dict_: if not given, then a new one is created. dict_ can be"
- " object as well, in which case it's __dict__ is used."
- "as_strings: if True, then string representations of values"
- " are fetched instead of native types. Useful for config and such."
- "inc_attributes: if True, then property attributes are added"
- " as @<propname>@<attr>."
- ""
- "Return value: dictionary with values. It is always a dictionary,"
- "so if dict_ was object with __dict__ attribute, then that attribute"
- "is returned."
-
- if dict_ is None:
- dict_ = {}
- elif hasattr(dict_,'__dict__'):
- dict_ = dict_.__dict__
-
- if not as_strings:
- getter = self.GetPropertyValue
- else:
- getter = self.GetPropertyValueAsString
-
- it = self.GetVIterator(PG_ITERATE_PROPERTIES)
- while not it.AtEnd():
- p = it.GetProperty()
- name = p.GetName()
-
- dict_[name] = getter(p)
-
- if inc_attributes:
- attrs = p.GetAttributes()
- if attrs and len(attrs):
- dict_['@%s@attr'%name] = attrs
-
- it.Next()
-
- return dict_
-
- GetValues = GetPropertyValues
-
-
- def SetPropertyValues(self,dict_):
- "Sets property values from dict_, which can be either\ndictionary or an object with __dict__ attribute."
- ""
- "autofill: If true, keys with not relevant properties"
- " are auto-created. For more info, see AutoFill."
- ""
- "Notes:"
- " * Keys starting with underscore are ignored."
- " * Attributes can be set with entries named @<propname>@<attr>."
- ""
-
- autofill = False
-
- if dict_ is None:
- dict_ = {}
- elif hasattr(dict_,'__dict__'):
- dict_ = dict_.__dict__
-
- attr_dicts = []
-
- def set_sub_obj(k0,dict_):
- for k,v in dict_.iteritems():
- if k[0] != '_':
- if k.endswith('@attr'):
- attr_dicts.append((k[1:-5],v))
- else:
- try:
- self.SetPropertyValue(k,v)
- except:
- try:
- if autofill:
- self._AutoFillOne(k0,k,v)
- continue
- except:
- if isinstance(v,dict):
- set_sub_obj(k,v)
- elif hasattr(v,'__dict__'):
- set_sub_obj(k,v.__dict__)
-
-
- for k,v in attr_dicts:
- p = GetPropertyByName(k)
- if not p:
- raise AssertionError("No such property: '%s'"%k)
- for an,av in v.iteritems():
- p.SetAttribute(an, av)
-
-
- cur_page = False
- is_manager = isinstance(self,PropertyGridManager)
-
- try:
- set_sub_obj(self.GetGrid().GetRoot(),dict_)
- except:
- import traceback
- traceback.print_exc()
-
- self.Refresh()
-
- SetValues = SetPropertyValues
-
- def _AutoFillMany(self,cat,dict_):
- for k,v in dict_.iteritems():
- self._AutoFillOne(cat,k,v)
-
-
- def _AutoFillOne(self,cat,k,v):
- global _type2property
-
- factory = _type2property.get(v.__class__,None)
-
- if factory:
- self.AppendIn( cat, factory(k,k,v) )
- elif hasattr(v,'__dict__'):
- cat2 = self.AppendIn( cat, PropertyCategory(k) )
- self._AutoFillMany(cat2,v.__dict__)
- elif isinstance(v,dict):
- cat2 = self.AppendIn( cat, PropertyCategory(k) )
- self._AutoFillMany(cat2,v)
- elif not k.startswith('_'):
- raise AssertionError("member '%s' is of unregisted type/class '%s'"%(k,v.__class__))
-
-
- def AutoFill(self,obj,parent=None):
- "Clears properties and re-fills to match members and\nvalues of given object or dictionary obj."
-
- self.edited_objects[parent] = obj
-
- cur_page = False
- is_manager = isinstance(self,PropertyGridManager)
-
- if not parent:
- if is_manager:
- page = self.GetCurrentPage()
- page.Clear()
- parent = page.GetRoot()
- else:
- self.Clear()
- parent = self.GetGrid().GetRoot()
- else:
- it = self.GetIterator(PG_ITERATE_PROPERTIES, parent)
- it.Next() # Skip the parent
- while not it.AtEnd():
- p = it.GetProperty()
- if not p.IsSomeParent(parent):
- break
-
- self.DeleteProperty(p)
-
- name = p.GetName()
- it.Next()
-
- if not is_manager or page == self.GetCurrentPage():
- self.Freeze()
- cur_page = True
-
- try:
- self._AutoFillMany(parent,obj.__dict__)
- except:
- import traceback
- traceback.print_exc()
-
- if cur_page:
- self.Thaw()
-
- def RegisterEditor(self, editor, editorName=None):
- "Transform class into instance, if necessary."
- if not isinstance(editor, PGEditor):
- editor = editor()
- if not editorName:
- editorName = editor.__class__.__name__
- try:
- self._editor_instances.append(editor)
- except:
- self._editor_instances = [editor]
- RegisterEditor(editor, editorName)
-
- def GetPropertyClientData(self, p):
- if isinstance(p, basestring):
- p = self.GetPropertyByName(p)
- return p.GetClientData()
-
- def SetPropertyClientData(self, p, data):
- if isinstance(p, basestring):
- p = self.GetPropertyByName(p)
- return p.SetClientData(data)