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()) {
41 // In order to make reselection possible, set empty object and shape should be done
42 setSelectionCustom(ModuleBase_ViewerPrs());
45 // it removes the processed value from the parameters list
46 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
49 if (!theToValidate || isValidInFilters(aValue)) {
50 isDone = setSelectionCustom(aValue);
51 // updateObject - to update/redisplay feature
52 // it is commented in order to perfom it outside the method
53 //updateObject(myFeature);
55 //emit valuesChanged();
60 //********************************************************************
61 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
63 return myPresentedObject;
66 //********************************************************************
67 void ModuleBase_WidgetValidated::clearValidatedCash()
73 //********************************************************************
74 void ModuleBase_WidgetValidated::storeAttributeValue()
76 myIsInValidate = true;
79 //********************************************************************
80 void ModuleBase_WidgetValidated::restoreAttributeValue(const bool theValid)
82 myIsInValidate = false;
85 //********************************************************************
86 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
89 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
91 // if an owner is null, the selection happens in the Object browser.
92 // creates a selection owner on the base of object shape and the object AIS object
93 if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
94 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
95 if (aResult.get() && aResult->shape().get()) {
96 // some results have no shape, e.g. the parameter one. So, they should not be validated
97 GeomShapePtr aShape = aResult->shape();
98 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
99 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
100 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
101 myPresentedObject = aResult;
104 aValid = false; // only results with a shape can be filtered
106 // checks the owner by the AIS context activated filters
107 if (!anOwner.IsNull()) {
108 // the widget validator filter should be active, but during check by preselection
109 // it is not yet activated, so we need to activate/deactivate it manually
110 bool isActivated = isFilterActivated();
112 activateFilters(true);
114 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
115 if (!aContext.IsNull()) {
116 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
117 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
118 for (; anIt.More() && aValid; anIt.Next()) {
119 Handle(SelectMgr_Filter) aFilter = anIt.Value();
120 aValid = aFilter->IsOk(anOwner);
124 activateFilters(false);
127 // removes created owner
128 if (!anOwner.IsNull() && anOwner != thePrs.owner()) {
130 myPresentedObject = ObjectPtr();
135 //********************************************************************
136 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
139 if (getValidState(theValue, aValid)) {
143 aValid = isValidSelectionCustom(theValue);
145 storeValidState(theValue, aValid);
149 // stores the current values of the widget attribute
150 bool isFlushesActived, isAttributeSetInitializedBlocked;
151 blockAttribute(true, isFlushesActived, isAttributeSetInitializedBlocked);
153 storeAttributeValue();
155 // saves the owner value to the widget attribute
156 aValid = setSelectionCustom(theValue);
158 // checks the attribute validity
159 aValid = isValidAttribute();
161 // restores the current values of the widget attribute
162 restoreAttributeValue(aValid);
164 blockAttribute(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);
175 storeValidState(theValue, aValid);
179 //********************************************************************
180 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
185 //********************************************************************
186 bool ModuleBase_WidgetValidated::isValidAttribute() const
188 SessionPtr aMgr = ModelAPI_Session::get();
189 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
190 AttributePtr anAttribute = myFeature->attribute(attributeID());
191 std::string aValidatorID, anError;
192 return aFactory->validate(anAttribute, aValidatorID, anError);
195 bool ModuleBase_WidgetValidated::isFilterActivated() const
197 bool isActivated = false;
199 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
200 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
202 return aViewer->hasSelectionFilter(aSelFilter);
205 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
207 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
209 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
211 aViewer->addSelectionFilter(aSelFilter);
213 aViewer->removeSelectionFilter(aSelFilter);
218 //********************************************************************
219 void ModuleBase_WidgetValidated::blockAttribute(const bool& theToBlock, bool& isFlushesActived,
220 bool& isAttributeSetInitializedBlocked)
222 Events_Loop* aLoop = Events_Loop::loop();
223 DataPtr aData = myFeature->data();
224 AttributePtr anAttribute = myFeature->attribute(attributeID());
226 // blocks the flush signals to avoid the temporary objects visualization in the viewer
227 // they should not be shown in order to do not lose highlight by erasing them
228 isFlushesActived = aLoop->activateFlushes(false);
230 aData->blockSendAttributeUpdated(true);
231 isAttributeSetInitializedBlocked = anAttribute->blockSetInitialized(true);
234 aData->blockSendAttributeUpdated(false);
235 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
236 aLoop->activateFlushes(isFlushesActived);
240 //********************************************************************
241 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
243 bool aValidPrs = myInvalidPrs.contains(theValue);
244 bool anInvalidPrs = myInvalidPrs.contains(theValue);
248 myValidPrs.append(theValue);
249 // the commented code will be useful when the valid state of the presentation
250 // will be changable between activate/deactivate. Currently it does not happen.
252 // myInvalidPrs.removeOne(theValue);
256 myInvalidPrs.append(theValue);
258 // myValidPrs.removeOne(theValue);
260 #ifdef DEBUG_VALID_STATE
261 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
262 .arg(myInvalidPrs.count()).toStdString().c_str());
266 //********************************************************************
267 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
269 bool aValidPrs = myValidPrs.contains(theValue);
270 bool anInvalidPrs = myInvalidPrs.contains(theValue);
274 else if (anInvalidPrs)
277 return aValidPrs || anInvalidPrs;
280 //********************************************************************
281 void ModuleBase_WidgetValidated::clearValidState()
283 #ifdef DEBUG_VALID_STATE
284 qDebug("clearValidState");
287 myInvalidPrs.clear();
290 //********************************************************************
291 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
293 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
294 ModuleBase_ISelection::Viewer);
296 QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
297 ModuleBase_ISelection::Browser);
298 // filter the OB presentations
299 filterPresentations(anOBSelected);
300 if (!anOBSelected.isEmpty())
301 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
306 //********************************************************************
307 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
309 QList<ModuleBase_ViewerPrs> aValidatedValues;
311 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
313 for (; anIt != aLast; anIt++) {
314 if (isValidInFilters(*anIt))
315 aValidatedValues.append(*anIt);
317 if (aValidatedValues.size() != theValues.size()) {
319 theValues = aValidatedValues;