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