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