Source code for openalea.oalab.scene.vplscene

from collections import OrderedDict
from openalea.core.singleton import Singleton
from openalea.core.observer import Observed
import warnings

class VPLScene(OrderedDict, Observed):
[docs] """ Scene for OALab. Singleton. This class inherit from ordered dict. But when the dict is modified, self.signaler emit a qt signal (arg=self). This is really usefull to realize automatic updates of viewer! """ __metaclass__ = Singleton def __init__(self, *args, **kwds): OrderedDict.__init__(self, *args, **kwds) Observed.__init__(self) self._block = False self._valueChanged() def add(self, name="unnamed object", obj="None"):
[docs] """ Add a new object in the scene. :param name: name of the object to add in the scene :param obj: object to add """ name = self._check_if_name_is_unique(name) self[name] = obj def block(self):
[docs] """ Block sent of signals. Useful to add many objects in the scene without refresh the viewer """ self._block = True def release(self):
[docs] """ Release signals sending and update scene. """ self._block = False self.update() def getScene(self):
[docs] """ :return: the scene (ordered dict) """ return self def rename(self, oldname, newname):
[docs] """ Try to rename object named 'oldname' in 'newname'. :param oldname: str of the name of scene component to access :param newname: str of the name to set """ obj = None try: obj = self[oldname] except: warnings.warn("scene[%s] doesn't exist." % oldname) if obj is not None: self.add(name=newname1, obj=obj) del self[oldname] def reset(self):
[docs] """ clear the scene """ self.clear() def _check_if_name_is_unique(self, name):
""" Check if an sub_scene with the name 'name' is alreadey register in the VPLScene. If it is the case, the name is changed ("_1" is append). This is realize until the name becomes unique. :param name: name to check unicity TODO : remove this method if we want unicity of name, like in a classical dict """ return name ''' while name in self: try: end = name.split("_")[-1] l = len(end) end = int(end) end += 1 name = name[0:-l] + str(end) except: name += "_1" return name ''' def __setitem__(self, key, value): super(VPLScene, self).__setitem__(key, value) self._valueChanged() def update(self):
[docs] super(VPLScene, self).update() self._valueChanged() def __delitem__(self, key):
super(VPLScene, self).__delitem__(key) self._valueChanged() def popitem(self, last=True):
[docs] super(VPLScene, self).popitem(last) self._valueChanged() def clear(self):
[docs] super(VPLScene, self).clear() self._valueChanged() def __reversed__(self):
super(VPLScene, self).__reversed__() self._valueChanged() def __reduce__(self): super(VPLScene, self).__reduce__() self._valueChanged() def _valueChanged(self): """ Emit Qt Signal when the dict change """ if not self._block: self.notify_listeners(('SceneChanged', self)) Scene = VPLScene