Salome HOME
236ee3dffae018b4abd0b3d589a3f5ede79e74b2
[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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModelAPI_Feature.h"
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Result.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Session.h>
27 #include <Events_Loop.h>
28 #include <Config_Translator.h>
29
30 void ModelAPI_Feature::setError(const std::string& theError,
31                                 bool isSend,
32                                 bool isTranslate)
33 {
34   std::string anError = isTranslate ? Config_Translator::translate(getKind(), theError)
35                                     : theError;
36   data()->setError(anError, isSend);
37 }
38
39 const std::list<std::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
40 {
41   return myResults;
42 }
43
44 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult() const
45 {
46   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
47 }
48
49 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
50 {
51   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
52 }
53
54 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult)
55 {
56   static Events_ID EVENT_UPD = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
57   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
58
59   if (firstResult() == theResult) {
60     // nothing to change
61   } else if (!myResults.empty()) {  // all except first become disabled
62     std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
63     *aResIter = theResult;
64     aECreator->sendUpdated(theResult, EVENT_UPD);
65     for(aResIter++; aResIter != myResults.end(); aResIter++) {
66       (*aResIter)->setDisabled((*aResIter), true);
67     }
68   } else {
69     myResults.push_back(theResult);
70   }
71   // in any case result becomes enabled
72   if (!isDisabled()) // disabled feature may be executed when it is added as not enabled (#2078)
73     theResult->setDisabled(theResult, false);
74 }
75
76 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
77                                  const int theIndex)
78 {
79   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
80   for (int anIndex = 0; anIndex < theIndex; anIndex++) {
81     aResIter++;
82   }
83   if (aResIter == myResults.end()) {  // append
84     myResults.push_back(theResult);
85   } else {  // update
86     *aResIter = theResult;
87   }
88   theResult->setDisabled(theResult, false);
89 }
90
91 void ModelAPI_Feature::removeResult(const std::shared_ptr<ModelAPI_Result>& theResult)
92 {
93   theResult->setDisabled(theResult, true);
94   // flush visualisation changes
95   static Events_Loop* aLoop = Events_Loop::loop();
96   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
97   aLoop->flush(aRedispEvent);
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 visualisation 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   static Events_Loop* aLoop = Events_Loop::loop();
170   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
171   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
172
173   while (!myResults.empty()) {  // remove one by one with messages
174     std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
175     aRes->setDisabled(aRes, true); // to avoid activation of the Part result
176     if (!myResults.empty()) // disabling result may erase the list (on undo of Part, issue 665)
177       myResults.erase(myResults.begin());
178   }
179   ModelAPI_Object::erase();
180 }
181
182 ModelAPI_Feature::~ModelAPI_Feature()
183 {
184   if (data() && data()->isValid())
185     erase();
186 }
187
188 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
189 {
190   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
191   if (!aFeature) {
192     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
193     if (aResult) {
194       DocumentPtr aDoc = aResult->document();
195       return aDoc->feature(aResult);
196     }
197   }
198   return aFeature;
199 }
200
201 bool ModelAPI_Feature::isMacro() const
202 {
203   return false;
204 }
205
206 bool ModelAPI_Feature::setDisabled(const bool theFlag)
207 {
208   if (myIsDisabled != theFlag) {
209     myIsDisabled = theFlag;
210     if (myIsDisabled) {
211       removeResults(0, false, false); // flush will be in setCurrentFeature
212     } else {
213       // enable all disabled previously results
214       std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
215       for(; aResIter != myResults.end(); aResIter++) {
216         (*aResIter)->setDisabled(*aResIter, false);
217       }
218       // update selection for the case something was updated higher in the history
219       // while this feature was disabled
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, false);
224     }
225     return true;
226   }
227   return false;
228 }
229
230 bool ModelAPI_Feature::isDisabled()
231 {
232   return myIsDisabled;
233 }
234
235 bool ModelAPI_Feature::setStable(const bool theFlag)
236 {
237   if (myIsStable != theFlag) {
238     myIsStable = theFlag;
239     // send an event about the stability change (editing is started/finished)
240     static Events_Loop* aLoop = Events_Loop::loop();
241     static Events_ID EVENT_STAB = aLoop->eventByName(EVENT_STABILITY_CHANGED);
242     std::shared_ptr<Events_Message> aMessage(new Events_Message(EVENT_STAB, this));
243     aLoop->send(aMessage, false);
244     return true;
245   }
246   return false;
247 }
248
249 bool ModelAPI_Feature::isStable()
250 {
251   return myIsStable;
252 }
253
254 bool ModelAPI_Feature::customAction(const std::string& theActionId)
255 {
256   return false;
257 }
258
259 bool ModelAPI_Feature::isPreviewNeeded() const
260 {
261   return true;
262 }
263
264 void ModelAPI_Feature::init()
265 {
266   myIsDisabled = false;
267   myIsStable = true;
268 }