Salome HOME
89be2d89985c956e785d783abde00e6b0f93ed25
[modules/shaper.git] / src / ModelAPI / ModelAPI_Events.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Events.h
4 // Created:     10 Apr 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef MODELAPI_EVENTS_H_
8 #define MODELAPI_EVENTS_H_
9
10 #include <ModelAPI.h>
11 #include <ModelAPI_Object.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Attribute.h>
14 #include <Events_MessageGroup.h>
15 #include <Events_Loop.h>
16
17 #include <memory>
18 #include <string>
19 #include <set>
20 #include <map>
21
22
23 class ModelAPI_Document;
24
25 /// Event ID that feature is created (comes with ModelAPI_ObjectUpdatedMessage)
26 static const char * EVENT_OBJECT_CREATED = "ObjectCreated";
27 /// Event ID that data of feature is updated (comes with Model_ObjectUpdatedMessage)
28 static const char * EVENT_OBJECT_UPDATED = "ObjectUpdated";
29 /// Event ID that data of feature is deleted (comes with Model_ObjectDeletedMessage)
30 static const char * EVENT_OBJECT_DELETED = "ObjectDeleted";
31 /// Event ID that data of feature is updated (comes with ModelAPI_ObjectUpdatedMessage)
32 static const char * EVENT_OBJECT_MOVED = "ObjectsMoved";
33 /// Event ID that visualization must be redisplayed (comes with ModelAPI_ObjectUpdatedMessage)
34 static const char * EVENT_OBJECT_TO_REDISPLAY = "ObjectsToRedisplay";
35 /// Event ID that visualization must be redisplayed (comes with ModelAPI_ObjectUpdatedMessage)
36 static const char * EVENT_OPERATION_LAUNCHED = "OperationLaunched";
37 /// Event ID that plugin is loaded (comes with ModelAPI_ObjectUpdatedMessage)
38 static const char * EVENT_PLUGIN_LOADED = "PliginLoaded";
39 //
40 static const char * EVENT_DOCUMENT_CHANGED = "CurrentDocumentChanged";
41
42 static const char * EVENT_FEATURE_STATE_REQUEST = "FeatureStateRequest";
43 static const char * EVENT_FEATURE_STATE_RESPONSE = "FeatureStateResponse";
44
45
46 static const char * EVENT_UPDATE_VIEWER_BLOCKED = "UpdateViewerBlocked";
47 static const char * EVENT_UPDATE_VIEWER_UNBLOCKED = "UpdateViewerUnblocked";
48
49 /// Message that feature was changed (used for Object Browser update): moved, updated and deleted
50 class MODELAPI_EXPORT ModelAPI_ObjectUpdatedMessage : public Events_MessageGroup
51 {
52  protected:
53   /// Creates an empty message
54   ModelAPI_ObjectUpdatedMessage(const Events_ID theID, const void* theSender = 0);
55   /// The virtual destructor
56   virtual ~ModelAPI_ObjectUpdatedMessage();
57
58  public:
59   /// Returns the feature that has been updated
60   virtual const std::set<ObjectPtr>& objects() const = 0;
61
62   //! Creates a new empty group (to store it in the loop before flush)
63   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
64
65   //! Allows to join the given message with the current one
66   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
67 };
68
69 /// Message that feature was deleted (used for Object Browser update)
70 class MODELAPI_EXPORT ModelAPI_ObjectDeletedMessage : public Events_MessageGroup
71 {
72 protected:
73   /// Creates an empty message
74   ModelAPI_ObjectDeletedMessage(const Events_ID theID, const void* theSender = 0);
75   /// The virtual destructor
76   virtual ~ModelAPI_ObjectDeletedMessage();
77
78 public:
79   /// Returns the feature that has been updated
80   virtual std::shared_ptr<ModelAPI_Document> document() const = 0;
81
82   /// Returns the group where the feature was deleted
83   virtual const std::set<std::string>& groups() const = 0;
84
85   /// Creates the new empty message of this kind
86   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
87
88   /// Returns the identifier of the kind of a message
89   virtual const Events_ID messageId() = 0;
90
91   /// Appenad to this message the given one.
92   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
93 };
94
95 /// Allows to create ModelAPI messages
96 class MODELAPI_EXPORT ModelAPI_EventCreator
97 {
98 public:
99   /// creates created, updated or moved messages and sends to the loop
100   virtual void sendUpdated(const ObjectPtr& theObject, const Events_ID& theEvent,
101                            const bool isGroupped = true) const = 0;
102   /// creates deleted message and sends to the loop
103   virtual void sendDeleted(const std::shared_ptr<ModelAPI_Document>& theDoc,
104                            const std::string& theGroup) const = 0;
105
106   /// returns the creator instance
107   static const ModelAPI_EventCreator* get();
108
109   /// sets the creator instance
110   static void set(const ModelAPI_EventCreator* theCreator);
111 };
112
113 // TODO(sbh): Move this message into a separate package, like "GuiAPI"
114 /// Contains the state information about the feature: is it enabled or disabled.
115 class ModelAPI_FeatureStateMessage : public Events_Message
116 {
117 public:
118   /// Creates an empty message
119   MODELAPI_EXPORT ModelAPI_FeatureStateMessage(const Events_ID theID, const void* theSender = 0);
120   /// The virtual destructor
121   MODELAPI_EXPORT virtual ~ModelAPI_FeatureStateMessage();
122
123   /// Returns the feature this message is related to
124   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Feature> feature() const;
125   /// Stores the feature this message is related to
126   MODELAPI_EXPORT void setFeature(std::shared_ptr<ModelAPI_Feature>& theFeature);
127   // For response
128   /// Returns true if feature has specific state
129   MODELAPI_EXPORT bool hasState(const std::string& theFeatureId) const;
130   /// Returns true if feature is enabled
131   MODELAPI_EXPORT bool state(const  std::string& theFeatureId, bool theDefault = false) const;
132   /// Stores the feature state
133   MODELAPI_EXPORT void setState(const std::string& theFeatureId, bool theValue);
134   /// Returns all feature IDs with states
135   MODELAPI_EXPORT std::list<std::string> features() const;
136
137  private:
138   /// For Request
139   std::shared_ptr<ModelAPI_Feature> myCurrentFeature;
140   /// For response
141   std::map<std::string, bool> myFeatureState;
142 };
143
144 /// Message that document (Part, PartSet) was created
145 class ModelAPI_DocumentCreatedMessage : public Events_Message
146 {
147   DocumentPtr myDocument;
148
149  public:
150   /// Creates an empty message
151   MODELAPI_EXPORT ModelAPI_DocumentCreatedMessage(const Events_ID theID, const void* theSender = 0);
152   /// The virtual destructor
153   MODELAPI_EXPORT virtual ~ModelAPI_DocumentCreatedMessage();
154   /// Static. Returns EventID of the message.
155   MODELAPI_EXPORT static Events_ID eventId()
156   {
157     static const char * MY_DOCUMENT_CREATED_EVENT_ID("DocumentCreated");
158     return Events_Loop::eventByName(MY_DOCUMENT_CREATED_EVENT_ID);
159   }
160
161   /// Returns a document stored in the message
162   MODELAPI_EXPORT DocumentPtr document() const;
163   /// Sets a document to the message
164   MODELAPI_EXPORT void setDocument(DocumentPtr theDocument);
165 };
166
167 /// Message that attribute text should be evaluated in the attribute value
168 class ModelAPI_AttributeEvalMessage : public Events_Message
169 {
170   AttributePtr myAttribute;
171
172  public:
173   /// Creates an empty message
174   MODELAPI_EXPORT ModelAPI_AttributeEvalMessage(const Events_ID theID, const void* theSender = 0);
175   /// The virtual destructor
176   MODELAPI_EXPORT virtual ~ModelAPI_AttributeEvalMessage();
177   /// Static. Returns EventID of the message.
178   MODELAPI_EXPORT static Events_ID eventId()
179   {
180     static const char * MY_ATTRIBUTE_EVALUATION_EVENT_ID("AttributeEvaluationRequest");
181     return Events_Loop::eventByName(MY_ATTRIBUTE_EVALUATION_EVENT_ID);
182   }
183
184   /// Returns a document stored in the message
185   MODELAPI_EXPORT AttributePtr attribute() const;
186   /// Sets a document to the message
187   MODELAPI_EXPORT void setAttribute(AttributePtr theDocument);
188 };
189
190 #endif