1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <ModuleBase_WidgetValidated.h>
4 #include <ModuleBase_FilterFactory.h>
5 #include <ModuleBase_IViewer.h>
6 #include <ModuleBase_ISelection.h>
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>
15 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
16 #include <SelectMgr_EntityOwner.hxx>
17 #include <StdSelect_BRepOwner.hxx>
19 #include <Events_Loop.h>
23 //#define DEBUG_VALID_STATE
25 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
26 ModuleBase_IWorkshop* theWorkshop,
27 const Config_WidgetAPI* theData)
28 : ModuleBase_ModelWidget(theParent, theData),
29 myWorkshop(theWorkshop), myIsInValidate(false)
33 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
37 //********************************************************************
38 bool ModuleBase_WidgetValidated::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
39 const bool theToValidate)
41 if (theValues.empty()) {
42 // In order to make reselection possible, set empty object and shape should be done
43 setSelectionCustom(ModuleBase_ViewerPrs());
46 // it removes the processed value from the parameters list
47 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
50 if (!theToValidate || isValidInFilters(aValue)) {
51 isDone = setSelectionCustom(aValue);
52 // updateObject - to update/redisplay feature
53 // it is commented in order to perfom it outside the method
54 //updateObject(myFeature);
56 //emit valuesChanged();
61 //********************************************************************
62 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
64 return myPresentedObject;
67 //********************************************************************
68 void ModuleBase_WidgetValidated::clearValidatedCash()
74 //********************************************************************
75 void ModuleBase_WidgetValidated::storeAttributeValue()
77 myIsInValidate = true;
80 //********************************************************************
81 void ModuleBase_WidgetValidated::restoreAttributeValue(const bool theValid)
83 myIsInValidate = false;
86 //********************************************************************
87 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
90 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
92 // if an owner is null, the selection happens in the Object browser.
93 // creates a selection owner on the base of object shape and the object AIS object
94 if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
95 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
96 if (aResult.get() && aResult->shape().get()) {
97 // some results have no shape, e.g. the parameter one. So, they should not be validated
98 GeomShapePtr aShape = aResult->shape();
99 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
100 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
101 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
102 myPresentedObject = aResult;
105 aValid = false; // only results with a shape can be filtered
107 // checks the owner by the AIS context activated filters
108 if (!anOwner.IsNull()) {
109 // the widget validator filter should be active, but during check by preselection
110 // it is not yet activated, so we need to activate/deactivate it manually
111 bool isActivated = isFilterActivated();
113 activateFilters(true);
115 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
116 if (!aContext.IsNull()) {
117 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
118 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
119 for (; anIt.More() && aValid; anIt.Next()) {
120 Handle(SelectMgr_Filter) aFilter = anIt.Value();
121 aValid = aFilter->IsOk(anOwner);
125 activateFilters(false);
128 // removes created owner
129 if (!anOwner.IsNull() && anOwner != thePrs.owner()) {
131 myPresentedObject = ObjectPtr();
136 //********************************************************************
137 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
140 if (getValidState(theValue, aValid)) {
144 aValid = isValidSelectionCustom(theValue);
146 storeValidState(theValue, aValid);
150 // stores the current values of the widget attribute
151 bool isFlushesActived, isAttributeSetInitializedBlocked;
152 blockAttribute(true, isFlushesActived, isAttributeSetInitializedBlocked);
154 storeAttributeValue();
156 // saves the owner value to the widget attribute
157 aValid = setSelectionCustom(theValue);
159 // checks the attribute validity
160 aValid = isValidAttribute();
162 // restores the current values of the widget attribute
163 restoreAttributeValue(aValid);
165 blockAttribute(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);
176 storeValidState(theValue, aValid);
180 //********************************************************************
181 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
186 //********************************************************************
187 bool ModuleBase_WidgetValidated::isValidAttribute() const
189 SessionPtr aMgr = ModelAPI_Session::get();
190 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
191 AttributePtr anAttribute = myFeature->attribute(attributeID());
192 std::string aValidatorID, anError;
193 return aFactory->validate(anAttribute, aValidatorID, anError);
196 bool ModuleBase_WidgetValidated::isFilterActivated() const
198 bool isActivated = false;
200 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
201 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
203 return aViewer->hasSelectionFilter(aSelFilter);
206 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
208 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
210 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
211 bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
214 aViewer->addSelectionFilter(aSelFilter);
216 aViewer->removeSelectionFilter(aSelFilter);
220 return aHasSelectionFilter;
223 //********************************************************************
224 void ModuleBase_WidgetValidated::blockAttribute(const bool& theToBlock, bool& isFlushesActived,
225 bool& isAttributeSetInitializedBlocked)
227 Events_Loop* aLoop = Events_Loop::loop();
228 DataPtr aData = myFeature->data();
229 AttributePtr anAttribute = myFeature->attribute(attributeID());
231 // blocks the flush signals to avoid the temporary objects visualization in the viewer
232 // they should not be shown in order to do not lose highlight by erasing them
233 isFlushesActived = aLoop->activateFlushes(false);
235 aData->blockSendAttributeUpdated(true);
236 isAttributeSetInitializedBlocked = anAttribute->blockSetInitialized(true);
239 aData->blockSendAttributeUpdated(false);
240 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
241 aLoop->activateFlushes(isFlushesActived);
245 //********************************************************************
246 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
248 bool aValidPrs = myInvalidPrs.contains(theValue);
249 bool anInvalidPrs = myInvalidPrs.contains(theValue);
253 myValidPrs.append(theValue);
254 // the commented code will be useful when the valid state of the presentation
255 // will be changable between activate/deactivate. Currently it does not happen.
257 // myInvalidPrs.removeOne(theValue);
261 myInvalidPrs.append(theValue);
263 // myValidPrs.removeOne(theValue);
265 #ifdef DEBUG_VALID_STATE
266 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
267 .arg(myInvalidPrs.count()).toStdString().c_str());
271 //********************************************************************
272 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
274 bool aValidPrs = myValidPrs.contains(theValue);
275 bool anInvalidPrs = myInvalidPrs.contains(theValue);
279 else if (anInvalidPrs)
282 return aValidPrs || anInvalidPrs;
285 //********************************************************************
286 void ModuleBase_WidgetValidated::clearValidState()
288 #ifdef DEBUG_VALID_STATE
289 qDebug("clearValidState");
292 myInvalidPrs.clear();
295 //********************************************************************
296 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
298 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
299 ModuleBase_ISelection::Viewer);
301 QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
302 ModuleBase_ISelection::Browser);
303 // filter the OB presentations
304 filterPresentations(anOBSelected);
305 if (!anOBSelected.isEmpty())
306 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
308 filterCompSolids(aSelected);
313 //********************************************************************
314 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
316 QList<ModuleBase_ViewerPrs> aValidatedValues;
318 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
320 for (; anIt != aLast; anIt++) {
321 if (isValidInFilters(*anIt))
322 aValidatedValues.append(*anIt);
324 if (aValidatedValues.size() != theValues.size()) {
326 theValues = aValidatedValues;
330 //********************************************************************
331 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrs>& theValues)
333 std::set<ResultCompSolidPtr> aCompSolids;
334 QList<ModuleBase_ViewerPrs> aValidatedValues;
336 // Collect compsolids.
337 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
338 for (; anIt != aLast; anIt++) {
339 const ModuleBase_ViewerPrs& aViewerPrs = *anIt;
340 ObjectPtr anObject = aViewerPrs.object();
341 ResultCompSolidPtr aResultCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
342 if(aResultCompSolid.get()) {
343 aCompSolids.insert(aResultCompSolid);
347 // Filter sub-solids of compsolids.
348 anIt = theValues.begin();
349 for (; anIt != aLast; anIt++) {
350 const ModuleBase_ViewerPrs& aViewerPrs = *anIt;
351 ObjectPtr anObject = aViewerPrs.object();
352 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
353 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
354 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
355 // Skip sub-solid of compsolid.
358 aValidatedValues.append(*anIt);
362 if (aValidatedValues.size() != theValues.size()) {
364 theValues = aValidatedValues;