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 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
21 const Config_WidgetAPI* theData,
22 const std::string& theParentId)
23 : ModuleBase_ModelWidget(theParent, theData, theParentId)
27 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
31 //********************************************************************
32 bool ModuleBase_WidgetValidated::setSelection(const QList<ModuleBase_ViewerPrs>& theValues, int& thePosition)
34 if (thePosition < 0 || thePosition >= theValues.size())
36 ModuleBase_ViewerPrs aValue = theValues[thePosition];
41 if (isValidSelection(aValue)) {
42 isDone = setSelectionCustom(aValue);
43 updateObject(myFeature);
49 //********************************************************************
50 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
52 DataPtr aData = myFeature->data();
53 AttributePtr anAttribute = myFeature->attribute(attributeID());
55 // stores the current values of the widget attribute
56 Events_Loop* aLoop = Events_Loop::loop();
57 // blocks the flush signals to avoid the temporary objects visualization in the viewer
58 // they should not be shown in order to do not lose highlight by erasing them
59 aLoop->activateFlushes(false);
61 aData->blockSendAttributeUpdated(true);
62 bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
63 storeAttributeValue();
65 // saves the owner value to the widget attribute
66 bool aValid = setSelectionCustom(theValue);
69 // checks the attribute validity
70 aValid = isValidAttribute();
72 // restores the current values of the widget attribute
73 restoreAttributeValue(aValid);
74 aData->blockSendAttributeUpdated(false);
75 anAttribute->blockSetInitialized(isAttributeBlocked);
76 aLoop->activateFlushes(true);
78 // In particular case the results are deleted and called as redisplayed inside of this
79 // highlight-selection, to they must be flushed as soon as possible.
80 // Example: selection of group-vertices subshapes with shift pressend on body. Without
81 // these 4 lines below the application crashes because of left presentations on
82 // removed results still in the viewer.
83 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
84 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
85 aLoop->flush(aDeletedEvent);
86 aLoop->flush(aRedispEvent);
91 //********************************************************************
92 bool ModuleBase_WidgetValidated::isValidAttribute() const
94 SessionPtr aMgr = ModelAPI_Session::get();
95 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
96 std::list<ModelAPI_Validator*> aValidators;
97 std::list<std::list<std::string> > anArguments;
98 aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
100 DataPtr aData = myFeature->data();
101 AttributePtr anAttribute = myFeature->attribute(attributeID());
103 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
104 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
106 for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
107 const ModelAPI_AttributeValidator* aAttrValidator =
108 dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
109 if (aAttrValidator) {
110 aValid = aAttrValidator->isValid(anAttribute, *aArgs);
116 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
117 const bool toActivate) const
119 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
121 Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
123 aViewer->addSelectionFilter(aSelFilter);
125 aViewer->removeSelectionFilter(aSelFilter);
128 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getSelectedEntitiesOrObjects(
129 ModuleBase_ISelection* theSelection) const
131 QList<ModuleBase_ViewerPrs> aSelectedPrs;
133 // find selected presentation either in the viewer or in OB
134 // the selection in OCC viewer - the selection of a sub-shapes in the viewer
135 aSelectedPrs = theSelection->getSelected();
136 if (aSelectedPrs.empty()) {
137 // the selection in Object Browser
138 QObjectPtrList anObjects = theSelection->selectedObjects();
139 aSelectedPrs = ModuleBase_ISelection::getViewerPrs(anObjects);