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>
7 #include <ModuleBase_WidgetSelectorStore.h>
8 #include <ModuleBase_ViewerPrs.h>
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>
17 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
18 #include <SelectMgr_EntityOwner.hxx>
19 #include <StdSelect_BRepOwner.hxx>
21 #include <Events_Loop.h>
25 //#define DEBUG_VALID_STATE
27 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
28 ModuleBase_IWorkshop* theWorkshop,
29 const Config_WidgetAPI* theData)
30 : ModuleBase_ModelWidget(theParent, theData),
31 myWorkshop(theWorkshop), myIsInValidate(false)
33 myAttributeStore = new ModuleBase_WidgetSelectorStore();
36 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
38 delete myAttributeStore;
41 //********************************************************************
42 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
44 return myPresentedObject;
47 //********************************************************************
48 void ModuleBase_WidgetValidated::clearValidatedCash()
50 #ifdef DEBUG_VALID_STATE
51 qDebug("clearValidatedState");
57 //********************************************************************
58 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
60 myIsInValidate = true;
61 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
64 //********************************************************************
65 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
68 myIsInValidate = false;
69 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
72 //********************************************************************
73 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
76 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
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;
91 aValid = false; // only results with a shape can be filtered
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();
99 activateFilters(true);
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);
111 activateFilters(false);
114 // removes created owner
115 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
117 myPresentedObject = ObjectPtr();
122 //********************************************************************
123 AttributePtr ModuleBase_WidgetValidated::attribute() const
125 return myFeature->attribute(attributeID());
128 //********************************************************************
129 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
132 if (getValidState(theValue, aValid)) {
135 aValid = isValidSelectionCustom(theValue);
137 aValid = isValidSelectionForAttribute(theValue, attribute());
139 storeValidState(theValue, aValid);
143 //********************************************************************
144 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(const ModuleBase_ViewerPrsPtr& theValue,
145 const AttributePtr& theAttribute)
149 // stores the current values of the widget attribute
150 bool isFlushesActived, isAttributeSetInitializedBlocked;
152 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked);
154 storeAttributeValue(theAttribute);
156 // saves the owner value to the widget attribute
157 aValid = setSelectionCustom(theValue);
159 // checks the attribute validity
160 aValid = isValidAttribute(theAttribute);
162 // restores the current values of the widget attribute
163 restoreAttributeValue(theAttribute, aValid);
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);
179 //********************************************************************
180 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
185 //********************************************************************
186 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute) const
188 SessionPtr aMgr = ModelAPI_Session::get();
189 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
190 std::string aValidatorID, anError;
191 return aFactory->validate(theAttribute, aValidatorID, anError);
194 bool ModuleBase_WidgetValidated::isFilterActivated() const
196 bool isActivated = false;
198 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
199 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
201 return aViewer->hasSelectionFilter(aSelFilter);
204 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
206 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
208 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
209 bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
212 aViewer->addSelectionFilter(aSelFilter);
214 aViewer->removeSelectionFilter(aSelFilter);
215 clearValidatedCash();
218 return aHasSelectionFilter;
221 //********************************************************************
222 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
223 const bool& theToBlock,
224 bool& isFlushesActived,
225 bool& isAttributeSetInitializedBlocked)
227 Events_Loop* aLoop = Events_Loop::loop();
228 DataPtr aData = myFeature->data();
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);
234 aData->blockSendAttributeUpdated(true);
235 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
238 aData->blockSendAttributeUpdated(false);
239 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
240 aLoop->activateFlushes(isFlushesActived);
244 //********************************************************************
245 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue, const bool theValid)
247 bool aValidPrs = myInvalidPrs.contains(theValue);
248 bool anInvalidPrs = myInvalidPrs.contains(theValue);
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.
256 // myInvalidPrs.removeOne(theValue);
260 myInvalidPrs.append(theValue);
262 // myValidPrs.removeOne(theValue);
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());
270 //********************************************************************
271 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue, bool& theValid)
273 bool aValidPrs = myValidPrs.contains(theValue);
274 bool anInvalidPrs = myInvalidPrs.contains(theValue);
278 else if (anInvalidPrs)
281 return aValidPrs || anInvalidPrs;
284 //********************************************************************
285 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
287 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
288 ModuleBase_ISelection::Viewer);
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);
297 filterCompSolids(aSelected);
302 //********************************************************************
303 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
305 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
307 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
309 for (; anIt != aLast; anIt++) {
310 if (isValidInFilters(*anIt))
311 aValidatedValues.append(*anIt);
313 if (aValidatedValues.size() != theValues.size()) {
315 theValues = aValidatedValues;
319 //********************************************************************
320 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
322 std::set<ResultCompSolidPtr> aCompSolids;
323 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
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);
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.
347 aValidatedValues.append(*anIt);
351 if (aValidatedValues.size() != theValues.size()) {
353 theValues = aValidatedValues;