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