Salome HOME
f4fee024af80ffcb7445c878186228bbc48287d9
[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 name of feature is changed (comes with Model_ObjectRenamedMessage)
32 static const char * EVENT_OBJECT_RENAMED = "ObjectRenamed";
33 /// Event ID that data of feature is updated (comes with ModelAPI_ObjectUpdatedMessage)
34 static const char * EVENT_OBJECT_MOVED = "ObjectsMoved";
35 /// Event ID that visualization must be redisplayed (comes with ModelAPI_ObjectUpdatedMessage)
36 static const char * EVENT_OBJECT_TO_REDISPLAY = "ObjectsToRedisplay";
37 /// Event ID that error state or error message was updated for the object
38 static const char * EVENT_OBJECT_ERROR_CHANGED = "ObjectsErrorChanged";
39 /// Event ID that visualization must be redisplayed (comes with ModelAPI_ObjectUpdatedMessage)
40 static const char * EVENT_OPERATION_LAUNCHED = "OperationLaunched";
41 /// Event ID that plugin is loaded (comes with ModelAPI_ObjectUpdatedMessage)
42 static const char * EVENT_PLUGIN_LOADED = "PliginLoaded";
43 //
44 static const char * EVENT_DOCUMENT_CHANGED = "CurrentDocumentChanged";
45
46 static const char * EVENT_FEATURE_STATE_REQUEST = "FeatureStateRequest";
47 static const char * EVENT_FEATURE_STATE_RESPONSE = "FeatureStateResponse";
48
49
50 static const char * EVENT_UPDATE_VIEWER_BLOCKED = "UpdateViewerBlocked";
51 static const char * EVENT_UPDATE_VIEWER_UNBLOCKED = "UpdateViewerUnblocked";
52
53 static const char * EVENT_SOLVER_FAILED = "SolverFailed";
54 static const char * EVENT_SOLVER_REPAIRED = "SolverRepaired";
55
56 /// Event ID that order of objects in group is changed, so, tree must be fully rectreated (movement of feature)
57 static const char * EVENT_ORDER_UPDATED = "OrderUpdated";
58
59 /// Event ID that informs that some object has changed the stability
60 static const char * EVENT_STABILITY_CHANGED = "StabilityChanged";
61
62 /// Message that feature was changed (used for Object Browser update): moved, updated and deleted
63 class MODELAPI_EXPORT ModelAPI_ObjectUpdatedMessage : public Events_MessageGroup
64 {
65  protected:
66   /// Creates an empty message
67   ModelAPI_ObjectUpdatedMessage(const Events_ID theID, const void* theSender = 0);
68   /// The virtual destructor
69   virtual ~ModelAPI_ObjectUpdatedMessage();
70
71  public:
72   /// Returns the feature that has been updated
73   virtual const std::set<ObjectPtr>& objects() const = 0;
74
75   //! Creates a new empty group (to store it in the loop before flush)
76   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
77
78   //! Allows to join the given message with the current one
79   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
80 };
81
82 /// Message that feature was deleted (used for Object Browser update)
83 class MODELAPI_EXPORT ModelAPI_ObjectDeletedMessage : public Events_MessageGroup
84 {
85 protected:
86   /// Creates an empty message
87   ModelAPI_ObjectDeletedMessage(const Events_ID theID, const void* theSender = 0);
88   /// The virtual destructor
89   virtual ~ModelAPI_ObjectDeletedMessage();
90
91 public:
92   /// Returns the document that has been updated
93   virtual std::shared_ptr<ModelAPI_Document> document() const = 0;
94
95   /// Returns the groups where the objects were deleted
96   virtual const std::set<std::string>& groups() const = 0;
97
98   /// Creates the new empty message of this kind
99   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
100
101   /// Returns the identifier of the kind of a message
102   virtual const Events_ID messageId() = 0;
103
104   /// Appenad to this message the given one.
105   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
106 };
107
108 /// Message that order changed (used for Object Browser update)
109 class MODELAPI_EXPORT ModelAPI_OrderUpdatedMessage : public Events_Message
110 {
111 protected:
112   /// Creates an empty message
113   ModelAPI_OrderUpdatedMessage(const Events_ID theID, const void* theSender = 0);
114   /// The virtual destructor
115   virtual ~ModelAPI_OrderUpdatedMessage();
116
117 public:
118   /// Returns the document that has been updated
119   virtual std::shared_ptr<ModelAPI_Document> document() const = 0;
120
121   /// Returns the groups where the objects were reordered
122   virtual const std::string& group() const = 0;
123
124   /// Returns the identifier of the kind of a message
125   virtual const Events_ID messageId() = 0;
126 };
127
128 /// Allows to create ModelAPI messages
129 class MODELAPI_EXPORT ModelAPI_EventCreator
130 {
131 public:
132   /// creates created, updated or moved messages and sends to the loop
133   virtual void sendUpdated(const ObjectPtr& theObject, const Events_ID& theEvent,
134                            const bool isGroupped = true) const = 0;
135   /// creates deleted message and sends to the loop
136   virtual void sendDeleted(const std::shared_ptr<ModelAPI_Document>& theDoc,
137                            const std::string& theGroup) const = 0;
138   /// creates reordered message and sends to the loop
139   virtual void sendReordered(const std::shared_ptr<ModelAPI_Document>& theDoc,
140                              const std::string& theGroup) const = 0;
141
142   /// returns the creator instance
143   static const ModelAPI_EventCreator* get();
144
145   /// sets the creator instance
146   static void set(const ModelAPI_EventCreator* theCreator);
147 };
148
149 // TODO(sbh): Move this message into a separate package, like "GuiAPI"
150 /// Contains the state information about the feature: is it enabled or disabled.
151 class ModelAPI_FeatureStateMessage : public Events_Message
152 {
153 public:
154   /// Creates an empty message
155   MODELAPI_EXPORT ModelAPI_FeatureStateMessage(const Events_ID theID, const void* theSender = 0);
156   /// The virtual destructor
157   MODELAPI_EXPORT virtual ~ModelAPI_FeatureStateMessage();
158
159   /// Returns the feature this message is related to
160   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Feature> feature() const;
161   /// Stores the feature this message is related to
162   MODELAPI_EXPORT void setFeature(std::shared_ptr<ModelAPI_Feature>& theFeature);
163   // For response
164   /// Returns true if feature has specific state
165   MODELAPI_EXPORT bool hasState(const std::string& theFeatureId) const;
166   /// Returns true if feature is enabled
167   MODELAPI_EXPORT bool state(const  std::string& theFeatureId, bool theDefault = false) const;
168   /// Stores the feature state
169   MODELAPI_EXPORT void setState(const std::string& theFeatureId, bool theValue);
170   /// Returns all feature IDs with states
171   MODELAPI_EXPORT std::list<std::string> features() const;
172
173  private:
174   /// For Request
175   std::shared_ptr<ModelAPI_Feature> myCurrentFeature;
176   /// For response
177   std::map<std::string, bool> myFeatureState;
178 };
179
180 /// Message that document (Part, PartSet) was created
181 class ModelAPI_DocumentCreatedMessage : public Events_Message
182 {
183   DocumentPtr myDocument;
184
185  public:
186   /// Creates an empty message
187   MODELAPI_EXPORT ModelAPI_DocumentCreatedMessage(const Events_ID theID, const void* theSender = 0);
188   /// The virtual destructor
189   MODELAPI_EXPORT virtual ~ModelAPI_DocumentCreatedMessage();
190   /// Static. Returns EventID of the message.
191   MODELAPI_EXPORT static Events_ID eventId()
192   {
193     static const char * MY_DOCUMENT_CREATED_EVENT_ID("DocumentCreated");
194     return Events_Loop::eventByName(MY_DOCUMENT_CREATED_EVENT_ID);
195   }
196
197   /// Returns a document stored in the message
198   MODELAPI_EXPORT DocumentPtr document() const;
199   /// Sets a document to the message
200   MODELAPI_EXPORT void setDocument(DocumentPtr theDocument);
201 };
202
203 /// Message that attribute text should be evaluated in the attribute value
204 class ModelAPI_AttributeEvalMessage : public Events_Message
205 {
206   AttributePtr myAttribute;
207
208  public:
209   /// Static. Returns EventID of the message.
210   MODELAPI_EXPORT static Events_ID& eventId()
211   {
212     static const char * MY_ATTRIBUTE_EVALUATION_EVENT_ID("AttributeEvaluationRequest");
213     static Events_ID anId = Events_Loop::eventByName(MY_ATTRIBUTE_EVALUATION_EVENT_ID);
214     return anId;
215   }
216
217   /// Useful method that creates and sends the AttributeEvalMessage event
218   MODELAPI_EXPORT static void send(AttributePtr theAttribute, const void* theSender)
219   {
220     std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
221       std::shared_ptr<ModelAPI_AttributeEvalMessage>(
222       new ModelAPI_AttributeEvalMessage(eventId(), theSender));
223     aMessage->setAttribute(theAttribute);
224     Events_Loop::loop()->send(aMessage);
225   }
226
227   /// Creates an empty message
228   MODELAPI_EXPORT ModelAPI_AttributeEvalMessage(const Events_ID theID, const void* theSender = 0);
229   /// The virtual destructor
230   MODELAPI_EXPORT virtual ~ModelAPI_AttributeEvalMessage();
231
232   /// Returns a document stored in the message
233   MODELAPI_EXPORT AttributePtr attribute() const;
234   /// Sets an attribute to the message
235   MODELAPI_EXPORT void setAttribute(AttributePtr theAttribute);
236 };
237
238 /// Message that the object is renamed
239 class ModelAPI_ObjectRenamedMessage : public Events_Message
240 {
241   ObjectPtr myObject;
242   std::string myOldName;
243   std::string myNewName;
244
245  public:
246   /// Static. Returns EventID of the message.
247   MODELAPI_EXPORT static Events_ID& eventId()
248   {
249     static const char * MY_OBJECT_RENAMED_EVENT_ID("ObjectRenamed");
250     static Events_ID anId = Events_Loop::eventByName(MY_OBJECT_RENAMED_EVENT_ID);
251     return anId;
252   }
253
254   /// Useful method that creates and sends the AttributeEvalMessage event
255   MODELAPI_EXPORT static void send(ObjectPtr theObject,
256                                    const std::string& theOldName,
257                                    const std::string& theNewName,
258                                    const void* theSender);
259
260   /// Creates an empty message
261   MODELAPI_EXPORT ModelAPI_ObjectRenamedMessage(const Events_ID theID, const void* theSender = 0);
262   /// The virtual destructor
263   MODELAPI_EXPORT virtual ~ModelAPI_ObjectRenamedMessage();
264
265   /// Returns an object
266   MODELAPI_EXPORT ObjectPtr object() const;
267   /// Sets an object
268   MODELAPI_EXPORT void setObject(ObjectPtr theObject);
269   /// Returns an old name
270   MODELAPI_EXPORT std::string oldName() const;
271   /// Sets an old name
272   MODELAPI_EXPORT void setOldName(const std::string& theOldName);
273   /// Returns a new name
274   MODELAPI_EXPORT std::string newName() const;
275   /// Sets a new name
276   MODELAPI_EXPORT void setNewName(const std::string& theNewName);
277 };
278
279 /// Message that the parameter should be replaced with its value
280 class ModelAPI_ReplaceParameterMessage : public Events_Message
281 {
282   ObjectPtr myObject;
283
284  public:
285   /// Static. Returns EventID of the message.
286   MODELAPI_EXPORT static Events_ID& eventId()
287   {
288     static const char * MY_EVENT_ID("ReplaceParameter");
289     static Events_ID anId = Events_Loop::eventByName(MY_EVENT_ID);
290     return anId;
291   }
292
293   /// Useful method that creates and sends the AttributeEvalMessage event
294   MODELAPI_EXPORT static void send(ObjectPtr theObject,
295                                    const void* theSender);
296
297   /// Creates an empty message
298   MODELAPI_EXPORT ModelAPI_ReplaceParameterMessage(const Events_ID theID, const void* theSender = 0);
299   /// The virtual destructor
300   MODELAPI_EXPORT virtual ~ModelAPI_ReplaceParameterMessage();
301
302   /// Returns an object
303   MODELAPI_EXPORT ObjectPtr object() const;
304   /// Sets an object
305   MODELAPI_EXPORT void setObject(ObjectPtr theObject);
306 };
307
308 #endif