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),
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 DataPtr aData = myFeature->data();
124 AttributePtr anAttribute = myFeature->attribute(attributeID());
126 // stores the current values of the widget attribute
127 Events_Loop* aLoop = Events_Loop::loop();
128 // blocks the flush signals to avoid the temporary objects visualization in the viewer
129 // they should not be shown in order to do not lose highlight by erasing them
130 bool isActive = aLoop->activateFlushes(false);
132 aData->blockSendAttributeUpdated(true);
133 bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
134 storeAttributeValue();
136 // saves the owner value to the widget attribute
137 aValid = setSelectionCustom(theValue);
139 // checks the attribute validity
140 aValid = isValidAttribute();
142 // restores the current values of the widget attribute
143 restoreAttributeValue(aValid);
144 aData->blockSendAttributeUpdated(false);
145 anAttribute->blockSetInitialized(isAttributeBlocked);
146 aLoop->activateFlushes(isActive);
148 // In particular case the results are deleted and called as redisplayed inside of this
149 // highlight-selection, to they must be flushed as soon as possible.
150 // Example: selection of group-vertices subshapes with shift pressend on body. Without
151 // these 4 lines below the application crashes because of left presentations on
152 // removed results still in the viewer.
153 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
154 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
155 aLoop->flush(aDeletedEvent);
156 aLoop->flush(aRedispEvent);
158 storeValidState(theValue, aValid);
162 //********************************************************************
163 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
168 //********************************************************************
169 bool ModuleBase_WidgetValidated::isValidAttribute() const
171 SessionPtr aMgr = ModelAPI_Session::get();
172 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
173 std::list<ModelAPI_Validator*> aValidators;
174 std::list<std::list<std::string> > anArguments;
175 aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
177 DataPtr aData = myFeature->data();
178 AttributePtr anAttribute = myFeature->attribute(attributeID());
180 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
181 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
183 for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
184 const ModelAPI_AttributeValidator* aAttrValidator =
185 dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
186 if (aAttrValidator) {
187 aValid = aAttrValidator->isValid(anAttribute, *aArgs);
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 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
205 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
207 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
209 aViewer->addSelectionFilter(aSelFilter);
211 aViewer->removeSelectionFilter(aSelFilter);
216 //********************************************************************
217 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
219 bool aValidPrs = myInvalidPrs.contains(theValue);
220 bool anInvalidPrs = myInvalidPrs.contains(theValue);
224 myValidPrs.append(theValue);
225 // the commented code will be useful when the valid state of the presentation
226 // will be changable between activate/deactivate. Currently it does not happen.
228 // myInvalidPrs.removeOne(theValue);
232 myInvalidPrs.append(theValue);
234 // myValidPrs.removeOne(theValue);
236 #ifdef DEBUG_VALID_STATE
237 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
238 .arg(myInvalidPrs.count()).toStdString().c_str());
242 //********************************************************************
243 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
245 bool aValidPrs = myValidPrs.contains(theValue);
246 bool anInvalidPrs = myInvalidPrs.contains(theValue);
250 else if (anInvalidPrs)
253 return aValidPrs || anInvalidPrs;
256 //********************************************************************
257 void ModuleBase_WidgetValidated::clearValidState()
259 #ifdef DEBUG_VALID_STATE
260 qDebug("clearValidState");
263 myInvalidPrs.clear();
266 //********************************************************************
267 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
269 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
270 ModuleBase_ISelection::Viewer);
272 QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
273 ModuleBase_ISelection::Browser);
274 // filter the OB presentations
275 filterPresentations(anOBSelected);
276 if (!anOBSelected.isEmpty())
277 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
282 //********************************************************************
283 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
285 QList<ModuleBase_ViewerPrs> aValidatedValues;
287 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
289 for (; anIt != aLast; anIt++) {
290 if (isValidInFilters(*anIt))
291 aValidatedValues.append(*anIt);
293 if (aValidatedValues.size() != theValues.size()) {
295 theValues = aValidatedValues;