Salome HOME
Several improvements mostly for automatic update of result
[modules/shaper.git] / src / Model / Model_Events.h
1 // File:        Model_Events.h
2 // Created:     10 Apr 2014
3 // Author:      Mikhail PONIKAROV
4
5 #ifndef Model_Events_HeaderFile
6 #define Model_Events_HeaderFile
7
8 #include <Model.h>
9 #include <Events_MessageGroup.h>
10 #include <Events_Loop.h>
11 #include <boost/shared_ptr.hpp>
12 #include <string>
13 #include <set>
14
15 #include "ModelAPI_Feature.h"
16
17 class ModelAPI_Document;
18
19 /// Event ID that feature is created (comes with Model_FeatureUpdatedMessage)
20 static const char * EVENT_FEATURE_CREATED = "FeatureCreated";
21 /// Event ID that data of feature is updated (comes with Model_FeatureUpdatedMessage)
22 static const char * EVENT_FEATURE_UPDATED = "FeatureUpdated";
23 /// Event ID that data of feature is deleted (comes with Model_FeatureDeletedMessage)
24 static const char * EVENT_FEATURE_DELETED = "FeatureDeleted";
25 /// Event ID that data of feature is updated (comes with Model_FeaturesMovedMessage)
26 static const char * EVENT_FEATURE_MOVED = "FeaturesMoved";
27
28 /// Message that feature was changed (used for Object Browser update): moved, updated and deleted
29 class Model_FeatureUpdatedMessage : public Events_MessageGroup {
30   std::set<FeaturePtr > myFeatures; ///< which feature is changed
31 public:
32   /// sender is not important, all information is located in the feature
33   Model_FeatureUpdatedMessage(
34     const FeaturePtr& theFeature,
35     const Events_ID& theEvent) : Events_MessageGroup(theEvent, 0)
36   {if (theFeature) myFeatures.insert(theFeature);}
37
38   /// Returns the feature that has been updated
39   std::set<FeaturePtr> features() const {return myFeatures;}
40
41   //! Creates a new empty group (to store it in the loop before flush)
42   virtual Events_MessageGroup* newEmpty() {
43     FeaturePtr anEmptyFeature;
44     return new Model_FeatureUpdatedMessage(anEmptyFeature, eventID());
45   }
46
47   //! Allows to join the given message with the current one
48   virtual void Join(Events_MessageGroup& theJoined) {
49     Model_FeatureUpdatedMessage* aJoined = dynamic_cast<Model_FeatureUpdatedMessage*>(&theJoined);
50     std::set<FeaturePtr >::iterator aFIter = aJoined->myFeatures.begin();
51     for(; aFIter != aJoined->myFeatures.end(); aFIter++) {
52       myFeatures.insert(*aFIter);
53     }
54   }
55 };
56
57 /// Message that feature was deleted (used for Object Browser update)
58 class Model_FeatureDeletedMessage : public Events_MessageGroup {
59   boost::shared_ptr<ModelAPI_Document> myDoc; ///< document owner of the feature
60   std::set<std::string> myGroups; ///< group identifiers that contained the deleted feature
61 public:
62   /// creates a message by initialization of fields
63 //  Model_FeatureDeletedMessage(const boost::shared_ptr<ModelAPI_Document>& theDoc,
64 //    const std::string& theGroup);
65
66   /// Returns the ID of this message (EVENT_FEATURE_DELETED)
67 //  static const Events_ID messageId();
68
69   /// Returns the feature that has been updated
70   boost::shared_ptr<ModelAPI_Document> document() const {return myDoc;}
71
72   /// Returns the group where the feature was deleted
73   const std::set<std::string >& groups() const {return myGroups;}
74
75   //! Creates a new empty group (to store it in the loop before flush)
76 //  virtual Events_MessageGroup* newEmpty();
77
78   //! Allows to join the given message with the current one
79 //  virtual void Join(Events_MessageGroup& theJoined);
80
81   Events_MessageGroup* newEmpty() {
82     return new Model_FeatureDeletedMessage(myDoc, "");
83   }
84
85   Model_FeatureDeletedMessage(
86     const boost::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theGroup)
87     : Events_MessageGroup(messageId(), 0), myDoc(theDoc)
88
89   {
90     if (!theGroup.empty())
91       myGroups.insert(theGroup);
92   }
93
94   const Events_ID messageId()
95   {
96     static Events_ID MY_ID = Events_Loop::eventByName(EVENT_FEATURE_DELETED);
97     return MY_ID;
98   }
99
100   void Join(Events_MessageGroup& theJoined)
101   {
102     Model_FeatureDeletedMessage* aJoined = dynamic_cast<Model_FeatureDeletedMessage*>(&theJoined);
103     std::set<std::string>::iterator aGIter = aJoined->myGroups.begin();
104     for(; aGIter != aJoined->myGroups.end(); aGIter++) {
105       myGroups.insert(*aGIter);
106     }
107   }
108 };
109
110 #endif