Salome HOME
Preselection using in operations: setSelection of widget returns a modified list...
[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 #include <ModelAPI_Events.h>
12
13 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
14 #include <SelectMgr_EntityOwner.hxx>
15
16 #include <Events_Loop.h>
17
18 #include <QWidget>
19
20 ModuleBase_WidgetValidated::ModuleBase_WidgetValidated(QWidget* theParent,
21                                                        const Config_WidgetAPI* theData,
22                                                        const std::string& theParentId)
23  : ModuleBase_ModelWidget(theParent, theData, theParentId)
24 {
25 }
26
27 ModuleBase_WidgetValidated::~ModuleBase_WidgetValidated()
28 {
29 }
30
31 //********************************************************************
32 bool ModuleBase_WidgetValidated::setSelection(QList<ModuleBase_ViewerPrs>& theValues)
33 {
34   if (theValues.empty())
35     return false;
36   // it removes the processed value from the parameters list
37   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
38   bool isDone = false;
39
40   if (isValidSelection(aValue)) {
41     isDone = setSelectionCustom(aValue);
42     // updateObject - to update/redisplay feature
43     // it is commented in order to perfom it outside the method
44     //updateObject(myFeature);
45     // to storeValue()
46     //emit valuesChanged();
47   }
48   return isDone;
49 }
50
51 //********************************************************************
52 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
53 {
54   bool aValid = isValidSelectionCustom(theValue);
55   if (!aValid)
56     return aValid;
57
58   DataPtr aData = myFeature->data();
59   AttributePtr anAttribute = myFeature->attribute(attributeID());
60
61   // stores the current values of the widget attribute
62   Events_Loop* aLoop = Events_Loop::loop();
63   // blocks the flush signals to avoid the temporary objects visualization in the viewer
64   // they should not be shown in order to do not lose highlight by erasing them
65   aLoop->activateFlushes(false);
66
67   aData->blockSendAttributeUpdated(true);
68   bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
69   storeAttributeValue();
70
71   // saves the owner value to the widget attribute
72   aValid = setSelectionCustom(theValue);
73   if (aValid)
74     // checks the attribute validity
75     aValid = isValidAttribute();
76
77   // restores the current values of the widget attribute
78   restoreAttributeValue(aValid);
79   aData->blockSendAttributeUpdated(false);
80   anAttribute->blockSetInitialized(isAttributeBlocked);
81   aLoop->activateFlushes(true);
82
83   // In particular case the results are deleted and called as redisplayed inside of this
84   // highlight-selection, to they must be flushed as soon as possible.
85   // Example: selection of group-vertices subshapes with shift pressend on body. Without
86   //  these 4 lines below the application crashes because of left presentations on
87   //  removed results still in the viewer.
88   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
89   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
90   aLoop->flush(aDeletedEvent);
91   aLoop->flush(aRedispEvent);
92
93   return aValid;
94 }
95
96 //********************************************************************
97 bool ModuleBase_WidgetValidated::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
98 {
99   return true;
100 }
101
102 //********************************************************************
103 bool ModuleBase_WidgetValidated::isValidAttribute() const
104 {
105   SessionPtr aMgr = ModelAPI_Session::get();
106   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
107   std::list<ModelAPI_Validator*> aValidators;
108   std::list<std::list<std::string> > anArguments;
109   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
110
111   customValidators(aValidators, anArguments);
112
113   DataPtr aData = myFeature->data();
114   AttributePtr anAttribute = myFeature->attribute(attributeID());
115
116   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
117   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
118   bool aValid = true;
119   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
120     const ModelAPI_AttributeValidator* aAttrValidator =
121         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
122     if (aAttrValidator) {
123       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
124     }
125   }
126   return aValid;
127 }
128
129 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
130                                                  const bool toActivate) const
131 {
132   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
133
134   Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
135   if (toActivate)
136     aViewer->addSelectionFilter(aSelFilter);
137   else
138     aViewer->removeSelectionFilter(aSelFilter);
139 }
140
141 void ModuleBase_WidgetValidated::customValidators(std::list<ModelAPI_Validator*>& theValidators,
142                                           std::list<std::list<std::string> >& theArguments) const
143 {
144 }