1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
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>
29 void ModelAPI_Feature::setError(const std::string& theError,
33 std::string anError = isTranslate ? Config_Translator::translate(getKind(), theError)
35 data()->setError(anError, isSend);
38 const std::list<std::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
43 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult() const
45 return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
48 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
50 return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
53 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult)
55 static Events_ID EVENT_UPD = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
56 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
58 if (firstResult() == theResult) {
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);
68 myResults.push_back(theResult);
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);
75 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
78 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
79 for (int anIndex = 0; anIndex < theIndex; anIndex++) {
82 if (aResIter == myResults.end()) { // append
83 myResults.push_back(theResult);
85 *aResIter = theResult;
87 theResult->setDisabled(theResult, false);
90 void ModelAPI_Feature::removeResult(const std::shared_ptr<ModelAPI_Result>& theResult)
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);
99 void ModelAPI_Feature::eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult)
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);
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);
120 void ModelAPI_Feature::removeResults(
121 const int theSinceIndex, const bool theForever, const bool theFlush)
123 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
124 for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
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
134 aNextIter = myResults.erase(aNextIter);
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);
149 void ModelAPI_Feature::eraseResults(const bool theForever)
151 removeResults(0, theForever, true);
154 const std::string& ModelAPI_Feature::documentToAdd()
156 // empty to use the current document
157 static const std::string anEmpty;
161 void ModelAPI_Feature::erase()
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();
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();
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());
178 ModelAPI_Object::erase();
181 ModelAPI_Feature::~ModelAPI_Feature()
183 if (data() && data()->isValid())
187 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
189 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
191 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
193 DocumentPtr aDoc = aResult->document();
194 return aDoc->feature(aResult);
200 bool ModelAPI_Feature::isMacro() const
205 bool ModelAPI_Feature::setDisabled(const bool theFlag)
207 if (myIsDisabled != theFlag) {
208 myIsDisabled = theFlag;
210 removeResults(0, false, false); // flush will be in setCurrentFeature
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);
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);
229 bool ModelAPI_Feature::isDisabled()
234 bool ModelAPI_Feature::setStable(const bool theFlag)
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);
248 bool ModelAPI_Feature::isStable()
253 bool ModelAPI_Feature::customAction(const std::string& theActionId)
258 bool ModelAPI_Feature::isPreviewNeeded() const
263 void ModelAPI_Feature::init()
265 myIsDisabled = false;