Salome HOME
Copyright update 2022
[modules/shaper.git] / src / Model / Model_Events.cpp
1 // Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Model_Events.h>
21 #include <Events_Loop.h>
22
23 /// Alone instance of the creator per application
24 Model_EventCreator MY_CREATOR;
25
26 /////////////////////// CREATOR /////////////////////////////
27 void Model_EventCreator::sendUpdated(const ObjectPtr& theObject, const Events_ID& theEvent,
28                                      const bool isGroupped) const
29 {
30   std::shared_ptr<Model_ObjectUpdatedMessage> aMsg(
31     new Model_ObjectUpdatedMessage(theObject, theEvent));
32   Events_Loop::loop()->send(aMsg, isGroupped);
33 }
34
35 void Model_EventCreator::sendUpdated(const std::list<ObjectPtr>& theObjects,
36   const Events_ID& theEvent, const bool isGroupped) const
37 {
38   if (theObjects.empty())
39     return;
40   std::list<ObjectPtr>::const_iterator anObj = theObjects.cbegin();
41   std::shared_ptr<Model_ObjectUpdatedMessage> aMsg(
42     new Model_ObjectUpdatedMessage(*anObj, theEvent));
43   for(anObj++; anObj != theObjects.cend(); anObj++) {
44     std::shared_ptr<Model_ObjectUpdatedMessage> aJoined(
45       new Model_ObjectUpdatedMessage(*anObj, theEvent));
46     aMsg->Join(aJoined);
47   }
48   Events_Loop::loop()->send(aMsg, isGroupped);
49 }
50
51 void Model_EventCreator::sendDeleted(const std::shared_ptr<ModelAPI_Document>& theDoc,
52                                      const std::string& theGroup) const
53 {
54   std::shared_ptr<Model_ObjectDeletedMessage> aMsg(
55     new Model_ObjectDeletedMessage(theDoc, theGroup));
56   Events_Loop::loop()->send(aMsg, true);
57 }
58
59 void Model_EventCreator::sendReordered(const std::shared_ptr<ModelAPI_Feature>& theReordered) const
60 {
61   std::shared_ptr<Model_OrderUpdatedMessage> aMsg(
62     new Model_OrderUpdatedMessage(theReordered));
63   Events_Loop::loop()->send(aMsg, true);
64 }
65
66 Model_EventCreator::Model_EventCreator()
67 {
68   ModelAPI_EventCreator::set(this);
69 }
70
71 /////////////////////// UPDATED MESSAGE /////////////////////////////
72 Model_ObjectUpdatedMessage::Model_ObjectUpdatedMessage(const ObjectPtr& theObject,
73                                                        const Events_ID& theEvent)
74     : ModelAPI_ObjectUpdatedMessage(theEvent, 0)
75 {
76   if (theObject) {
77     myObjects.insert(theObject);
78   }
79 }
80
81 const std::set<ObjectPtr>& Model_ObjectUpdatedMessage::objects() const
82 {
83   return myObjects;
84 }
85
86 std::shared_ptr<Events_MessageGroup> Model_ObjectUpdatedMessage::newEmpty()
87 {
88   ObjectPtr anEmptyObject;
89   return std::shared_ptr<Model_ObjectUpdatedMessage>(
90     new Model_ObjectUpdatedMessage(anEmptyObject, eventID()));
91 }
92
93 void Model_ObjectUpdatedMessage::Join(const std::shared_ptr<Events_MessageGroup>& theJoined)
94 {
95   std::shared_ptr<Model_ObjectUpdatedMessage> aJoined =
96     std::dynamic_pointer_cast<Model_ObjectUpdatedMessage>(theJoined);
97   std::set<ObjectPtr>::iterator aFIter = aJoined->myObjects.begin();
98   for (; aFIter != aJoined->myObjects.end(); aFIter++) {
99     myObjects.insert(*aFIter);
100   }
101 }
102
103 /////////////////////// DELETED MESSAGE /////////////////////////////
104 Model_ObjectDeletedMessage::Model_ObjectDeletedMessage(
105     const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theGroup)
106     : ModelAPI_ObjectDeletedMessage(messageId(), 0)
107 {
108   if (!theGroup.empty()) {
109     myGroups.push_back(
110       std::pair<std::shared_ptr<ModelAPI_Document>, std::string>(theDoc, theGroup));
111   }
112 }
113
114 std::shared_ptr<Events_MessageGroup> Model_ObjectDeletedMessage::newEmpty()
115 {
116   static const std::shared_ptr<ModelAPI_Document> anEmpty;
117   return std::shared_ptr<Model_ObjectDeletedMessage>(new Model_ObjectDeletedMessage(anEmpty, ""));
118 }
119
120 const Events_ID Model_ObjectDeletedMessage::messageId()
121 {
122   static Events_ID MY_ID = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
123   return MY_ID;
124 }
125
126 void Model_ObjectDeletedMessage::Join(const std::shared_ptr<Events_MessageGroup>& theJoined)
127 {
128   std::shared_ptr<Model_ObjectDeletedMessage> aJoined =
129     std::dynamic_pointer_cast<Model_ObjectDeletedMessage>(theJoined);
130
131   const std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>& aJGroups =
132     aJoined->groups();
133
134   std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::iterator aGIter;
135   std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator aJIter;
136   for (aJIter = aJGroups.cbegin(); aJIter != aJGroups.cend(); aJIter++) {
137     for (aGIter = myGroups.begin(); aGIter != myGroups.end(); aGIter++) {
138       if (aGIter->first == aJIter->first && aGIter->second == aJIter->second)
139         break; // exists, so no need to insert
140     }
141     if (aGIter == myGroups.end())
142       myGroups.push_back(*aJIter);
143   }
144 }
145
146 /////////////////////// REORDERED MESSAGE /////////////////////////////
147 Model_OrderUpdatedMessage::Model_OrderUpdatedMessage(
148     FeaturePtr theReordered, const void* theSender)
149     : ModelAPI_OrderUpdatedMessage(messageId(), theSender),
150     myReordered(theReordered)
151 {
152 }
153
154 const Events_ID Model_OrderUpdatedMessage::messageId()
155 {
156   static Events_ID MY_ID = Events_Loop::eventByName(EVENT_ORDER_UPDATED);
157   return MY_ID;
158 }