AnimateDocumentObserver.AnimateDocumentObserver Class Reference

Class that keeps Animate workbench objects in recommended structures. More...

Inheritance diagram for AnimateDocumentObserver.AnimateDocumentObserver:

Public Member Functions

def __new__ (cls, *args, **kwargs)
 Method creating an AnimateDocumentObserver singleton instance. More...
 
def slotBeforeChangeObject (self, obj, prop)
 Qt slot method called when an object in a surveyed document is about to change. More...
 
def slotChangedObject (self, obj, prop)
 Qt slot method called when an object in an observed document was changed. More...
 
def isAnimateGroup (self, obj)
 Method to check whether a group object comes from the Animate workbench. More...
 
def isAnimateObject (self, obj)
 Method to check whether an object comes from the Animate workbench. More...
 
def foreignObjectInAnimateGroup (self, obj, group)
 Method testing whether a forbidden object is in an Animate group object. More...
 
def animateObjectInForeignGroup (self, obj, group)
 Method testing whether an Animate object is in a foreign group object. More...
 
def slotDeletedDocument (self, doc)
 Qt slot method called if a document is about to be closed. More...
 
def addServerToNotify (self, server_proxy, document_name)
 Method to add a server which needs to be notified when its document is closing. More...
 

Public Attributes

 group_before
 A list of objects inside a group object about to change. More...
 

Static Public Attributes

dictionary server_proxies = {}
 A dict of document names and ServerProxies in them. More...
 

Detailed Description

Class that keeps Animate workbench objects in recommended structures.

Definition at line 63 of file AnimateDocumentObserver.py.

Member Function Documentation

◆ __new__()

def AnimateDocumentObserver.AnimateDocumentObserver.__new__ (   cls,
args,
**  kwargs 
)

Method creating an AnimateDocumentObserver singleton instance.

Returns
An AnimateDocumentObserver singleton instance.

Definition at line 86 of file AnimateDocumentObserver.py.

86  def __new__(cls, *args, **kwargs):
87  # Make AnimateDocumentObserver a singleton
88  if cls.__instance is None:
89  cls.__instance = super(AnimateDocumentObserver,
90  cls).__new__(cls, *args, **kwargs)
91  return cls.__instance
92 

◆ addServerToNotify()

def AnimateDocumentObserver.AnimateDocumentObserver.addServerToNotify (   self,
  server_proxy,
  document_name 
)

Method to add a server which needs to be notified when its document is closing.

An active server must close a socket, it is using, when a document it's on is closing, so that a socket won't be blocked when it is needed again.

Parameters
server_proxyA ServerProxy which takes care of a Server instance.
document_nameName of a document with the Server instance.

Definition at line 308 of file AnimateDocumentObserver.py.

308  def addServerToNotify(self, server_proxy, document_name):
309  # Add a server proxy to the dictionary under a document name it's on
310  if document_name in self.server_proxies:
311  if server_proxy not in self.server_proxies[document_name]:
312  self.server_proxies[document_name].append(server_proxy)
313 
314  # Add a new document name to the dictionary and assign it a list
315  # with a server proxy
316  else:
317  self.server_proxies[document_name] = [server_proxy]
318 
319 

◆ animateObjectInForeignGroup()

def AnimateDocumentObserver.AnimateDocumentObserver.animateObjectInForeignGroup (   self,
  obj,
  group 
)

Method testing whether an Animate object is in a foreign group object.

Parameters
objA suspected Animate object.
groupA group object which is possibly not from the Animate workbench.
Returns
True if an Animate object is not in a Animate group and false otherwise.

Definition at line 279 of file AnimateDocumentObserver.py.

279  def animateObjectInForeignGroup(self, obj, group):
280  return not self.isAnimateGroup(group) and self.isAnimateObject(obj)
281 

◆ foreignObjectInAnimateGroup()

def AnimateDocumentObserver.AnimateDocumentObserver.foreignObjectInAnimateGroup (   self,
  obj,
  group 
)

Method testing whether a forbidden object is in an Animate group object.

Trajectory group objects are allowed to be stacked. Control objects can contain any other Animate object. CollisionDetector objects can accommodate only Collision objects.

