File: AbstractModel.h

package info (click to toggle)
itksnap 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,196 kB
  • ctags: 9,196
  • sloc: cpp: 62,895; sh: 175; makefile: 13
file content (111 lines) | stat: -rw-r--r-- 3,278 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef ABSTRACTMODEL_H
#define ABSTRACTMODEL_H

#include "itkObject.h"
#include "itkObjectFactory.h"
#include "SNAPCommon.h"
#include "SNAPEvents.h"
#include "EventBucket.h"
#include <set>
#include <string>

class vtkObject;


/**
  \class AbstractModel
  \brief Parent class for all UI models

  This class provides basic common functionality for all UI models. This
  includes the event/update mechanism.
  */
class AbstractModel : public itk::Object
{
public:
  /*
  typedef AbstractModel                  Self;
  typedef itk::Object                    Superclass;
  typedef itk::SmartPointer<Self>        Pointer;
  typedef itk::SmartPointer<const Self>  ConstPointer;

  itkTypeMacro(AbstractModel, itk::Object)

  itkNewMacro(Self)
  */

  irisITKObjectMacro(AbstractModel, itk::Object)


  /**
    Call this function to update the model based on events that have been
    fired since the last time the model was updated. This function simply
    checks if the event bucket is empty; if not, it calls the OnUpdate()
    protected virtual method. Subclassers should reimplement OnUpdate(). The
    event bucket is emptied at the end of this call.
    */
  void Update();


  /**
    Listen to events of type srcEvent on the object src, and rebroadcast
    them as event trgEvent. In the process, record the srcEvent in the
    event bucket. This is the main mechanism for model updates. The model
    listens to events occurring upstream. When an event occurs, the model
    only records the event and invokes its own event, to which the view
    objects downstream are listening. It is then the view's responsibility
    to call the Update() function on the model. This function checks what
    events are in the event bucket, and processes them in an orderly fashion.
    */
  unsigned long Rebroadcast(
      itk::Object *src, const itk::EventObject &srcEvent,
      const itk::EventObject &trgEvent);

  /**
   We can also rebroadcast events from vtk objects. This is handled similar
   to ITK but events are just unsigned long values. The event bucket will
   include a VTKEvent() object with NULL caller if an event from VTK occurred.
   (at the present, EventBucket does not support differentiating between different
   kinds of VTK events and callers).
   */
  unsigned long Rebroadcast(vtkObject *src, unsigned long srcEvent,
      const itk::EventObject &trgEvent);


protected:

  AbstractModel();
  virtual ~AbstractModel();

  /**
    Helper class for AbstractModel used to rebroadcast events
    */
  class Rebroadcaster
  {
  public:
    Rebroadcaster(AbstractModel *model, const itk::EventObject &evt);
    virtual ~Rebroadcaster();

    void Broadcast(itk::Object *source, const itk::EventObject &evt);
    void Broadcast(const itk::Object *source, const itk::EventObject &evt);

    void BroadcastVTK(vtkObject *source, unsigned long event, void *);

  private:
    AbstractModel *m_Model;
    itk::EventObject *m_Event;
  };

  /**
    This is the method called by Update() if there are events in the
    event bucket
    */
  virtual void OnUpdate() {}

  // List of rebroadcasters
  std::list<Rebroadcaster *> m_Rebroadcast;

  // Bucket that stores events fired since last call to Update()
  EventBucket *m_EventBucket;
};

#endif // ABSTRACTMODEL_H