Salome HOME
a6f05af6025c5eb23d39d9bc6ad3ca0b4a825a12
[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 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
7
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Validator.h>
10 #include <ModelAPI_ResultValidator.h>
11
12 #include <QWidget>
13
14 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
15                                                        const Config_WidgetAPI* theData,
16                                                        const std::string& theParentId)
17     : ModuleBase_ModelWidget(theParent, theData, theParentId)
18 {
19 }
20
21 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
22 {
23 }
24
25 //********************************************************************
26 bool ModuleBase_WidgetValidated::isValid(ObjectPtr theObj, GeomShapePtr theShape) const
27 {
28   SessionPtr aMgr = ModelAPI_Session::get();
29   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
30   std::list<ModelAPI_Validator*> aValidators;
31   std::list<std::list<std::string> > anArguments;
32   aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
33
34   // Check the type of selected object
35   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
36   bool isValid = true;
37   for (; aValidator != aValidators.end(); aValidator++) {
38     const ModelAPI_ResultValidator* aResValidator =
39         dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
40     if (aResValidator) {
41       isValid = false;
42       if (aResValidator->isValid(theObj)) {
43         isValid = true;
44         break;
45       }
46     }
47   }
48   return isValid;
49 }
50
51 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
52                                                  const bool toActivate) const
53 {
54   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
55
56   // apply filters loaded from the XML definition of the widget
57   ModuleBase_FilterFactory* aFactory = theWorkshop->selectionFilters();
58   SelectMgr_ListOfFilter aFactoryFilters;
59   aFactory->filters(parentID(), attributeID(), aFactoryFilters);
60   SelectMgr_ListIteratorOfListOfFilter aFactoryIt(aFactoryFilters);
61   for (; aFactoryIt.More(); aFactoryIt.Next()) {
62     Handle(SelectMgr_Filter) aSelFilter = aFactoryIt.Value();
63     if (aSelFilter.IsNull())
64       continue;
65     if (toActivate)
66       aViewer->addSelectionFilter(aSelFilter);
67     else
68       aViewer->removeSelectionFilter(aSelFilter);
69   }
70 }
71
72 void ModuleBase_WidgetValidated::selectionFilters(ModuleBase_IWorkshop* theWorkshop,
73                                                   SelectMgr_ListOfFilter& theFilters) const
74 {
75   ModuleBase_FilterFactory* aFactory = theWorkshop->selectionFilters();
76   SelectMgr_ListOfFilter aFilters;
77   aFactory->filters(parentID(), attributeID(), aFilters);
78   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
79   for (; aIt.More(); aIt.Next()) {
80     Handle(SelectMgr_Filter) aSelFilter = aIt.Value();
81     if (aSelFilter.IsNull())
82       continue;
83
84     theFilters.Append(aSelFilter);
85   }
86 }