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