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>
13 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
14 #include <SelectMgr_EntityOwner.hxx>
15 #include <StdSelect_BRepOwner.hxx>
17 #include <Events_Loop.h>
21 //#define DEBUG_VALID_STATE
23 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
24 ModuleBase_IWorkshop* theWorkshop,
25 const Config_WidgetAPI* theData,
26 const std::string& theParentId)
27 : ModuleBase_ModelWidget(theParent, theData, theParentId), isValidateBlocked(false),
28 myWorkshop(theWorkshop)
32 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
36 //********************************************************************
37 bool ModuleBase_WidgetValidated::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
38 const bool theToValidate)
40 if (theValues.empty())
42 // it removes the processed value from the parameters list
43 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
46 if (!theToValidate || isValidInFilters(aValue)) {
47 isDone = setSelectionCustom(aValue);
48 // updateObject - to update/redisplay feature
49 // it is commented in order to perfom it outside the method
50 //updateObject(myFeature);
52 //emit valuesChanged();
57 //********************************************************************
58 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
60 return myPresentedObject;
63 //********************************************************************
64 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
67 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
69 // if an owner is null, the selection happens in the Object browser.
70 // creates a selection owner on the base of object shape and the object AIS object
71 if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
72 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
74 GeomShapePtr aShape = aResult->shape();
75 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
76 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
77 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
78 myPresentedObject = aResult;
81 aValid = false; // only results can be filtered
83 // checks the owner by the AIS context activated filters
84 if (!anOwner.IsNull()) {
85 // the widget validator filter should be active, but during check by preselection
86 // it is not yet activated, so we need to activate/deactivate it manually
87 bool isActivated = isFilterActivated();
89 activateFilters(true);
91 const SelectMgr_ListOfFilter& aFilters = myWorkshop->viewer()->AISContext()->Filters();
92 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
93 for (; anIt.More() && aValid; anIt.Next()) {
94 Handle(SelectMgr_Filter) aFilter = anIt.Value();
95 aValid = aFilter->IsOk(anOwner);
98 activateFilters(false);
101 // removes created owner
102 if (!anOwner.IsNull() && anOwner != thePrs.owner()) {
104 myPresentedObject = ObjectPtr();
109 //********************************************************************
110 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
113 if (getValidState(theValue, aValid)) {
117 aValid = isValidSelectionCustom(theValue);
119 storeValidState(theValue, aValid);
123 if (isValidateBlocked)
125 isValidateBlocked = true;
127 DataPtr aData = myFeature->data();
128 AttributePtr anAttribute = myFeature->attribute(attributeID());
130 // stores the current values of the widget attribute
131 Events_Loop* aLoop = Events_Loop::loop();
132 // blocks the flush signals to avoid the temporary objects visualization in the viewer
133 // they should not be shown in order to do not lose highlight by erasing them
134 bool isActive = aLoop->activateFlushes(false);
136 aData->blockSendAttributeUpdated(true);
137 bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
138 storeAttributeValue();
140 // saves the owner value to the widget attribute
141 aValid = setSelectionCustom(theValue);
143 // checks the attribute validity
144 aValid = isValidAttribute();
146 // restores the current values of the widget attribute
147 restoreAttributeValue(aValid);
148 aData->blockSendAttributeUpdated(false);
149 anAttribute->blockSetInitialized(isAttributeBlocked);
150 aLoop->activateFlushes(isActive);
152 // In particular case the results are deleted and called as redisplayed inside of this
153 // highlight-selection, to they must be flushed as soon as possible.
154 // Example: selection of group-vertices subshapes with shift pressend on body. Without
155 // these 4 lines below the application crashes because of left presentations on
156 // removed results still in the viewer.
157 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
158 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
159 aLoop->flush(aDeletedEvent);
160 aLoop->flush(aRedispEvent);
162 storeValidState(theValue, aValid);
163 isValidateBlocked = false;
167 //********************************************************************
168 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
173 //********************************************************************
174 bool ModuleBase_WidgetValidated::isValidAttribute() const
176 SessionPtr aMgr = ModelAPI_Session::get();
177 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
178 std::list<ModelAPI_Validator*> aValidators;
179 std::list<std::list<std::string> > anArguments;
180 aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
182 DataPtr aData = myFeature->data();
183 AttributePtr anAttribute = myFeature->attribute(attributeID());
185 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
186 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
188 for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
189 const ModelAPI_AttributeValidator* aAttrValidator =
190 dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
191 if (aAttrValidator) {
192 aValid = aAttrValidator->isValid(anAttribute, *aArgs);
198 bool ModuleBase_WidgetValidated::isFilterActivated() const
200 bool isActivated = false;
202 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
204 const SelectMgr_ListOfFilter& aFilters = myWorkshop->viewer()->AISContext()->Filters();
205 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
206 for (; aIt.More(); aIt.Next()) {
207 if (aSelFilter.Access() == aIt.Value().Access())
214 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
216 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
218 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
220 aViewer->addSelectionFilter(aSelFilter);
222 aViewer->removeSelectionFilter(aSelFilter);
227 //********************************************************************
228 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
230 bool aValidPrs = myInvalidPrs.contains(theValue);
231 bool anInvalidPrs = myInvalidPrs.contains(theValue);
235 myValidPrs.append(theValue);
236 // the commented code will be useful when the valid state of the presentation
237 // will be changable between activate/deactivate. Currently it does not happen.
239 // myInvalidPrs.removeOne(theValue);
243 myInvalidPrs.append(theValue);
245 // myValidPrs.removeOne(theValue);
247 #ifdef DEBUG_VALID_STATE
248 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
249 .arg(myInvalidPrs.count()).toStdString().c_str());
253 //********************************************************************
254 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
256 bool aValidPrs = myValidPrs.contains(theValue);
257 bool anInvalidPrs = myInvalidPrs.contains(theValue);
261 else if (anInvalidPrs)
264 return aValidPrs || anInvalidPrs;
267 //********************************************************************
268 void ModuleBase_WidgetValidated::clearValidState()
270 #ifdef DEBUG_VALID_STATE
271 qDebug("clearValidState");
274 myInvalidPrs.clear();
277 //********************************************************************
278 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
280 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
281 ModuleBase_ISelection::Viewer);
283 QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
284 ModuleBase_ISelection::Browser);
285 // filter the OB presentations
286 filterPresentations(anOBSelected);
287 if (!anOBSelected.isEmpty())
288 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
293 //********************************************************************
294 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
296 QList<ModuleBase_ViewerPrs> aValidatedValues;
298 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
300 for (; anIt != aLast; anIt++) {
301 if (isValidInFilters(*anIt))
302 aValidatedValues.append(*anIt);
304 if (aValidatedValues.size() != theValues.size()) {
306 theValues = aValidatedValues;