Salome HOME
Union of validator and filter functionalities.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidated.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <ModuleBase_WidgetValidated.h>
4 #include <ModuleBase_FilterFactory.h>
5 #include <ModuleBase_IViewer.h>
6
7 #include <ModelAPI_Session.h>
8 #include <ModelAPI_Validator.h>
9 #include <ModelAPI_AttributeValidator.h>
10
11 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
12 #include <SelectMgr_EntityOwner.hxx>
13
14 #include <QWidget>
15
16 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
17                                                        const Config_WidgetAPI* theData,
18                                                        const std::string& theParentId)
19  : ModuleBase_ModelWidget(theParent, theData, theParentId)
20 {
21 }
22
23 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
24 {
25 }
26
27 //********************************************************************
28 bool ModuleBase_WidgetValidated::setSelection(ModuleBase_ViewerPrs theValue)
29 {
30   bool isDone = false;
31
32   Handle(SelectMgr_EntityOwner) anOwner = theValue.owner();
33   if (isValid(anOwner)) {
34     setSelection(anOwner);
35     updateObject(myFeature);
36     emit valuesChanged();
37   }
38   return isDone;
39 }
40
41 //********************************************************************
42 bool ModuleBase_WidgetValidated::isValid(const Handle_SelectMgr_EntityOwner& theOwner)
43 {
44   // stores the current values of the widget attribute
45   backupAttributeValue(true);
46
47   // saves the owner value to the widget attribute
48   setSelection(theOwner);
49
50   // checks the attribute validity
51   bool aValid = isValidAttribute();
52
53   // restores the current values of the widget attribute
54   backupAttributeValue(false);
55
56   return aValid;
57 }
58
59 //********************************************************************
60 bool ModuleBase_WidgetValidated::isValidAttribute() const
61 {
62   SessionPtr aMgr = ModelAPI_Session::get();
63   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
64   std::list<ModelAPI_Validator*> aValidators;
65   std::list<std::list<std::string> > anArguments;
66   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
67
68   DataPtr aData = myFeature->data();
69   AttributePtr anAttribute = myFeature->attribute(attributeID());
70
71   aData->blockSendAttributeUpdated(true);
72
73     // 3. check the acceptability of the current values
74   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
75   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
76   bool aValid = true;
77   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
78     const ModelAPI_AttributeValidator* aAttrValidator =
79         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
80     if (aAttrValidator) {
81       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
82     }
83   }
84   aData->blockSendAttributeUpdated(false);
85
86   return aValid;
87 }
88
89 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
90                                                  const bool toActivate) const
91 {
92   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
93
94   Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
95   if (toActivate)
96     aViewer->addSelectionFilter(aSelFilter);
97   else
98     aViewer->removeSelectionFilter(aSelFilter);
99 }