Saturday, May 2, 2009

A Note about casting in TinkerCell

The qgraphicsitem_cast is used to cast at the "top level" graphical items, i.e. PartGraphicsItem, ConnectionGraphicsItem, TextGraphicsItem, PartGraphicsItem::ControlPoint, ConnectionGraphicsItem::ControlPoint, and GraphicalTool. Note, ControlPoint::getControlPoint() can be used to get either one of the ControlPoint classes.

For example:
PartGraphicsItem * part =  qgraphicsitem_cast(item)
ConnectionGraphicsItem * connection = qgraphicsitem_cast(item)

However, this type casting is NOT used for subclasses, such as ArrowHeadItem or ModuleLinkerItem, both of which inherit from PartGraphicsItem. Casting for these subclasses is done using the class_name static field.

For example:
PartGraphicsItem * part =  qgraphicsitem_cast(item)
if (part->className == ArrowHeadItem::class_name)
      ArroHeadItem * arrow = static_cast(part); 

Reason for this way of casting:
The top level classes are used by various tools, so the qgraphicsitem_cast is reseved for tools to identify which of the top level class an item belong to. The subclasses are usually only relevant for one or two tools, e.g. the ModuleLinkerItem. Other tools do not need to differentiate between PartGraphicsItem and ModuleLinkerItem. 

No comments: