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