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), myIsInValidate(false)
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 void ModuleBase_WidgetValidated::storeAttributeValue()
66 myIsInValidate = true;
69 //********************************************************************
70 void ModuleBase_WidgetValidated::restoreAttributeValue(const bool theValid)
72 myIsInValidate = false;
75 //********************************************************************
76 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
79 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
81 // if an owner is null, the selection happens in the Object browser.
82 // creates a selection owner on the base of object shape and the object AIS object
83 if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
84 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
85 if (aResult.get() && aResult->shape().get()) {
86 // some results have no shape, e.g. the parameter one. So, they should not be validated
87 GeomShapePtr aShape = aResult->shape();
88 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
89 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
90 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
91 myPresentedObject = aResult;
94 aValid = false; // only results with a shape can be filtered
96 // checks the owner by the AIS context activated filters
97 if (!anOwner.IsNull()) {
98 // the widget validator filter should be active, but during check by preselection
99 // it is not yet activated, so we need to activate/deactivate it manually
100 bool isActivated = isFilterActivated();
102 activateFilters(true);
104 const SelectMgr_ListOfFilter& aFilters = myWorkshop->viewer()->AISContext()->Filters();
105 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
106 for (; anIt.More() && aValid; anIt.Next()) {
107 Handle(SelectMgr_Filter) aFilter = anIt.Value();
108 aValid = aFilter->IsOk(anOwner);
111 activateFilters(false);
114 // removes created owner
115 if (!anOwner.IsNull() && anOwner != thePrs.owner()) {
117 myPresentedObject = ObjectPtr();
122 //********************************************************************
123 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
126 if (getValidState(theValue, aValid)) {
130 aValid = isValidSelectionCustom(theValue);
132 storeValidState(theValue, aValid);
136 DataPtr aData = myFeature->data();
137 AttributePtr anAttribute = myFeature->attribute(attributeID());
139 // stores the current values of the widget attribute
140 Events_Loop* aLoop = Events_Loop::loop();
141 // blocks the flush signals to avoid the temporary objects visualization in the viewer
142 // they should not be shown in order to do not lose highlight by erasing them
143 bool isActive = aLoop->activateFlushes(false);
145 aData->blockSendAttributeUpdated(true);
146 bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
147 storeAttributeValue();
149 // saves the owner value to the widget attribute
150 aValid = setSelectionCustom(theValue);
152 // checks the attribute validity
153 aValid = isValidAttribute();
155 // restores the current values of the widget attribute
156 restoreAttributeValue(aValid);
157 aData->blockSendAttributeUpdated(false);
158 anAttribute->blockSetInitialized(isAttributeBlocked);
159 aLoop->activateFlushes(isActive);
161 // In particular case the results are deleted and called as redisplayed inside of this
162 // highlight-selection, to they must be flushed as soon as possible.
163 // Example: selection of group-vertices subshapes with shift pressend on body. Without
164 // these 4 lines below the application crashes because of left presentations on
165 // removed results still in the viewer.
166 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
167 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
168 aLoop->flush(aDeletedEvent);
169 aLoop->flush(aRedispEvent);
171 storeValidState(theValue, aValid);
175 //********************************************************************
176 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
181 //********************************************************************
182 bool ModuleBase_WidgetValidated::isValidAttribute() const
184 SessionPtr aMgr = ModelAPI_Session::get();
185 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
186 AttributePtr anAttribute = myFeature->attribute(attributeID());
187 std::string aValidatorID, anError;
188 return aFactory->validate(anAttribute, aValidatorID, anError);
191 bool ModuleBase_WidgetValidated::isFilterActivated() const
193 bool isActivated = false;
195 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
196 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
198 return aViewer->hasSelectionFilter(aSelFilter);
201 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
203 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
205 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
207 aViewer->addSelectionFilter(aSelFilter);
209 aViewer->removeSelectionFilter(aSelFilter);
214 //********************************************************************
215 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
217 bool aValidPrs = myInvalidPrs.contains(theValue);
218 bool anInvalidPrs = myInvalidPrs.contains(theValue);
222 myValidPrs.append(theValue);
223 // the commented code will be useful when the valid state of the presentation
224 // will be changable between activate/deactivate. Currently it does not happen.
226 // myInvalidPrs.removeOne(theValue);
230 myInvalidPrs.append(theValue);
232 // myValidPrs.removeOne(theValue);
234 #ifdef DEBUG_VALID_STATE
235 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
236 .arg(myInvalidPrs.count()).toStdString().c_str());
240 //********************************************************************
241 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
243 bool aValidPrs = myValidPrs.contains(theValue);
244 bool anInvalidPrs = myInvalidPrs.contains(theValue);
248 else if (anInvalidPrs)
251 return aValidPrs || anInvalidPrs;
254 //********************************************************************
255 void ModuleBase_WidgetValidated::clearValidState()
257 #ifdef DEBUG_VALID_STATE
258 qDebug("clearValidState");
261 myInvalidPrs.clear();
264 //********************************************************************
265 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
267 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
268 ModuleBase_ISelection::Viewer);
270 QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
271 ModuleBase_ISelection::Browser);
272 // filter the OB presentations
273 filterPresentations(anOBSelected);
274 if (!anOBSelected.isEmpty())
275 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
280 //********************************************************************
281 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
283 QList<ModuleBase_ViewerPrs> aValidatedValues;
285 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
287 for (; anIt != aLast; anIt++) {
288 if (isValidInFilters(*anIt))
289 aValidatedValues.append(*anIt);
291 if (aValidatedValues.size() != theValues.size()) {
293 theValues = aValidatedValues;