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), isValidateBlocked(false),
28 myWorkshop(theWorkshop)
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 bool ModuleBase_WidgetValidated::isValidInFilters(const ModuleBase_ViewerPrs& thePrs)
61 Handle(SelectMgr_EntityOwner) anOwner = thePrs.owner();
63 // if an owern is null, the selection happens in the Object browser.
64 // creates a selection owner on the base of object shape and the object AIS object
65 if (anOwner.IsNull() && thePrs.owner().IsNull() && thePrs.object().get()) {
66 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
68 GeomShapePtr aShape = aResult->shape();
70 const TopoDS_Shape aTDShape = aShape->impl<TopoDS_Shape>();
71 Handle(AIS_InteractiveObject) anIO = myWorkshop->selection()->getIO(thePrs);
72 anOwner = new StdSelect_BRepOwner(aTDShape, anIO);
76 if (!anOwner.IsNull()) {
77 const SelectMgr_ListOfFilter& aFilters = myWorkshop->viewer()->AISContext()->Filters();
78 SelectMgr_ListIteratorOfListOfFilter anIt(aFilters);
79 for (; anIt.More() && aValid; anIt.Next()) {
80 Handle(SelectMgr_Filter) aFilter = anIt.Value();
81 //if (aFilter == myWorkshop->validatorFilter())
83 aValid = aFilter->IsOk(anOwner);
86 // removes created owner
87 if (!anOwner.IsNull() && anOwner != thePrs.owner())
92 //********************************************************************
93 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
96 if (getValidState(theValue, aValid)) {
100 aValid = isValidSelectionCustom(theValue);
102 storeValidState(theValue, aValid);
106 if (isValidateBlocked)
108 isValidateBlocked = true;
110 DataPtr aData = myFeature->data();
111 AttributePtr anAttribute = myFeature->attribute(attributeID());
113 // stores the current values of the widget attribute
114 Events_Loop* aLoop = Events_Loop::loop();
115 // blocks the flush signals to avoid the temporary objects visualization in the viewer
116 // they should not be shown in order to do not lose highlight by erasing them
117 bool isActive = aLoop->activateFlushes(false);
119 aData->blockSendAttributeUpdated(true);
120 bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
121 storeAttributeValue();
123 // saves the owner value to the widget attribute
124 aValid = setSelectionCustom(theValue);
126 // checks the attribute validity
127 aValid = isValidAttribute();
129 // restores the current values of the widget attribute
130 restoreAttributeValue(aValid);
131 aData->blockSendAttributeUpdated(false);
132 anAttribute->blockSetInitialized(isAttributeBlocked);
133 aLoop->activateFlushes(isActive);
135 // In particular case the results are deleted and called as redisplayed inside of this
136 // highlight-selection, to they must be flushed as soon as possible.
137 // Example: selection of group-vertices subshapes with shift pressend on body. Without
138 // these 4 lines below the application crashes because of left presentations on
139 // removed results still in the viewer.
140 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
141 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
142 aLoop->flush(aDeletedEvent);
143 aLoop->flush(aRedispEvent);
145 storeValidState(theValue, aValid);
146 isValidateBlocked = false;
150 //********************************************************************
151 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
156 //********************************************************************
157 bool ModuleBase_WidgetValidated::isValidAttribute() const
159 SessionPtr aMgr = ModelAPI_Session::get();
160 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
161 std::list<ModelAPI_Validator*> aValidators;
162 std::list<std::list<std::string> > anArguments;
163 aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
165 DataPtr aData = myFeature->data();
166 AttributePtr anAttribute = myFeature->attribute(attributeID());
168 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
169 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
171 for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
172 const ModelAPI_AttributeValidator* aAttrValidator =
173 dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
174 if (aAttrValidator) {
175 aValid = aAttrValidator->isValid(anAttribute, *aArgs);
181 void ModuleBase_WidgetValidated::activateFilters(const bool toActivate)
183 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
185 Handle(SelectMgr_Filter) aSelFilter = myWorkshop->validatorFilter();
187 aViewer->addSelectionFilter(aSelFilter);
189 aViewer->removeSelectionFilter(aSelFilter);
194 //********************************************************************
195 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
197 bool aValidPrs = myInvalidPrs.contains(theValue);
198 bool anInvalidPrs = myInvalidPrs.contains(theValue);
202 myValidPrs.append(theValue);
203 // the commented code will be useful when the valid state of the presentation
204 // will be changable between activate/deactivate. Currently it does not happen.
206 // myInvalidPrs.removeOne(theValue);
210 myInvalidPrs.append(theValue);
212 // myValidPrs.removeOne(theValue);
214 #ifdef DEBUG_VALID_STATE
215 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
216 .arg(myInvalidPrs.count()).toStdString().c_str());
220 //********************************************************************
221 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
223 bool aValidPrs = myValidPrs.contains(theValue);
224 bool anInvalidPrs = myInvalidPrs.contains(theValue);
228 else if (anInvalidPrs)
231 return aValidPrs || anInvalidPrs;
234 //********************************************************************
235 void ModuleBase_WidgetValidated::clearValidState()
237 #ifdef DEBUG_VALID_STATE
238 qDebug("clearValidState");
241 myInvalidPrs.clear();