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