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), isValidateBlocked(false)
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 if (isValidateBlocked)
69 isValidateBlocked = true;
71 DataPtr aData = myFeature->data();
72 AttributePtr anAttribute = myFeature->attribute(attributeID());
74 // stores the current values of the widget attribute
75 Events_Loop* aLoop = Events_Loop::loop();
76 // blocks the flush signals to avoid the temporary objects visualization in the viewer
77 // they should not be shown in order to do not lose highlight by erasing them
78 bool isActive = aLoop->activateFlushes(false);
80 aData->blockSendAttributeUpdated(true);
81 bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
82 storeAttributeValue();
84 // saves the owner value to the widget attribute
85 aValid = setSelectionCustom(theValue);
87 // checks the attribute validity
88 aValid = isValidAttribute();
90 // restores the current values of the widget attribute
91 restoreAttributeValue(aValid);
92 aData->blockSendAttributeUpdated(false);
93 anAttribute->blockSetInitialized(isAttributeBlocked);
94 aLoop->activateFlushes(isActive);
96 // In particular case the results are deleted and called as redisplayed inside of this
97 // highlight-selection, to they must be flushed as soon as possible.
98 // Example: selection of group-vertices subshapes with shift pressend on body. Without
99 // these 4 lines below the application crashes because of left presentations on
100 // removed results still in the viewer.
101 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
102 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
103 aLoop->flush(aDeletedEvent);
104 aLoop->flush(aRedispEvent);
106 storeValidState(theValue, aValid);
107 isValidateBlocked = false;
111 //********************************************************************
112 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
117 //********************************************************************
118 bool ModuleBase_WidgetValidated::isValidAttribute() const
120 SessionPtr aMgr = ModelAPI_Session::get();
121 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
122 std::list<ModelAPI_Validator*> aValidators;
123 std::list<std::list<std::string> > anArguments;
124 aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
126 DataPtr aData = myFeature->data();
127 AttributePtr anAttribute = myFeature->attribute(attributeID());
129 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
130 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
132 for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
133 const ModelAPI_AttributeValidator* aAttrValidator =
134 dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
135 if (aAttrValidator) {
136 aValid = aAttrValidator->isValid(anAttribute, *aArgs);
142 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
143 const bool toActivate)
145 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
147 Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
149 aViewer->addSelectionFilter(aSelFilter);
151 aViewer->removeSelectionFilter(aSelFilter);
156 //********************************************************************
157 void ModuleBase_WidgetValidated::storeValidState(const ModuleBase_ViewerPrs& theValue, const bool theValid)
159 bool aValidPrs = myInvalidPrs.contains(theValue);
160 bool anInvalidPrs = myInvalidPrs.contains(theValue);
164 myValidPrs.append(theValue);
165 // the commented code will be useful when the valid state of the presentation
166 // will be changable between activate/deactivate. Currently it does not happen.
168 // myInvalidPrs.removeOne(theValue);
172 myInvalidPrs.append(theValue);
174 // myValidPrs.removeOne(theValue);
176 #ifdef DEBUG_VALID_STATE
177 qDebug(QString("storeValidState: myValidPrs.size() = %1, myInvalidPrs.size() = %2").arg(myValidPrs.count())
178 .arg(myInvalidPrs.count()).toStdString().c_str());
182 //********************************************************************
183 bool ModuleBase_WidgetValidated::getValidState(const ModuleBase_ViewerPrs& theValue, bool& theValid)
185 bool aValidPrs = myValidPrs.contains(theValue);
186 bool anInvalidPrs = myInvalidPrs.contains(theValue);
190 else if (anInvalidPrs)
193 return aValidPrs || anInvalidPrs;
196 //********************************************************************
197 void ModuleBase_WidgetValidated::clearValidState()
199 #ifdef DEBUG_VALID_STATE
200 qDebug("clearValidState");
203 myInvalidPrs.clear();