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