Salome HOME
4dcf23973ff31c7239325deba633f9aa06dfabf9
[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(const QList<ModuleBase_ViewerPrs>& theValues, int& thePosition)
33 {
34   if (thePosition < 0 || thePosition >= theValues.size())
35     return false;
36   ModuleBase_ViewerPrs aValue = theValues[thePosition];
37   thePosition++;
38
39   bool isDone = false;
40
41   if (isValidSelection(aValue)) {
42     isDone = setSelectionCustom(aValue);
43     updateObject(myFeature);
44     emit valuesChanged();
45   }
46   return isDone;
47 }
48
49 //********************************************************************
50 bool ModuleBase_WidgetValidated::isValidSelection(const ModuleBase_ViewerPrs& theValue)
51 {
52   DataPtr aData = myFeature->data();
53   AttributePtr anAttribute = myFeature->attribute(attributeID());
54
55   // stores the current values of the widget attribute
56   Events_Loop* aLoop = Events_Loop::loop();
57   // blocks the flush signals to avoid the temporary objects visualization in the viewer
58   // they should not be shown in order to do not lose highlight by erasing them
59   aLoop->activateFlushes(false);
60
61   aData->blockSendAttributeUpdated(true);
62   bool isAttributeBlocked = anAttribute->blockSetInitialized(true);
63   storeAttributeValue();
64
65   // saves the owner value to the widget attribute
66   bool aValid = setSelectionCustom(theValue);
67
68   if (aValid)
69     // checks the attribute validity
70     aValid = isValidAttribute();
71
72   // restores the current values of the widget attribute
73   restoreAttributeValue(aValid);
74   aData->blockSendAttributeUpdated(false);
75   anAttribute->blockSetInitialized(isAttributeBlocked);
76   aLoop->activateFlushes(true);
77
78   // In particular case the results are deleted and called as redisplayed inside of this
79   // highlight-selection, to they must be flushed as soon as possible.
80   // Example: selection of group-vertices subshapes with shift pressend on body. Without
81   //  these 4 lines below the application crashes because of left presentations on
82   //  removed results still in the viewer.
83   static Events_ID aDeletedEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
84   static Events_ID aRedispEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
85   aLoop->flush(aDeletedEvent);
86   aLoop->flush(aRedispEvent);
87
88   return aValid;
89 }
90
91 //********************************************************************
92 bool ModuleBase_WidgetValidated::isValidAttribute() const
93 {
94   SessionPtr aMgr = ModelAPI_Session::get();
95   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
96   std::list<ModelAPI_Validator*> aValidators;
97   std::list<std::list<std::string> > anArguments;
98   aFactory->validators(myFeature->getKind(), attributeID(), aValidators, anArguments);
99
100   DataPtr aData = myFeature->data();
101   AttributePtr anAttribute = myFeature->attribute(attributeID());
102
103   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
104   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
105   bool aValid = true;
106   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
107     const ModelAPI_AttributeValidator* aAttrValidator =
108         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
109     if (aAttrValidator) {
110       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
111     }
112   }
113   return aValid;
114 }
115
116 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
117                                                  const bool toActivate) const
118 {
119   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
120
121   Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
122   if (toActivate)
123     aViewer->addSelectionFilter(aSelFilter);
124   else
125     aViewer->removeSelectionFilter(aSelFilter);
126 }
127
128 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getSelectedEntitiesOrObjects(
129                                                   ModuleBase_ISelection* theSelection) const
130 {
131   QList<ModuleBase_ViewerPrs> aSelectedPrs;
132
133   // find selected presentation either in the viewer or in OB
134   // the selection in OCC viewer - the selection of a sub-shapes in the viewer
135   aSelectedPrs = theSelection->getSelected();
136   if (aSelectedPrs.empty()) {
137     // the selection in Object Browser
138     QObjectPtrList anObjects = theSelection->selectedObjects();
139     aSelectedPrs = ModuleBase_ISelection::getViewerPrs(anObjects);
140   }
141   return aSelectedPrs;
142 }