Salome HOME
Fix the problem with change of Base of Recover feature in GUI when some result is...
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Recover.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Recover.cpp
4 // Created:     29 Jul 2016
5 // Author:      Natalia ERMOLAEVA
6
7 #include "FeaturesPlugin_Recover.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_AttributeBoolean.h>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
16 #include <ModelAPI_Result.h>
17 #include <ModelAPI_Tools.h>
18
19 using namespace std;
20
21 FeaturesPlugin_Recover::FeaturesPlugin_Recover()
22 {
23 }
24
25 void FeaturesPlugin_Recover::initAttributes()
26 {
27   data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
28   data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
29   data()->addAttribute(PERSISTENT(), ModelAPI_AttributeBoolean::typeId());
30
31   myPersistent = boolean(PERSISTENT())->value();
32   // temporary modification for empty list
33   // ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), RECOVERED_ENTITIES());
34
35   synchronizeRegistered();
36 }
37
38 void FeaturesPlugin_Recover::execute()
39 {
40   synchronizeRegistered();
41 }
42
43 void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
44 {
45   synchronizeRegistered();
46 }
47
48 void FeaturesPlugin_Recover::synchronizeRegistered()
49 {
50   FeaturePtr aBase = baseFeature();
51   bool aNewPersistent = boolean(PERSISTENT())->value();
52   if (aNewPersistent != myPersistent || myCurrentBase != aBase)
53     clearRegistered();
54
55   std::set<ObjectPtr> aRecoveredInList;
56   // add unconcealed which are not in the myRegistered map
57   if (isStable() && !isDisabled()) { // if unstable, clear any unconcealment effect
58     AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
59     for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
60       ObjectPtr anObj = aRecovered->object(anIndex);
61       aRecoveredInList.insert(anObj);
62       if (myRegistered.find(anObj) == myRegistered.end()) {
63         // not found, so register a new
64         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
65         ModelAPI_Session::get()->validators()->registerUnconcealment(
66           aRes, aNewPersistent ? FeaturePtr() : aBase);
67         myRegistered.insert(anObj);
68       }
69     }
70   }
71   // remove unconcealed which are not in the stored list, but in the map
72   std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
73   while(aMyReg != myRegistered.end()) {
74     if (aRecoveredInList.find(*aMyReg) == aRecoveredInList.end()) {
75       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
76       ModelAPI_Session::get()->validators()->disableUnconcealment(
77         aRes, aNewPersistent ? FeaturePtr() : myCurrentBase);
78       myRegistered.erase(aMyReg);
79       aMyReg = myRegistered.begin(); // restart iteration because after erase iterator may be bad
80     } else {
81       aMyReg++;
82     }
83   }
84   myCurrentBase = aBase;
85   myPersistent = aNewPersistent;
86 }
87
88 void FeaturesPlugin_Recover::erase()
89 {
90   // clears myRegistered before all information is destroyed
91   clearRegistered();
92   ModelAPI_Feature::erase();
93 }
94
95 bool FeaturesPlugin_Recover::setStable(const bool theFlag)
96 {
97   bool aRes = ModelAPI_Feature::setStable(theFlag);
98   synchronizeRegistered();
99   return aRes;
100 }
101
102 bool FeaturesPlugin_Recover::setDisabled(const bool theFlag)
103 {
104   bool aRes = ModelAPI_Feature::setDisabled(theFlag);
105   synchronizeRegistered();
106   return aRes;
107 }
108
109 FeaturePtr FeaturesPlugin_Recover::baseFeature()
110 {
111   // for the current moment it can be result of feature of feature: GUI is not debugged
112   ObjectPtr aBaseObj = reference(BASE_FEATURE())->value();
113   FeaturePtr aResult;
114   if (aBaseObj.get() == NULL)
115     return aResult;
116   aResult = std::dynamic_pointer_cast<ModelAPI_Feature>(aBaseObj);
117   if (aResult.get() == NULL)
118     aResult = aBaseObj->document()->feature(std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObj));
119   return aResult;
120 }
121
122 void FeaturesPlugin_Recover::clearRegistered()
123 {
124   std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
125   for(; aMyReg != myRegistered.end(); aMyReg++) {
126     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
127     ModelAPI_Session::get()->validators()->disableUnconcealment(
128       aRes, myPersistent ? FeaturePtr() : myCurrentBase);
129   }
130   myRegistered.clear();
131 }