Salome HOME
Put groups to the separated plugin: Collection
[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 FeaturesPlugin_Recover::FeaturesPlugin_Recover()
20 {
21 }
22
23 void FeaturesPlugin_Recover::initAttributes()
24 {
25   data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
26   data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
27   data()->addAttribute(PERSISTENT(), ModelAPI_AttributeBoolean::typeId());
28
29   myPersistent = boolean(PERSISTENT())->value();
30   synchronizeRegistered();
31 }
32
33 void FeaturesPlugin_Recover::execute()
34 {
35   synchronizeRegistered();
36 }
37
38 void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
39 {
40   synchronizeRegistered();
41 }
42
43 void FeaturesPlugin_Recover::synchronizeRegistered()
44 {
45   FeaturePtr aBase = baseFeature();
46   bool aNewPersistent = boolean(PERSISTENT())->value();
47   if (aNewPersistent != myPersistent || myCurrentBase != aBase)
48     clearRegistered();
49
50   std::set<ObjectPtr> aRecoveredInList;
51   // add unconcealed which are not in the myRegistered map
52   if (isStable() && !isDisabled()) { // if unstable, clear any unconcealment effect
53     AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
54     for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
55       ObjectPtr anObj = aRecovered->object(anIndex);
56       aRecoveredInList.insert(anObj);
57       if (myRegistered.find(anObj) == myRegistered.end()) {
58         // not found, so register a new
59         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
60         if (aRes.get()) { // this may be on first update after "open"
61           ModelAPI_Session::get()->validators()->registerUnconcealment(
62             aRes, aNewPersistent ? FeaturePtr() : aBase);
63           myRegistered.insert(anObj);
64         }
65       }
66     }
67   }
68   // remove unconcealed which are not in the stored list, but in the map
69   std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
70   while(aMyReg != myRegistered.end()) {
71     if (aRecoveredInList.find(*aMyReg) == aRecoveredInList.end()) {
72       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
73       ModelAPI_Session::get()->validators()->disableUnconcealment(
74         aRes, aNewPersistent ? FeaturePtr() : myCurrentBase);
75       myRegistered.erase(aMyReg);
76       aMyReg = myRegistered.begin(); // restart iteration because after erase iterator may be bad
77     } else {
78       aMyReg++;
79     }
80   }
81   myCurrentBase = aBase;
82   myPersistent = aNewPersistent;
83 }
84
85 void FeaturesPlugin_Recover::erase()
86 {
87   // clears myRegistered before all information is destroyed
88   clearRegistered();
89   ModelAPI_Feature::erase();
90 }
91
92 bool FeaturesPlugin_Recover::setStable(const bool theFlag)
93 {
94   bool aRes = ModelAPI_Feature::setStable(theFlag);
95   synchronizeRegistered();
96   return aRes;
97 }
98
99 bool FeaturesPlugin_Recover::setDisabled(const bool theFlag)
100 {
101   bool aRes = ModelAPI_Feature::setDisabled(theFlag);
102   synchronizeRegistered();
103   return aRes;
104 }
105
106 FeaturePtr FeaturesPlugin_Recover::baseFeature()
107 {
108   // for the current moment it can be result of feature of feature: GUI is not debugged
109   ObjectPtr aBaseObj = reference(BASE_FEATURE())->value();
110   FeaturePtr aResult;
111   if (aBaseObj.get() == NULL)
112     return aResult;
113   aResult = std::dynamic_pointer_cast<ModelAPI_Feature>(aBaseObj);
114   if (aResult.get() == NULL)
115     aResult = aBaseObj->document()->feature(std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObj));
116   return aResult;
117 }
118
119 void FeaturesPlugin_Recover::clearRegistered()
120 {
121   std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
122   for(; aMyReg != myRegistered.end(); aMyReg++) {
123     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
124     ModelAPI_Session::get()->validators()->disableUnconcealment(
125       aRes, myPersistent ? FeaturePtr() : myCurrentBase);
126   }
127   myRegistered.clear();
128 }