Salome HOME
Boolean correction: clean the validation cash by other widget value change if it...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidated.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <ModuleBase_WidgetValidated.h>
4 #include <ModuleBase_FilterFactory.h>
5 #include <ModuleBase_IViewer.h>
6 #include <ModuleBase_ISelection.h>
7
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Validator.h>
10 #include <ModelAPI_AttributeValidator.h>
11 #include <ModelAPI_Events.h>
12
13 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
14 #include <SelectMgr_EntityOwner.hxx>
15 #include <StdSelect_BRepOwner.hxx>
16
17 #include <Events_Loop.h>
18
19 #include <QWidget>
20
21 //#define DEBUG_VALID_STATE
22
23 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
24                                                        ModuleBase_IWorkshop* theWorkshop,
25                                                        const Config_WidgetAPI* theData,
26                                                        const std::string& theParentId)
27 : ModuleBase_ModelWidget(theParent, theData, theParentId),
28   myWorkshop(theWorkshop), myIsInValidate(false)
29 {
30 }
31
32 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
33 {
34 }
35
36 //********************************************************************
37 bool ModuleBase_WidgetValidated::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
38                                               const bool theToValidate)
39 {
40   if (theValues.empty())
41     return false;
42   // it removes the processed value from the parameters list
43   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
44   bool isDone = false;
45
46   if (!theToValidate || isValidInFilters(aValue)) {
47     isDone = setSelectionCustom(aValue);
48     // updateObject - to update/redisplay feature
49     // it is commented in order to perfom it outside the method
50     //updateObject(myFeature);
51     // to storeValue()
52     //emit valuesChanged();
53   }
54   return isDone;
55 }
56
57 //********************************************************************
58 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
59 {
60   return myPresentedObject;
61 }
62
63 //********************************************************************
64 void ModuleBase_WidgetValidated::clearValidatedCash()
65 {
66   myValidPrs.clear();
67   myInvalidPrs.clear();
68 }
69
70 //********************************************************************
71 void ModuleBase_WidgetValidated::storeAttributeValue()
72 {
73   myIsInValidate = true;
74 }
75
76 //********************************************************************
77 void ModuleBase_WidgetValidated::restoreAttributeValue(const bool theValid)
78 {
79   myIsInValidate = false;
80 }
81
82 //********************************************************************
83 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
84 {
85   bool aValid = true;
86   Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
87
88   // if an owner is null, the selection happens in the Object browser.
89   // creates a selection owner on the base of object shape and the object AIS object
90   if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
91     ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
92     if (aResult.get() && aResult->shape().get()) {
93       // some results have no shape, e.g. the parameter one. So, they should not be validated
94       GeomShapePtr aShape = aResult->shape();
95       const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
96       Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
97       anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
98       myPresentedObject = aResult;
99     }
100     else
101       aValid = false; // only results with a shape can be filtered
102   }
103   // checks the owner by the AIS context activated filters
104   if (!anOwner.IsNull()) {
105     // the widget validator filter should be active, but during check by preselection
106     // it is not yet activated, so we need to activate/deactivate it manually
107     bool isActivated = isFilterActivated();
108     if (!isActivated)
109       activateFilters(true);
110
111     Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
112     if (!aContext.IsNull()) {
113       const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
114       SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
115       for (; anIt.More() && aValid; anIt.Next()) {
116         Handle(SelectMgr_Filter) aFilter = anIt.Value();
117         aValid = aFilter->IsOk(anOwner);
118       }
119     }
120     if (!isActivated)
121       activateFilters(false);
122   }
123
124   // removes created owner
125   if (!anOwner.IsNull() && anOwner != thePrs.owner()) {
126     anOwner.Nullify();
127     myPresentedObject = ObjectPtr();
128   }
129   return aValid;
130 }
131
132 //********************************************************************
133 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
134 {
135   bool aValid = false;
136   if (getValidState(theValue, aValid)) {
137     return aValid;
138   }
139
140   aValid = isValidSelectionCustom(theValue);
141   if (!aValid) {
142     storeValidState(theValue, aValid);
143     return aValid;
144   }
145
146   // stores the current values of the widget attribute
147   bool isFlushesActived, isAttributeSetInitializedBlocked;
148   blockAttribute(true, isFlushesActived, isAttributeSetInitializedBlocked);
149
150   storeAttributeValue();
151
152   // saves the owner value to the widget attribute
153   aValid = setSelectionCustom(theValue);
154   if (aValid)
155     // checks the attribute validity
156     aValid = isValidAttribute();
157
158   // restores the current values of the widget attribute
159   restoreAttributeValue(aValid);
160
161   blockAttribute(false, isFlushesActived, isAttributeSetInitializedBlocked);
162   // In particular case the results are deleted and called as redisplayed inside of this
163   // highlight-selection, to they must be flushed as soon as possible.
164   // Example: selection of group-vertices subshapes with shift pressend on body. Without
165   //  these 4 lines below the application crashes because of left presentations on
166   //  removed results still in the viewer.
167   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
168   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
169   Events_Loop::loop()->flush(aDeletedEvent);
170   Events_Loop::loop()->flush(aRedispEvent);
171
172   storeValidState(theValue, aValid);
173   return aValid;
174 }
175
176 //********************************************************************
177 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
178 {
179   return true;
180 }
181
182 //********************************************************************
183 bool ModuleBase_WidgetValidated::isValidAttribute() const
184 {
185   SessionPtr aMgr = ModelAPI_Session::get();
186   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
187   AttributePtr anAttribute = myFeature->attribute(attributeID());
188   std::string aValidatorID, anError;
189   return aFactory->validate(anAttribute, aValidatorID, anError);
190 }
191
192 bool ModuleBase_WidgetValidated::isFilterActivated() const
193 {
194   bool isActivated = false;
195
196   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
197   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
198
199   return aViewer->hasSelectionFilter(aSelFilter);
200 }
201
202 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
203 {
204   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
205
206   Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
207   if (toActivate)
208     aViewer->addSelectionFilter(aSelFilter);
209   else {
210     aViewer->removeSelectionFilter(aSelFilter);
211     clearValidState();
212   }
213 }
214
215 //********************************************************************
216 void ModuleBase_WidgetValidated::blockAttribute(const bool& theToBlock, bool& isFlushesActived,
217                                                 bool& isAttributeSetInitializedBlocked)
218 {
219   Events_Loop* aLoop = Events_Loop::loop();
220   DataPtr aData = myFeature->data();
221   AttributePtr anAttribute = myFeature->attribute(attributeID());
222   if (theToBlock) {
223     // blocks the flush signals to avoid the temporary objects visualization in the viewer
224     // they should not be shown in order to do not lose highlight by erasing them
225     isFlushesActived = aLoop->activateFlushes(false);
226
227     aData->blockSendAttributeUpdated(true);
228     isAttributeSetInitializedBlocked = anAttribute->blockSetInitialized(true);
229   }
230   else {
231     aData->blockSendAttributeUpdated(false);
232     anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
233     aLoop->activateFlushes(isFlushesActived);
234   }
235 }
236
237 //********************************************************************
238 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
239 {
240   bool aValidPrs = myInvalidPrs.contains(theValue);
241   bool anInvalidPrs = myInvalidPrs.contains(theValue);
242
243   if (theValid) {
244     if (!aValidPrs)
245       myValidPrs.append(theValue);
246     // the commented code will be useful when the valid state of the presentation
247     // will be changable between activate/deactivate. Currently it does not happen.
248     //if (anInvalidPrs)
249     //  myInvalidPrs.removeOne(theValue);
250   }
251   else { // !theValid
252     if (!anInvalidPrs)
253       myInvalidPrs.append(theValue);
254     //if (!aValidPrs)
255     //  myValidPrs.removeOne(theValue);
256   }
257 #ifdef DEBUG_VALID_STATE
258   qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
259                  .arg(myInvalidPrs.count()).toStdString().c_str());
260 #endif
261 }
262
263 //********************************************************************
264 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
265 {
266   bool aValidPrs = myValidPrs.contains(theValue);
267   bool anInvalidPrs = myInvalidPrs.contains(theValue);
268
269   if (aValidPrs)
270     theValid = true;
271   else if (anInvalidPrs)
272     theValid = false;
273
274   return aValidPrs || anInvalidPrs;
275 }
276
277 //********************************************************************
278 void ModuleBase_WidgetValidated::clearValidState()
279 {
280 #ifdef DEBUG_VALID_STATE
281   qDebug("clearValidState");
282 #endif
283   myValidPrs.clear();
284   myInvalidPrs.clear();
285 }
286
287 //********************************************************************
288 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
289 {
290   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
291                                                        ModuleBase_ISelection::Viewer);
292
293   QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
294                                                        ModuleBase_ISelection::Browser);
295   // filter the OB presentations
296   filterPresentations(anOBSelected);
297   if (!anOBSelected.isEmpty())
298     ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
299
300   return aSelected;
301 }
302
303 //********************************************************************
304 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
305 {
306   QList<ModuleBase_ViewerPrs> aValidatedValues;
307
308   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
309   bool isDone = false;
310   for (; anIt != aLast; anIt++) {
311     if (isValidInFilters(*anIt))
312       aValidatedValues.append(*anIt);
313   }
314   if (aValidatedValues.size() != theValues.size()) {
315     theValues.clear();
316     theValues = aValidatedValues;
317   }
318 }