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_IViewer.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_ISelection.h>
25 #include <ModuleBase_ISelectionActivate.h>
26 #include <ModuleBase_WidgetSelectorStore.h>
27 #include <ModuleBase_ViewerPrs.h>
29 #include <Events_InfoMessage.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Validator.h>
33 #include <ModelAPI_AttributeValidator.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_ResultCompSolid.h>
36 #include <ModelAPI_Tools.h>
38 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
39 #include <SelectMgr_EntityOwner.hxx>
40 #include <StdSelect_BRepOwner.hxx>
42 #include <Events_Loop.h>
46 //#define DEBUG_VALID_STATE
48 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
49 ModuleBase_IWorkshop* theWorkshop,
50 const Config_WidgetAPI* theData)
51 : ModuleBase_ModelWidget(theParent, theData),
52 myWorkshop(theWorkshop), myIsInValidate(false)
54 myAttributeStore = new ModuleBase_WidgetSelectorStore();
57 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
59 delete myAttributeStore;
62 //********************************************************************
63 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
65 return myPresentedObject;
68 //********************************************************************
69 void ModuleBase_WidgetValidated::deactivate()
74 //********************************************************************
75 void ModuleBase_WidgetValidated::clearValidatedCash()
77 #ifdef DEBUG_VALID_STATE
78 qDebug("clearValidatedCash");
84 //********************************************************************
85 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
87 myIsInValidate = true;
88 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
91 //********************************************************************
92 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
95 myIsInValidate = false;
96 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
99 //********************************************************************
100 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
103 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
105 // if an owner is null, the selection happens in the Object browser.
106 // creates a selection owner on the base of object shape and the object AIS object
107 if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
108 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
109 GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
110 // some results have no shape, e.g. the parameter one. So, they should not be validated
112 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
113 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
114 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
115 myPresentedObject = aResult;
118 aValid = false; // only results with a shape can be filtered
120 // checks the owner by the AIS context activated filters
121 if (!anOwner.IsNull()) {
122 // the widget validator filter should be active, but during check by preselection
123 // it is not yet activated, so we need to activate/deactivate it manually
124 bool isActivated = isFilterActivated();
126 SelectMgr_ListOfFilter aSelectionFilters;
127 selectionFilters(aSelectionFilters);
128 /// after validation, the selection filters should be restored
129 myWorkshop->selectionActivate()->activateSelectionFilters(aSelectionFilters);
132 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
133 if (!aContext.IsNull()) {
134 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
135 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
136 for (; anIt.More() && aValid; anIt.Next()) {
137 Handle(SelectMgr_Filter) aFilter = anIt.Value();
138 aValid = aFilter->IsOk(anOwner);
143 myWorkshop->selectionActivate()->updateSelectionFilters();
144 clearValidatedCash();
148 // removes created owner
149 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
151 myPresentedObject = ObjectPtr();
156 //********************************************************************
157 AttributePtr ModuleBase_WidgetValidated::attribute() const
159 return myFeature->attribute(attributeID());
162 //********************************************************************
163 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
166 if (getValidState(theValue, aValid)) {
169 aValid = isValidSelectionCustom(theValue);
171 aValid = isValidSelectionForAttribute(theValue, attribute());
173 storeValidState(theValue, aValid);
177 //********************************************************************
178 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
179 const ModuleBase_ViewerPrsPtr& theValue,
180 const AttributePtr& theAttribute)
184 // stores the current values of the widget attribute
185 bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
187 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
188 isAttributeSendUpdatedBlocked);
190 storeAttributeValue(theAttribute);
192 // saves the owner value to the widget attribute
193 aValid = setSelectionCustom(theValue);
195 // checks the attribute validity
196 aValid = isValidAttribute(theAttribute);
198 // restores the current values of the widget attribute
199 restoreAttributeValue(theAttribute, aValid);
201 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
202 isAttributeSendUpdatedBlocked);
203 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
204 /// This is not correct to perform it here because it might cause update selection and
205 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
206 // In particular case the results are deleted and called as redisplayed inside of this
207 // highlight-selection, to they must be flushed as soon as possible.
208 // Example: selection of group-vertices subshapes with shift pressend on body. Without
209 // these 4 lines below the application crashes because of left presentations on
210 // removed results still in the viewer.
211 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
212 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
213 Events_Loop::loop()->flush(aDeletedEvent);
214 Events_Loop::loop()->flush(aRedispEvent);
219 //********************************************************************
220 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
225 //********************************************************************
226 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
228 SessionPtr aMgr = ModelAPI_Session::get();
229 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
230 std::string aValidatorID;
231 Events_InfoMessage anError;
232 return aFactory->validate(theAttribute, aValidatorID, anError);
235 //********************************************************************
236 bool ModuleBase_WidgetValidated::isFilterActivated() const
238 bool isActivated = false;
240 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
241 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
243 return aViewer->hasSelectionFilter(aSelFilter);
246 //********************************************************************
247 void ModuleBase_WidgetValidated::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
249 theSelectionFilters.Append(myWorkshop->validatorFilter());
252 //********************************************************************
253 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
254 const bool& theToBlock,
255 bool& isFlushesActived,
256 bool& isAttributeSetInitializedBlocked,
257 bool& isAttributeSendUpdatedBlocked)
259 blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
260 isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
263 //********************************************************************
264 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
265 const FeaturePtr& theFeature,
266 const bool& theToBlock,
267 bool& isFlushesActived,
268 bool& isAttributeSetInitializedBlocked,
269 bool& isAttributeSendUpdatedBlocked)
271 Events_Loop* aLoop = Events_Loop::loop();
272 DataPtr aData = theFeature->data();
274 // blocks the flush signals to avoid the temporary objects visualization in the viewer
275 // they should not be shown in order to do not lose highlight by erasing them
276 isFlushesActived = aLoop->activateFlushes(false);
278 isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
279 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
282 aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
283 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
284 aLoop->activateFlushes(isFlushesActived);
288 //********************************************************************
289 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
292 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
295 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
296 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
297 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
298 if (!aValidPrsContains) {
299 #ifdef LIST_OF_VALID_PRS
300 myValidPrs.append(theValue);
302 myValidPrs.Bind(aTDShape, theValue);
304 // the commented code will be useful when the valid state of the presentation
305 // will be changable between activate/deactivate. Currently it does not happen.
307 // myInvalidPrs.removeOne(theValue);
312 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
313 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
314 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
315 if (!anIValidPrsContains) {
316 #ifdef LIST_OF_VALID_PRS
317 myInvalidPrs.append(theValue);
319 myInvalidPrs.Bind(aTDShape, theValue);
322 // myValidPrs.removeOne(theValue);
327 #ifdef DEBUG_VALID_STATE
328 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
329 .arg(myValidPrs.count())
330 .arg(myInvalidPrs.count()).toStdString().c_str());
334 //********************************************************************
335 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
341 #ifdef LIST_OF_VALID_PRS
342 bool aValidPrsContains = myValidPrs.contains(theValue);
343 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
345 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
349 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
350 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
351 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
353 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
354 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
356 bool aValidPrsContains = false, anInvalidPrsContains = false;
357 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
359 aValidPrsContains = myValidPrs.contains(aShape);
360 anInvalidPrsContains = myInvalidPrs.contains(aShape);
362 if (aValidPrsContains)
363 aValidPrsContains = theValue == myValidPrs[aShape];
365 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
368 if (aValidPrsContains)
370 else if (anInvalidPrsContains)
373 return aValidPrsContains || anInvalidPrsContains;
376 //********************************************************************
377 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
379 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
380 ModuleBase_ISelection::Viewer);
382 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
383 ModuleBase_ISelection::Browser);
384 // filter the OB presentations
385 filterPresentations(anOBSelected);
386 if (!anOBSelected.isEmpty())
387 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
389 filterCompSolids(aSelected);
394 //********************************************************************
395 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
397 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
399 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
401 for (; anIt != aLast; anIt++) {
402 if (isValidInFilters(*anIt))
403 aValidatedValues.append(*anIt);
405 if (aValidatedValues.size() != theValues.size()) {
407 theValues = aValidatedValues;
411 //********************************************************************
412 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
414 std::set<ResultCompSolidPtr> aCompSolids;
415 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
417 // Collect compsolids.
418 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
419 for (; anIt != aLast; anIt++) {
420 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
421 ObjectPtr anObject = aViewerPrs->object();
422 ResultCompSolidPtr aResultCompSolid =
423 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
424 if(aResultCompSolid.get()) {
425 aCompSolids.insert(aResultCompSolid);
429 // Filter sub-solids of compsolids.
430 anIt = theValues.begin();
431 for (; anIt != aLast; anIt++) {
432 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
433 ObjectPtr anObject = aViewerPrs->object();
434 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
435 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
436 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
437 // Skip sub-solid of compsolid.
440 aValidatedValues.append(*anIt);
444 if (aValidatedValues.size() != theValues.size()) {
446 theValues = aValidatedValues;