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