Salome HOME
Geometrical naming for edges and intersections debug.
[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::sendDeleted(const std::shared_ptr<ModelAPI_Document>& theDoc,
37                                      const std::string& theGroup) const
38 {
39   std::shared_ptr<Model_ObjectDeletedMessage> aMsg(
40     new Model_ObjectDeletedMessage(theDoc, theGroup));
41   Events_Loop::loop()->send(aMsg, true);
42 }
43
44 void Model_EventCreator::sendReordered(const std::shared_ptr<ModelAPI_Feature>& theReordered) const
45 {
46   std::shared_ptr<Model_OrderUpdatedMessage> aMsg(
47     new Model_OrderUpdatedMessage(theReordered));
48   Events_Loop::loop()->send(aMsg, true);
49 }
50
51 Model_EventCreator::Model_EventCreator()
52 {
53   ModelAPI_EventCreator::set(this);
54 }
55
56 /////////////////////// UPDATED MESSAGE /////////////////////////////
57 Model_ObjectUpdatedMessage::Model_ObjectUpdatedMessage(const ObjectPtr& theObject,
58                                                        const Events_ID& theEvent)
59     : ModelAPI_ObjectUpdatedMessage(theEvent, 0)
60 {
61   if (theObject) {
62     myObjects.insert(theObject);
63   }
64 }
65
66 const std::set<ObjectPtr>& Model_ObjectUpdatedMessage::objects() const
67 {
68   return myObjects;
69 }
70
71 std::shared_ptr<Events_MessageGroup> Model_ObjectUpdatedMessage::newEmpty()
72 {
73   ObjectPtr anEmptyObject;
74   return std::shared_ptr<Model_ObjectUpdatedMessage>(
75     new Model_ObjectUpdatedMessage(anEmptyObject, eventID()));
76 }
77
78 void Model_ObjectUpdatedMessage::Join(const std::shared_ptr<Events_MessageGroup>& theJoined)
79 {
80   std::shared_ptr<Model_ObjectUpdatedMessage> aJoined =
81     std::dynamic_pointer_cast<Model_ObjectUpdatedMessage>(theJoined);
82   std::set<ObjectPtr>::iterator aFIter = aJoined->myObjects.begin();
83   for (; aFIter != aJoined->myObjects.end(); aFIter++) {
84     myObjects.insert(*aFIter);
85   }
86 }
87
88 /////////////////////// DELETED MESSAGE /////////////////////////////
89 Model_ObjectDeletedMessage::Model_ObjectDeletedMessage(
90     const std::shared_ptr<ModelAPI_Document>& theDoc, const std::string& theGroup)
91     : ModelAPI_ObjectDeletedMessage(messageId(), 0)
92 {
93   if (!theGroup.empty()) {
94     myGroups.push_back(
95       std::pair<std::shared_ptr<ModelAPI_Document>, std::string>(theDoc, theGroup));
96   }
97 }
98
99 std::shared_ptr<Events_MessageGroup> Model_ObjectDeletedMessage::newEmpty()
100 {
101   static const std::shared_ptr<ModelAPI_Document> anEmpty;
102   return std::shared_ptr<Model_ObjectDeletedMessage>(new Model_ObjectDeletedMessage(anEmpty, ""));
103 }
104
105 const Events_ID Model_ObjectDeletedMessage::messageId()
106 {
107   static Events_ID MY_ID = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
108   return MY_ID;
109 }
110
111 void Model_ObjectDeletedMessage::Join(const std::shared_ptr<Events_MessageGroup>& theJoined)
112 {
113   std::shared_ptr<Model_ObjectDeletedMessage> aJoined =
114     std::dynamic_pointer_cast<Model_ObjectDeletedMessage>(theJoined);
115
116   const std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>& aJGroups =
117     aJoined->groups();
118
119   std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::iterator aGIter;
120   std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator aJIter;
121   for (aJIter = aJGroups.cbegin(); aJIter != aJGroups.cend(); aJIter++) {
122     for (aGIter = myGroups.begin(); aGIter != myGroups.end(); aGIter++) {
123       if (aGIter->first == aJIter->first && aGIter->second == aJIter->second)
124         break; // exists, so no need to insert
125     }
126     if (aGIter == myGroups.end())
127       myGroups.push_back(*aJIter);
128   }
129 }
130
131 /////////////////////// REORDERED MESSAGE /////////////////////////////
132 Model_OrderUpdatedMessage::Model_OrderUpdatedMessage(
133     FeaturePtr theReordered, const void* theSender)
134     : ModelAPI_OrderUpdatedMessage(messageId(), theSender),
135     myReordered(theReordered)
136 {
137 }
138
139 const Events_ID Model_OrderUpdatedMessage::messageId()
140 {
141   static Events_ID MY_ID = Events_Loop::eventByName(EVENT_ORDER_UPDATED);
142   return MY_ID;
143 }