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