1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModuleBase_WidgetValidated.h>
22 #include <ModuleBase_IModule.h>
23 #include <ModuleBase_IViewer.h>
24 #include <ModuleBase_IWorkshop.h>
25 #include <ModuleBase_ISelection.h>
26 #include <ModuleBase_ISelectionActivate.h>
27 #include <ModuleBase_WidgetSelectorStore.h>
28 #include <ModuleBase_ViewerPrs.h>
30 #include <Events_InfoMessage.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_AttributeValidator.h>
35 #include <ModelAPI_Events.h>
36 #include <ModelAPI_ResultCompSolid.h>
37 #include <ModelAPI_Tools.h>
39 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
40 #include <SelectMgr_EntityOwner.hxx>
41 #include <StdSelect_BRepOwner.hxx>
43 #include <Events_Loop.h>
47 //#define DEBUG_VALID_STATE
49 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
50 ModuleBase_IWorkshop* theWorkshop,
51 const Config_WidgetAPI* theData)
52 : ModuleBase_ModelWidget(theParent, theData),
53 myWorkshop(theWorkshop), myIsInValidate(false)
55 myAttributeStore = new ModuleBase_WidgetSelectorStore();
58 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
60 delete myAttributeStore;
63 //********************************************************************
64 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
66 return myPresentedObject;
69 //********************************************************************
70 void ModuleBase_WidgetValidated::deactivate()
75 //********************************************************************
76 void ModuleBase_WidgetValidated::clearValidatedCash()
78 #ifdef DEBUG_VALID_STATE
79 qDebug("clearValidatedCash");
85 //********************************************************************
86 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
88 myIsInValidate = true;
89 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
92 //********************************************************************
93 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
96 myIsInValidate = false;
97 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
100 //********************************************************************
101 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
104 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
106 // if an owner is null, the selection happens in the Object browser.
107 // creates a selection owner on the base of object shape and the object AIS object
108 if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
109 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
110 GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
111 // some results have no shape, e.g. the parameter one. So, they should not be validated
113 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
114 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
115 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
116 myPresentedObject = aResult;
119 aValid = false; // only results with a shape can be filtered
121 // checks the owner by the AIS context activated filters
122 if (!anOwner.IsNull()) {
123 // the widget validator filter should be active, but during check by preselection
124 // it is not yet activated, so we need to activate/deactivate it manually
125 bool isActivated = isFilterActivated();
127 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
128 SelectMgr_ListOfFilter aSelectionFilters;
129 selectionFilters(aModuleSelectionFilters, aSelectionFilters);
130 /// after validation, the selection filters should be restored
131 myWorkshop->selectionActivate()->activateSelectionFilters(aSelectionFilters);
134 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
135 if (!aContext.IsNull()) {
136 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
137 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
138 for (; anIt.More() && aValid; anIt.Next()) {
139 Handle(SelectMgr_Filter) aFilter = anIt.Value();
140 aValid = aFilter->IsOk(anOwner);
145 // reset filters set in activateSelectionFilters above
146 myWorkshop->selectionActivate()->updateSelectionFilters();
147 clearValidatedCash();
151 // removes created owner
152 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
154 myPresentedObject = ObjectPtr();
159 //********************************************************************
160 AttributePtr ModuleBase_WidgetValidated::attribute() const
162 return myFeature->attribute(attributeID());
165 //********************************************************************
166 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
169 if (getValidState(theValue, aValid)) {
172 aValid = isValidSelectionCustom(theValue);
174 aValid = isValidSelectionForAttribute(theValue, attribute());
176 storeValidState(theValue, aValid);
180 //********************************************************************
181 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
182 const ModuleBase_ViewerPrsPtr& theValue,
183 const AttributePtr& theAttribute)
187 // stores the current values of the widget attribute
188 bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
190 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
191 isAttributeSendUpdatedBlocked);
193 storeAttributeValue(theAttribute);
195 // saves the owner value to the widget attribute
196 aValid = setSelectionCustom(theValue);
198 // checks the attribute validity
199 aValid = isValidAttribute(theAttribute);
201 // restores the current values of the widget attribute
202 restoreAttributeValue(theAttribute, aValid);
204 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
205 isAttributeSendUpdatedBlocked);
206 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
207 /// This is not correct to perform it here because it might cause update selection and
208 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
209 // In particular case the results are deleted and called as redisplayed inside of this
210 // highlight-selection, to they must be flushed as soon as possible.
211 // Example: selection of group-vertices subshapes with shift pressend on body. Without
212 // these 4 lines below the application crashes because of left presentations on
213 // removed results still in the viewer.
214 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
215 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
216 Events_Loop::loop()->flush(aDeletedEvent);
217 Events_Loop::loop()->flush(aRedispEvent);
222 //********************************************************************
223 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
228 //********************************************************************
229 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
231 SessionPtr aMgr = ModelAPI_Session::get();
232 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
233 std::string aValidatorID;
234 Events_InfoMessage anError;
235 return aFactory->validate(theAttribute, aValidatorID, anError);
238 //********************************************************************
239 bool ModuleBase_WidgetValidated::isFilterActivated() const
241 bool isActivated = false;
243 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
244 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
246 return aViewer->hasSelectionFilter(aSelFilter);
249 //********************************************************************
250 void ModuleBase_WidgetValidated::selectionFilters(QIntList& theModuleSelectionFilters,
251 SelectMgr_ListOfFilter& theSelectionFilters)
253 theSelectionFilters.Append(myWorkshop->validatorFilter());
256 //********************************************************************
257 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
258 const bool& theToBlock,
259 bool& isFlushesActived,
260 bool& isAttributeSetInitializedBlocked,
261 bool& isAttributeSendUpdatedBlocked)
263 blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
264 isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
267 //********************************************************************
268 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
269 const FeaturePtr& theFeature,
270 const bool& theToBlock,
271 bool& isFlushesActived,
272 bool& isAttributeSetInitializedBlocked,
273 bool& isAttributeSendUpdatedBlocked)
275 Events_Loop* aLoop = Events_Loop::loop();
276 DataPtr aData = theFeature->data();
278 // blocks the flush signals to avoid the temporary objects visualization in the viewer
279 // they should not be shown in order to do not lose highlight by erasing them
280 isFlushesActived = aLoop->activateFlushes(false);
282 isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
283 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
286 aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
287 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
288 aLoop->activateFlushes(isFlushesActived);
292 //********************************************************************
293 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
296 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
299 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
300 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
301 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
302 if (!aValidPrsContains) {
303 #ifdef LIST_OF_VALID_PRS
304 myValidPrs.append(theValue);
306 myValidPrs.Bind(aTDShape, theValue);
308 // the commented code will be useful when the valid state of the presentation
309 // will be changable between activate/deactivate. Currently it does not happen.
311 // myInvalidPrs.removeOne(theValue);
316 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
317 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
318 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
319 if (!anIValidPrsContains) {
320 #ifdef LIST_OF_VALID_PRS
321 myInvalidPrs.append(theValue);
323 myInvalidPrs.Bind(aTDShape, theValue);
326 // myValidPrs.removeOne(theValue);
331 #ifdef DEBUG_VALID_STATE
332 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
333 .arg(myValidPrs.count())
334 .arg(myInvalidPrs.count()).toStdString().c_str());
338 //********************************************************************
339 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
345 #ifdef LIST_OF_VALID_PRS
346 bool aValidPrsContains = myValidPrs.contains(theValue);
347 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
349 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
353 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
354 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
355 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
357 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
358 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
360 bool aValidPrsContains = false, anInvalidPrsContains = false;
361 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
363 aValidPrsContains = myValidPrs.contains(aShape);
364 anInvalidPrsContains = myInvalidPrs.contains(aShape);
366 if (aValidPrsContains)
367 aValidPrsContains = theValue == myValidPrs[aShape];
369 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
372 if (aValidPrsContains)
374 else if (anInvalidPrsContains)
377 return aValidPrsContains || anInvalidPrsContains;
380 //********************************************************************
381 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
383 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
384 ModuleBase_ISelection::Viewer);
386 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
387 ModuleBase_ISelection::Browser);
388 // filter the OB presentations
389 filterPresentations(anOBSelected);
390 if (!anOBSelected.isEmpty())
391 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
393 filterCompSolids(aSelected);
398 //********************************************************************
399 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
401 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
403 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
405 for (; anIt != aLast; anIt++) {
406 if (isValidInFilters(*anIt))
407 aValidatedValues.append(*anIt);
409 if (aValidatedValues.size() != theValues.size()) {
411 theValues = aValidatedValues;
415 //********************************************************************
416 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
418 std::set<ResultCompSolidPtr> aCompSolids;
419 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
421 // Collect compsolids.
422 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
423 for (; anIt != aLast; anIt++) {
424 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
425 ObjectPtr anObject = aViewerPrs->object();
426 ResultCompSolidPtr aResultCompSolid =
427 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
428 if(aResultCompSolid.get()) {
429 aCompSolids.insert(aResultCompSolid);
433 // Filter sub-solids of compsolids.
434 anIt = theValues.begin();
435 for (; anIt != aLast; anIt++) {
436 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
437 ObjectPtr anObject = aViewerPrs->object();
438 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
439 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
440 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
441 // Skip sub-solid of compsolid.
444 aValidatedValues.append(*anIt);
448 if (aValidatedValues.size() != theValues.size()) {
450 theValues = aValidatedValues;