Salome HOME
ae84e6cbc606cc96b62992e6d1f275e21530a827
[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 visualization must be redisplayed (comes with ModelAPI_ObjectUpdatedMessage)
38 static const char * EVENT_OPERATION_LAUNCHED = "OperationLaunched";
39 /// Event ID that plugin is loaded (comes with ModelAPI_ObjectUpdatedMessage)
40 static const char * EVENT_PLUGIN_LOADED = "PliginLoaded";
41 //
42 static const char * EVENT_DOCUMENT_CHANGED = "CurrentDocumentChanged";
43
44 static const char * EVENT_FEATURE_STATE_REQUEST = "FeatureStateRequest";
45 static const char * EVENT_FEATURE_STATE_RESPONSE = "FeatureStateResponse";
46
47
48 static const char * EVENT_UPDATE_VIEWER_BLOCKED = "UpdateViewerBlocked";
49 static const char * EVENT_UPDATE_VIEWER_UNBLOCKED = "UpdateViewerUnblocked";
50
51 static const char * EVENT_SOLVER_FAILED = "SolverFailed";
52 static const char * EVENT_SOLVER_REPAIRED = "SolverRepaired";
53
54 /// Event ID that order of objects in group is changed, so, tree must be fully rectreated (movement of feature)
55 static const char * EVENT_ORDER_UPDATED = "OrderUpdated";
56
57 /// Message that feature was changed (used for Object Browser update): moved, updated and deleted
58 class MODELAPI_EXPORT ModelAPI_ObjectUpdatedMessage : public Events_MessageGroup
59 {
60  protected:
61   /// Creates an empty message
62   ModelAPI_ObjectUpdatedMessage(const Events_ID theID, const void* theSender = 0);
63   /// The virtual destructor
64   virtual ~ModelAPI_ObjectUpdatedMessage();
65
66  public:
67   /// Returns the feature that has been updated
68   virtual const std::set<ObjectPtr>& objects() const = 0;
69
70   //! Creates a new empty group (to store it in the loop before flush)
71   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
72
73   //! Allows to join the given message with the current one
74   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
75 };
76
77 /// Message that feature was deleted (used for Object Browser update)
78 class MODELAPI_EXPORT ModelAPI_ObjectDeletedMessage : public Events_MessageGroup
79 {
80 protected:
81   /// Creates an empty message
82   ModelAPI_ObjectDeletedMessage(const Events_ID theID, const void* theSender = 0);
83   /// The virtual destructor
84   virtual ~ModelAPI_ObjectDeletedMessage();
85
86 public:
87   /// Returns the document that has been updated
88   virtual std::shared_ptr<ModelAPI_Document> document() const = 0;
89
90   /// Returns the groups where the objects were deleted
91   virtual const std::set<std::string>& groups() const = 0;
92
93   /// Creates the new empty message of this kind
94   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
95
96   /// Returns the identifier of the kind of a message
97   virtual const Events_ID messageId() = 0;
98
99   /// Appenad to this message the given one.
100   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
101 };
102
103 /// Message that order changed (used for Object Browser update)
104 class MODELAPI_EXPORT ModelAPI_OrderUpdatedMessage : public Events_MessageGroup
105 {
106 protected:
107   /// Creates an empty message
108   ModelAPI_OrderUpdatedMessage(const Events_ID theID, const void* theSender = 0);
109   /// The virtual destructor
110   virtual ~ModelAPI_OrderUpdatedMessage();
111
112 public:
113   /// Returns the document that has been updated
114   virtual std::shared_ptr<ModelAPI_Document> document() const = 0;
115
116   /// Returns the groups where the objects were reordered
117   virtual const std::set<std::string>& groups() const = 0;
118
119   /// Creates the new empty message of this kind
120   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
121
122   /// Returns the identifier of the kind of a message
123   virtual const Events_ID messageId() = 0;
124
125   /// Appenad to this message the given one.
126   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
127 };
128
129 /// Allows to create ModelAPI messages
130 class MODELAPI_EXPORT ModelAPI_EventCreator
131 {
132 public:
133   /// creates created, updated or moved messages and sends to the loop
134   virtual void sendUpdated(const ObjectPtr& theObject, const Events_ID& theEvent,
135                            const bool isGroupped = true) const = 0;
136   /// creates deleted message and sends to the loop
137   virtual void sendDeleted(const std::shared_ptr<ModelAPI_Document>& theDoc,
138                            const std::string& theGroup) const = 0;
139   /// creates reordered message and sends to the loop
140   virtual void sendReordered(const std::shared_ptr<ModelAPI_Document>& theDoc,
141                              const std::string& theGroup) const = 0;
142
143   /// returns the creator instance
144   static const ModelAPI_EventCreator* get();
145
146   /// sets the creator instance
147   static void set(const ModelAPI_EventCreator* theCreator);
148 };
149
150 // TODO(sbh): Move this message into a separate package, like "GuiAPI"
151 /// Contains the state information about the feature: is it enabled or disabled.
152 class ModelAPI_FeatureStateMessage : public Events_Message
153 {
154 public:
155   /// Creates an empty message
156   MODELAPI_EXPORT ModelAPI_FeatureStateMessage(const Events_ID theID, const void* theSender = 0);
157   /// The virtual destructor
158   MODELAPI_EXPORT virtual ~ModelAPI_FeatureStateMessage();
159
160   /// Returns the feature this message is related to
161   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Feature> feature() const;
162   /// Stores the feature this message is related to
163   MODELAPI_EXPORT void setFeature(std::shared_ptr<ModelAPI_Feature>& theFeature);
164   // For response
165   /// Returns true if feature has specific state
166   MODELAPI_EXPORT bool hasState(const std::string& theFeatureId) const;
167   /// Returns true if feature is enabled
168   MODELAPI_EXPORT bool state(const  std::string& theFeatureId, bool theDefault = false) const;
169   /// Stores the feature state
170   MODELAPI_EXPORT void setState(const std::string& theFeatureId, bool theValue);
171   /// Returns all feature IDs with states
172   MODELAPI_EXPORT std::list<std::string> features() const;
173
174  private:
175   /// For Request
176   std::shared_ptr<ModelAPI_Feature> myCurrentFeature;
177   /// For response
178   std::map<std::string, bool> myFeatureState;
179 };
180
181 /// Message that document (Part, PartSet) was created
182 class ModelAPI_DocumentCreatedMessage : public Events_Message
183 {
184   DocumentPtr myDocument;
185
186  public:
187   /// Creates an empty message
188   MODELAPI_EXPORT ModelAPI_DocumentCreatedMessage(const Events_ID theID, const void* theSender = 0);
189   /// The virtual destructor
190   MODELAPI_EXPORT virtual ~ModelAPI_DocumentCreatedMessage();
191   /// Static. Returns EventID of the message.
192   MODELAPI_EXPORT static Events_ID eventId()
193   {
194     static const char * MY_DOCUMENT_CREATED_EVENT_ID("DocumentCreated");
195     return Events_Loop::eventByName(MY_DOCUMENT_CREATED_EVENT_ID);
196   }
197
198   /// Returns a document stored in the message
199   MODELAPI_EXPORT DocumentPtr document() const;
200   /// Sets a document to the message
201   MODELAPI_EXPORT void setDocument(DocumentPtr theDocument);
202 };
203
204 /// Message that attribute text should be evaluated in the attribute value
205 class ModelAPI_AttributeEvalMessage : public Events_Message
206 {
207   AttributePtr myAttribute;
208
209  public:
210   /// Static. Returns EventID of the message.
211   MODELAPI_EXPORT static Events_ID& eventId()
212   {
213     static const char * MY_ATTRIBUTE_EVALUATION_EVENT_ID("AttributeEvaluationRequest");
214     static Events_ID anId = Events_Loop::eventByName(MY_ATTRIBUTE_EVALUATION_EVENT_ID);
215     return anId;
216   }
217
218   /// Useful method that creates and sends the AttributeEvalMessage event
219   MODELAPI_EXPORT static void send(AttributePtr theAttribute, const void* theSender)
220   {
221     std::shared_ptr<ModelAPI_AttributeEvalMessage> aMessage =
222       std::shared_ptr<ModelAPI_AttributeEvalMessage>(
223       new ModelAPI_AttributeEvalMessage(eventId(), theSender));
224     aMessage->setAttribute(theAttribute);
225     Events_Loop::loop()->send(aMessage);
226   }
227
228   /// Creates an empty message
229   MODELAPI_EXPORT ModelAPI_AttributeEvalMessage(const Events_ID theID, const void* theSender = 0);
230   /// The virtual destructor
231   MODELAPI_EXPORT virtual ~ModelAPI_AttributeEvalMessage();
232
233   /// Returns a document stored in the message
234   MODELAPI_EXPORT AttributePtr attribute() const;
235   /// Sets an attribute to the message
236   MODELAPI_EXPORT void setAttribute(AttributePtr theAttribute);
237 };
238
239 /// Message that the object is renamed
240 class ModelAPI_ObjectRenamedMessage : public Events_Message
241 {
242   ObjectPtr myObject;
243   std::string myOldName;
244   std::string myNewName;
245
246  public:
247   /// Static. Returns EventID of the message.
248   MODELAPI_EXPORT static Events_ID& eventId()
249   {
250     static const char * MY_OBJECT_RENAMED_EVENT_ID("ObjectRenamed");
251     static Events_ID anId = Events_Loop::eventByName(MY_OBJECT_RENAMED_EVENT_ID);
252     return anId;
253   }
254
255   /// Useful method that creates and sends the AttributeEvalMessage event
256   MODELAPI_EXPORT static void send(ObjectPtr theObject,
257                                    const std::string& theOldName,
258                                    const std::string& theNewName,
259                                    const void* theSender);
260
261   /// Creates an empty message
262   MODELAPI_EXPORT ModelAPI_ObjectRenamedMessage(const Events_ID theID, const void* theSender = 0);
263   /// The virtual destructor
264   MODELAPI_EXPORT virtual ~ModelAPI_ObjectRenamedMessage();
265
266   /// Returns an object
267   MODELAPI_EXPORT ObjectPtr object() const;
268   /// Sets an object
269   MODELAPI_EXPORT void setObject(ObjectPtr theObject);
270   /// Returns an old name
271   MODELAPI_EXPORT std::string oldName() const;
272   /// Sets an old name
273   MODELAPI_EXPORT void setOldName(const std::string& theOldName);
274   /// Returns a new name
275   MODELAPI_EXPORT std::string newName() const;
276   /// Sets a new name
277   MODELAPI_EXPORT void setNewName(const std::string& theNewName);
278 };
279
280 #endif