CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ObjectHolder.py
Go to the documentation of this file.
1 import logging
2 
3 class ObjectHolder(object):
4  """ Abstract class for holders of objects which are accessed via a data accessor.
5 
6  Objects can be filtered using a filter function.
7  """
8  def __init__(self):
9  #logging.debug(__name__ + ": __init__")
10  self._dataAccessor = None
11  self._dataObjects = []
12  self._filter = self._noFilter
13  self._exclusiveMode = False
14 
15  def setExclusiveMode(self, exclusive=True):
16  """ Sets exclusive mode to given value.
17 
18  If exclusive mode is set to True objects will only appear once in the list of objects if they are added using appendObject.
19  """
20  self._exclusiveMode = exclusive
21 
22  def setDataAccessor(self, accessor):
23  """ Sets the DataAccessor from which the nodes are created.
24 
25  You need to call updateContent() in order to make the changes visible.
26  """
27  self._dataAccessor = accessor
28 
29  def dataAccessor(self):
30  return self._dataAccessor
31 
32  def setDataObjects(self, objects):
33  """ Sets the objects that shall be shown.
34 
35  You need to call updateContent() in order to make the changes visible.
36  """
37  self._dataObjects = objects
38 
39  def dataObjects(self):
40  return self._dataObjects
41 
42  def setDataObject(self, object):
43  """ Sets the one object that shall be shown.
44 
45  This method is provided for e.g. views which show a single object.
46  You need to call updateContent() in order to make the changes visible.
47  """
48  self._dataObjects = [object]
49 
50  def dataObject(self):
51  """ Return the first object.
52 
53  This method is provided for e.g. views which show a single object.
54  """
55  if len(self.dataObjects())>0:
56  return self.dataObjects()[0]
57  else:
58  return None
59 
60  def appendObject(self, object):
61  """ Appends object to lists of data objects.
62  """
63  if not self._exclusiveMode or (self._exclusiveMode and object not in self._dataObjects):
64  self._dataObjects.append(object)
65  return object
66 
67  def removeObject(self, object):
68  """ Removes object from list of data objects.
69  """
70  if object in self._dataObjects:
71  self._dataObjects.remove(object)
72 
73  def dataObjectsCount(self):
74  """ Return number of data objects.
75  """
76  return len(self.dataObjects())
77 
78  def clearObjects(self):
79  """ Removes all objects from this ObjectHolder.
80  """
81  self._dataObjects = []
82 
83  def setFilter(self, filter):
84  """ Set the filter function used in the view.
85  """
86  self._filter = filter
87 
88  def _noFilter(self, objects):
89  """ The default filter function for objects.
90  """
91  return objects
92 
93  def applyFilter(self, objects):
94  """ Apply the filter to a list of objects.
95 
96  This function should be used any time the view handles a list of objects
97  e.g. on self.dataObjects() or self.dataAccessor().children(object):
98  self.applyFilter(self.dataAccessor().children(object))
99  """
100  return self._filter(objects)
101 
102  def allDataObjectChildren(self,objects=None):
103  if objects==None:
104  objects=self._dataObjects[:]
105  for object in objects[:]:
106  objects+=self.allDataObjectChildren(self.applyFilter(self.dataAccessor().children(object)))
107  return objects
108 
109  def numberDataObjectChildren(self,objects=None):
110  if objects==None:
111  objects=self._dataObjects
112  number=len(objects)
113  for object in objects:
114  number+=self.numberDataObjectChildren(self.applyFilter(self.dataAccessor().children(object)))
115  return number