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