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