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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
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>
30 void ModelAPI_Feature::setError(const std::string& theError,
34 std::string anError = isTranslate ? Config_Translator::translate(getKind(), theError)
36 data()->setError(anError, isSend);
39 const std::list<std::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
44 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult() const
46 return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
49 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
51 return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
54 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult)
56 static Events_ID EVENT_UPD = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
57 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
59 if (firstResult() == theResult) {
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);
69 myResults.push_back(theResult);
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);
76 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
79 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
80 for (int anIndex = 0; anIndex < theIndex; anIndex++) {
83 if (aResIter == myResults.end()) { // append
84 myResults.push_back(theResult);
86 *aResIter = theResult;
88 theResult->setDisabled(theResult, false);
91 void ModelAPI_Feature::eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult)
93 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
94 for(; aResIter != myResults.end(); aResIter++) {
95 ResultPtr aRes = *aResIter;
96 if (aRes == theResult) {
97 std::string aGroup = aRes->groupName();
98 aRes->setDisabled(aRes, true); // for complex results to disable all subs
99 aRes->data()->erase();
100 myResults.erase(aResIter);
102 static Events_Loop* aLoop = Events_Loop::loop();
103 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
104 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
105 aECreator->sendDeleted(document(), aGroup);
106 aECreator->sendUpdated(aRes, EVENT_DISP);
112 void ModelAPI_Feature::removeResults(
113 const int theSinceIndex, const bool theForever, const bool theFlush)
115 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
116 for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
120 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aNextIter = aResIter;
121 while( aNextIter != myResults.end()) {
122 aGroup = (*aNextIter)->groupName();
123 // remove previously erased results: to enable later if needed only actual (of history change)
124 (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
126 aNextIter = myResults.erase(aNextIter);
131 if (!aGroup.empty() && theFlush) {
132 // flush visualization changes
133 static Events_Loop* aLoop = Events_Loop::loop();
134 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
135 aLoop->flush(aRedispEvent);
136 static Events_ID aDelEvent = aLoop->eventByName(EVENT_OBJECT_DELETED);
137 aLoop->flush(aDelEvent);
141 void ModelAPI_Feature::eraseResults(const bool theForever)
143 removeResults(0, theForever, true);
146 const std::string& ModelAPI_Feature::documentToAdd()
148 // empty to use the current document
149 static const std::string anEmpty;
153 void ModelAPI_Feature::erase()
155 // if this is the current feature, make the upper feature as current before removing
156 if (document().get() && document()->currentFeature(false).get() == this) {
157 document()->setCurrentFeatureUp();
160 static Events_Loop* aLoop = Events_Loop::loop();
161 static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
162 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
164 while (!myResults.empty()) { // remove one by one with messages
165 std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
166 aRes->setDisabled(aRes, true); // to avoid activation of the Part result
167 if (!myResults.empty()) {// disabling result may erase the list (on undo of Part, issue 665)
168 myResults.erase(myResults.begin());
172 ModelAPI_Object::erase();
175 ModelAPI_Feature::~ModelAPI_Feature()
177 if (data() && data()->isValid())
181 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
183 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
185 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
187 DocumentPtr aDoc = aResult->document();
188 return aDoc->feature(aResult);
194 bool ModelAPI_Feature::isMacro() const
199 bool ModelAPI_Feature::setDisabled(const bool theFlag)
201 if (myIsDisabled != theFlag) {
202 myIsDisabled = theFlag;
204 removeResults(0, false, false); // flush will be in setCurrentFeature
206 // enable all disabled previously results
207 std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
208 for(; aResIter != myResults.end(); aResIter++) {
209 (*aResIter)->setDisabled(*aResIter, false);
211 // update selection for the case something was updated higher in the history
212 // while this feature was disabled
213 static Events_Loop* aLoop = Events_Loop::loop();
214 static Events_ID kUpdatedSel = aLoop->eventByName(EVENT_UPDATE_SELECTION);
215 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
216 aECreator->sendUpdated(data()->owner(), kUpdatedSel, false);
223 bool ModelAPI_Feature::isDisabled()
229 bool ModelAPI_Feature::setStable(const bool theFlag)
231 if (myIsStable != theFlag) {
232 myIsStable = theFlag;
233 // send an event about the stability change (editing is started/finished)
234 static Events_Loop* aLoop = Events_Loop::loop();
235 static Events_ID EVENT_STAB = aLoop->eventByName(EVENT_STABILITY_CHANGED);
236 std::shared_ptr<Events_Message> aMessage(new Events_Message(EVENT_STAB, this));
237 aLoop->send(aMessage, false);
244 bool ModelAPI_Feature::isStable()
249 bool ModelAPI_Feature::customAction(const std::string& theActionId)
254 bool ModelAPI_Feature::isPreviewNeeded() const
259 void ModelAPI_Feature::init()
261 myIsDisabled = false;