Observers
Applications can be notified when the state of a manager changes. This notification mechanism is based on
IlvManagerObserver, a subclass of
IlvObserver. Observers are created by the application and set on the manager. The manager is in charge of sending messages to the observer under certain circumstances called
reasons.
Notification messages are classified by their reason into different categories. An observer can choose to receive messages of one or several categories by setting its
interest mask. The manager will only send a message to the observer if the notification reason belongs to a category of the observer interest mask. These categories are shown in
Notification Categories:
Notification Categories
Category Description | Mask |
---|
General | IlvMgrMsgGeneralMask |
Manager view | IlvMgrMsgViewMask |
Manager layer | IlvMgrMsgLayerMask |
Manager contents | IlvMgrMsgContentsMask |
Object geometry | IlvMgrMsgObjectGeometryMask |
An application wishing to get notification messages must define a subclass of
IlvManagerObserver and overload the virtual member function
update. In this member function, the observer receives an instance of
IlvManagerMessage, or a subclass, containing the reason and additional relevant information about the notification.
General Notifications
This category concerns general notifications on the managers.
Interest mask: IlvMgrMsgGeneralMask
Delete the manager
Reason: IlvMgrMsgDelete
Manager View Notifications
This category concerns notifications on operations performed on manager views.
Interest mask: IlvMgrMsgViewMask
Add a view to the manager
Reason: IlvMgrMsgAddView
Remove a view from the manager
Reason: IlvMgrMsgRemoveView
Set an interactor on a view
Reason: IlvMgrMsgSetInteractor
Set a transformer on a view
Reason: IlvMgrMsgSetTransformer
Manager Layer Notifications
This category concerns notifications on operations performed on manager layers.
Interest mask: IlvMgrMsgLayerMask
Add a layer to the manager
Reason: IlvMgrMsgAddLayer
Remove a layer from the manager
Reason: IlvMgrMsgRemoveLayer
Change the index of a layer
Reason: IlvMgrMsgMoveLayer
Swap indexes between two layers
Reason: IlvMgrMsgSwapLayer
Set the name of a layer
Reason: IlvMgrMsgLayerName
Set the visibility of a layer
Reason: IlvMgrMsgLayerVisibility
Set the selectabililty of a layer
Reason: IlvMgrMsgLayerSelectability
Manager Contents Notifications
This category concerns notifications on the changes in the contents of managers.
Interest mask: IlvMgrMsgContentsMask
Add a graphic object to the manager
Reason: IlvMgrMsgAddObject
Remove a graphic object from the manager
Reason: IlvMgrMsgRemoveObject
Set the layer of a graphic object
Reason: IlvMgrMsgObjectLayer
Graphic Object Geometry Notifications
This category concerns notifications on a change of geometry of the objects (for example, move, resize, and rotate).
Interest mask: IlvMgrMsgObjectGeometryMask
Change the geometry of a graphic object
Reason: IlvMgrMsgObjectGeometry
Example
Here is the implementation of an observer that receives notifications on adding or removing layers and views.
class MyManagerObserver : public IlvManagerObserver { public: MyManagerObserver(IlvManager* manager) : IlvManagerObserver(manager, IlvMgrMsgLayerMask | IlvMgrMsgViewMask) {} virtual void update(IlvObservable* o, IlAny arg); }; |
The update member function:
void MyManagerObserver::update(IlvObservable* obs, IlAny arg) { IlvManager* manager = ((IlvManagerObservable*)obs)->getManager(); switch(((IlvManagerMessage*) arg)->_reason) { // __ Notification on manager view case IlvMgrMsgAddView: IlvPrint("Add view notification"); break; case IlvMgrMsgRemoveView: IlvPrint("Remove view notification"); break; // __ Notification on manager layer case IlvMgrMsgAddLayer: IlvPrint("Add layer notification: %d", ((IlvManagerLayerMessage*)arg)->getLayer()); break; case IlvMgrMsgRemoveLayer: IlvPrint("Remove layer notification: %d", ((IlvManagerLayerMessage*)arg)->getLayer()); break; default: IlvPrint("Unhandled notification"); break; } } |
To attach the observer to the manager:
MyManagerObserver* observer = new MyManagerObserver(manager);
Published date: 05/24/2022
Last modified date: 02/24/2022