Salome HOME
Hide "by general equation" case for plane creation
[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 <Events_Loop.h>
15
16 #include <QWidget>
17
18 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
19                                                        const Config_WidgetAPI* theData,
20                                                        const std::string& theParentId)
21  : ModuleBase_ModelWidget(theParent, theData, theParentId)
22 {
23 }
24
25 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
26 {
27 }
28
29 //********************************************************************
30 bool ModuleBase_WidgetValidated::setSelection(ModuleBase_ViewerPrs theValue)
31 {
32   bool isDone = false;
33
34   if (isValidSelection(theValue)) {
35     isDone = setSelectionCustom(theValue);
36     updateObject(myFeature);
37     emit valuesChanged();
38   }
39   return isDone;
40 }
41
42 //********************************************************************
43 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
44 {
45   DataPtr aData = myFeature->data();
46   AttributePtr anAttribute = myFeature->attribute(attributeID());
47
48   // stores the current values of the widget attribute
49   Events_Loop* aLoop = Events_Loop::loop();
50   // blocks the flush signals to avoid the temporary objects visualization in the viewer
51   // they should not be shown in order to do not lose highlight by erasing them
52   aLoop->activateFlushes(false);
53
54   aData->blockSendAttributeUpdated(true);
55   bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
56   storeAttributeValue();
57
58   // saves the owner value to the widget attribute
59   bool aValid = setSelectionCustom(theValue);
60
61   if (aValid)
62     // checks the attribute validity
63     aValid = isValidAttribute();
64
65   // restores the current values of the widget attribute
66   restoreAttributeValue(aValid);
67   aData->blockSendAttributeUpdated(false);
68   anAttribute->blockSetInitialized(isAttributeBlocked);
69   aLoop->activateFlushes(true);
70
71   return aValid;
72 }
73
74 //********************************************************************
75 bool ModuleBase_WidgetValidated::isValidAttribute() const
76 {
77   SessionPtr aMgr = ModelAPI_Session::get();
78   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
79   std::list<ModelAPI_Validator*> aValidators;
80   std::list<std::list<std::string> > anArguments;
81   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
82
83   DataPtr aData = myFeature->data();
84   AttributePtr anAttribute = myFeature->attribute(attributeID());
85
86   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
87   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
88   bool aValid = true;
89   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
90     const ModelAPI_AttributeValidator* aAttrValidator =
91         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
92     if (aAttrValidator) {
93       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
94     }
95   }
96   return aValid;
97 }
98
99 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
100                                                  const bool toActivate) const
101 {
102   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
103
104   Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
105   if (toActivate)
106     aViewer->addSelectionFilter(aSelFilter);
107   else
108     aViewer->removeSelectionFilter(aSelFilter);
109 }