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