]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Events.h
Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into BR_coding_rules
[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 class ModelAPI_ResultParameter;
25
26 /// Event ID that feature is created (comes with ModelAPI_ObjectUpdatedMessage)
27 static const char * EVENT_OBJECT_CREATED = "ObjectCreated";
28 /// Event ID that data of feature is updated (comes with Model_ObjectUpdatedMessage)
29 static const char * EVENT_OBJECT_UPDATED = "ObjectUpdated";
30 /// Event ID that data of feature is deleted (comes with Model_ObjectDeletedMessage)
31 static const char * EVENT_OBJECT_DELETED = "ObjectDeleted";
32 /// Event ID that name of feature is changed (comes with Model_ObjectRenamedMessage)
33 static const char * EVENT_OBJECT_RENAMED = "ObjectRenamed";
34 /// Event ID that data of feature is updated (comes with ModelAPI_ObjectUpdatedMessage)
35 static const char * EVENT_OBJECT_MOVED = "ObjectsMoved";
36 /// Event ID that visualization must be redisplayed (comes with ModelAPI_ObjectUpdatedMessage)
37 static const char * EVENT_OBJECT_TO_REDISPLAY = "ObjectsToRedisplay";
38 /// Event ID that plugin is loaded (comes with ModelAPI_ObjectUpdatedMessage)
39 static const char * EVENT_PLUGIN_LOADED = "PluginLoaded";
40 /// The active document becomes another one
41 static const char * EVENT_DOCUMENT_CHANGED = "CurrentDocumentChanged";
42
43 /// Event ID that order of objects in group is changed,
44 /// so, tree must be fully rectreated (movement of feature)
45 static const char * EVENT_ORDER_UPDATED = "OrderUpdated";
46 /// Event ID that the sketch is prepared and all grouped messages for the solver may be flushed
47 static const char * EVENT_UPDATE_SELECTION = "UpdateSelection";
48
49 /// Request for the enabled/disabled actions behavior for some specific features
50 static const char * EVENT_FEATURE_STATE_REQUEST = "FeatureStateRequest";
51 /// Reply for the enabled/disabled actions behavior for some specific features
52 static const char * EVENT_FEATURE_STATE_RESPONSE = "FeatureStateResponse";
53
54 /// To block the viewer updates
55 static const char * EVENT_UPDATE_VIEWER_BLOCKED = "UpdateViewerBlocked";
56 /// To unblock the viewer updates
57 static const char * EVENT_UPDATE_VIEWER_UNBLOCKED = "UpdateViewerUnblocked";
58
59 /// To inform that there is an empty presentation in the viewer
60 static const char * EVENT_EMPTY_AIS_PRESENTATION = "EmptyAISPresentation";
61 /// To inform that there is an empty operation for presentation in the viewer
62 static const char * EVENT_EMPTY_OPERATION_PRESENTATION = "EmptyOperationPresentation";
63 /// To block preview
64 static const char * EVENT_PREVIEW_BLOCKED = "PreviewBlocked";
65 /// To preview the current feature in the viewer (to compute the result)
66 static const char * EVENT_PREVIEW_REQUESTED = "PreviewRequested";
67
68 /// Event ID that solver has conflicting constraints (comes with ModelAPI_SolverFailedMessage)
69 static const char * EVENT_SOLVER_FAILED = "SolverFailed";
70 /// Event ID that the problem in solver disappeared
71 static const char * EVENT_SOLVER_REPAIRED = "SolverRepaired";
72
73 /// Event ID that informs that some object has changed the stability
74 static const char * EVENT_STABILITY_CHANGED = "StabilityChanged";
75
76 /// Event ID that the sketch is prepared and all grouped messages for the solver may be flushed
77 static const char * EVENT_SKETCH_PREPARED = "SketchPrepared";
78
79 /// Message that feature was changed (used for Object Browser update): moved, updated and deleted
80 class MODELAPI_EXPORT ModelAPI_ObjectUpdatedMessage : public Events_MessageGroup
81 {
82  protected:
83   /// Creates an empty message
84   ModelAPI_ObjectUpdatedMessage(const Events_ID theID, const void* theSender = 0);
85   /// The virtual destructor
86   virtual ~ModelAPI_ObjectUpdatedMessage();
87
88  public:
89   /// Returns the feature that has been updated
90   virtual const std::set<ObjectPtr>& objects() const = 0;
91
92   //! Creates a new empty group (to store it in the loop before flush)
93   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
94
95   //! Allows to join the given message with the current one
96   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
97 };
98
99 /// Message that feature was deleted (used for Object Browser update)
100 class MODELAPI_EXPORT ModelAPI_ObjectDeletedMessage : public Events_MessageGroup
101 {
102 protected:
103   /// Creates an empty message
104   ModelAPI_ObjectDeletedMessage(const Events_ID theID, const void* theSender = 0);
105   /// The virtual destructor
106   virtual ~ModelAPI_ObjectDeletedMessage();
107
108 public:
109   /// Returns the document that has been updated
110   virtual std::shared_ptr<ModelAPI_Document> document() const = 0;
111
112   /// Returns the groups where the objects were deleted
113   virtual const std::set<std::string>& groups() const = 0;
114
115   /// Creates the new empty message of this kind
116   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
117
118   /// Returns the identifier of the kind of a message
119   virtual const Events_ID messageId() = 0;
120
121   /// Appenad to this message the given one.
122   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
123 };
124
125 /// Message that order changed (used for Object Browser update)
126 class MODELAPI_EXPORT ModelAPI_OrderUpdatedMessage : public Events_Message
127 {
128 protected:
129   /// Creates a message:
130   ModelAPI_OrderUpdatedMessage(const Events_ID theID, const void* theSender = 0);
131   /// The virtual destructor
132   virtual ~ModelAPI_OrderUpdatedMessage();
133
134 public:
135   /// Returns the document that has been updated
136   virtual std::shared_ptr<ModelAPI_Feature> reordered() = 0;
137
138   /// Returns the identifier of the kind of a message
139   virtual const Events_ID messageId() = 0;
140 };
141
142 /// Allows to create ModelAPI messages
143 class MODELAPI_EXPORT ModelAPI_EventCreator
144 {
145 public:
146   /// creates created, updated or moved messages and sends to the loop
147   virtual void sendUpdated(const ObjectPtr& theObject, const Events_ID& theEvent,
148                            const bool isGroupped = true) const = 0;
149   /// creates deleted message and sends to the loop
150   virtual void sendDeleted(const std::shared_ptr<ModelAPI_Document>& theDoc,
151                            const std::string& theGroup) const = 0;
152   /// creates reordered message and sends to the loop
153   virtual void sendReordered(const std::shared_ptr<ModelAPI_Feature>& theReordered) const = 0;
154
155   /// returns the creator instance
156   static const ModelAPI_EventCreator* get();
157
158   /// sets the creator instance
159   static void set(const ModelAPI_EventCreator* theCreator);
160 };
161
162 // TODO(sbh): Move this message into a separate package, like "GuiAPI"
163 /// Contains the state information about the feature: is it enabled or disabled.
164 class ModelAPI_FeatureStateMessage : public Events_Message
165 {
166 public:
167   /// Creates an empty message
168   MODELAPI_EXPORT ModelAPI_FeatureStateMessage(const Events_ID theID, const void* theSender = 0);
169   /// The virtual destructor
170   MODELAPI_EXPORT virtual ~ModelAPI_FeatureStateMessage();
171
172   /// Returns the feature this message is related to
173   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Feature> feature() const;
174   /// Stores the feature this message is related to
175   MODELAPI_EXPORT void setFeature(std::shared_ptr<ModelAPI_Feature>& theFeature);
176   // For response
177   /// Returns true if feature has specific state
178   MODELAPI_EXPORT bool hasState(const std::string& theFeatureId) const;
179   /// Returns true if feature is enabled
180   MODELAPI_EXPORT bool state(const  std::string& theFeatureId, bool theDefault = false) const;
181   /// Stores the feature state
182   MODELAPI_EXPORT void setState(const std::string& theFeatureId, bool theValue);
183   /// Returns all feature IDs with states
184   MODELAPI_EXPORT std::list<std::string> features() const;
185
186  private:
187   /// For Request
188   std::shared_ptr<ModelAPI_Feature> myCurrentFeature;
189   /// For response
190   std::map<std::string, bool> myFeatureState;
191 };
192
193 /// Message that document (Part, PartSet) was created
194 class ModelAPI_DocumentCreatedMessage : public Events_Message
195 {
196   DocumentPtr myDocument;
197
198  public:
199   /// Creates an empty message
200   MODELAPI_EXPORT ModelAPI_DocumentCreatedMessage(const Events_ID theID, const void* theSender = 0);
201   /// The virtual destructor
202   MODELAPI_EXPORT virtual ~ModelAPI_DocumentCreatedMessage();
203   /// Static. Returns EventID of the message.
204   MODELAPI_EXPORT static Events_ID eventId()
205   {
206     static const char * MY_DOCUMENT_CREATED_EVENT_ID("DocumentCreated");
207     return Events_Loop::eventByName(MY_DOCUMENT_CREATED_EVENT_ID);
208   }
209
210   /// Returns a document stored in the message
211   MODELAPI_EXPORT DocumentPtr document() const;
212   /// Sets a document to the message
213   MODELAPI_EXPORT void setDocument(DocumentPtr theDocument);
214 };
215
216 /// Message that attribute text should be evaluated in the attribute value
217 class ModelAPI_AttributeEvalMessage : public Events_Message
218 {
219   AttributePtr myAttribute;
220
221  public:
222   /// Static. Returns EventID of the message.
223   MODELAPI_EXPORT static Events_ID& eventId()
224   {
225     static const char * MY_ATTRIBUTE_EVALUATION_EVENT_ID("AttributeEvaluationRequest");
226     static Events_ID anId = Events_Loop::eventByName(MY_ATTRIBUTE_EVALUATION_EVENT_ID);
227     return anId;
228   }
229
230   /// Useful method that creates and sends the AttributeEvalMessage event
231   MODELAPI_EXPORT static void send(AttributePtr theAttribute, const void* theSender)
232   {
233     std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
234       std::shared_ptr<ModelAPI_AttributeEvalMessage>(
235       new ModelAPI_AttributeEvalMessage(eventId(), theSender));
236     aMessage->setAttribute(theAttribute);
237     Events_Loop::loop()->send(aMessage);
238   }
239
240   /// Creates an empty message
241   MODELAPI_EXPORT ModelAPI_AttributeEvalMessage(const Events_ID theID, const void* theSender = 0);
242   /// The virtual destructor
243   MODELAPI_EXPORT virtual ~ModelAPI_AttributeEvalMessage();
244
245   /// Returns a document stored in the message
246   MODELAPI_EXPORT AttributePtr attribute() const;
247   /// Sets an attribute to the message
248   MODELAPI_EXPORT void setAttribute(AttributePtr theAttribute);
249 };
250
251 /// Message that parameter feature expression should be evaluated: value and error producing
252 class ModelAPI_ParameterEvalMessage : public Events_Message
253 {
254   FeaturePtr myParam; ///< parameters that should be evaluated
255   bool myIsProcessed; ///< true if results were set
256   /// result of processing, list of parameters in expression found
257   std::list<std::shared_ptr<ModelAPI_ResultParameter> > myParamsList;
258   double myResult; ///< result of processing, the computed value of the expression
259   std::string myError; ///< error of processing, empty if there is no error
260
261  public:
262   /// Static. Returns EventID of the message.
263   MODELAPI_EXPORT static Events_ID& eventId()
264   {
265     static const char * MY_PARAMETER_EVALUATION_EVENT_ID("ParameterEvaluationRequest");
266     static Events_ID anId = Events_Loop::eventByName(MY_PARAMETER_EVALUATION_EVENT_ID);
267     return anId;
268   }
269
270   /// Useful method that creates and sends the event.
271   /// Returns the message, processed, with the resulting fields filled.
272   MODELAPI_EXPORT static std::shared_ptr<ModelAPI_ParameterEvalMessage>
273     send(FeaturePtr theParameter, const void* theSender)
274   {
275     std::shared_ptr<ModelAPI_ParameterEvalMessage> aMessage =
276       std::shared_ptr<ModelAPI_ParameterEvalMessage>(
277       new ModelAPI_ParameterEvalMessage(eventId(), theSender));
278     aMessage->setParameter(theParameter);
279     Events_Loop::loop()->send(aMessage);
280     return aMessage;
281   }
282
283   /// Creates an empty message
284   MODELAPI_EXPORT ModelAPI_ParameterEvalMessage(const Events_ID theID, const void* theSender = 0);
285   /// The virtual destructor
286   MODELAPI_EXPORT virtual ~ModelAPI_ParameterEvalMessage();
287
288   /// Returns a parameter stored in the message
289   MODELAPI_EXPORT FeaturePtr parameter() const;
290   /// Sets a parameter to the message
291   MODELAPI_EXPORT void setParameter(FeaturePtr theParam);
292   /// Sets the results of processing
293   MODELAPI_EXPORT void setResults(
294     const std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
295     const double theResult, const std::string& theError);
296   /// Returns true if the expression is processed
297   MODELAPI_EXPORT bool isProcessed();
298   /// Returns the results of processing: list of parameters found in the expression
299   MODELAPI_EXPORT const std::list<std::shared_ptr<ModelAPI_ResultParameter> >& params() const;
300   /// Returns the expression result
301   MODELAPI_EXPORT const double& result() const;
302   /// Returns the interpreter error (empty if no error)
303   MODELAPI_EXPORT const std::string& error() const;
304 };
305
306
307 /// Message to ask compute the positions of parameters in the expression
308 class ModelAPI_ComputePositionsMessage : public Events_Message
309 {
310   std::string myExpression; ///< the expression string
311   std::string myParamName; ///< name of the parameter to be searched
312   std::list<std::pair<int, int> > myPositions; ///< computation result: start-end position indices
313
314 public:
315   /// Static. Returns EventID of the message.
316   MODELAPI_EXPORT static Events_ID& eventId()
317   {
318     static const char * MY_COMPUTE_POSITIOND_EVENT_ID("ComputePositionsRequest");
319     static Events_ID anId = Events_Loop::eventByName(MY_COMPUTE_POSITIOND_EVENT_ID);
320     return anId;
321   }
322
323   /// Useful method that creates and sends the AttributeEvalMessage event
324   /// Returns the message, processed, with the resulting fields filled
325   MODELAPI_EXPORT static std::shared_ptr<ModelAPI_ComputePositionsMessage>
326     send(const std::string& theExpression, const std::string& theParameter, const void* theSender)
327   {
328     std::shared_ptr<ModelAPI_ComputePositionsMessage> aMessage =
329       std::shared_ptr<ModelAPI_ComputePositionsMessage>(
330       new ModelAPI_ComputePositionsMessage(eventId(), theSender));
331     aMessage->set(theExpression, theParameter);
332     Events_Loop::loop()->send(aMessage);
333     return aMessage;
334   }
335
336   /// Creates an empty message
337   MODELAPI_EXPORT ModelAPI_ComputePositionsMessage(
338     const Events_ID theID, const void* theSender = 0);
339   /// The virtual destructor
340   MODELAPI_EXPORT virtual ~ModelAPI_ComputePositionsMessage();
341
342   /// Returns an expression stored in the message
343   MODELAPI_EXPORT const std::string& expression() const;
344   /// Returns a parameter name stored in the message
345   MODELAPI_EXPORT const std::string& parameter() const;
346   /// Sets an expression and parameter needed for computation
347   MODELAPI_EXPORT void set(const std::string& theExpression, const std::string& theParameter);
348   /// Sets the results of processing
349   MODELAPI_EXPORT void setPositions(const std::list<std::pair<int, int> >& thePositions);
350   /// Returns the results of processing: position start and end indices
351   MODELAPI_EXPORT const std::list<std::pair<int, int> >& positions() const;
352 };
353
354 /// Message that the object is renamed
355 class ModelAPI_ObjectRenamedMessage : public Events_Message
356 {
357   ObjectPtr myObject;
358   std::string myOldName;
359   std::string myNewName;
360
361  public:
362   /// Static. Returns EventID of the message.
363   MODELAPI_EXPORT static Events_ID& eventId()
364   {
365     static const char * MY_OBJECT_RENAMED_EVENT_ID("ObjectRenamed");
366     static Events_ID anId = Events_Loop::eventByName(MY_OBJECT_RENAMED_EVENT_ID);
367     return anId;
368   }
369
370   /// Useful method that creates and sends the AttributeEvalMessage event
371   MODELAPI_EXPORT static void send(ObjectPtr theObject,
372                                    const std::string& theOldName,
373                                    const std::string& theNewName,
374                                    const void* theSender);
375
376   /// Creates an empty message
377   MODELAPI_EXPORT ModelAPI_ObjectRenamedMessage(const Events_ID theID, const void* theSender = 0);
378   /// The virtual destructor
379   MODELAPI_EXPORT virtual ~ModelAPI_ObjectRenamedMessage();
380
381   /// Returns an object
382   MODELAPI_EXPORT ObjectPtr object() const;
383   /// Sets an object
384   MODELAPI_EXPORT void setObject(ObjectPtr theObject);
385   /// Returns an old name
386   MODELAPI_EXPORT std::string oldName() const;
387   /// Sets an old name
388   MODELAPI_EXPORT void setOldName(const std::string& theOldName);
389   /// Returns a new name
390   MODELAPI_EXPORT std::string newName() const;
391   /// Sets a new name
392   MODELAPI_EXPORT void setNewName(const std::string& theNewName);
393 };
394
395 /// Message that the parameter should be replaced with its value
396 class ModelAPI_ReplaceParameterMessage : public Events_Message
397 {
398   ObjectPtr myObject;
399
400  public:
401   /// Static. Returns EventID of the message.
402   MODELAPI_EXPORT static Events_ID& eventId()
403   {
404     static const char * MY_EVENT_ID("ReplaceParameter");
405     static Events_ID anId = Events_Loop::eventByName(MY_EVENT_ID);
406     return anId;
407   }
408
409   /// Useful method that creates and sends the AttributeEvalMessage event
410   MODELAPI_EXPORT static void send(ObjectPtr theObject,
411                                    const void* theSender);
412
413   /// Creates an empty message
414   MODELAPI_EXPORT
415     ModelAPI_ReplaceParameterMessage(const Events_ID theID, const void* theSender = 0);
416   /// The virtual destructor
417   MODELAPI_EXPORT virtual ~ModelAPI_ReplaceParameterMessage();
418
419   /// Returns an object
420   MODELAPI_EXPORT ObjectPtr object() const;
421   /// Sets an object
422   MODELAPI_EXPORT void setObject(ObjectPtr theObject);
423 };
424
425 /// Message that sends the sketch solver and sketcher GUI processes to show in the property panel
426 class ModelAPI_SolverFailedMessage : public Events_Message
427 {
428 public:
429   /// Creates an message
430   MODELAPI_EXPORT ModelAPI_SolverFailedMessage(const Events_ID theID, const void* theSender = 0);
431   /// Default destructor
432   MODELAPI_EXPORT virtual ~ModelAPI_SolverFailedMessage();
433
434   /// Sets list of conflicting constraints
435   MODELAPI_EXPORT void setObjects(const std::set<ObjectPtr>& theObjects);
436   /// Returns list of conflicting constraints
437   MODELAPI_EXPORT const std::set<ObjectPtr>& objects() const;
438
439   /// Sets degrees of freedom
440   void dof(const int theDOF) { myDOF = theDOF; }
441   /// Returns degrees of freedom
442   const int& dof() const { return myDOF; }
443
444 private:
445   std::set<ObjectPtr> myObjects;
446   int myDOF;
447 };
448
449 #endif