Parameters
objA suspected foreign object in an Animate group object.
groupThe Animate group object possibly harboring an illegal object.
Returns
True if a forbidden object is in a Animate group obj. and false otherwise.

Definition at line 234 of file AnimateDocumentObserver.py.

234  def foreignObjectInAnimateGroup(self, obj, group):
235  # If an obj proxy is NoneType extrapolate it from object name
236  if not hasattr(obj, "Proxy"):
237  return False
238  else:
239  if obj.Proxy.__class__.__name__ == "NoneType":
240  obj_type = obj.Name.rstrip('0123456789') + "Proxy"
241  else:
242  obj_type = obj.Proxy.__class__.__name__
243 
244  # If an obj proxy is NoneType extrapolate it from object name
245  if group.Proxy.__class__.__name__ == "NoneType":
246  group_type = group.Name.rstrip('0123456789') + "Proxy"
247  else:
248  group_type = group.Proxy.__class__.__name__
249 
250  # Only a Trajectory can be in a Trajectory group
251  if group_type == "TrajectoryProxy" and obj_type == "TrajectoryProxy":
252  return False
253 
254  # Only some objects can be in a Control group
255  elif group_type == "ControlProxy" and obj_type in ALLOWED_IN_CONTROL:
256  return False
257 
258  elif group_type == "CollisionDetectorProxy" and \
259  obj.Name.find("Collision") != -1:
260  return False
261  # Only RobRotation and RobTranslation can be in RobWorld, RobRotation
262  # and RobTRanslation groups
263  elif (group_type in ["RobWorldProxy", "RobRotationProxy",
264  "RobTranslationProxy"]) and \
265  (obj_type in ["RobRotationProxy", "RobTranslationProxy"]):
266  return False
267  return True
268 

◆ isAnimateGroup()

def AnimateDocumentObserver.AnimateDocumentObserver.isAnimateGroup (   self,
  obj 
)

Method to check whether a group object comes from the Animate workbench.

As all Animate objects have a Proxy attached, its reasonable to firstly check for it and then decide according to the Proxy object's name if it is an Animate group.

Returns
True if the group object is from Animate workbench and false otherwise.

Definition at line 178 of file AnimateDocumentObserver.py.

178  def isAnimateGroup(self, obj):
179  # If a proxy is NoneType extrapolate it from object name
180  if not hasattr(obj, "Proxy"):
181  return False
182  else:
183  if obj.Proxy.__class__.__name__ == "NoneType":
184  obj_type = obj.Name.rstrip('0123456789') + "Proxy"
185  else:
186  obj_type = obj.Proxy.__class__.__name__
187 
188  # Check if proxy is an Animate group class
189  if obj_type not in ANIMATE_OBJECT_GROUP_CLASSES:
190  return False
191  else:
192  return True
193 

◆ isAnimateObject()

def AnimateDocumentObserver.AnimateDocumentObserver.isAnimateObject (   self,
  obj 
)

Method to check whether an object comes from the Animate workbench.

As all Animate objects have a Proxy attached, its reasonable to firstly check for it and then decide according to the Proxy object's name if it is from the Animate workbench.

Returns
True if the object is from Animate workbench and false otherwise.

Definition at line 204 of file AnimateDocumentObserver.py.

204  def isAnimateObject(self, obj):
205  # If a proxy is NoneType extrapolate it from object name
206  if not hasattr(obj, "Proxy"):
207  return False
208  else:
209  if obj.Proxy.__class__.__name__ == "NoneType":
210  obj_type = obj.Name.rstrip('0123456789') + "Proxy"
211  else:
212  obj_type = obj.Proxy.__class__.__name__
213 
214  # Check if proxy is an Animate object class
215  if obj_type not in ANIMATE_CLASSES:
216  return False
217  else:
218  return True
219 

◆ slotBeforeChangeObject()

def AnimateDocumentObserver.AnimateDocumentObserver.slotBeforeChangeObject (   self,
  obj,
  prop 
)

Qt slot method called when an object in a surveyed document is about to change.

If the object which is going to change has a Group property, then a transaction is begun and a current state of the Group property is recorded.

