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>
9 #include <ModelAPI_Session.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_AttributeValidator.h>
12 #include <ModelAPI_Events.h>
13 #include <ModelAPI_ResultCompSolid.h>
14 #include <ModelAPI_Tools.h>
16 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
17 #include <SelectMgr_EntityOwner.hxx>
18 #include <StdSelect_BRepOwner.hxx>
20 #include <Events_Loop.h>
24 //#define DEBUG_VALID_STATE
26 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
27 ModuleBase_IWorkshop* theWorkshop,
28 const Config_WidgetAPI* theData)
29 : ModuleBase_ModelWidget(theParent, theData),
30 myWorkshop(theWorkshop)
32 myAttributeStore = new ModuleBase_WidgetSelectorStore();
35 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
37 delete myAttributeStore;
40 //********************************************************************
41 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
43 return myPresentedObject;
46 //********************************************************************
47 void ModuleBase_WidgetValidated::clearValidatedCash()
49 #ifdef DEBUG_VALID_STATE
50 qDebug("clearValidatedState");
56 //********************************************************************
57 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
59 myIsInValidate = true;
60 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
63 //********************************************************************
64 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
67 myIsInValidate = false;
68 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
71 //********************************************************************
72 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
75 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
77 // if an owner is null, the selection happens in the Object browser.
78 // creates a selection owner on the base of object shape and the object AIS object
79 if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
80 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
81 if (aResult.get() && aResult->shape().get()) {
82 // some results have no shape, e.g. the parameter one. So, they should not be validated
83 GeomShapePtr aShape = aResult->shape();
84 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
85 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
86 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
87 myPresentedObject = aResult;
90 aValid = false; // only results with a shape can be filtered
92 // checks the owner by the AIS context activated filters
93 if (!anOwner.IsNull()) {
94 // the widget validator filter should be active, but during check by preselection
95 // it is not yet activated, so we need to activate/deactivate it manually
96 bool isActivated = isFilterActivated();
98 activateFilters(true);
100 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
101 if (!aContext.IsNull()) {
102 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
103 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
104 for (; anIt.More() && aValid; anIt.Next()) {
105 Handle(SelectMgr_Filter) aFilter = anIt.Value();
106 aValid = aFilter->IsOk(anOwner);
110 activateFilters(false);
113 // removes created owner
114 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
116 myPresentedObject = ObjectPtr();
121 //********************************************************************
122 AttributePtr ModuleBase_WidgetValidated::attribute() const
124 return myFeature->attribute(attributeID());
127 //********************************************************************
128 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
131 if (getValidState(theValue, aValid)) {
134 aValid = isValidSelectionCustom(theValue);
136 aValid = isValidSelectionForAttribute(theValue, attribute());
138 storeValidState(theValue, aValid);
142 //********************************************************************
143 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(const ModuleBase_ViewerPrsPtr& theValue,
144 const AttributePtr& theAttribute)
148 // stores the current values of the widget attribute
149 bool isFlushesActived, isAttributeSetInitializedBlocked;
151 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked);
153 storeAttributeValue(theAttribute);
155 // saves the owner value to the widget attribute
156 aValid = setSelectionCustom(theValue);
158 // checks the attribute validity
159 aValid = isValidAttribute(theAttribute);
161 // restores the current values of the widget attribute
162 restoreAttributeValue(theAttribute, aValid);
164 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked);
165 // In particular case the results are deleted and called as redisplayed inside of this
166 // highlight-selection, to they must be flushed as soon as possible.
167 // Example: selection of group-vertices subshapes with shift pressend on body. Without
168 // these 4 lines below the application crashes because of left presentations on
169 // removed results still in the viewer.
170 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
171 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
172 Events_Loop::loop()->flush(aDeletedEvent);
173 Events_Loop::loop()->flush(aRedispEvent);
178 //********************************************************************
179 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
184 //********************************************************************
185 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute) const
187 SessionPtr aMgr = ModelAPI_Session::get();
188 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
189 std::string aValidatorID, anError;
190 return aFactory->validate(theAttribute, aValidatorID, anError);
193 bool ModuleBase_WidgetValidated::isFilterActivated() const
195 bool isActivated = false;
197 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
198 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
200 return aViewer->hasSelectionFilter(aSelFilter);
203 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
205 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
207 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
208 bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
211 aViewer->addSelectionFilter(aSelFilter);
213 aViewer->removeSelectionFilter(aSelFilter);
214 clearValidatedCash();
217 return aHasSelectionFilter;
220 //********************************************************************
221 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
222 const bool& theToBlock,
223 bool& isFlushesActived,
224 bool& isAttributeSetInitializedBlocked)
226 Events_Loop* aLoop = Events_Loop::loop();
227 DataPtr aData = myFeature->data();
229 // blocks the flush signals to avoid the temporary objects visualization in the viewer
230 // they should not be shown in order to do not lose highlight by erasing them
231 isFlushesActived = aLoop->activateFlushes(false);
233 aData->blockSendAttributeUpdated(true);
234 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
237 aData->blockSendAttributeUpdated(false);
238 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
239 aLoop->activateFlushes(isFlushesActived);
243 //********************************************************************
244 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue, const bool theValid)
246 bool aValidPrs = myInvalidPrs.contains(theValue);
247 bool anInvalidPrs = myInvalidPrs.contains(theValue);
251 myValidPrs.append(theValue);
252 // the commented code will be useful when the valid state of the presentation
253 // will be changable between activate/deactivate. Currently it does not happen.
255 // myInvalidPrs.removeOne(theValue);
259 myInvalidPrs.append(theValue);
261 // myValidPrs.removeOne(theValue);
263 #ifdef DEBUG_VALID_STATE
264 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
265 .arg(myInvalidPrs.count()).toStdString().c_str());
269 //********************************************************************
270 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue, bool& theValid)
272 bool aValidPrs = myValidPrs.contains(theValue);
273 bool anInvalidPrs = myInvalidPrs.contains(theValue);
277 else if (anInvalidPrs)
280 return aValidPrs || anInvalidPrs;
283 //********************************************************************
284 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
286 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
287 ModuleBase_ISelection::Viewer);
289 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
290 ModuleBase_ISelection::Browser);
291 // filter the OB presentations
292 filterPresentations(anOBSelected);
293 if (!anOBSelected.isEmpty())
294 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
296 filterCompSolids(aSelected);
301 //********************************************************************
302 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
304 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
306 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
308 for (; anIt != aLast; anIt++) {
309 if (isValidInFilters(*anIt))
310 aValidatedValues.append(*anIt);
312 if (aValidatedValues.size() != theValues.size()) {
314 theValues = aValidatedValues;
318 //********************************************************************
319 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
321 std::set<ResultCompSolidPtr> aCompSolids;
322 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
324 // Collect compsolids.
325 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
326 for (; anIt != aLast; anIt++) {
327 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
328 ObjectPtr anObject = aViewerPrs->object();
329 ResultCompSolidPtr aResultCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
330 if(aResultCompSolid.get()) {
331 aCompSolids.insert(aResultCompSolid);
335 // Filter sub-solids of compsolids.
336 anIt = theValues.begin();
337 for (; anIt != aLast; anIt++) {
338 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
339 ObjectPtr anObject = aViewerPrs->object();
340 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
341 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
342 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
343 // Skip sub-solid of compsolid.
346 aValidatedValues.append(*anIt);
350 if (aValidatedValues.size() != theValues.size()) {
352 theValues = aValidatedValues;