1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetValidated.h>
21 #include <ModuleBase_IModule.h>
22 #include <ModuleBase_IViewer.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_ISelection.h>
25 #include <ModuleBase_ISelectionActivate.h>
26 #include <ModuleBase_WidgetSelectorStore.h>
27 #include <ModuleBase_ViewerPrs.h>
29 #include <Events_InfoMessage.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_AttributeValidator.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_Tools.h>
37 #include <ModelAPI_AttributeSelection.h>
39 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
40 #include <SelectMgr_EntityOwner.hxx>
41 #include <StdSelect_BRepOwner.hxx>
43 #include <Events_Loop.h>
47 //#define DEBUG_VALID_STATE
49 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
50 ModuleBase_IWorkshop* theWorkshop,
51 const Config_WidgetAPI* theData)
52 : ModuleBase_ModelWidget(theParent, theData),
53 myWorkshop(theWorkshop), myIsInValidate(false)
55 myAttributeStore = new ModuleBase_WidgetSelectorStore();
58 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
60 delete myAttributeStore;
63 //********************************************************************
64 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
66 return myPresentedObject;
69 //********************************************************************
70 void ModuleBase_WidgetValidated::deactivate()
75 //********************************************************************
76 void ModuleBase_WidgetValidated::clearValidatedCash()
78 #ifdef DEBUG_VALID_STATE
79 qDebug("clearValidatedCash");
85 //********************************************************************
86 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
88 myIsInValidate = true;
89 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
92 //********************************************************************
93 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
96 myIsInValidate = false;
97 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
100 //********************************************************************
101 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
104 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
106 // if an owner is null, the selection happens in the Object browser.
107 // creates a selection owner on the base of object shape and the object AIS object
108 if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
109 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
110 GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
111 // some results have no shape, e.g. the parameter one. So, they should not be validated
113 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
114 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
115 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
116 myPresentedObject = aResult;
119 FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
120 if (aFeature.get()) {
121 // Use feature as a reference to all its results
122 myPresentedObject = aFeature;
123 AttributePtr anAttr = attribute();
124 std::string aType = anAttr->attributeType();
126 // Check that results of Feature is acceptable by filters for selection attribute
127 if (aType == ModelAPI_AttributeSelection::typeId()) {
128 AttributeSelectionPtr aSelectAttr =
129 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(anAttr);
130 aSelectAttr->setValue(myPresentedObject, GeomShapePtr(), true);
131 GeomShapePtr aShape = aSelectAttr->value();
132 if (!aShape.get() && aSelectAttr->contextFeature().get() &&
133 aSelectAttr->contextFeature()->firstResult().get()) {
134 aShape = aSelectAttr->contextFeature()->firstResult()->shape();
137 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
138 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
139 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
143 aSelectAttr->setValue(ObjectPtr(), GeomShapePtr(), true);
146 ResultPtr aResult = aFeature->firstResult();
148 GeomShapePtr aShapePtr = ModelAPI_Tools::shape(aResult);
149 if (aShapePtr.get()) {
150 const TopoDS_Shape aTDShape = aShapePtr->impl<TopoDS_Shape>();
151 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
152 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
155 aValid = !anOwner.IsNull(); // only results with a shape can be filtered
158 aValid = false; // only results with a shape can be filtered
161 // checks the owner by the AIS context activated filters
162 if (!anOwner.IsNull()) {
163 // the widget validator filter should be active, but during check by preselection
164 // it is not yet activated, so we need to activate/deactivate it manually
165 bool isActivated = isFilterActivated();
167 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
168 SelectMgr_ListOfFilter aSelectionFilters;
169 selectionFilters(aModuleSelectionFilters, aSelectionFilters);
170 /// after validation, the selection filters should be restored
171 myWorkshop->selectionActivate()->activateSelectionFilters(aSelectionFilters);
174 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
175 if (!aContext.IsNull()) {
176 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
177 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
178 for (; anIt.More() && aValid; anIt.Next()) {
179 Handle(SelectMgr_Filter) aFilter = anIt.Value();
180 aValid = aFilter->IsOk(anOwner);
185 // reset filters set in activateSelectionFilters above
186 myWorkshop->selectionActivate()->updateSelectionFilters();
187 clearValidatedCash();
191 // removes created owner
192 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
194 myPresentedObject = ObjectPtr();
199 //********************************************************************
200 AttributePtr ModuleBase_WidgetValidated::attribute() const
202 return myFeature->attribute(attributeID());
205 //********************************************************************
206 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
209 if (getValidState(theValue, aValid)) {
212 aValid = isValidSelectionCustom(theValue);
214 aValid = isValidSelectionForAttribute(theValue, attribute());
216 storeValidState(theValue, aValid);
220 //********************************************************************
221 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
222 const ModuleBase_ViewerPrsPtr& theValue,
223 const AttributePtr& theAttribute)
227 // stores the current values of the widget attribute
228 bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
230 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
231 isAttributeSendUpdatedBlocked);
233 storeAttributeValue(theAttribute);
235 // saves the owner value to the widget attribute
236 aValid = setSelectionCustom(theValue);
238 // checks the attribute validity
239 aValid = isValidAttribute(theAttribute);
241 // restores the current values of the widget attribute
242 restoreAttributeValue(theAttribute, aValid);
244 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
245 isAttributeSendUpdatedBlocked);
246 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
247 /// This is not correct to perform it here because it might cause update selection and
248 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
249 // In particular case the results are deleted and called as redisplayed inside of this
250 // highlight-selection, to they must be flushed as soon as possible.
251 // Example: selection of group-vertices subshapes with shift pressend on body. Without
252 // these 4 lines below the application crashes because of left presentations on
253 // removed results still in the viewer.
254 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
255 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
256 Events_Loop::loop()->flush(aDeletedEvent);
257 Events_Loop::loop()->flush(aRedispEvent);
262 //********************************************************************
263 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
268 //********************************************************************
269 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
271 SessionPtr aMgr = ModelAPI_Session::get();
272 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
273 std::string aValidatorID;
274 Events_InfoMessage anError;
275 return aFactory->validate(theAttribute, aValidatorID, anError);
278 //********************************************************************
279 bool ModuleBase_WidgetValidated::isFilterActivated() const
281 bool isActivated = false;
283 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
284 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
286 return aViewer->hasSelectionFilter(aSelFilter);
289 //********************************************************************
290 void ModuleBase_WidgetValidated::selectionFilters(QIntList& theModuleSelectionFilters,
291 SelectMgr_ListOfFilter& theSelectionFilters)
293 theSelectionFilters.Append(myWorkshop->validatorFilter());
296 //********************************************************************
297 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
298 const bool& theToBlock,
299 bool& isFlushesActived,
300 bool& isAttributeSetInitializedBlocked,
301 bool& isAttributeSendUpdatedBlocked)
303 blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
304 isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
307 //********************************************************************
308 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
309 const FeaturePtr& theFeature,
310 const bool& theToBlock,
311 bool& isFlushesActived,
312 bool& isAttributeSetInitializedBlocked,
313 bool& isAttributeSendUpdatedBlocked)
315 Events_Loop* aLoop = Events_Loop::loop();
316 DataPtr aData = theFeature->data();
318 // blocks the flush signals to avoid the temporary objects visualization in the viewer
319 // they should not be shown in order to do not lose highlight by erasing them
320 isFlushesActived = aLoop->activateFlushes(false);
322 isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
323 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
326 aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
327 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
328 aLoop->activateFlushes(isFlushesActived);
332 //********************************************************************
333 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
336 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
339 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
340 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
341 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
342 if (!aValidPrsContains) {
343 #ifdef LIST_OF_VALID_PRS
344 myValidPrs.append(theValue);
346 myValidPrs.Bind(aTDShape, theValue);
348 // the commented code will be useful when the valid state of the presentation
349 // will be changable between activate/deactivate. Currently it does not happen.
351 // myInvalidPrs.removeOne(theValue);
356 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
357 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
358 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
359 if (!anIValidPrsContains) {
360 #ifdef LIST_OF_VALID_PRS
361 myInvalidPrs.append(theValue);
363 myInvalidPrs.Bind(aTDShape, theValue);
366 // myValidPrs.removeOne(theValue);
371 #ifdef DEBUG_VALID_STATE
372 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
373 .arg(myValidPrs.count())
374 .arg(myInvalidPrs.count()).toStdString().c_str());
378 //********************************************************************
379 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
385 #ifdef LIST_OF_VALID_PRS
386 bool aValidPrsContains = myValidPrs.contains(theValue);
387 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
389 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
393 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
394 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
395 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
397 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
398 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
400 bool aValidPrsContains = false, anInvalidPrsContains = false;
401 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
403 aValidPrsContains = myValidPrs.contains(aShape);
404 anInvalidPrsContains = myInvalidPrs.contains(aShape);
406 if (aValidPrsContains)
407 aValidPrsContains = theValue == myValidPrs[aShape];
409 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
412 if (aValidPrsContains)
414 else if (anInvalidPrsContains)
417 return aValidPrsContains || anInvalidPrsContains;
420 //********************************************************************
421 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
423 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
424 ModuleBase_ISelection::Viewer);
426 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
427 ModuleBase_ISelection::Browser);
428 // filter the OB presentations
429 filterPresentations(anOBSelected);
430 if (!anOBSelected.isEmpty())
431 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
433 filterCompSolids(aSelected);
438 //********************************************************************
439 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
441 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
443 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
445 for (; anIt != aLast; anIt++) {
446 if (isValidInFilters(*anIt))
447 aValidatedValues.append(*anIt);
449 if (aValidatedValues.size() != theValues.size()) {
451 theValues = aValidatedValues;
455 //********************************************************************
456 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
458 std::set<ResultPtr> aFilterOut; // all objects that must be filtered out with their children
459 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
461 // Collect compsolids.
462 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
463 for(; anIt != aLast; anIt++) {
464 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
465 ObjectPtr anObject = aViewerPrs->object();
466 ResultBodyPtr aResultCompSolid =
467 std::dynamic_pointer_cast<ModelAPI_ResultBody>(anObject);
468 if (aResultCompSolid.get()) {
469 for(int aSubIndex = 0; aSubIndex < aResultCompSolid->numberOfSubs(); aSubIndex++)
470 aFilterOut.insert(aResultCompSolid->subResult(aSubIndex));
471 } else { // it could be a whole feature selected, so, add all results of this feature
472 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
473 if (aFeature.get()) {
474 std::list<ResultPtr>::const_iterator aRes = aFeature->results().cbegin();
475 for(; aRes != aFeature->results().cend(); aRes++)
476 aFilterOut.insert(*aRes);
481 // Filter sub-solids of compsolids.
482 anIt = theValues.begin();
483 for(; anIt != aLast; anIt++) {
484 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
485 ObjectPtr anObject = aViewerPrs->object();
486 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
487 while(aResult.get()) {
488 if (aFilterOut.find(aResult) != aFilterOut.end()) // skip if parent is filtered out
490 aResult = ModelAPI_Tools::bodyOwner(aResult); // iterate all parents
495 aValidatedValues.append(*anIt);
499 if (aValidatedValues.size() != theValues.size()) {
501 theValues = aValidatedValues;