]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Feature.cpp
Salome HOME
CEA : Lot 2 - Auto Color for Groups
[modules/shaper.git] / src / ModelAPI / ModelAPI_Feature.cpp
1 // Copyright (C) 2014-2021  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 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   Config_Prop* aProp = Config_PropManager::findProp("Visualization", "result_group_auto_color");
78
79   if (aProp) {
80     bool anIsAutoColor = Config_PropManager::boolean("Visualization", "result_group_auto_color");
81
82     if (anIsAutoColor && theResult->groupName() == ModelAPI_ResultGroup::group()) {
83       std::vector<int> aColor;
84       ModelAPI_Tools::findRandomColor(aColor);
85       ModelAPI_Tools::setColor(theResult,  aColor);
86     }
87   }
88 }
89
90 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
91                                  const int theIndex)
92 {
93   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
94   for (int anIndex = 0; anIndex < theIndex; anIndex++) {
95     aResIter++;
96   }
97   if (aResIter == myResults.end()) {  // append
98     myResults.push_back(theResult);
99   } else {  // update
100     *aResIter = theResult;
101   }
102   theResult->setDisabled(theResult, false);
103 }
104
105 void ModelAPI_Feature::eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult)
106 {
107   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
108   for(; aResIter != myResults.end(); aResIter++) {
109     ResultPtr aRes = *aResIter;
110     if (aRes == theResult) {
111       std::string aGroup = aRes->groupName();
112       aRes->setDisabled(aRes, true); // for complex results to disable all subs
113       aRes->data()->erase();
114       myResults.erase(aResIter);
115
116       static Events_Loop* aLoop = Events_Loop::loop();
117       static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
118       static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
119       aECreator->sendDeleted(document(), aGroup);
120       aECreator->sendUpdated(aRes, EVENT_DISP);
121       break;
122     }
123   }
124 }
125
126 void ModelAPI_Feature::removeResults(
127   const int theSinceIndex, const bool theForever, const bool theFlush)
128 {
129   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
130   for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
131     aResIter++;
132
133   std::string aGroup;
134   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aNextIter = aResIter;
135   while( aNextIter != myResults.end()) {
136     aGroup = (*aNextIter)->groupName();
137     // remove previously erased results: to enable later if needed only actual (of history change)
138     (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
139     if (theForever) {
140       aNextIter = myResults.erase(aNextIter);
141     } else {
142       aNextIter++;
143     }
144   }
145   if (!aGroup.empty() && theFlush) {
146     // flush visualization changes
147     static Events_Loop* aLoop = Events_Loop::loop();
148     static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
149     aLoop->flush(aRedispEvent);
150     static Events_ID aDelEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
151     aLoop->flush(aDelEvent);
152   }
153 }
154
155 void ModelAPI_Feature::eraseResults(const bool theForever)
156 {
157   removeResults(0, theForever, true);
158 }
159
160 const std::string& ModelAPI_Feature::documentToAdd()
161 {
162   // empty to use the current document
163   static const std::string anEmpty;
164   return anEmpty;
165 }
166
167 void ModelAPI_Feature::erase()
168 {
169   // if this is the current feature, make the upper feature as current before removing
170   if (document().get() && document()->currentFeature(false).get() == this) {
171     document()->setCurrentFeatureUp();
172   }
173
174   while (!myResults.empty()) {  // remove one by one with messages
175     std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
176     aRes->setDisabled(aRes, true); // to avoid activation of the Part result
177     if (!myResults.empty()) {// disabling result may erase the list (on undo of Part, issue 665)
178       myResults.erase(myResults.begin());
179       aRes->erase();
180     }
181   }
182   ModelAPI_Object::erase();
183 }
184
185 ModelAPI_Feature::~ModelAPI_Feature()
186 {
187   if (data() && data()->isValid())
188     erase();
189 }
190
191 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
192 {
193   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
194   if (!aFeature) {
195     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
196     if (aResult) {
197       DocumentPtr aDoc = aResult->document();
198       return aDoc->feature(aResult);
199     }
200   }
201   return aFeature;
202 }
203
204 bool ModelAPI_Feature::isMacro() const
205 {
206   return false;
207 }
208
209 bool ModelAPI_Feature::setDisabled(const bool theFlag)
210 {
211   if (myIsDisabled != theFlag) {
212     myIsDisabled = theFlag;
213     if (myIsDisabled) {
214       removeResults(0, false, false); // flush will be in setCurrentFeature
215     } else {
216       // enable all disabled previously results
217       std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
218       for(; aResIter != myResults.end(); aResIter++) {
219         (*aResIter)->setDisabled(*aResIter, false);
220       }
221       // update selection for the case something was updated higher in the history
222       // while this feature was disabled, but avoid flushing it immediately and
223       // wait while all the previous features update myIsDisabled flag
224       // (flush will be called by the document)
225       static Events_Loop* aLoop = Events_Loop::loop();
226       static Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
227       static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
228       aECreator->sendUpdated(data()->owner(), kUpdatedSel, true);
229     }
230     return true;
231   }
232   return false;
233 }
234
235 bool ModelAPI_Feature::isDisabled()
236 {
237   return myIsDisabled;
238 }
239
240 // LCOV_EXCL_START
241 bool ModelAPI_Feature::setStable(const bool theFlag)
242 {
243   if (myIsStable != theFlag) {
244     myIsStable = theFlag;
245     // send an event about the stability change (editing is started/finished)
246     static Events_Loop* aLoop = Events_Loop::loop();
247     static Events_ID EVENT_STAB = aLoop->eventByName(EVENT_STABILITY_CHANGED);
248     std::shared_ptr<Events_Message> aMessage(new Events_Message(EVENT_STAB, this));
249     aLoop->send(aMessage, false);
250     return true;
251   }
252   return false;
253 }
254 // LCOV_EXCL_STOP
255
256 bool ModelAPI_Feature::isStable()
257 {
258   return myIsStable;
259 }
260
261 bool ModelAPI_Feature::customAction(const std::string& /*theActionId*/)
262 {
263   return false;
264 }
265
266 bool ModelAPI_Feature::isPreviewNeeded() const
267 {
268   return true;
269 }
270
271 void ModelAPI_Feature::init()
272 {
273   myIsDisabled = false;
274   myIsStable = true;
275 }