Salome HOME
797dbb8e04060826a7446caec0c15595010c5567
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetValidated.h
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef MODULEBASE_WIDGETVALIDATED_H_
21 #define MODULEBASE_WIDGETVALIDATED_H_
22
23 #include <ModuleBase.h>
24 #include <ModuleBase_ModelWidget.h>
25
26 #include <GeomAPI_Shape.h>
27 #include <GeomAPI_AISObject.h>
28 #include <ModelAPI_Object.h>
29 #include <ModelAPI_Attribute.h>
30 #include <ModelAPI_ResultBody.h>
31
32 #include <SelectMgr_ListOfFilter.hxx>
33 #include <NCollection_DataMap.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <AIS_NListOfEntityOwner.hxx>
36
37 #include <QList>
38 #include <QMap>
39
40 class QWidget;
41 class ModuleBase_IWorkshop;
42 class ModuleBase_ISelection;
43 class ModuleBase_WidgetSelectorStore;
44 class ModelAPI_Validator;
45 class Config_WidgetAPI;
46
47 //#define LIST_OF_VALID_PRS
48
49 /**
50 * \ingroup GUI
51 * Implementation of widget with validators and filters processing.
52 */
53 class MODULEBASE_EXPORT ModuleBase_WidgetValidated : public ModuleBase_ModelWidget
54 {
55   Q_OBJECT
56  public:
57   /// Constructor
58   /// \param theParent the parent object
59   /// \param theWorkshop a reference to workshop
60   /// \param theData the widget configuation. The attribute of the model widget is obtained from
61   ModuleBase_WidgetValidated(QWidget* theParent,
62                              ModuleBase_IWorkshop* theWorkshop,
63                              const Config_WidgetAPI* theData);
64   virtual ~ModuleBase_WidgetValidated();
65
66   /// Checks all widget validator if the owner is valid. Firstly it checks custom widget validating,
67   /// next, the attribute's validating. It trying on the give selection to current attribute by
68   /// setting the value inside and calling validators. After this, the previous attribute value is
69   /// restored.The valid/invalid value is cashed.
70   /// \param theValue a selected presentation in the view
71   /// \return a boolean value
72   virtual bool isValidSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
73
74   //! Returns data object by AIS
75   ObjectPtr findPresentedObject(const AISObjectPtr& theAIS) const;
76
77   /// The method called when widget is deactivated
78   virtual void deactivate();
79
80   //! Clear all validated cash in the widget
81   void clearValidatedCash();
82
83   /// Returns true if the workshop validator filter has been already activated
84   /// \return boolean value
85   bool isFilterActivated() const;
86
87   /// Appends into container of workshop selection filters
88   /// \param [out] theModuleSelectionFilters module additional modes, -1 means all default modes
89   /// \param [out] selection filters
90   virtual void selectionFilters(QIntList& theModuleSelectionFilters,
91                                 SelectMgr_ListOfFilter& theSelectionFilters);
92
93   /// Block the model flush of update and intialization of attribute
94   /// \param theAttribute an attribute of blocking
95   /// \param theFeature a feature
96   /// \param theToBlock flag whether the model is blocked or unblocked
97   /// \param isFlushesActived out value if model is blocked, in value if model is unblocked
98   /// to be used to restore flush state when unblocked
99   /// \param isAttributeSetInitializedBlocked out value if model is blocked
100   /// in value if model is unblocked to be used to restore previous state when unblocked
101   /// \param isAttributeSendUpdatedBlocked out value if model signal is blocked
102   static void blockFeatureAttribute(const AttributePtr& theAttribute,
103                                     const FeaturePtr& theFeature,
104                                     const bool& theToBlock,
105                                     bool& isFlushesActived,
106                                     bool& isAttributeSetInitializedBlocked,
107                                     bool& isAttributeSendUpdatedBlocked);
108
109   /// Checks the current attibute in all attribute validators
110   /// \param theAttribute an attribute to be validated
111   /// \return true if all validators return that the attribute is valid
112   static bool isValidAttribute(const AttributePtr& theAttribute);
113
114 protected:
115   /// Checks whether all active viewer filters validate the presentation
116   /// \param thePrs a selected presentation in the view
117   /// \return a boolean value
118   virtual bool isValidInFilters(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
119
120   /// Checks all attribute validators returns valid. It tries on the given selection
121   /// to current attribute by setting the value inside and calling validators. After this,
122   /// the previous attribute value is restored.The valid/invalid value is cashed.
123   /// \param theValue a selected presentation in the view
124   /// \param theAttribute the attribute
125   /// \return a boolean value
126   bool isValidSelectionForAttribute(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue,
127                                     const AttributePtr& theAttribute);
128
129   /// Retunrs attribute, which should be validated. In default implementation,
130   /// this is an attribute of ID
131   /// \return an attribute
132   virtual AttributePtr attribute() const;
133
134   /// Creates a backup of the current values of the attribute
135   /// It should be realized in the specific widget because of different
136   /// parameters of the current attribute
137   /// \param theAttribute an attribute to be stored
138   virtual void storeAttributeValue(const AttributePtr& theAttribute);
139
140   /// Creates a backup of the current values of the attribute
141   /// It should be realized in the specific widget because of different
142   /// parameters of the current attribute
143   /// \param theAttribute an attribute to be restored
144   /// \param theValid a boolean flag, if restore happens for valid parameters
145   virtual void restoreAttributeValue(const AttributePtr& theAttribute, const bool theValid);
146
147   /// Checks the widget validity. By default, it returns true.
148   /// \param thePrs a selected presentation in the view
149   /// \return a boolean value
150   virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs);
151
152   /// Fills the attribute with the value of the selected owner
153   /// \param thePrs a selected owner
154   virtual bool setSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs) = 0;
155
156   /// Returns a list of selected presentations in the viewer and object browser
157   /// The presentations from the object browser are filtered by the AIS context filters
158   /// \return a list of presentations
159   QList<std::shared_ptr<ModuleBase_ViewerPrs>> getFilteredSelected();
160
161   /// Block the model flush of update and intialization of attribute
162   /// \param theAttribute an attribute of blocking
163   /// \param theToBlock flag whether the model is blocked or unblocked
164   /// \param isFlushesActived out value if model is blocked, in value if model is unblocked
165   /// to be used to restore flush state when unblocked
166   /// \param isAttributeSetInitializedBlocked out value if model is blocked
167   /// in value if model is unblocked to be used to restore previous state when unblocked
168   /// \param isAttributeSendUpdatedBlocked out value if model signal is blocked
169   virtual void blockAttribute(const AttributePtr& theAttribute, const bool& theToBlock,
170                               bool& isFlushesActived, bool& isAttributeSetInitializedBlocked,
171                               bool& isAttributeSendUpdatedBlocked);
172
173 protected:
174   /// Gets the validity state of the presentation in an internal map.
175   /// Returns true if the valid state of value is stored
176   /// \param theValue a viewer presentation
177   /// \param theValid a valid state
178   bool getValidState(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue, bool& theValid);
179
180   /// Store the validity state of the presentation in an internal map
181   /// \param theValue a viewer presentation
182   /// \param theValid a valid state
183   void storeValidState(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue, const bool theValid);
184
185 private:
186   /// Applies AIS context filters to the parameter list. The not approved presentations are
187   /// removed from the parameters.
188   /// \param theValues a list of presentations.
189   void filterPresentations(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
190
191   /// Remove subshapes of compsolids if whole compsolid is present.
192   /// \param theValues a list of presentations.
193   void filterCompSolids(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
194
195   void collectSubBodies(const ResultBodyPtr& theBody, AIS_NListOfEntityOwner& theList);
196
197 protected:
198   /// Reference to workshop
199   ModuleBase_IWorkshop* myWorkshop;
200   /// The widget is in validation mode: store is performed, restore is not
201   bool myIsInValidate;
202
203 private:
204   ObjectPtr myPresentedObject; /// back up of the filtered object
205 #ifdef LIST_OF_VALID_PRS
206   /// cash of valid selection presentations
207   QList<std::shared_ptr<ModuleBase_ViewerPrs>> myValidPrs;
208
209   /// cash of invalid selection presentations
210   QList<std::shared_ptr<ModuleBase_ViewerPrs>> myInvalidPrs;
211 #else
212   // assume that one presentation selection presentation corresponds only one shape
213   NCollection_DataMap<TopoDS_Shape, std::shared_ptr<ModuleBase_ViewerPrs> > myValidPrs;
214   NCollection_DataMap<TopoDS_Shape, std::shared_ptr<ModuleBase_ViewerPrs> > myInvalidPrs;
215 #endif
216
217   /// store to backup parameters of the model
218   ModuleBase_WidgetSelectorStore* myAttributeStore;
219 };
220
221 #endif /* MODULEBASE_WIDGETVALIDATED_H_ */