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_WidgetSelectorStore.h>
26 #include <ModuleBase_ViewerPrs.h>
28 #include <Events_InfoMessage.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Validator.h>
32 #include <ModelAPI_AttributeValidator.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_ResultCompSolid.h>
35 #include <ModelAPI_Tools.h>
37 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
38 #include <SelectMgr_EntityOwner.hxx>
39 #include <StdSelect_BRepOwner.hxx>
41 #include <Events_Loop.h>
45 //#define DEBUG_VALID_STATE
47 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
48 ModuleBase_IWorkshop* theWorkshop,
49 const Config_WidgetAPI* theData)
50 : ModuleBase_ModelWidget(theParent, theData),
51 myWorkshop(theWorkshop), myIsInValidate(false)
53 myAttributeStore = new ModuleBase_WidgetSelectorStore();
56 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
58 delete myAttributeStore;
61 //********************************************************************
62 ObjectPtr ModuleBase_WidgetValidated::findPresentedObject(const AISObjectPtr& theAIS) const
64 return myPresentedObject;
67 //********************************************************************
68 void ModuleBase_WidgetValidated::clearValidatedCash()
70 #ifdef DEBUG_VALID_STATE
71 qDebug("clearValidatedCash");
77 //********************************************************************
78 void ModuleBase_WidgetValidated::storeAttributeValue(const AttributePtr& theAttribute)
80 myIsInValidate = true;
81 myAttributeStore->storeAttributeValue(theAttribute, myWorkshop);
84 //********************************************************************
85 void ModuleBase_WidgetValidated::restoreAttributeValue(const AttributePtr& theAttribute,
88 myIsInValidate = false;
89 myAttributeStore->restoreAttributeValue(theAttribute, myWorkshop);
92 //********************************************************************
93 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
96 Handle(SelectMgr_EntityOwner) anOwner = thePrs->owner();
98 // if an owner is null, the selection happens in the Object browser.
99 // creates a selection owner on the base of object shape and the object AIS object
100 if (anOwner.IsNull() && thePrs->owner().IsNull() && thePrs->object().get()) {
101 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
102 GeomShapePtr aShape = aResult.get() ? aResult->shape() : GeomShapePtr();
103 // some results have no shape, e.g. the parameter one. So, they should not be validated
105 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
106 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
107 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
108 myPresentedObject = aResult;
111 aValid = false; // only results with a shape can be filtered
113 // checks the owner by the AIS context activated filters
114 if (!anOwner.IsNull()) {
115 // the widget validator filter should be active, but during check by preselection
116 // it is not yet activated, so we need to activate/deactivate it manually
117 bool isActivated = isFilterActivated();
119 activateFilters(true);
121 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
122 if (!aContext.IsNull()) {
123 const SelectMgr_ListOfFilter& aFilters = aContext->Filters();
124 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
125 for (; anIt.More() && aValid; anIt.Next()) {
126 Handle(SelectMgr_Filter) aFilter = anIt.Value();
127 aValid = aFilter->IsOk(anOwner);
131 activateFilters(false);
134 // removes created owner
135 if (!anOwner.IsNull() && anOwner != thePrs->owner()) {
137 myPresentedObject = ObjectPtr();
142 //********************************************************************
143 AttributePtr ModuleBase_WidgetValidated::attribute() const
145 return myFeature->attribute(attributeID());
148 //********************************************************************
149 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
152 if (getValidState(theValue, aValid)) {
155 aValid = isValidSelectionCustom(theValue);
157 aValid = isValidSelectionForAttribute(theValue, attribute());
159 storeValidState(theValue, aValid);
163 //********************************************************************
164 bool ModuleBase_WidgetValidated::isValidSelectionForAttribute(
165 const ModuleBase_ViewerPrsPtr& theValue,
166 const AttributePtr& theAttribute)
170 // stores the current values of the widget attribute
171 bool isFlushesActived, isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked;
173 blockAttribute(theAttribute, true, isFlushesActived, isAttributeSetInitializedBlocked,
174 isAttributeSendUpdatedBlocked);
176 storeAttributeValue(theAttribute);
178 // saves the owner value to the widget attribute
179 aValid = setSelectionCustom(theValue);
181 // checks the attribute validity
182 aValid = isValidAttribute(theAttribute);
184 // restores the current values of the widget attribute
185 restoreAttributeValue(theAttribute, aValid);
187 blockAttribute(theAttribute, false, isFlushesActived, isAttributeSetInitializedBlocked,
188 isAttributeSendUpdatedBlocked);
189 /// NDS: The following rows are commented for issue #1452 (to be removed after debug)
190 /// This is not correct to perform it here because it might cause update selection and
191 /// the selection mechanizm will be circled: use the scenario of the bug with preselected point.
192 // In particular case the results are deleted and called as redisplayed inside of this
193 // highlight-selection, to they must be flushed as soon as possible.
194 // Example: selection of group-vertices subshapes with shift pressend on body. Without
195 // these 4 lines below the application crashes because of left presentations on
196 // removed results still in the viewer.
197 /*static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
198 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
199 Events_Loop::loop()->flush(aDeletedEvent);
200 Events_Loop::loop()->flush(aRedispEvent);
205 //********************************************************************
206 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
211 //********************************************************************
212 bool ModuleBase_WidgetValidated::isValidAttribute(const AttributePtr& theAttribute)
214 SessionPtr aMgr = ModelAPI_Session::get();
215 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
216 std::string aValidatorID;
217 Events_InfoMessage anError;
218 return aFactory->validate(theAttribute, aValidatorID, anError);
221 bool ModuleBase_WidgetValidated::isFilterActivated() const
223 bool isActivated = false;
225 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
226 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
228 return aViewer->hasSelectionFilter(aSelFilter);
231 bool ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
233 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
235 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
236 bool aHasSelectionFilter = aViewer->hasSelectionFilter(aSelFilter);
239 aViewer->addSelectionFilter(aSelFilter);
241 aViewer->removeSelectionFilter(aSelFilter);
242 clearValidatedCash();
245 return aHasSelectionFilter;
248 //********************************************************************
249 void ModuleBase_WidgetValidated::blockAttribute(const AttributePtr& theAttribute,
250 const bool& theToBlock,
251 bool& isFlushesActived,
252 bool& isAttributeSetInitializedBlocked,
253 bool& isAttributeSendUpdatedBlocked)
255 blockFeatureAttribute(theAttribute, myFeature, theToBlock, isFlushesActived,
256 isAttributeSetInitializedBlocked, isAttributeSendUpdatedBlocked);
259 //********************************************************************
260 void ModuleBase_WidgetValidated::blockFeatureAttribute(const AttributePtr& theAttribute,
261 const FeaturePtr& theFeature,
262 const bool& theToBlock,
263 bool& isFlushesActived,
264 bool& isAttributeSetInitializedBlocked,
265 bool& isAttributeSendUpdatedBlocked)
267 Events_Loop* aLoop = Events_Loop::loop();
268 DataPtr aData = theFeature->data();
270 // blocks the flush signals to avoid the temporary objects visualization in the viewer
271 // they should not be shown in order to do not lose highlight by erasing them
272 isFlushesActived = aLoop->activateFlushes(false);
274 isAttributeSendUpdatedBlocked = aData->blockSendAttributeUpdated(true);
275 isAttributeSetInitializedBlocked = theAttribute->blockSetInitialized(true);
278 aData->blockSendAttributeUpdated(isAttributeSendUpdatedBlocked, false);
279 theAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
280 aLoop->activateFlushes(isFlushesActived);
284 //********************************************************************
285 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrsPtr& theValue,
288 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
291 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
292 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
293 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
294 if (!aValidPrsContains) {
295 #ifdef LIST_OF_VALID_PRS
296 myValidPrs.append(theValue);
298 myValidPrs.Bind(aTDShape, theValue);
300 // the commented code will be useful when the valid state of the presentation
301 // will be changable between activate/deactivate. Currently it does not happen.
303 // myInvalidPrs.removeOne(theValue);
308 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
309 bool anIValidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
310 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
311 if (!anIValidPrsContains) {
312 #ifdef LIST_OF_VALID_PRS
313 myInvalidPrs.append(theValue);
315 myInvalidPrs.Bind(aTDShape, theValue);
318 // myValidPrs.removeOne(theValue);
323 #ifdef DEBUG_VALID_STATE
324 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2")
325 .arg(myValidPrs.count())
326 .arg(myInvalidPrs.count()).toStdString().c_str());
330 //********************************************************************
331 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrsPtr& theValue,
337 #ifdef LIST_OF_VALID_PRS
338 bool aValidPrsContains = myValidPrs.contains(theValue);
339 bool anInvalidPrsContains = myInvalidPrs.contains(theValue);
341 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
345 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
346 bool aValidPrsContains = myValidPrs.IsBound(aTDShape) &&
347 theValue.get()->isEqual(myValidPrs.Find(aTDShape).get());
349 bool anInvalidPrsContains = myInvalidPrs.IsBound(aTDShape) &&
350 theValue.get()->isEqual(myInvalidPrs.Find(aTDShape).get());
352 bool aValidPrsContains = false, anInvalidPrsContains = false;
353 GeomShapePtr aShape = theValue.get() ? theValue->shape() : GeomShapePtr();
355 aValidPrsContains = myValidPrs.contains(aShape);
356 anInvalidPrsContains = myInvalidPrs.contains(aShape);
358 if (aValidPrsContains)
359 aValidPrsContains = theValue == myValidPrs[aShape];
361 anInvalidPrsContains = theValue == myInvalidPrs[aShape];*/
364 if (aValidPrsContains)
366 else if (anInvalidPrsContains)
369 return aValidPrsContains || anInvalidPrsContains;
372 //********************************************************************
373 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetValidated::getFilteredSelected()
375 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
376 ModuleBase_ISelection::Viewer);
378 QList<ModuleBase_ViewerPrsPtr> anOBSelected = myWorkshop->selection()->getSelected(
379 ModuleBase_ISelection::Browser);
380 // filter the OB presentations
381 filterPresentations(anOBSelected);
382 if (!anOBSelected.isEmpty())
383 ModuleBase_ISelection::appendSelected(anOBSelected, aSelected);
385 filterCompSolids(aSelected);
390 //********************************************************************
391 void ModuleBase_WidgetValidated::filterPresentations(QList<ModuleBase_ViewerPrsPtr>& theValues)
393 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
395 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
397 for (; anIt != aLast; anIt++) {
398 if (isValidInFilters(*anIt))
399 aValidatedValues.append(*anIt);
401 if (aValidatedValues.size() != theValues.size()) {
403 theValues = aValidatedValues;
407 //********************************************************************
408 void ModuleBase_WidgetValidated::filterCompSolids(QList<ModuleBase_ViewerPrsPtr>& theValues)
410 std::set<ResultCompSolidPtr> aCompSolids;
411 QList<ModuleBase_ViewerPrsPtr> aValidatedValues;
413 // Collect compsolids.
414 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
415 for (; anIt != aLast; anIt++) {
416 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
417 ObjectPtr anObject = aViewerPrs->object();
418 ResultCompSolidPtr aResultCompSolid =
419 std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(anObject);
420 if(aResultCompSolid.get()) {
421 aCompSolids.insert(aResultCompSolid);
425 // Filter sub-solids of compsolids.
426 anIt = theValues.begin();
427 for (; anIt != aLast; anIt++) {
428 const ModuleBase_ViewerPrsPtr& aViewerPrs = *anIt;
429 ObjectPtr anObject = aViewerPrs->object();
430 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
431 ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aResult);
432 if(aResCompSolidPtr.get() && (aCompSolids.find(aResCompSolidPtr) != aCompSolids.end())) {
433 // Skip sub-solid of compsolid.
436 aValidatedValues.append(*anIt);
440 if (aValidatedValues.size() != theValues.size()) {
442 theValues = aValidatedValues;