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