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