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 const std::string& theParentId)
29 : ModuleBase_ModelWidget(theParent, theData, theParentId),
30 myWorkshop(theWorkshop), myIsInValidate(false)
34 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
38 //********************************************************************
39 bool ModuleBase_WidgetValidated::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
40 const bool theToValidate)
42 if (theValues.empty()) {
43 // In order to make reselection possible, set empty object and shape should be done
44 setSelectionCustom(ModuleBase_ViewerPrs());
47 // it removes the processed value from the parameters list
48 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
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);
57 //emit valuesChanged();
62 //********************************************************************
63 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
65 return myPresentedObject;
68 //********************************************************************
69 void ModuleBase_WidgetValidated::clearValidatedCash()
75 //********************************************************************
76 void ModuleBase_WidgetValidated::storeAttributeValue()
78 myIsInValidate = true;
81 //********************************************************************
82 void ModuleBase_WidgetValidated::restoreAttributeValue(const bool theValid)
84 myIsInValidate = false;
87 //********************************************************************
88 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
91 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
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;
106 aValid = false; // only results with a shape can be filtered
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();
114 activateFilters(true);
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);
126 activateFilters(false);
129 // removes created owner
130 if (!anOwner.IsNull() && anOwner != thePrs.owner()) {
132 myPresentedObject = ObjectPtr();
137 //********************************************************************
138 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
141 if (getValidState(theValue, aValid)) {
145 aValid = isValidSelectionCustom(theValue);
147 storeValidState(theValue, aValid);
151 // stores the current values of the widget attribute
152 bool isFlushesActived, isAttributeSetInitializedBlocked;
153 blockAttribute(true, isFlushesActived, isAttributeSetInitializedBlocked);
155 storeAttributeValue();
157 // saves the owner value to the widget attribute
158 aValid = setSelectionCustom(theValue);
160 // checks the attribute validity
161 aValid = isValidAttribute();
163 // restores the current values of the widget attribute
164 restoreAttributeValue(aValid);
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);
177 storeValidState(theValue, aValid);
181 //********************************************************************
182 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
187 //********************************************************************
188 bool ModuleBase_WidgetValidated::isValidAttribute() const
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);
197 bool ModuleBase_WidgetValidated::isFilterActivated() const
199 bool isActivated = false;
201 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
202 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
204 return aViewer->hasSelectionFilter(aSelFilter);
207 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
209 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
211 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
212 bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
215 aViewer->addSelectionFilter(aSelFilter);
217 aViewer->removeSelectionFilter(aSelFilter);
221 return aHasSelectionFilter;
224 //********************************************************************
225 void ModuleBase_WidgetValidated::blockAttribute(const bool& theToBlock, bool& isFlushesActived,
226 bool& isAttributeSetInitializedBlocked)
228 Events_Loop* aLoop = Events_Loop::loop();
229 DataPtr aData = myFeature->data();
230 AttributePtr anAttribute = myFeature->attribute(attributeID());
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);
236 aData->blockSendAttributeUpdated(true);
237 isAttributeSetInitializedBlocked = anAttribute->blockSetInitialized(true);
240 aData->blockSendAttributeUpdated(false);
241 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
242 aLoop->activateFlushes(isFlushesActived);
246 //********************************************************************
247 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
249 bool aValidPrs = myInvalidPrs.contains(theValue);
250 bool anInvalidPrs = myInvalidPrs.contains(theValue);
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.
258 // myInvalidPrs.removeOne(theValue);
262 myInvalidPrs.append(theValue);
264 // myValidPrs.removeOne(theValue);
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());
272 //********************************************************************
273 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
275 bool aValidPrs = myValidPrs.contains(theValue);
276 bool anInvalidPrs = myInvalidPrs.contains(theValue);
280 else if (anInvalidPrs)
283 return aValidPrs || anInvalidPrs;
286 //********************************************************************
287 void ModuleBase_WidgetValidated::clearValidState()
289 #ifdef DEBUG_VALID_STATE
290 qDebug("clearValidState");
293 myInvalidPrs.clear();
296 //********************************************************************
297 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
299 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
300 ModuleBase_ISelection::Viewer);
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);
309 filterCompSolids(aSelected);
314 //********************************************************************
315 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
317 QList<ModuleBase_ViewerPrs> aValidatedValues;
319 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
321 for (; anIt != aLast; anIt++) {
322 if (isValidInFilters(*anIt))
323 aValidatedValues.append(*anIt);
325 if (aValidatedValues.size() != theValues.size()) {
327 theValues = aValidatedValues;
331 //********************************************************************
332 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrs>& theValues)
334 std::set<ResultCompSolidPtr> aCompSolids;
335 QList<ModuleBase_ViewerPrs> aValidatedValues;
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);
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.
359 aValidatedValues.append(*anIt);
363 if (aValidatedValues.size() != theValues.size()) {
365 theValues = aValidatedValues;