]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Events.cpp
Salome HOME
Optimization in the frames of issue #1668
[modules/shaper.git] / src / Model / Model_Events.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Model_Events.h>
22 #include <Events_Loop.h>
23
24 /// Alone instance of the creator per application
25 Model_EventCreator MY_CREATOR;
26
27 /////////////////////// CREATOR /////////////////////////////
28 void Model_EventCreator::sendUpdated(const ObjectPtr& theObject, const Events_ID& theEvent,
29                                      const bool isGroupped) const
30 {
31   std::shared_ptr<Model_ObjectUpdatedMessage> aMsg(
32     new Model_ObjectUpdatedMessage(theObject, theEvent));
33   Events_Loop::loop()->send(aMsg, isGroupped);
34 }
35
36 void Model_EventCreator::sendUpdated(const std::list<ObjectPtr>& theObjects,
37   const Events_ID& theEvent, const bool isGroupped) const
38 {
39   if (theObjects.empty())
40     return;
41   std::list<ObjectPtr>::const_iterator anObj = theObjects.cbegin();
42   std::shared_ptr<Model_ObjectUpdatedMessage> aMsg(
43     new Model_ObjectUpdatedMessage(*anObj, theEvent));
44   for(anObj++; anObj != theObjects.cend(); anObj++) {
45     std::shared_ptr<Model_ObjectUpdatedMessage> aJoined(
46       new Model_ObjectUpdatedMessage(*anObj, theEvent));
47     aMsg->Join(aJoined);
48   }
49   Events_Loop::loop()->send(aMsg, isGroupped);
50 }
51
52 void Model_EventCreator::sendDeleted(const std::shared_ptr<ModelAPI_Document>& theDoc,
53                                      const std::string& theGroup) const
54 {
55   std::shared_ptr<Model_ObjectDeletedMessage> aMsg(
56     new Model_ObjectDeletedMessage(theDoc, theGroup));
57   Events_Loop::loop()->send(aMsg, true);
58 }
59
60 void Model_EventCreator::sendReordered(const std::shared_ptr<ModelAPI_Feature>& theReordered) const
61 {
62   std::shared_ptr<Model_OrderUpdatedMessage> aMsg(
63     new Model_OrderUpdatedMessage(theReordered));
64   Events_Loop::loop()->send(aMsg, true);
65 }
66
67 Model_EventCreator::Model_EventCreator()
68 {
69   ModelAPI_EventCreator::set(this);
70 }
71
72 /////////////////////// UPDATED MESSAGE /////////////////////////////
73 Model_ObjectUpdatedMessage::Model_ObjectUpdatedMessage(const ObjectPtr& theObject,
74                                                        const Events_ID& theEvent)
75     : ModelAPI_ObjectUpdatedMessage(theEvent, 0)
76 {
77   if (theObject) {
78     myObjects.insert(theObject);
79   }
80 }
81
82 const std::set<ObjectPtr>& Model_ObjectUpdatedMessage::objects() const
83 {
84   return myObjects;
85 }
86
87 std::shared_ptr<Events_MessageGroup> Model_ObjectUpdatedMessage::newEmpty()
88 {
89   ObjectPtr anEmptyObject;
90   return std::shared_ptr<Model_ObjectUpdatedMessage>(
91     new Model_ObjectUpdatedMessage(anEmptyObject, eventID()));
92 }
93
94 void Model_ObjectUpdatedMessage::Join(const std::shared_ptr<Events_MessageGroup>& theJoined)
95 {
96   std::shared_ptr<Model_ObjectUpdatedMessage> aJoined =
97     std::dynamic_pointer_cast<Model_ObjectUpdatedMessage>(theJoined);
98   std::set<ObjectPtr>::iterator aFIter = aJoined->myObjects.begin();
99   for (; aFIter != aJoined->myObjects.end(); aFIter++) {
100     myObjects.insert(*aFIter);
101   }
102 }
103
104 /////////////////////// DELETED MESSAGE /////////////////////////////
105 Model_ObjectDeletedMessage::Model_ObjectDeletedMessage(
106     const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theGroup)
107     : ModelAPI_ObjectDeletedMessage(messageId(), 0)
108 {
109   if (!theGroup.empty()) {
110     myGroups.push_back(
111       std::pair<std::shared_ptr<ModelAPI_Document>, std::string>(theDoc, theGroup));
112   }
113 }
114
115 std::shared_ptr<Events_MessageGroup> Model_ObjectDeletedMessage::newEmpty()
116 {
117   static const std::shared_ptr<ModelAPI_Document> anEmpty;
118   return std::shared_ptr<Model_ObjectDeletedMessage>(new Model_ObjectDeletedMessage(anEmpty, ""));
119 }
120
121 const Events_ID Model_ObjectDeletedMessage::messageId()
122 {
123   static Events_ID MY_ID = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
124   return MY_ID;
125 }
126
127 void Model_ObjectDeletedMessage::Join(const std::shared_ptr<Events_MessageGroup>& theJoined)
128 {
129   std::shared_ptr<Model_ObjectDeletedMessage> aJoined =
130     std::dynamic_pointer_cast<Model_ObjectDeletedMessage>(theJoined);
131
132   const std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>& aJGroups =
133     aJoined->groups();
134
135   std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::iterator aGIter;
136   std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator aJIter;
137   for (aJIter = aJGroups.cbegin(); aJIter != aJGroups.cend(); aJIter++) {
138     for (aGIter = myGroups.begin(); aGIter != myGroups.end(); aGIter++) {
139       if (aGIter->first == aJIter->first && aGIter->second == aJIter->second)
140         break; // exists, so no need to insert
141     }
142     if (aGIter == myGroups.end())
143       myGroups.push_back(*aJIter);
144   }
145 }
146
147 /////////////////////// REORDERED MESSAGE /////////////////////////////
148 Model_OrderUpdatedMessage::Model_OrderUpdatedMessage(
149     FeaturePtr theReordered, const void* theSender)
150     : ModelAPI_OrderUpdatedMessage(messageId(), theSender),
151     myReordered(theReordered)
152 {
153 }
154
155 const Events_ID Model_OrderUpdatedMessage::messageId()
156 {
157   static Events_ID MY_ID = Events_Loop::eventByName(EVENT_ORDER_UPDATED);
158   return MY_ID;
159 }