Salome HOME
#2084 Unknown error when trim: Do not copyAttribute
[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   erase();
171 }
172
173 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
174 {
175   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
176   if (!aFeature) {
177     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
178     if (aResult) {
179       DocumentPtr aDoc = aResult->document();
180       return aDoc->feature(aResult);
181     }
182   }
183   return aFeature;
184 }
185
186 bool ModelAPI_Feature::isMacro() const
187 {
188   return false;
189 }
190
191 bool ModelAPI_Feature::setDisabled(const bool theFlag)
192 {
193   if (myIsDisabled != theFlag) {
194     myIsDisabled = theFlag;
195     if (myIsDisabled) {
196       removeResults(0, false, false); // flush will be in setCurrentFeature
197     } else {
198       // enable all disabled previously results
199       std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
200       for(; aResIter != myResults.end(); aResIter++) {
201         (*aResIter)->setDisabled(*aResIter, false);
202       }
203       // update selection for the case something was updated higher in the history
204       // while this feature was disabled
205       static Events_Loop* aLoop = Events_Loop::loop();
206       static Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
207       static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
208       aECreator->sendUpdated(data()->owner(), kUpdatedSel, false);
209     }
210     return true;
211   }
212   return false;
213 }
214
215 bool ModelAPI_Feature::isDisabled()
216 {
217   return myIsDisabled;
218 }
219
220 bool ModelAPI_Feature::setStable(const bool theFlag)
221 {
222   if (myIsStable != theFlag) {
223     myIsStable = theFlag;
224     // send an event about the stability change (editing is started/finished)
225     static Events_Loop* aLoop = Events_Loop::loop();
226     static Events_ID EVENT_STAB = aLoop->eventByName(EVENT_STABILITY_CHANGED);
227     std::shared_ptr<Events_Message> aMessage(new Events_Message(EVENT_STAB, this));
228     aLoop->send(aMessage, false);
229     return true;
230   }
231   return false;
232 }
233
234 bool ModelAPI_Feature::isStable()
235 {
236   return myIsStable;
237 }
238
239 bool ModelAPI_Feature::customAction(const std::string& theActionId)
240 {
241   return false;
242 }
243
244 bool ModelAPI_Feature::isPreviewNeeded() const
245 {
246   return true;
247 }
248
249 void ModelAPI_Feature::init()
250 {
251   myIsDisabled = false;
252   myIsStable = true;
253 }