Salome HOME
Merge branch 'Dev_1.2.0' of newgeom:newgeom into Dev_1.2.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 #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   customValidators(aValidators, anArguments);
101
102   DataPtr aData = myFeature->data();
103   AttributePtr anAttribute = myFeature->attribute(attributeID());
104
105   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
106   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
107   bool aValid = true;
108   for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
109     const ModelAPI_AttributeValidator* aAttrValidator =
110         dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
111     if (aAttrValidator) {
112       aValid = aAttrValidator->isValid(anAttribute, *aArgs);
113     }
114   }
115   return aValid;
116 }
117
118 void ModuleBase_WidgetValidated::activateFilters(ModuleBase_IWorkshop* theWorkshop,
119                                                  const bool toActivate) const
120 {
121   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
122
123   Handle(SelectMgr_Filter) aSelFilter = theWorkshop->validatorFilter();
124   if (toActivate)
125     aViewer->addSelectionFilter(aSelFilter);
126   else
127     aViewer->removeSelectionFilter(aSelFilter);
128 }
129
130 void ModuleBase_WidgetValidated::customValidators(std::list<ModelAPI_Validator*>& theValidators,
131                                           std::list<std::list<std::string> >& theArguments) const
132 {
133 }
134
135 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetValidated::getSelectedEntitiesOrObjects(
136                                                   ModuleBase_ISelection* theSelection) const
137 {
138   QList<ModuleBase_ViewerPrs> aSelectedPrs;
139
140   // find selected presentation either in the viewer or in OB
141   // the selection in OCC viewer - the selection of a sub-shapes in the viewer
142   aSelectedPrs = theSelection->getSelected();
143   if (aSelectedPrs.empty()) {
144     // the selection in Object Browser
145     QObjectPtrList anObjects = theSelection->selectedObjects();
146     aSelectedPrs = ModuleBase_ISelection::getViewerPrs(anObjects);
147   }
148   return aSelectedPrs;
149 }