Salome HOME
Fix importParameters plugin
[modules/shaper.git] / src / ModelAPI / ModelAPI_Events.h
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef MODELAPI_EVENTS_H_
21 #define MODELAPI_EVENTS_H_
22
23 #include <ModelAPI.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Attribute.h>
27 #include <Events_MessageGroup.h>
28 #include <Events_Loop.h>
29
30 #include <memory>
31 #include <string>
32 #include <set>
33 #include <map>
34
35
36 class ModelAPI_Document;
37 class ModelAPI_ResultParameter;
38 class GeomAPI_Pnt2d;
39
40 /// Event ID that feature is created (comes with ModelAPI_ObjectUpdatedMessage)
41 static const char * EVENT_OBJECT_CREATED = "ObjectCreated";
42 /// Event ID that data of feature is updated (comes with Model_ObjectUpdatedMessage)
43 static const char * EVENT_OBJECT_UPDATED = "ObjectUpdated";
44 /// Event ID that data of feature is deleted (comes with Model_ObjectDeletedMessage)
45 static const char * EVENT_OBJECT_DELETED = "ObjectDeleted";
46 /// Event ID that name of feature is changed (comes with Model_ObjectRenamedMessage)
47 static const char * EVENT_OBJECT_RENAMED = "ObjectRenamed";
48 /// Event ID that data of feature is updated (comes with ModelAPI_ObjectUpdatedMessage)
49 static const char * EVENT_OBJECT_MOVED = "ObjectsMoved";
50 /// Event ID that visualization must be redisplayed (comes with ModelAPI_ObjectUpdatedMessage)
51 static const char * EVENT_OBJECT_TO_REDISPLAY = "ObjectsToRedisplay";
52 /// Event ID that plugin is loaded (comes with ModelAPI_ObjectUpdatedMessage)
53 static const char * EVENT_PLUGIN_LOADED = "PluginLoaded";
54 /// The active document becomes another one
55 static const char * EVENT_DOCUMENT_CHANGED = "CurrentDocumentChanged";
56
57 /// Event ID that order of objects in group is changed,
58 /// so, tree must be fully recreated (movement of feature)
59 static const char * EVENT_ORDER_UPDATED = "OrderUpdated";
60 /// Event ID that the sketch is prepared and all grouped messages for the solver may be flushed
61 static const char * EVENT_UPDATE_SELECTION = "UpdateSelection";
62
63 /// Request for the enabled/disabled actions behavior for some specific features
64 static const char * EVENT_FEATURE_STATE_REQUEST = "FeatureStateRequest";
65 /// Reply for the enabled/disabled actions behavior for some specific features
66 static const char * EVENT_FEATURE_STATE_RESPONSE = "FeatureStateResponse";
67
68 /// To block the viewer updates
69 static const char * EVENT_UPDATE_VIEWER_BLOCKED = "UpdateViewerBlocked";
70 /// To unblock the viewer updates
71 static const char * EVENT_UPDATE_VIEWER_UNBLOCKED = "UpdateViewerUnblocked";
72
73 /// To inform that there is an empty presentation in the viewer
74 static const char * EVENT_EMPTY_AIS_PRESENTATION = "EmptyAISPresentation";
75 /// To inform that there is an empty operation for presentation in the viewer
76 static const char * EVENT_EMPTY_OPERATION_PRESENTATION = "EmptyOperationPresentation";
77 /// To block preview
78 static const char * EVENT_PREVIEW_BLOCKED = "PreviewBlocked";
79 /// To preview the current feature in the viewer (to compute the result)
80 static const char * EVENT_PREVIEW_REQUESTED = "PreviewRequested";
81 /// To block automatic recomputation of any feature (by the GUI button press)
82 static const char * EVENT_AUTOMATIC_RECOMPUTATION_DISABLE = "DisableAutomaticRecomputation";
83 /// To unblock block automatic recomputation (default state: the GUI button unpressed)
84 static const char * EVENT_AUTOMATIC_RECOMPUTATION_ENABLE = "EnableAutomaticRecomputation";
85
86 /// Event ID that solver has conflicting constraints (comes with ModelAPI_SolverFailedMessage)
87 static const char * EVENT_SOLVER_FAILED = "SolverFailed";
88 /// Event ID that the problem in solver disappeared
89 static const char * EVENT_SOLVER_REPAIRED = "SolverRepaired";
90
91 /// Event Id that sketch has DoF = 0
92 static const char * EVENT_SKETCH_FULLY_CONSTRAINED = "SketchFullyConstrainted";
93 /// Event Id that sketch has DoF > 0
94 static const char * EVENT_SKETCH_UNDER_CONSTRAINED = "SketchUnderConstrainted";
95 /// Event Id that sketch has DoF < 0
96 static const char * EVENT_SKETCH_OVER_CONSTRAINED  = "SketchOverConstrainted";
97
98 /// Event ID that informs that some object has changed the stability
99 static const char * EVENT_STABILITY_CHANGED = "StabilityChanged";
100
101 /// Event ID that the sketch is prepared and all grouped messages for the solver may be flushed
102 static const char * EVENT_SKETCH_PREPARED = "SketchPrepared";
103
104 /// Message that feature was changed (used for Object Browser update): moved, updated and deleted
105 class MODELAPI_EXPORT ModelAPI_ObjectUpdatedMessage : public Events_MessageGroup
106 {
107  protected:
108   /// Creates an empty message
109   ModelAPI_ObjectUpdatedMessage(const Events_ID theID, const void* theSender = 0);
110   /// The virtual destructor
111   virtual ~ModelAPI_ObjectUpdatedMessage();
112
113  public:
114   /// Returns the feature that has been updated
115   virtual const std::set<ObjectPtr>& objects() const = 0;
116
117   //! Creates a new empty group (to store it in the loop before flush)
118   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
119
120   //! Allows to join the given message with the current one
121   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
122 };
123
124 /// Message that feature was deleted (used for Object Browser update)
125 class MODELAPI_EXPORT ModelAPI_ObjectDeletedMessage : public Events_MessageGroup
126 {
127 protected:
128   /// Creates an empty message
129   ModelAPI_ObjectDeletedMessage(const Events_ID theID, const void* theSender = 0);
130   /// The virtual destructor
131   virtual ~ModelAPI_ObjectDeletedMessage();
132
133 public:
134   /// Returns the groups where the objects were deleted
135   virtual const std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string> >&
136     groups() const = 0;
137
138   /// Creates the new empty message of this kind
139   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
140
141   /// Returns the identifier of the kind of a message
142   virtual const Events_ID messageId() = 0;
143
144   /// Appends to this message the given one.
145   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
146 };
147
148 /// Message that order changed (used for Object Browser update)
149 class MODELAPI_EXPORT ModelAPI_OrderUpdatedMessage : public Events_Message
150 {
151 protected:
152   /// Creates a message:
153   ModelAPI_OrderUpdatedMessage(const Events_ID theID, const void* theSender = 0);
154   /// The virtual destructor
155   virtual ~ModelAPI_OrderUpdatedMessage();
156
157 public:
158   /// Returns the document that has been updated
159   virtual std::shared_ptr<ModelAPI_Feature> reordered() = 0;
160
161   /// Returns the identifier of the kind of a message
162   virtual const Events_ID messageId() = 0;
163 };
164
165 /// Allows to create ModelAPI messages
166 class MODELAPI_EXPORT ModelAPI_EventCreator
167 {
168 public:
169   /// creates created, updated or moved messages and sends to the loop
170   virtual void sendUpdated(const ObjectPtr& theObject, const Events_ID& theEvent,
171                            const bool isGroupped = true) const = 0;
172   /// creates created, updated or moved messages with the objects collection and sends to the loop
173   virtual void sendUpdated(const std::list<ObjectPtr>& theObjects, const Events_ID& theEvent,
174     const bool isGroupped = true) const = 0;
175   /// creates deleted message and sends to the loop
176   virtual void sendDeleted(const std::shared_ptr<ModelAPI_Document>& theDoc,
177                            const std::string& theGroup) const = 0;
178   /// creates reordered message and sends to the loop
179   virtual void sendReordered(const std::shared_ptr<ModelAPI_Feature>& theReordered) const = 0;
180
181   /// returns the creator instance
182   static const ModelAPI_EventCreator* get();
183
184   /// sets the creator instance
185   static void set(const ModelAPI_EventCreator* theCreator);
186 };
187
188 /// Contains the state information about the feature: is it enabled or disabled.
189 class ModelAPI_FeatureStateMessage : public Events_Message
190 {
191 public:
192   /// Creates an empty message
193   MODELAPI_EXPORT ModelAPI_FeatureStateMessage(const Events_ID theID, const void* theSender = 0);
194   /// The virtual destructor
195   MODELAPI_EXPORT virtual ~ModelAPI_FeatureStateMessage();
196
197   /// Returns the feature this message is related to
198   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Feature> feature() const;
199   /// Stores the feature this message is related to
200   MODELAPI_EXPORT void setFeature(std::shared_ptr<ModelAPI_Feature>& theFeature);
201   // For response
202   /// Returns true if feature has specific state
203   MODELAPI_EXPORT bool hasState(const std::string& theFeatureId) const;
204   /// Returns true if feature is enabled
205   MODELAPI_EXPORT bool state(const  std::string& theFeatureId, bool theDefault = false) const;
206   /// Stores the feature state
207   MODELAPI_EXPORT void setState(const std::string& theFeatureId, bool theValue);
208   /// Returns all feature IDs with states
209   MODELAPI_EXPORT std::list<std::string> features() const;
210
211  private:
212   /// For Request
213   std::shared_ptr<ModelAPI_Feature> myCurrentFeature;
214   /// For response
215   std::map<std::string, bool> myFeatureState;
216 };
217
218 /// Message that document (Part, PartSet) was created
219 class ModelAPI_DocumentCreatedMessage : public Events_Message
220 {
221   DocumentPtr myDocument;
222
223  public:
224   /// Creates an empty message
225   MODELAPI_EXPORT ModelAPI_DocumentCreatedMessage(const Events_ID theID, const void* theSender = 0);
226   /// The virtual destructor
227   MODELAPI_EXPORT virtual ~ModelAPI_DocumentCreatedMessage();
228   /// Static. Returns EventID of the message.
229   MODELAPI_EXPORT static Events_ID eventId()
230   {
231     static const char * MY_DOCUMENT_CREATED_EVENT_ID("DocumentCreated");
232     return Events_Loop::eventByName(MY_DOCUMENT_CREATED_EVENT_ID);
233   }
234
235   /// Returns a document stored in the message
236   MODELAPI_EXPORT DocumentPtr document() const;
237   /// Sets a document to the message
238   MODELAPI_EXPORT void setDocument(DocumentPtr theDocument);
239 };
240
241 /// Message that attribute text should be evaluated in the attribute value
242 class ModelAPI_AttributeEvalMessage : public Events_Message
243 {
244   AttributePtr myAttribute;
245
246  public:
247   /// Static. Returns EventID of the message.
248   MODELAPI_EXPORT static Events_ID& eventId()
249   {
250     static const char * MY_ATTRIBUTE_EVALUATION_EVENT_ID("AttributeEvaluationRequest");
251     static Events_ID anId = Events_Loop::eventByName(MY_ATTRIBUTE_EVALUATION_EVENT_ID);
252     return anId;
253   }
254
255   /// Useful method that creates and sends the AttributeEvalMessage event
256   MODELAPI_EXPORT static void send(AttributePtr theAttribute, const void* theSender)
257   {
258     std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
259       std::shared_ptr<ModelAPI_AttributeEvalMessage>(
260       new ModelAPI_AttributeEvalMessage(eventId(), theSender));
261     aMessage->setAttribute(theAttribute);
262     Events_Loop::loop()->send(aMessage);
263   }
264
265   /// Creates an empty message
266   MODELAPI_EXPORT ModelAPI_AttributeEvalMessage(const Events_ID theID, const void* theSender = 0);
267   /// The virtual destructor
268   MODELAPI_EXPORT virtual ~ModelAPI_AttributeEvalMessage();
269
270   /// Returns a document stored in the message
271   MODELAPI_EXPORT AttributePtr attribute() const;
272   /// Sets an attribute to the message
273   MODELAPI_EXPORT void setAttribute(AttributePtr theAttribute);
274 };
275
276 /// Message that parameter feature expression should be evaluated: value and error producing
277 class ModelAPI_ParameterEvalMessage : public Events_Message
278 {
279   FeaturePtr myParam; ///< parameters that should be evaluated
280   bool myIsProcessed; ///< true if results were set
281   /// result of processing, list of parameters in expression found
282   std::list<std::shared_ptr<ModelAPI_ResultParameter> > myParamsList;
283   double myResult; ///< result of processing, the computed value of the expression
284   std::string myError; ///< error of processing, empty if there is no error
285
286  public:
287   /// Static. Returns EventID of the message.
288   MODELAPI_EXPORT static Events_ID& eventId()
289   {
290     static const char * MY_PARAMETER_EVALUATION_EVENT_ID("ParameterEvaluationRequest");
291     static Events_ID anId = Events_Loop::eventByName(MY_PARAMETER_EVALUATION_EVENT_ID);
292     return anId;
293   }
294
295   /// Useful method that creates and sends the event.
296   /// Returns the message, processed, with the resulting fields filled.
297   MODELAPI_EXPORT static std::shared_ptr<ModelAPI_ParameterEvalMessage>
298     send(FeaturePtr theParameter, const void* theSender)
299   {
300     std::shared_ptr<ModelAPI_ParameterEvalMessage> aMessage =
301       std::shared_ptr<ModelAPI_ParameterEvalMessage>(
302       new ModelAPI_ParameterEvalMessage(eventId(), theSender));
303     aMessage->setParameter(theParameter);
304     Events_Loop::loop()->send(aMessage);
305     return aMessage;
306   }
307
308   /// Creates an empty message
309   MODELAPI_EXPORT ModelAPI_ParameterEvalMessage(const Events_ID theID, const void* theSender = 0);
310   /// The virtual destructor
311   MODELAPI_EXPORT virtual ~ModelAPI_ParameterEvalMessage();
312
313   /// Returns a parameter stored in the message
314   MODELAPI_EXPORT FeaturePtr parameter() const;
315   /// Sets a parameter to the message
316   MODELAPI_EXPORT void setParameter(FeaturePtr theParam);
317   /// Sets the results of processing
318   MODELAPI_EXPORT void setResults(
319     const std::list<std::shared_ptr<ModelAPI_ResultParameter> >& theParamsList,
320     const double theResult, const std::string& theError);
321   /// Returns true if the expression is processed
322   MODELAPI_EXPORT bool isProcessed();
323   /// Returns the results of processing: list of parameters found in the expression
324   MODELAPI_EXPORT const std::list<std::shared_ptr<ModelAPI_ResultParameter> >& params() const;
325   /// Returns the expression result
326   MODELAPI_EXPORT const double& result() const;
327   /// Returns the interpreter error (empty if no error)
328   MODELAPI_EXPORT const std::string& error() const;
329 };
330
331
332 /// Message to ask compute the positions of parameters in the expression
333 class ModelAPI_ComputePositionsMessage : public Events_Message
334 {
335   std::string myExpression; ///< the expression string
336   std::string myParamName; ///< name of the parameter to be searched
337   std::list<std::pair<int, int> > myPositions; ///< computation result: start-end position indices
338
339 public:
340   /// Static. Returns EventID of the message.
341   MODELAPI_EXPORT static Events_ID& eventId()
342   {
343     static const char * MY_COMPUTE_POSITIOND_EVENT_ID("ComputePositionsRequest");
344     static Events_ID anId = Events_Loop::eventByName(MY_COMPUTE_POSITIOND_EVENT_ID);
345     return anId;
346   }
347
348   /// Useful method that creates and sends the AttributeEvalMessage event
349   /// Returns the message, processed, with the resulting fields filled
350   MODELAPI_EXPORT static std::shared_ptr<ModelAPI_ComputePositionsMessage>
351     send(const std::string& theExpression, const std::string& theParameter, const void* theSender)
352   {
353     std::shared_ptr<ModelAPI_ComputePositionsMessage> aMessage =
354       std::shared_ptr<ModelAPI_ComputePositionsMessage>(
355       new ModelAPI_ComputePositionsMessage(eventId(), theSender));
356     aMessage->set(theExpression, theParameter);
357     Events_Loop::loop()->send(aMessage);
358     return aMessage;
359   }
360
361   /// Creates an empty message
362   MODELAPI_EXPORT ModelAPI_ComputePositionsMessage(
363     const Events_ID theID, const void* theSender = 0);
364   /// The virtual destructor
365   MODELAPI_EXPORT virtual ~ModelAPI_ComputePositionsMessage();
366
367   /// Returns an expression stored in the message
368   MODELAPI_EXPORT const std::string& expression() const;
369   /// Returns a parameter name stored in the message
370   MODELAPI_EXPORT const std::string& parameter() const;
371   /// Sets an expression and parameter needed for computation
372   MODELAPI_EXPORT void set(const std::string& theExpression, const std::string& theParameter);
373   /// Sets the results of processing
374   MODELAPI_EXPORT void setPositions(const std::list<std::pair<int, int> >& thePositions);
375   /// Returns the results of processing: position start and end indices
376   MODELAPI_EXPORT const std::list<std::pair<int, int> >& positions() const;
377 };
378
379 /// Message that the object is renamed
380 class ModelAPI_ObjectRenamedMessage : public Events_Message
381 {
382   ObjectPtr myObject;
383   std::string myOldName;
384   std::string myNewName;
385
386  public:
387   /// Static. Returns EventID of the message.
388   MODELAPI_EXPORT static Events_ID& eventId()
389   {
390     static const char * MY_OBJECT_RENAMED_EVENT_ID("ObjectRenamed");
391     static Events_ID anId = Events_Loop::eventByName(MY_OBJECT_RENAMED_EVENT_ID);
392     return anId;
393   }
394
395   /// Useful method that creates and sends the AttributeEvalMessage event
396   MODELAPI_EXPORT static void send(ObjectPtr theObject,
397                                    const std::string& theOldName,
398                                    const std::string& theNewName,
399                                    const void* theSender);
400
401   /// Creates an empty message
402   MODELAPI_EXPORT ModelAPI_ObjectRenamedMessage(const Events_ID theID, const void* theSender = 0);
403   /// The virtual destructor
404   MODELAPI_EXPORT virtual ~ModelAPI_ObjectRenamedMessage();
405
406   /// Returns an object
407   MODELAPI_EXPORT ObjectPtr object() const;
408   /// Sets an object
409   MODELAPI_EXPORT void setObject(ObjectPtr theObject);
410   /// Returns an old name
411   MODELAPI_EXPORT std::string oldName() const;
412   /// Sets an old name
413   MODELAPI_EXPORT void setOldName(const std::string& theOldName);
414   /// Returns a new name
415   MODELAPI_EXPORT std::string newName() const;
416   /// Sets a new name
417   MODELAPI_EXPORT void setNewName(const std::string& theNewName);
418 };
419
420 /// Message that the parameter should be replaced with its value
421 class ModelAPI_ReplaceParameterMessage : public Events_Message
422 {
423   ObjectPtr myObject;
424
425  public:
426   /// Static. Returns EventID of the message.
427   MODELAPI_EXPORT static Events_ID& eventId()
428   {
429     static const char * MY_EVENT_ID("ReplaceParameter");
430     static Events_ID anId = Events_Loop::eventByName(MY_EVENT_ID);
431     return anId;
432   }
433
434   /// Useful method that creates and sends the AttributeEvalMessage event
435   MODELAPI_EXPORT static void send(ObjectPtr theObject,
436                                    const void* theSender);
437
438   /// Creates an empty message
439   MODELAPI_EXPORT
440     ModelAPI_ReplaceParameterMessage(const Events_ID theID, const void* theSender = 0);
441   /// The virtual destructor
442   MODELAPI_EXPORT virtual ~ModelAPI_ReplaceParameterMessage();
443
444   /// Returns an object
445   MODELAPI_EXPORT ObjectPtr object() const;
446   /// Sets an object
447   MODELAPI_EXPORT void setObject(ObjectPtr theObject);
448 };
449
450 /// Message that sends the sketch solver and sketcher GUI processes to show in the property panel
451 class ModelAPI_SolverFailedMessage : public Events_Message
452 {
453 public:
454   /// Creates an message
455   MODELAPI_EXPORT ModelAPI_SolverFailedMessage(const Events_ID theID, const void* theSender = 0);
456   /// Default destructor
457   MODELAPI_EXPORT virtual ~ModelAPI_SolverFailedMessage();
458
459   /// Sets list of conflicting constraints
460   MODELAPI_EXPORT void setObjects(const std::set<ObjectPtr>& theObjects);
461   /// Returns list of conflicting constraints
462   MODELAPI_EXPORT const std::set<ObjectPtr>& objects() const;
463
464   /// Sets degrees of freedom
465   void dof(const int theDOF) { myDOF = theDOF; }
466   /// Returns degrees of freedom
467   const int& dof() const { return myDOF; }
468
469 private:
470   std::set<ObjectPtr> myObjects;
471   int myDOF;
472 };
473
474 /// Message sent when feature or attribute has been moved.
475 /// Stores the moving object/attribute, original and new positions of mouse.
476 class ModelAPI_ObjectMovedMessage : public Events_Message
477 {
478   ObjectPtr myMovedObject;
479   AttributePtr myMovedAttribute;
480
481   std::shared_ptr<GeomAPI_Pnt2d> myOriginalPosition;
482   std::shared_ptr<GeomAPI_Pnt2d> myCurrentPosition;
483
484 public:
485   MODELAPI_EXPORT ModelAPI_ObjectMovedMessage(const void* theSender = 0);
486
487   /// Set object which is being moved (if the message already contains attribute it will be cleared)
488   MODELAPI_EXPORT void setMovedObject(const ObjectPtr& theMovedObject);
489   /// Set attribute which is being moved (if the message already contains object it will be cleared)
490   MODELAPI_EXPORT void setMovedAttribute(const AttributePtr& theMovedAttribute);
491
492   /// Return moved object
493   ObjectPtr movedObject() const
494   { return myMovedObject; }
495   /// Return moved attribute
496   AttributePtr movedAttribute() const
497   { return myMovedAttribute; }
498
499   /// Set original mouse position
500   MODELAPI_EXPORT void setOriginalPosition(double theX, double theY);
501   /// Set original mouse position
502   MODELAPI_EXPORT void setOriginalPosition(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
503   /// Return original mouse position
504   const std::shared_ptr<GeomAPI_Pnt2d>& originalPosition() const
505   { return myOriginalPosition; }
506
507   /// Set current mouse position
508   MODELAPI_EXPORT void setCurrentPosition(double theX, double theY);
509   /// Set current mouse position
510   MODELAPI_EXPORT void setCurrentPosition(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
511   /// Return current mouse position
512   const std::shared_ptr<GeomAPI_Pnt2d>& currentPosition() const
513   { return myCurrentPosition; }
514 };
515
516 #endif