]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetValidated.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[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 <ModuleBase_ISelection.h>
7
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Validator.h>
10 #include <ModelAPI_AttributeValidator.h>
11
12 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
13 #include <SelectMgr_EntityOwner.hxx>
14
15 #include <Events_Loop.h>
16
17 #include <QWidget>
18
19 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
20                                                        const Config_WidgetAPI* theData,
21                                                        const std::string& theParentId)
22  : ModuleBase_ModelWidget(theParent, theData, theParentId)
23 {
24 }
25
26 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
27 {
28 }
29
30 //********************************************************************
31 bool ModuleBase_WidgetValidated::setSelection(ModuleBase_ViewerPrs theValue)
32 {
33   bool isDone = false;
34
35   if (isValidSelection(theValue)) {
36     isDone = setSelectionCustom(theValue);
37     updateObject(myFeature);
38     emit valuesChanged();
39   }
40   return isDone;
41 }
42
43 //********************************************************************
44 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
45 {
46   DataPtr aData = myFeature->data();
47   AttributePtr anAttribute = myFeature->attribute(attributeID());
48
49   // stores the current values of the widget attribute
50   Events_Loop* aLoop = Events_Loop::loop();
51   // blocks the flush signals to avoid the temporary objects visualization in the viewer
52   // they should not be shown in order to do not lose highlight by erasing them
53   aLoop->activateFlushes(false);
54
55   aData->blockSendAttributeUpdated(true);
56   bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
57   storeAttributeValue();
58
59   // saves the owner value to the widget attribute
60   bool aValid = setSelectionCustom(theValue);
61
62   if (aValid)
63     // checks the attribute validity
64     aValid = isValidAttribute();
65
66   // restores the current values of the widget attribute
67   restoreAttributeValue(aValid);
68   aData->blockSendAttributeUpdated(false);
69   anAttribute->blockSetInitialized(isAttributeBlocked);
70   aLoop->activateFlushes(true);
71
72   return aValid;
73 }
74
75 //********************************************************************
76 bool ModuleBase_WidgetValidated::isValidAttribute() const
77 {
78   SessionPtr aMgr = ModelAPI_Session::get();
79   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
80   std::list<ModelAPI_Validator*> aValidators;
81   std::list<std::list<std::string> > anArguments;
82   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
83
84   DataPtr aData = myFeature->data();
85   AttributePtr anAttribute = myFeature->attribute(attributeID());
86
87   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
88   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
89   bool aValid = true;
90   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
91     const ModelAPI_AttributeValidator* aAttrValidator =
92         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
93     if (aAttrValidator) {
94       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
95     }
96   }
97   return aValid;
98 }
99
100 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
101                                                  const bool toActivate) const
102 {
103   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
104
105   Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
106   if (toActivate)
107     aViewer->addSelectionFilter(aSelFilter);
108   else
109     aViewer->removeSelectionFilter(aSelFilter);
110 }
111
112 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getSelectedEntitiesOrObjects(
113                                                   ModuleBase_ISelection* theSelection) const
114 {
115   QList<ModuleBase_ViewerPrs> aSelectedPrs;
116
117   // find selected presentation either in the viewer or in OB
118   // the selection in OCC viewer - the selection of a sub-shapes in the viewer
119   aSelectedPrs = theSelection->getSelected();
120   if (aSelectedPrs.empty()) {
121     // the selection in Object Browser
122     QObjectPtrList anObjects = theSelection->selectedObjects();
123     QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
124     for (; anIt != aLast; anIt++) {
125       ObjectPtr anObject = *anIt;
126       if (anObject.get() != NULL) {
127         aSelectedPrs.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
128       }
129     }
130   }
131   return aSelectedPrs;
132 }