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