Salome HOME
69153a98847fc449c4962a6aeb0145a27ae4983d
[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 data of feature is updated (comes with ModelAPI_ObjectUpdatedMessage)
32 static const char * EVENT_OBJECT_MOVED = "ObjectsMoved";
33 /// Event ID that visualization must be redisplayed (comes with ModelAPI_ObjectUpdatedMessage)
34 static const char * EVENT_OBJECT_TO_REDISPLAY = "ObjectsToRedisplay";
35 /// Event ID that visualization must be redisplayed (comes with ModelAPI_ObjectUpdatedMessage)
36 static const char * EVENT_OPERATION_LAUNCHED = "OperationLaunched";
37 /// Event ID that plugin is loaded (comes with ModelAPI_ObjectUpdatedMessage)
38 static const char * EVENT_PLUGIN_LOADED = "PliginLoaded";
39 /// Event ID that data of feature has to be shown (comes with ModelAPI_ObjectUpdatedMessage)
40 static const char * EVENT_OBJECT_TOSHOW = "ObjectShow";
41 /// Event ID that data of feature has to be shown (comes with ModelAPI_ObjectUpdatedMessage)
42 static const char * EVENT_OBJECT_TOHIDE = "ObjectHide";
43 //
44 static const char * EVENT_DOCUMENT_CHANGED = "CurrentDocumentChanged";
45
46 static const char * EVENT_FEATURE_STATE_REQUEST = "FeatureStateRequest";
47 static const char * EVENT_FEATURE_STATE_RESPONSE = "FeatureStateResponse";
48
49
50 static const char * EVENT_UPDATE_VIEWER_BLOCKED = "UpdateViewerBlocked";
51 static const char * EVENT_UPDATE_VIEWER_UNBLOCKED = "UpdateViewerUnblocked";
52
53 /// Message that feature was changed (used for Object Browser update): moved, updated and deleted
54 class MODELAPI_EXPORT ModelAPI_ObjectUpdatedMessage : public Events_MessageGroup
55 {
56  protected:
57   /// Creates an empty message
58   ModelAPI_ObjectUpdatedMessage(const Events_ID theID, const void* theSender = 0);
59   /// The virtual destructor
60   virtual ~ModelAPI_ObjectUpdatedMessage();
61
62  public:
63   /// Returns the feature that has been updated
64   virtual const std::set<ObjectPtr>& objects() const = 0;
65
66   //! Creates a new empty group (to store it in the loop before flush)
67   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
68
69   //! Allows to join the given message with the current one
70   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
71 };
72
73 /// Message that feature was deleted (used for Object Browser update)
74 class MODELAPI_EXPORT ModelAPI_ObjectDeletedMessage : public Events_MessageGroup
75 {
76 protected:
77   /// Creates an empty message
78   ModelAPI_ObjectDeletedMessage(const Events_ID theID, const void* theSender = 0);
79   /// The virtual destructor
80   virtual ~ModelAPI_ObjectDeletedMessage();
81
82 public:
83   /// Returns the feature that has been updated
84   virtual std::shared_ptr<ModelAPI_Document> document() const = 0;
85
86   /// Returns the group where the feature was deleted
87   virtual const std::set<std::string>& groups() const = 0;
88
89   /// Creates the new empty message of this kind
90   virtual std::shared_ptr<Events_MessageGroup> newEmpty() = 0;
91
92   /// Returns the identifier of the kind of a message
93   virtual const Events_ID messageId() = 0;
94
95   /// Appenad to this message the given one.
96   virtual void Join(const std::shared_ptr<Events_MessageGroup>& theJoined) = 0;
97 };
98
99 /// Allows to create ModelAPI messages
100 class MODELAPI_EXPORT ModelAPI_EventCreator
101 {
102 public:
103   /// creates created, updated or moved messages and sends to the loop
104   virtual void sendUpdated(const ObjectPtr& theObject, const Events_ID& theEvent,
105                            const bool isGroupped = true) const = 0;
106   /// creates deleted message and sends to the loop
107   virtual void sendDeleted(const std::shared_ptr<ModelAPI_Document>& theDoc,
108                            const std::string& theGroup) const = 0;
109
110   /// returns the creator instance
111   static const ModelAPI_EventCreator* get();
112
113   /// sets the creator instance
114   static void set(const ModelAPI_EventCreator* theCreator);
115 };
116
117 // TODO(sbh): Move this message into a separate package, like "GuiAPI"
118 /// Contains the state information about the feature: is it enabled or disabled.
119 class ModelAPI_FeatureStateMessage : public Events_Message
120 {
121 public:
122   /// Creates an empty message
123   MODELAPI_EXPORT ModelAPI_FeatureStateMessage(const Events_ID theID, const void* theSender = 0);
124   /// The virtual destructor
125   MODELAPI_EXPORT virtual ~ModelAPI_FeatureStateMessage();
126
127   /// Returns the feature this message is related to
128   MODELAPI_EXPORT std::shared_ptr<ModelAPI_Feature> feature() const;
129   /// Stores the feature this message is related to
130   MODELAPI_EXPORT void setFeature(std::shared_ptr<ModelAPI_Feature>& theFeature);
131   // For response
132   /// Returns true if feature has specific state
133   MODELAPI_EXPORT bool hasState(const std::string& theFeatureId) const;
134   /// Returns true if feature is enabled
135   MODELAPI_EXPORT bool state(const  std::string& theFeatureId, bool theDefault = false) const;
136   /// Stores the feature state
137   MODELAPI_EXPORT void setState(const std::string& theFeatureId, bool theValue);
138   /// Returns all feature IDs with states
139   MODELAPI_EXPORT std::list<std::string> features() const;
140
141  private:
142   /// For Request
143   std::shared_ptr<ModelAPI_Feature> myCurrentFeature;
144   /// For response
145   std::map<std::string, bool> myFeatureState;
146 };
147
148 /// Message that document (Part, PartSet) was created
149 class ModelAPI_DocumentCreatedMessage : public Events_Message
150 {
151   DocumentPtr myDocument;
152
153  public:
154   /// Creates an empty message
155   MODELAPI_EXPORT ModelAPI_DocumentCreatedMessage(const Events_ID theID, const void* theSender = 0);
156   /// The virtual destructor
157   MODELAPI_EXPORT virtual ~ModelAPI_DocumentCreatedMessage();
158   /// Static. Returns EventID of the message.
159   MODELAPI_EXPORT static Events_ID eventId()
160   {
161     static const char * MY_DOCUMENT_CREATED_EVENT_ID("DocumentCreated");
162     return Events_Loop::eventByName(MY_DOCUMENT_CREATED_EVENT_ID);
163   }
164
165   /// Returns a document stored in the message
166   MODELAPI_EXPORT DocumentPtr document() const;
167   /// Sets a document to the message
168   MODELAPI_EXPORT void setDocument(DocumentPtr theDocument);
169 };
170
171 /// Message that attribute text should be evaluated in the attribute value
172 class ModelAPI_AttributeEvalMessage : public Events_Message
173 {
174   AttributePtr myAttribute;
175
176  public:
177   /// Creates an empty message
178   MODELAPI_EXPORT ModelAPI_AttributeEvalMessage(const Events_ID theID, const void* theSender = 0);
179   /// The virtual destructor
180   MODELAPI_EXPORT virtual ~ModelAPI_AttributeEvalMessage();
181   /// Static. Returns EventID of the message.
182   MODELAPI_EXPORT static Events_ID eventId()
183   {
184     static const char * MY_ATTRIBUTE_EVALUATION_EVENT_ID("AttributeEvaluationRequest");
185     return Events_Loop::eventByName(MY_ATTRIBUTE_EVALUATION_EVENT_ID);
186   }
187
188   /// Returns a document stored in the message
189   MODELAPI_EXPORT AttributePtr attribute() const;
190   /// Sets a document to the message
191   MODELAPI_EXPORT void setAttribute(AttributePtr theDocument);
192 };
193
194 #endif