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