Salome HOME
f80dd62403021514b87b43367fe1aeaacda33083
[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 = "PluginLoaded";
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 a 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_Feature> reordered() = 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_Feature>& theReordered) const = 0;
143
144   /// returns the creator instance
145   static const ModelAPI_EventCreator* get();
146
147   /// sets the creator instance
148   static void set(const ModelAPI_EventCreator* theCreator);
149 };
150
151 // TODO(sbh): Move this message into a separate package, like "GuiAPI"
152 /// Contains the state information about the feature: is it enabled or disabled.
153 class ModelAPI_FeatureStateMessage : public Events_Message
154 {
155 public:
156   /// Creates an empty message
157   MODELAPI_EXPORT ModelAPI_FeatureStateMessage(const Events_ID theID, const void* theSender = 0);
158   /// The virtual destructor
159   MODELAPI_EXPORT virtual ~ModelAPI_FeatureStateMessage();
160
161   /// Returns the feature this message is related to
162   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Feature> feature() const;
163   /// Stores the feature this message is related to
164   MODELAPI_EXPORT void setFeature(std::shared_ptr<ModelAPI_Feature>& theFeature);
165   // For response
166   /// Returns true if feature has specific state
167   MODELAPI_EXPORT bool hasState(const std::string& theFeatureId) const;
168   /// Returns true if feature is enabled
169   MODELAPI_EXPORT bool state(const  std::string& theFeatureId, bool theDefault = false) const;
170   /// Stores the feature state
171   MODELAPI_EXPORT void setState(const std::string& theFeatureId, bool theValue);
172   /// Returns all feature IDs with states
173   MODELAPI_EXPORT std::list<std::string> features() const;
174
175  private:
176   /// For Request
177   std::shared_ptr<ModelAPI_Feature> myCurrentFeature;
178   /// For response
179   std::map<std::string, bool> myFeatureState;
180 };
181
182 /// Message that document (Part, PartSet) was created
183 class ModelAPI_DocumentCreatedMessage : public Events_Message
184 {
185   DocumentPtr myDocument;
186
187  public:
188   /// Creates an empty message
189   MODELAPI_EXPORT ModelAPI_DocumentCreatedMessage(const Events_ID theID, const void* theSender = 0);
190   /// The virtual destructor
191   MODELAPI_EXPORT virtual ~ModelAPI_DocumentCreatedMessage();
192   /// Static. Returns EventID of the message.
193   MODELAPI_EXPORT static Events_ID eventId()
194   {
195     static const char * MY_DOCUMENT_CREATED_EVENT_ID("DocumentCreated");
196     return Events_Loop::eventByName(MY_DOCUMENT_CREATED_EVENT_ID);
197   }
198
199   /// Returns a document stored in the message
200   MODELAPI_EXPORT DocumentPtr document() const;
201   /// Sets a document to the message
202   MODELAPI_EXPORT void setDocument(DocumentPtr theDocument);
203 };
204
205 /// Message that attribute text should be evaluated in the attribute value
206 class ModelAPI_AttributeEvalMessage : public Events_Message
207 {
208   AttributePtr myAttribute;
209
210  public:
211   /// Static. Returns EventID of the message.
212   MODELAPI_EXPORT static Events_ID& eventId()
213   {
214     static const char * MY_ATTRIBUTE_EVALUATION_EVENT_ID("AttributeEvaluationRequest");
215     static Events_ID anId = Events_Loop::eventByName(MY_ATTRIBUTE_EVALUATION_EVENT_ID);
216     return anId;
217   }
218
219   /// Useful method that creates and sends the AttributeEvalMessage event
220   MODELAPI_EXPORT static void send(AttributePtr theAttribute, const void* theSender)
221   {
222     std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
223       std::shared_ptr<ModelAPI_AttributeEvalMessage>(
224       new ModelAPI_AttributeEvalMessage(eventId(), theSender));
225     aMessage->setAttribute(theAttribute);
226     Events_Loop::loop()->send(aMessage);
227   }
228
229   /// Creates an empty message
230   MODELAPI_EXPORT ModelAPI_AttributeEvalMessage(const Events_ID theID, const void* theSender = 0);
231   /// The virtual destructor
232   MODELAPI_EXPORT virtual ~ModelAPI_AttributeEvalMessage();
233
234   /// Returns a document stored in the message
235   MODELAPI_EXPORT AttributePtr attribute() const;
236   /// Sets an attribute to the message
237   MODELAPI_EXPORT void setAttribute(AttributePtr theAttribute);
238 };
239
240 /// Message that the object is renamed
241 class ModelAPI_ObjectRenamedMessage : public Events_Message
242 {
243   ObjectPtr myObject;
244   std::string myOldName;
245   std::string myNewName;
246
247  public:
248   /// Static. Returns EventID of the message.
249   MODELAPI_EXPORT static Events_ID& eventId()
250   {
251     static const char * MY_OBJECT_RENAMED_EVENT_ID("ObjectRenamed");
252     static Events_ID anId = Events_Loop::eventByName(MY_OBJECT_RENAMED_EVENT_ID);
253     return anId;
254   }
255
256   /// Useful method that creates and sends the AttributeEvalMessage event
257   MODELAPI_EXPORT static void send(ObjectPtr theObject,
258                                    const std::string& theOldName,
259                                    const std::string& theNewName,
260                                    const void* theSender);
261
262   /// Creates an empty message
263   MODELAPI_EXPORT ModelAPI_ObjectRenamedMessage(const Events_ID theID, const void* theSender = 0);
264   /// The virtual destructor
265   MODELAPI_EXPORT virtual ~ModelAPI_ObjectRenamedMessage();
266
267   /// Returns an object
268   MODELAPI_EXPORT ObjectPtr object() const;
269   /// Sets an object
270   MODELAPI_EXPORT void setObject(ObjectPtr theObject);
271   /// Returns an old name
272   MODELAPI_EXPORT std::string oldName() const;
273   /// Sets an old name
274   MODELAPI_EXPORT void setOldName(const std::string& theOldName);
275   /// Returns a new name
276   MODELAPI_EXPORT std::string newName() const;
277   /// Sets a new name
278   MODELAPI_EXPORT void setNewName(const std::string& theNewName);
279 };
280
281 /// Message that the parameter should be replaced with its value
282 class ModelAPI_ReplaceParameterMessage : public Events_Message
283 {
284   ObjectPtr myObject;
285
286  public:
287   /// Static. Returns EventID of the message.
288   MODELAPI_EXPORT static Events_ID& eventId()
289   {
290     static const char * MY_EVENT_ID("ReplaceParameter");
291     static Events_ID anId = Events_Loop::eventByName(MY_EVENT_ID);
292     return anId;
293   }
294
295   /// Useful method that creates and sends the AttributeEvalMessage event
296   MODELAPI_EXPORT static void send(ObjectPtr theObject,
297                                    const void* theSender);
298
299   /// Creates an empty message
300   MODELAPI_EXPORT ModelAPI_ReplaceParameterMessage(const Events_ID theID, const void* theSender = 0);
301   /// The virtual destructor
302   MODELAPI_EXPORT virtual ~ModelAPI_ReplaceParameterMessage();
303
304   /// Returns an object
305   MODELAPI_EXPORT ObjectPtr object() const;
306   /// Sets an object
307   MODELAPI_EXPORT void setObject(ObjectPtr theObject);
308 };
309
310 /// Message that sends the sketch solver and sketcher GUI processes to show in the property panel
311 class ModelAPI_SolverFailedMessage : public Events_Message
312 {
313 public:
314   /// Creates an message
315   MODELAPI_EXPORT ModelAPI_SolverFailedMessage(const Events_ID theID, const void* theSender = 0);
316   /// Default destructor
317   MODELAPI_EXPORT virtual ~ModelAPI_SolverFailedMessage();
318
319   /// Sets list of conflicting constraints
320   MODELAPI_EXPORT void setObjects(const std::set<ObjectPtr>& theObjects);
321   /// Returns list of conflicting constraints
322   MODELAPI_EXPORT const std::set<ObjectPtr>& objects() const;
323
324   /// Sets degrees of freedom
325   void dof(const int theDOF) { myDOF = theDOF; }
326   /// Returns degrees of freedom
327   const int& dof() const { return myDOF; }
328
329 private:
330   std::set<ObjectPtr> myObjects;
331   int myDOF;
332 };
333
334 #endif