Salome HOME
2271c2be1407878bf0b25dfc961dcd98e7b4f10e
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidated.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetValidated.h
4 // Created:     12 Mar 2015
5 // Author:      Natalia ERMOLAEVA
6
7
8 #ifndef MODULEBASE_WIDGETVALIDATED_H_
9 #define MODULEBASE_WIDGETVALIDATED_H_
10
11 #include <ModuleBase.h>
12 #include <ModuleBase_ModelWidget.h>
13
14 #include <GeomAPI_Shape.h>
15 #include <GeomAPI_AISObject.h>
16 #include <ModelAPI_Object.h>
17 #include <ModelAPI_Attribute.h>
18
19 #include <SelectMgr_ListOfFilter.hxx>
20 #include <NCollection_DataMap.hxx>
21 #include <TopoDS_Shape.hxx>
22
23 #include <QList>
24 #include <QMap>
25
26 class QWidget;
27 class ModuleBase_IWorkshop;
28 class ModuleBase_ISelection;
29 class ModuleBase_WidgetSelectorStore;
30 class ModelAPI_Validator;
31 class Config_WidgetAPI;
32 class Handle_SelectMgr_EntityOwner;
33
34 //#define LIST_OF_VALID_PRS
35
36 /**
37 * \ingroup GUI
38 * Implementation of widget with validators and filters processing.
39 */
40 class MODULEBASE_EXPORT ModuleBase_WidgetValidated : public ModuleBase_ModelWidget
41 {
42   Q_OBJECT
43  public:
44   /// Constructor
45   /// \param theParent the parent object
46   /// \param theWorkshop a reference to workshop
47   /// \param theData the widget configuation. The attribute of the model widget is obtained from
48   ModuleBase_WidgetValidated(QWidget* theParent,
49                              ModuleBase_IWorkshop* theWorkshop,
50                              const Config_WidgetAPI* theData);
51   virtual ~ModuleBase_WidgetValidated();
52
53   /// Checks all widget validator if the owner is valid. Firstly it checks custom widget validating,
54   /// next, the attribute's validating. It trying on the give selection to current attribute by
55   /// setting the value inside and calling validators. After this, the previous attribute value is
56   /// restored.The valid/invalid value is cashed.
57   /// \param theValue a selected presentation in the view
58   /// \return a boolean value
59   virtual bool isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
60
61   //! Returns data object by AIS
62   ObjectPtr findPresentedObject(const AISObjectPtr& theAIS) const;
63
64   //! Clear all validated cash in the widget
65   void clearValidatedCash();
66
67   /// Returns true if the workshop validator filter has been already activated
68   /// \return boolean value
69   bool isFilterActivated() const;
70
71 protected:
72   /// Checks whether all active viewer filters validate the presentation
73   /// \param thePrs a selected presentation in the view
74   /// \return a boolean value
75   virtual bool isValidInFilters(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
76
77   /// Checks all attribute validators returns valid. It tries on the given selection
78   /// to current attribute by setting the value inside and calling validators. After this,
79   /// the previous attribute value is restored.The valid/invalid value is cashed.
80   /// \param theValue a selected presentation in the view
81   /// \param theAttribute the attribute
82   /// \return a boolean value
83   bool isValidSelectionForAttribute(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue,
84                                     const AttributePtr& theAttribute);
85
86   /// Retunrs attribute, which should be validated. In default implementation,
87   /// this is an attribute of ID
88   /// \return an attribute
89   virtual AttributePtr attribute() const;
90
91   /// Creates a backup of the current values of the attribute
92   /// It should be realized in the specific widget because of different
93   /// parameters of the current attribute
94   /// \param theAttribute an attribute to be stored
95   virtual void storeAttributeValue(const AttributePtr& theAttribute);
96
97   /// Creates a backup of the current values of the attribute
98   /// It should be realized in the specific widget because of different
99   /// parameters of the current attribute
100   /// \param theAttribute an attribute to be restored
101   /// \param theValid a boolean flag, if restore happens for valid parameters
102   virtual void restoreAttributeValue(const AttributePtr& theAttribute, const bool theValid);
103
104   /// Checks the widget validity. By default, it returns true.
105   /// \param thePrs a selected presentation in the view
106   /// \return a boolean value
107   virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
108
109   /// Fills the attribute with the value of the selected owner
110   /// \param thePrs a selected owner
111   virtual bool setSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs) = 0;
112
113   /// Returns a list of selected presentations in the viewer and object browser
114   /// The presentations from the object browser are filtered by the AIS context filters
115   /// \return a list of presentations
116   QList<std::shared_ptr<ModuleBase_ViewerPrs>> getFilteredSelected();
117
118   /// It obtains selection filters from the workshop and activates them in the active viewer
119   /// \param toActivate a flag about activation or deactivation the filters
120   /// \return true if the selection filter of the widget is activated in viewer context
121   bool activateFilters(const bool toActivate);
122
123   /// Block the model flush of update and intialization of attribute
124   /// \param theAttribute an attribute of blocking
125   /// \param theToBlock flag whether the model is blocked or unblocked
126   /// \param isFlushesActived out value if model is blocked, in value if model is unblocked
127   /// to be used to restore flush state when unblocked
128   /// \param isAttributeSetInitializedBlocked out value if model is blocked
129   /// in value if model is unblocked to be used to restore previous state when unblocked
130   virtual void blockAttribute(const AttributePtr& theAttribute, const bool& theToBlock,
131                               bool& isFlushesActived, bool& isAttributeSetInitializedBlocked);
132
133 private:
134   /// Checks the current attibute in all attribute validators
135   /// \param theAttribute an attribute to be validated
136   /// \return true if all validators return that the attribute is valid
137   bool isValidAttribute(const AttributePtr& theAttribute) const;
138
139 protected:
140   /// Gets the validity state of the presentation in an internal map. 
141   /// Returns true if the valid state of value is stored
142   /// \param theValue a viewer presentation
143   /// \param theValid a valid state
144   bool getValidState(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue, bool& theValid);
145
146   /// Store the validity state of the presentation in an internal map
147   /// \param theValue a viewer presentation
148   /// \param theValid a valid state
149   void storeValidState(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue, const bool theValid);
150
151 private:
152   /// Applies AIS context filters to the parameter list. The not approved presentations are
153   /// removed from the parameters.
154   /// \param theValues a list of presentations.
155   void filterPresentations(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
156
157   /// Remove subshapes of compsolids if whole compsolid is present.
158   /// \param theValues a list of presentations.
159   void filterCompSolids(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
160
161 protected:
162   /// Reference to workshop
163   ModuleBase_IWorkshop* myWorkshop;
164   /// The widget is in validation mode: store is performed, restore is not
165   bool myIsInValidate;
166
167 private:
168   ObjectPtr myPresentedObject; /// back up of the filtered object
169 #ifdef LIST_OF_VALID_PRS
170   /// cash of valid selection presentations
171   QList<std::shared_ptr<ModuleBase_ViewerPrs>> myValidPrs; 
172
173   /// cash of invalid selection presentations
174   QList<std::shared_ptr<ModuleBase_ViewerPrs>> myInvalidPrs; 
175 #else
176   // assume that one presentation selection presentation corresponds only one shape
177   NCollection_DataMap<TopoDS_Shape, std::shared_ptr<ModuleBase_ViewerPrs> > myValidPrs;
178   NCollection_DataMap<TopoDS_Shape, std::shared_ptr<ModuleBase_ViewerPrs> > myInvalidPrs;
179 #endif
180
181   /// store to backup parameters of the model
182   ModuleBase_WidgetSelectorStore* myAttributeStore;
183 };
184
185 #endif /* MODULEBASE_WIDGETVALIDATED_H_ */