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::clearValidatedCash()
70 //********************************************************************
71 void ModuleBase_WidgetValidated::storeAttributeValue()
73 myIsInValidate = true;
76 //********************************************************************
77 void ModuleBase_WidgetValidated::restoreAttributeValue(const bool theValid)
79 myIsInValidate = false;
82 //********************************************************************
83 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
86 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
88 // if an owner is null, the selection happens in the Object browser.
89 // creates a selection owner on the base of object shape and the object AIS object
90 if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
91 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
92 if (aResult.get() && aResult->shape().get()) {
93 // some results have no shape, e.g. the parameter one. So, they should not be validated
94 GeomShapePtr aShape = aResult->shape();
95 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
96 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
97 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
98 myPresentedObject = aResult;
101 aValid = false; // only results with a shape can be filtered
103 // checks the owner by the AIS context activated filters
104 if (!anOwner.IsNull()) {
105 // the widget validator filter should be active, but during check by preselection
106 // it is not yet activated, so we need to activate/deactivate it manually
107 bool isActivated = isFilterActivated();
109 activateFilters(true);
111 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
112 if (!aContext.IsNull()) {
113 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
114 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
115 for (; anIt.More() && aValid; anIt.Next()) {
116 Handle(SelectMgr_Filter) aFilter = anIt.Value();
117 aValid = aFilter->IsOk(anOwner);
121 activateFilters(false);
124 // removes created owner
125 if (!anOwner.IsNull() && anOwner != thePrs.owner()) {
127 myPresentedObject = ObjectPtr();
132 //********************************************************************
133 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
136 if (getValidState(theValue, aValid)) {
140 aValid = isValidSelectionCustom(theValue);
142 storeValidState(theValue, aValid);
146 // stores the current values of the widget attribute
147 bool isFlushesActived, isAttributeSetInitializedBlocked;
148 blockAttribute(true, isFlushesActived, isAttributeSetInitializedBlocked);
150 storeAttributeValue();
152 // saves the owner value to the widget attribute
153 aValid = setSelectionCustom(theValue);
155 // checks the attribute validity
156 aValid = isValidAttribute();
158 // restores the current values of the widget attribute
159 restoreAttributeValue(aValid);
161 blockAttribute(false, isFlushesActived, isAttributeSetInitializedBlocked);
162 // In particular case the results are deleted and called as redisplayed inside of this
163 // highlight-selection, to they must be flushed as soon as possible.
164 // Example: selection of group-vertices subshapes with shift pressend on body. Without
165 // these 4 lines below the application crashes because of left presentations on
166 // removed results still in the viewer.
167 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
168 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
169 Events_Loop::loop()->flush(aDeletedEvent);
170 Events_Loop::loop()->flush(aRedispEvent);
172 storeValidState(theValue, aValid);
176 //********************************************************************
177 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
182 //********************************************************************
183 bool ModuleBase_WidgetValidated::isValidAttribute() const
185 SessionPtr aMgr = ModelAPI_Session::get();
186 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
187 AttributePtr anAttribute = myFeature->attribute(attributeID());
188 std::string aValidatorID, anError;
189 return aFactory->validate(anAttribute, aValidatorID, anError);
192 bool ModuleBase_WidgetValidated::isFilterActivated() const
194 bool isActivated = false;
196 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
197 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
199 return aViewer->hasSelectionFilter(aSelFilter);
202 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
204 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
206 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
208 aViewer->addSelectionFilter(aSelFilter);
210 aViewer->removeSelectionFilter(aSelFilter);
215 //********************************************************************
216 void ModuleBase_WidgetValidated::blockAttribute(const bool& theToBlock, bool& isFlushesActived,
217 bool& isAttributeSetInitializedBlocked)
219 Events_Loop* aLoop = Events_Loop::loop();
220 DataPtr aData = myFeature->data();
221 AttributePtr anAttribute = myFeature->attribute(attributeID());
223 // blocks the flush signals to avoid the temporary objects visualization in the viewer
224 // they should not be shown in order to do not lose highlight by erasing them
225 isFlushesActived = aLoop->activateFlushes(false);
227 aData->blockSendAttributeUpdated(true);
228 isAttributeSetInitializedBlocked = anAttribute->blockSetInitialized(true);
231 aData->blockSendAttributeUpdated(false);
232 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
233 aLoop->activateFlushes(isFlushesActived);
237 //********************************************************************
238 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
240 bool aValidPrs = myInvalidPrs.contains(theValue);
241 bool anInvalidPrs = myInvalidPrs.contains(theValue);
245 myValidPrs.append(theValue);
246 // the commented code will be useful when the valid state of the presentation
247 // will be changable between activate/deactivate. Currently it does not happen.
249 // myInvalidPrs.removeOne(theValue);
253 myInvalidPrs.append(theValue);
255 // myValidPrs.removeOne(theValue);
257 #ifdef DEBUG_VALID_STATE
258 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
259 .arg(myInvalidPrs.count()).toStdString().c_str());
263 //********************************************************************
264 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
266 bool aValidPrs = myValidPrs.contains(theValue);
267 bool anInvalidPrs = myInvalidPrs.contains(theValue);
271 else if (anInvalidPrs)
274 return aValidPrs || anInvalidPrs;
277 //********************************************************************
278 void ModuleBase_WidgetValidated::clearValidState()
280 #ifdef DEBUG_VALID_STATE
281 qDebug("clearValidState");
284 myInvalidPrs.clear();
287 //********************************************************************
288 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getFilteredSelected()
290 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
291 ModuleBase_ISelection::Viewer);
293 QList<ModuleBase_ViewerPrs> anOBSelected = myWorkshop->selection()->getSelected(
294 ModuleBase_ISelection::Browser);
295 // filter the OB presentations
296 filterPresentations(anOBSelected);
297 if (!anOBSelected.isEmpty())
298 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
303 //********************************************************************
304 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrs>& theValues)
306 QList<ModuleBase_ViewerPrs> aValidatedValues;
308 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
310 for (; anIt != aLast; anIt++) {
311 if (isValidInFilters(*anIt))
312 aValidatedValues.append(*anIt);
314 if (aValidatedValues.size() != theValues.size()) {
316 theValues = aValidatedValues;