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>
16 #include <Events_Loop.h>
20 //#define DEBUG_VALID_STATE
22 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
23 const Config_WidgetAPI* theData,
24 const std::string& theParentId)
25 : ModuleBase_ModelWidget(theParent, theData, theParentId)
29 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
33 //********************************************************************
34 bool ModuleBase_WidgetValidated::setSelection(QList<ModuleBase_ViewerPrs>& theValues)
36 if (theValues.empty())
38 // it removes the processed value from the parameters list
39 ModuleBase_ViewerPrs aValue = theValues.takeFirst();
42 if (isValidSelection(aValue)) {
43 isDone = setSelectionCustom(aValue);
44 // updateObject - to update/redisplay feature
45 // it is commented in order to perfom it outside the method
46 //updateObject(myFeature);
48 //emit valuesChanged();
53 //********************************************************************
54 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
57 if (getValidState(theValue, aValid)) {
61 aValid = isValidSelectionCustom(theValue);
63 storeValidState(theValue, aValid);
67 DataPtr aData = myFeature->data();
68 AttributePtr anAttribute = myFeature->attribute(attributeID());
70 // stores the current values of the widget attribute
71 Events_Loop* aLoop = Events_Loop::loop();
72 // blocks the flush signals to avoid the temporary objects visualization in the viewer
73 // they should not be shown in order to do not lose highlight by erasing them
74 aLoop->activateFlushes(false);
76 aData->blockSendAttributeUpdated(true);
77 bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
78 storeAttributeValue();
80 // saves the owner value to the widget attribute
81 aValid = setSelectionCustom(theValue);
83 // checks the attribute validity
84 aValid = isValidAttribute();
86 // restores the current values of the widget attribute
87 restoreAttributeValue(aValid);
88 aData->blockSendAttributeUpdated(false);
89 anAttribute->blockSetInitialized(isAttributeBlocked);
90 aLoop->activateFlushes(true);
92 // In particular case the results are deleted and called as redisplayed inside of this
93 // highlight-selection, to they must be flushed as soon as possible.
94 // Example: selection of group-vertices subshapes with shift pressend on body. Without
95 // these 4 lines below the application crashes because of left presentations on
96 // removed results still in the viewer.
97 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
98 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
99 aLoop->flush(aDeletedEvent);
100 aLoop->flush(aRedispEvent);
102 storeValidState(theValue, aValid);
106 //********************************************************************
107 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
112 //********************************************************************
113 bool ModuleBase_WidgetValidated::isValidAttribute() const
115 SessionPtr aMgr = ModelAPI_Session::get();
116 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
117 std::list<ModelAPI_Validator*> aValidators;
118 std::list<std::list<std::string> > anArguments;
119 aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
121 DataPtr aData = myFeature->data();
122 AttributePtr anAttribute = myFeature->attribute(attributeID());
124 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
125 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
127 for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
128 const ModelAPI_AttributeValidator* aAttrValidator =
129 dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
130 if (aAttrValidator) {
131 aValid = aAttrValidator->isValid(anAttribute, *aArgs);
137 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
138 const bool toActivate)
140 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
142 Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
144 aViewer->addSelectionFilter(aSelFilter);
146 aViewer->removeSelectionFilter(aSelFilter);
151 //********************************************************************
152 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
154 bool aValidPrs = myInvalidPrs.contains(theValue);
155 bool anInvalidPrs = myInvalidPrs.contains(theValue);
159 myValidPrs.append(theValue);
160 // the commented code will be useful when the valid state of the presentation
161 // will be changable between activate/deactivate. Currently it does not happen.
163 // myInvalidPrs.removeOne(theValue);
167 myInvalidPrs.append(theValue);
169 // myValidPrs.removeOne(theValue);
171 #ifdef DEBUG_VALID_STATE
172 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
173 .arg(myInvalidPrs.count()).toStdString().c_str());
177 //********************************************************************
178 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
180 bool aValidPrs = myValidPrs.contains(theValue);
181 bool anInvalidPrs = myInvalidPrs.contains(theValue);
185 else if (anInvalidPrs)
188 return aValidPrs || anInvalidPrs;
191 //********************************************************************
192 void ModuleBase_WidgetValidated::clearValidState()
194 #ifdef DEBUG_VALID_STATE
195 qDebug("clearValidState");
198 myInvalidPrs.clear();