Salome HOME
Issue #2112 performance for partition
[modules/shaper.git] / src / ModelAPI / ModelAPI_Feature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Feature.cpp
4 // Created:     17 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "ModelAPI_Feature.h"
8 #include <ModelAPI_Events.h>
9 #include <ModelAPI_Result.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Session.h>
13 #include <Events_Loop.h>
14 #include <Config_Translator.h>
15
16 void ModelAPI_Feature::setError(const std::string& theError,
17                                 bool isSend,
18                                 bool isTranslate)
19 {
20   std::string anError = isTranslate ? Config_Translator::translate(getKind(), theError)
21                                     : theError;
22   data()->setError(anError, isSend);
23 }
24
25 const std::list<std::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
26 {
27   return myResults;
28 }
29
30 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult() const
31 {
32   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
33 }
34
35 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
36 {
37   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
38 }
39
40 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult)
41 {
42   static Events_ID EVENT_UPD = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
43   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
44
45   if (firstResult() == theResult) {
46     // nothing to change
47   } else if (!myResults.empty()) {  // all except first become disabled
48     std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
49     *aResIter = theResult;
50     aECreator->sendUpdated(theResult, EVENT_UPD);
51     for(aResIter++; aResIter != myResults.end(); aResIter++) {
52       (*aResIter)->setDisabled((*aResIter), true);
53     }
54   } else {
55     myResults.push_back(theResult);
56   }
57   // in any case result becomes enabled
58   if (!isDisabled()) // disabled feature may be executed when it is added as not enabled (#2078)
59     theResult->setDisabled(theResult, false);
60 }
61
62 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
63                                  const int theIndex)
64 {
65   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
66   for (int anIndex = 0; anIndex < theIndex; anIndex++) {
67     aResIter++;
68   }
69   if (aResIter == myResults.end()) {  // append
70     myResults.push_back(theResult);
71   } else {  // update
72     *aResIter = theResult;
73   }
74   theResult->setDisabled(theResult, false);
75 }
76
77 void ModelAPI_Feature::removeResult(const std::shared_ptr<ModelAPI_Result>& theResult)
78 {
79   theResult->setDisabled(theResult, true);
80   // flush visualisation changes
81   static Events_Loop* aLoop = Events_Loop::loop();
82   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
83   aLoop->flush(aRedispEvent);
84 }
85
86 void ModelAPI_Feature::eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult)
87 {
88   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
89   for(; aResIter != myResults.end(); aResIter++) {
90     ResultPtr aRes = *aResIter;
91     if (aRes == theResult) {
92       std::string aGroup = aRes->groupName();
93       aRes->setDisabled(aRes, true); // for complex results to disable all subs
94       aRes->data()->erase();
95       myResults.erase(aResIter);
96
97       static Events_Loop* aLoop = Events_Loop::loop();
98       static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
99       static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
100       aECreator->sendDeleted(document(), aGroup);
101       aECreator->sendUpdated(aRes, EVENT_DISP);
102       break;
103     }
104   }
105 }
106
107 void ModelAPI_Feature::removeResults(
108   const int theSinceIndex, const bool theForever, const bool theFlush)
109 {
110   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
111   for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
112     aResIter++;
113
114   std::string aGroup;
115   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aNextIter = aResIter;
116   while( aNextIter != myResults.end()) {
117     aGroup = (*aNextIter)->groupName();
118     // remove previously erased results: to enable later if needed only actual (of history change)
119     (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
120     if (theForever) {
121       aNextIter = myResults.erase(aNextIter);
122     } else {
123       aNextIter++;
124     }
125   }
126   if (!aGroup.empty() && theFlush) {
127     // flush visualisation changes
128     static Events_Loop* aLoop = Events_Loop::loop();
129     static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
130     aLoop->flush(aRedispEvent);
131     static Events_ID aDelEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
132     aLoop->flush(aDelEvent);
133   }
134 }
135
136 void ModelAPI_Feature::eraseResults(const bool theForever)
137 {
138   removeResults(0, theForever, true);
139 }
140
141 const std::string& ModelAPI_Feature::documentToAdd()
142 {
143   // empty to use the current document
144   static const std::string anEmpty;
145   return anEmpty;
146 }
147
148 void ModelAPI_Feature::erase()
149 {
150   // if this is the current feature, make the upper feature as current before removing
151   if (document().get() && document()->currentFeature(false).get() == this) {
152     document()->setCurrentFeatureUp();
153   }
154
155   static Events_Loop* aLoop = Events_Loop::loop();
156   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
157   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
158
159   while (!myResults.empty()) {  // remove one by one with messages
160     std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
161     aRes->setDisabled(aRes, true); // to avoid activation of the Part result
162     if (!myResults.empty()) // disabling result may erase the list (on undo of Part, issue 665)
163       myResults.erase(myResults.begin());
164   }
165   ModelAPI_Object::erase();
166 }
167
168 ModelAPI_Feature::~ModelAPI_Feature()
169 {
170   if (data() && data()->isValid())
171     erase();
172 }
173
174 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
175 {
176   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
177   if (!aFeature) {
178     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
179     if (aResult) {
180       DocumentPtr aDoc = aResult->document();
181       return aDoc->feature(aResult);
182     }
183   }
184   return aFeature;
185 }
186
187 bool ModelAPI_Feature::isMacro() const
188 {
189   return false;
190 }
191
192 bool ModelAPI_Feature::setDisabled(const bool theFlag)
193 {
194   if (myIsDisabled != theFlag) {
195     myIsDisabled = theFlag;
196     if (myIsDisabled) {
197       removeResults(0, false, false); // flush will be in setCurrentFeature
198     } else {
199       // enable all disabled previously results
200       std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
201       for(; aResIter != myResults.end(); aResIter++) {
202         (*aResIter)->setDisabled(*aResIter, false);
203       }
204       // update selection for the case something was updated higher in the history
205       // while this feature was disabled
206       static Events_Loop* aLoop = Events_Loop::loop();
207       static Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
208       static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
209       aECreator->sendUpdated(data()->owner(), kUpdatedSel, false);
210     }
211     return true;
212   }
213   return false;
214 }
215
216 bool ModelAPI_Feature::isDisabled()
217 {
218   return myIsDisabled;
219 }
220
221 bool ModelAPI_Feature::setStable(const bool theFlag)
222 {
223   if (myIsStable != theFlag) {
224     myIsStable = theFlag;
225     // send an event about the stability change (editing is started/finished)
226     static Events_Loop* aLoop = Events_Loop::loop();
227     static Events_ID EVENT_STAB = aLoop->eventByName(EVENT_STABILITY_CHANGED);
228     std::shared_ptr<Events_Message> aMessage(new Events_Message(EVENT_STAB, this));
229     aLoop->send(aMessage, false);
230     return true;
231   }
232   return false;
233 }
234
235 bool ModelAPI_Feature::isStable()
236 {
237   return myIsStable;
238 }
239
240 bool ModelAPI_Feature::customAction(const std::string& theActionId)
241 {
242   return false;
243 }
244
245 bool ModelAPI_Feature::isPreviewNeeded() const
246 {
247   return true;
248 }
249
250 void ModelAPI_Feature::init()
251 {
252   myIsDisabled = false;
253   myIsStable = true;
254 }