Parameters
objAn object in the observed document about to change.
propA str of a property about to change.

Definition at line 103 of file AnimateDocumentObserver.py.

103  def slotBeforeChangeObject(self, obj, prop):
104  # If any group is about to be changed, start a transaction
105  if prop == "Group":
106  FreeCAD.ActiveDocument.openTransaction()
107  self.group_before = obj.Group
108 

◆ slotChangedObject()

def AnimateDocumentObserver.AnimateDocumentObserver.slotChangedObject (   self,
  obj,
  prop 
)

Qt slot method called when an object in an observed document was changed.

This method is used to enforce a recommended structure of Animate objects in Tree View. It checks that only allowed objects are inside Animate group objects. It also checks that no Animate object is inside a group object that does not belong to the Animate workbench. Lastly, it obstructs user from deleting Collision objects from a CollisionDetector group object.

Parameters
objAn object in the observed document about to change.
propA str of a property about to change.

Definition at line 122 of file AnimateDocumentObserver.py.

122  def slotChangedObject(self, obj, prop):
123  # If objects are added to a group
124  if prop == "Group" and len(obj.Group) > len(self.group_before):
125  # If a new object is added to a group object from Animate workbench
126  new_obj = obj.Group[-1]
127  if self.isAnimateGroup(obj):
128  # An object not from Animate workbench was added to it
129  if self.foreignObjectInAnimateGroup(new_obj, obj):
130  QMessageBox.warning(
131  None, 'Forbidden action detected',
132  "Group objects from Animate workbench can group\n"
133  + "only selected objects from Animate workbench.\n"
134  + "Check the user guide for more info.")
135  FreeCAD.ActiveDocument.undo()
136  else:
137  FreeCAD.ActiveDocument.commitTransaction()
138 
139  # An object is added to a group not from Animate workbench
140  else:
141  # The added object was from Animate workbench
142  if self.animateObjectInForeignGroup(new_obj, obj):
143  QMessageBox.warning(
144  None, 'Forbidden action detected',
145  "Objects from Animate workbench can be grouped\n"
146  + "only by selected objects from Animate "
147  + "workbench.\nCheck the user guide "
148  + "for more info.")
149  FreeCAD.ActiveDocument.undo()
150  else:
151  FreeCAD.ActiveDocument.commitTransaction()
152 
153  # If objects are removed from a group
154  elif prop == "Group" and len(obj.Group) < len(self.group_before):
155  # If a Collision is removed from
156  removed = set(self.group_before).difference(set(obj.Group)).pop()
157  if removed.Proxy.__class__.__name__ == "CollisionProxy" and \
158  hasattr(obj, "Proxy") and not obj.Proxy.resetting and \
159  not obj.Proxy.checking:
160  QMessageBox.warning(
161  None, 'Forbidden action detected',
162  "Collision objects cannot be removed from\n"
163  + "a CollisionDetector group.")
164  FreeCAD.ActiveDocument.undo()
165  else:
166  FreeCAD.ActiveDocument.commitTransaction()
167 

◆ slotDeletedDocument()

def AnimateDocumentObserver.AnimateDocumentObserver.slotDeletedDocument (   self,
  doc 
)

Qt slot method called if a document is about to be closed.

This method is used to notify all ServerProxies that the document they are in is going to be closed.

Parameters
docA FreeCAD's App.Document document about to be closed.

Definition at line 291 of file AnimateDocumentObserver.py.

291  def slotDeletedDocument(self, doc):
292  # Check at least one server is in the document about to be closed
293  if doc.Name in self.server_proxies:
294  # Notify all servers in the document
295  for server_proxy in self.server_proxies[doc.Name]:
296  server_proxy.onDocumentClosed()
297 

Member Data Documentation

◆ group_before

AnimateDocumentObserver.AnimateDocumentObserver.group_before

A list of objects inside a group object about to change.

Definition at line 107 of file AnimateDocumentObserver.py.

◆ server_proxies

AnimateDocumentObserver.AnimateDocumentObserver.server_proxies = {}
static

A dict of document names and ServerProxies in them.

Definition at line 78 of file AnimateDocumentObserver.py.


The documentation for this class was generated from the following file: