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