Salome HOME
c3a06ae1f9830536138a6bee0dc024d0ec528485
[modules/shaper.git] / src / PartSet / PartSet_WidgetBSplinePoints.h
1 // Copyright (C) 2019-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 PartSet_WidgetBSplinePoints_H
21 #define PartSet_WidgetBSplinePoints_H
22
23 #include "PartSet.h"
24 #include "PartSet_MouseProcessor.h"
25
26 #include <ModuleBase_ModelWidget.h>
27
28 #include <QObject>
29
30 class GeomAPI_Pnt2d;
31 class ModelAPI_CompositeFeature;
32 class ModuleBase_LabelValue;
33 class PartSet_ExternalObjectsMgr;
34 class QGroupBox;
35 class QScrollArea;
36
37 /**\class PartSet_WidgetBSplinePoints
38  * \ingroup Modules
39  * \brief Implementation of model widget to provide widget to input a list of 2D poles
40  *        of B-spline curve in association with weights
41  * In XML can be defined as following:
42  * \code
43  * <sketch-bspline_selector id="poles" weights="weights"/>
44  * \endcode
45  */
46 class PARTSET_EXPORT PartSet_WidgetBSplinePoints : public ModuleBase_ModelWidget,
47                                                    public PartSet_MouseProcessor
48 {
49 Q_OBJECT
50 public:
51   /// Constructor
52   /// \param theParent the parent object
53   /// \param theWorkshop a current workshop
54   /// \param theData the widget configuation. The attribute of the model widget is obtained from
55   PartSet_WidgetBSplinePoints(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop,
56                               const Config_WidgetAPI* theData);
57   /// Destructor
58   virtual ~PartSet_WidgetBSplinePoints();
59
60   /// Fills given container with selection modes if the widget has it
61   /// \param [out] theModuleSelectionModes module additional modes, -1 means all default modes
62   /// \param theModes [out] a container of modes
63   virtual void selectionModes(int& theModuleSelectionModes, QIntList& theModes);
64
65   /// Checks if the selection presentation is valid in widget
66   /// \param theValue a selected presentation in the view
67   /// \return a boolean value
68   virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
69
70   /// Checks all attribute validators returns valid. It tries on the given selection
71   /// to current attribute by setting the value inside and calling validators. After this,
72   /// the previous attribute value is restored.The valid/invalid value is cashed.
73   /// \param theValue a selected presentation in the view
74   /// \param theAttribute the attribute
75   /// \return a boolean value
76   bool isValidSelectionForAttribute_(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue,
77                                      const std::shared_ptr<ModelAPI_Attribute>& theAttribute);
78
79   /// Fills the attribute with the value of the selected owner
80   /// \param thePrs a selected owner
81   bool setSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
82
83   /// Returns list of widget controls
84   /// \return a control list
85   virtual QList<QWidget*> getControls() const;
86
87   /// The methiod called when widget is deactivated
88   virtual void deactivate();
89
90   /// \returns the sketch instance
91   std::shared_ptr<ModelAPI_CompositeFeature> sketch() const { return mySketch; }
92
93   /// Set sketch instance
94   void setSketch(std::shared_ptr<ModelAPI_CompositeFeature> theSketch) { mySketch = theSketch; }
95
96   /// Fill the widget values by given point
97   /// \param theX the X coordinate
98   /// \param theY the Y coordinate
99   /// \returns True in case of success
100   bool setPoint(double theX, double theY);
101
102   /// Returns true if the event is processed.
103   virtual bool processEscape();
104
105   /// Returns true if the attribute can be changed using the selected shapes in the viewer
106   /// and creating a coincidence constraint to them. This control use them.
107   virtual bool useSelectedShapes() const;
108
109   /// Processing the mouse move event in the viewer
110   /// \param theWindow a view window
111   /// \param theEvent a mouse event
112   virtual void mouseMoved(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent);
113
114   /// Processing the mouse release event in the viewer
115   /// \param theWindow a view window
116   /// \param theEvent a mouse event
117   virtual void mouseReleased(ModuleBase_IViewWindow* theWindow, QMouseEvent* theEvent);
118
119 protected:
120   /// Saves the internal parameters to the given feature
121   /// \return True in success
122   virtual bool storeValueCustom();
123
124   /// Restore value from attribute data to the widget's control
125   virtual bool restoreValueCustom();
126
127   /// Store current value in cashed value
128   void storeCurentValue();
129
130   /// Restore cashed value in the model attribute
131   /// \return boolean state if the restored feature shoud be hidden
132   bool restoreCurentValue();
133
134   /// Fills the widget with default values
135   /// \return true if the widget current value is reset
136   virtual bool resetCustom();
137
138 private:
139   /// Create labels for the next B-spline point
140   void createNextPoint();
141   /// Remove labels for the last B-spline point
142   void removeLastPoint();
143
144   /// Save B-spline poles and weights to corresponding attributes
145   void storePolesAndWeights() const;
146
147   /// Returns attribute reference if the key is defined in XML definition of this control
148   /// \return found attribute or null
149   std::shared_ptr<ModelAPI_AttributeRefAttrList> attributeRefAttrList() const;
150
151   void fillRefAttribute(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
152   void fillRefAttribute(std::shared_ptr<GeomAPI_Pnt2d> theClickedPoint,
153                         const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
154   void fillRefAttribute(const ObjectPtr& theObject);
155
156   ObjectPtr getGeomSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue);
157
158 protected:
159   ModuleBase_IWorkshop* myWorkshop; ///< workshop
160
161 private:
162   QGroupBox* myBox;
163   QWidget* myGroupBox;  ///< the parent group box for all intenal widgets
164   QScrollArea* myScrollArea;
165   std::vector<ModuleBase_LabelValue*> myXSpin; ///< the label for the X coordinate
166   std::vector<ModuleBase_LabelValue*> myYSpin; ///< the label for the Y coordinate
167   PartSet_ExternalObjectsMgr* myExternalObjectMgr; ///< reference to external objects manager
168
169   /// value used as selection in mouse release method
170   std::shared_ptr<ModuleBase_ViewerPrs> myPreSelected;
171
172   /// it is important during restart operation
173   CompositeFeaturePtr mySketch;
174
175   std::string myRefAttribute; /// if not empty, coincidences are not set but attribute is filled
176
177   bool myValueIsCashed; /// boolean state if the value is cashed during value state change
178   bool myIsFeatureVisibleInCash; /// boolean value if the feature was visible when cash if filled
179   std::vector<double> myXValueInCash; /// the cashed X value during value state change
180   std::vector<double> myYValueInCash; /// the cashed Y value during value state change
181   std::vector<double> myWeightInCash; /// the cached Weight value during valude state change
182
183   std::string myWeightsAttr;
184
185   int myPointIndex; /// index of the changing point
186
187   bool myFinished; /// \c true if building the B-spline is finished (escape pressed)
188 };
189
190 #endif