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(ModuleBase_ViewerPrs theValue)
36 if (isValidSelection(theValue)) {
37 isDone = setSelectionCustom(theValue);
38 updateObject(myFeature);
44 //********************************************************************
45 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
47 DataPtr aData = myFeature->data();
48 AttributePtr anAttribute = myFeature->attribute(attributeID());
50 // stores the current values of the widget attribute
51 Events_Loop* aLoop = Events_Loop::loop();
52 // blocks the flush signals to avoid the temporary objects visualization in the viewer
53 // they should not be shown in order to do not lose highlight by erasing them
54 aLoop->activateFlushes(false);
56 aData->blockSendAttributeUpdated(true);
57 bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
58 storeAttributeValue();
60 // saves the owner value to the widget attribute
61 bool aValid = setSelectionCustom(theValue);
64 // checks the attribute validity
65 aValid = isValidAttribute();
67 // restores the current values of the widget attribute
68 restoreAttributeValue(aValid);
69 aData->blockSendAttributeUpdated(false);
70 anAttribute->blockSetInitialized(isAttributeBlocked);
71 aLoop->activateFlushes(true);
73 // In particular case the results are deleted and called as redisplayed inside of this
74 // highlight-selection, to they must be flushed as soon as possible.
75 // Example: selection of group-vertices subshapes with shift pressend on body. Without
76 // these 4 lines below the application crashes because of left presentations on
77 // removed results still in the viewer.
78 static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
79 static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
80 aLoop->flush(aDeletedEvent);
81 aLoop->flush(aRedispEvent);
86 //********************************************************************
87 bool ModuleBase_WidgetValidated::isValidAttribute() const
89 SessionPtr aMgr = ModelAPI_Session::get();
90 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
91 std::list<ModelAPI_Validator*> aValidators;
92 std::list<std::list<std::string> > anArguments;
93 aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
95 DataPtr aData = myFeature->data();
96 AttributePtr anAttribute = myFeature->attribute(attributeID());
98 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
99 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
101 for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
102 const ModelAPI_AttributeValidator* aAttrValidator =
103 dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
104 if (aAttrValidator) {
105 aValid = aAttrValidator->isValid(anAttribute, *aArgs);
111 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
112 const bool toActivate) const
114 ModuleBase_IViewer* aViewer = theWorkshop->viewer();
116 Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
118 aViewer->addSelectionFilter(aSelFilter);
120 aViewer->removeSelectionFilter(aSelFilter);
123 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getSelectedEntitiesOrObjects(
124 ModuleBase_ISelection* theSelection) const
126 QList<ModuleBase_ViewerPrs> aSelectedPrs;
128 // find selected presentation either in the viewer or in OB
129 // the selection in OCC viewer - the selection of a sub-shapes in the viewer
130 aSelectedPrs = theSelection->getSelected();
131 if (aSelectedPrs.empty()) {
132 // the selection in Object Browser
133 QObjectPtrList anObjects = theSelection->selectedObjects();
134 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
135 for (; anIt != aLast; anIt++) {
136 ObjectPtr anObject = *anIt;
137 if (anObject.get() != NULL) {
138 aSelectedPrs.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));