]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Feature.cpp
Salome HOME
Fix on floating bug on abort of parameter creation
[modules/shaper.git] / src / ModelAPI / ModelAPI_Feature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Feature.cpp
4 // Created:     17 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "ModelAPI_Feature.h"
8 #include <ModelAPI_Events.h>
9 #include <ModelAPI_Result.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Session.h>
13 #include <Events_Loop.h>
14
15 const std::list<std::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
16 {
17   return myResults;
18 }
19
20 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult()
21 {
22   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
23 }
24
25 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
26 {
27   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
28 }
29
30 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult)
31 {
32   static Events_Loop* aLoop = Events_Loop::loop();
33   static Events_ID EVENT_UPD = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
34   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
35
36   if (firstResult() == theResult) {
37     // nothing to change
38   } else if (!myResults.empty()) {  // all except first become disabled
39     std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
40     *aResIter = theResult;
41     aECreator->sendUpdated(theResult, EVENT_UPD);
42     for(aResIter++; aResIter != myResults.end(); aResIter++) {
43       (*aResIter)->setDisabled((*aResIter), true);
44     }
45   } else {
46     myResults.push_back(theResult);
47   }
48   // in any case result becomes enabled
49   theResult->setDisabled(theResult, false);
50   // flush vidualisation changes
51   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
52   aLoop->flush(aRedispEvent);
53 }
54
55 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
56                                  const int theIndex)
57 {
58   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
59   for (int anIndex = 0; anIndex < theIndex; anIndex++) {
60     aResIter++;
61   }
62   if (aResIter == myResults.end()) {  // append
63     myResults.push_back(theResult);
64   } else {  // update
65     *aResIter = theResult;
66   }
67   theResult->setDisabled(theResult, false);
68   // flush visualisation changes
69   static Events_Loop* aLoop = Events_Loop::loop();
70   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
71   aLoop->flush(aRedispEvent);
72 }
73
74 void ModelAPI_Feature::removeResult(const std::shared_ptr<ModelAPI_Result>& theResult)
75 {
76   theResult->setDisabled(theResult, true);
77   // flush visualisation changes
78   static Events_Loop* aLoop = Events_Loop::loop();
79   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
80   aLoop->flush(aRedispEvent);
81 }
82
83 void ModelAPI_Feature::eraseResultFromList(const std::shared_ptr<ModelAPI_Result>& theResult)
84 {
85   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
86   for(; aResIter != myResults.end(); aResIter++) {
87     ResultPtr aRes = *aResIter;
88     if (aRes == theResult) {
89       std::string aGroup = aRes->groupName();
90       aRes->data()->erase();
91       myResults.erase(aResIter);
92
93       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
94       static Events_Loop* aLoop = Events_Loop::loop();
95       static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
96       static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
97       ModelAPI_EventCreator::get()->sendDeleted(document(), aGroup);
98       aECreator->sendUpdated(aRes, EVENT_DISP);
99       break;
100     }
101   }
102 }
103
104 void ModelAPI_Feature::removeResults(const int theSinceIndex, const bool theFlush)
105 {
106   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
107   for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
108     aResIter++;
109   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aNextIter = aResIter;
110   while( aNextIter != myResults.end()) {
111     // remove previously erased results: to enable later if needed only actual (of history change)
112     if (theSinceIndex == 0 && (*aNextIter)->isDisabled()) {
113       aNextIter = myResults.erase(aNextIter);
114     } else {
115       (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
116       aNextIter++;
117     }
118   }
119   if (theFlush) {
120     // flush visualisation changes
121     static Events_Loop* aLoop = Events_Loop::loop();
122     static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
123     aLoop->flush(aRedispEvent);
124   }
125 }
126
127 void ModelAPI_Feature::eraseResults()
128 {
129   removeResults(0);
130 }
131
132 const std::string& ModelAPI_Feature::documentToAdd()
133 {
134   // empty to use the current document
135   static const std::string anEmpty;
136   return anEmpty;
137 }
138
139 void ModelAPI_Feature::erase()
140 {
141   // if this is the current feature, make the upper feature as current before removing
142   if (document().get() && document()->currentFeature(false).get() == this) {
143     document()->setCurrentFeatureUp();
144   }
145
146   static Events_Loop* aLoop = Events_Loop::loop();
147   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
148   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
149
150   while (!myResults.empty()) {  // remove one by one with messages
151     std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
152     myResults.erase(myResults.begin());
153     aECreator->sendDeleted(aRes->document(), aRes->groupName());
154     aECreator->sendUpdated(aRes, EVENT_DISP);
155   }
156   ModelAPI_Object::erase();
157 }
158
159 ModelAPI_Feature::~ModelAPI_Feature()
160 {
161   erase();
162 }
163
164 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
165 {
166   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
167   if (!aFeature) {
168     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
169     if (aResult) {
170       DocumentPtr aDoc = aResult->document();
171       return aDoc->feature(aResult);
172     }
173   }
174   return aFeature;
175 }
176
177 bool ModelAPI_Feature::isMacro() const
178 {
179   return false;
180 }
181
182 bool ModelAPI_Feature::setDisabled(const bool theFlag)
183 {
184   if (myIsDisabled != theFlag) {
185     myIsDisabled = theFlag;
186     if (myIsDisabled) {
187       removeResults(0, false); // flush will be in setCurrentFeature
188     } else {
189       // enable all disabled previously results
190       std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
191       for(; aResIter != myResults.end(); aResIter++) {
192         (*aResIter)->setDisabled(*aResIter, false);
193       }
194     }
195     return true;
196   }
197   return false;
198 }
199
200 bool ModelAPI_Feature::isDisabled() const
201 {
202   return myIsDisabled;
203 }
204
205 bool ModelAPI_Feature::isPreviewNeeded() const
206 {
207   return true;
208 }