]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_BSplineWidget.cpp
Salome HOME
Update B-Spline GUI
[modules/shaper.git] / src / PartSet / PartSet_BSplineWidget.cpp
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 #include <PartSet_BSplineWidget.h>
21
22 #include <SketchPlugin_BSpline.h>
23
24 #include <ModuleBase_Tools.h>
25
26 #include <ModelAPI_AttributeDoubleArray.h>
27
28 #include <GeomDataAPI_Point2DArray.h>
29
30 #include <GeomAPI_Pnt2d.h>
31
32 #include <QFormLayout>
33 #include <QGroupBox>
34 #include <QLabel>
35 #include <QVBoxLayout>
36
37
38 PartSet_BSplineWidget::PartSet_BSplineWidget(
39     QWidget* theParent,
40     const Config_WidgetAPI* theData)
41   : ModuleBase_ModelWidget(theParent, theData)
42 {
43   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
44   ModuleBase_Tools::adjustMargins(aMainLayout);
45
46   // GroupBox to keep widgets for B-spline poles and weights
47   myPolesGroupBox = new QGroupBox(tr("Poles and weights"), theParent);
48   aMainLayout->addWidget(myPolesGroupBox);
49   // layout of GroupBox
50   QGridLayout* aGroupLayout = new QGridLayout(myPolesGroupBox);
51   aGroupLayout->setSpacing(4);
52   aGroupLayout->setColumnStretch(1, 1);
53   ModuleBase_Tools::adjustMargins(aGroupLayout);
54
55   restoreValueCustom();
56 }
57
58 void PartSet_BSplineWidget::setFeature(const FeaturePtr& theFeature,
59                                             const bool theToStoreValue,
60                                             const bool isUpdateFlushed)
61 {
62   ModuleBase_ModelWidget::setFeature(theFeature, theToStoreValue, isUpdateFlushed);
63   restoreValueCustom();
64 }
65
66 void PartSet_BSplineWidget::deactivate()
67 {
68   ModuleBase_ModelWidget::deactivate();
69   storeValueCustom();
70 }
71
72
73 QList<QWidget*> PartSet_BSplineWidget::getControls() const
74 {
75   QList<QWidget*> aControls;
76   std::list<BSplinePoleWidgets>::const_iterator anIt = myPoles.begin();
77   for (; anIt != myPoles.end(); ++anIt) {
78     aControls.append(anIt->myWeight);
79   }
80   return aControls;
81 }
82
83 void PartSet_BSplineWidget::storePolesAndWeights() const
84 {
85   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
86   //AttributePoint2DArrayPtr aPointArray = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
87   //    aData->attribute(SketchPlugin_BSpline::POLES_ID()));
88   AttributeDoubleArrayPtr aWeightsArray = aData->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
89
90   //aPointArray->setSize((int)myPoles.size());
91   //aWeightsArray->setSize((int)myPoles.size());
92
93   std::list<BSplinePoleWidgets>::const_iterator anIt = myPoles.begin();
94   for (int anIndex = 0; anIt != myPoles.end(); ++anIndex, ++anIt) {
95     //aPointArray->setPnt(anIndex, anIt->myX->value(), anIt->myY->value());
96     aWeightsArray->setValue(anIndex, anIt->myWeight->value());
97   }
98 }
99
100 bool PartSet_BSplineWidget::storeValueCustom()
101 {
102   std::shared_ptr<ModelAPI_Data> aData = myFeature->data();
103   if (!aData || !aData->isValid()) // can be on abort of sketcher element
104     return false;
105
106   //AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
107   //    aData->attribute(SketchPlugin_BSpline::POLES_ID()));
108   AttributeDoubleArrayPtr aWeights = aData->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
109
110   bool isBlocked = blockSignals(true);
111   //bool isImmutable = aPoles->setImmutable(true);
112
113   storePolesAndWeights();
114   ModuleBase_Tools::flushUpdated(myFeature);
115
116   //aPoles->setImmutable(isImmutable);
117   blockSignals(isBlocked);
118
119   updateObject(myFeature);
120   return true;
121 }
122
123 bool PartSet_BSplineWidget::restoreValueCustom()
124 {
125   if (!myFeature)
126     return false;
127
128   DataPtr aData = myFeature->data();
129
130   AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
131       aData->attribute(SketchPlugin_BSpline::POLES_ID()));
132   AttributeDoubleArrayPtr aWeights = aData->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
133
134   while (myPoles.size() < aPoles->size())
135     addPoleWidget();
136
137   std::list<BSplinePoleWidgets>::iterator anIt = myPoles.begin();
138   for (int anIndex = 0; anIt != myPoles.end(); ++anIt, ++anIndex) {
139     GeomPnt2dPtr aPoint = aPoles->pnt(anIndex);
140     anIt->myX->setValue(aPoint->x());
141     anIt->myY->setValue(aPoint->y());
142     bool isBlocked = anIt->myWeight->blockSignals(true);
143     anIt->myWeight->setValue(aWeights->value(anIndex));
144     anIt->myWeight->blockSignals(isBlocked);
145   }
146
147   return true;
148 }
149
150 void PartSet_BSplineWidget::addPoleWidget()
151 {
152   QGridLayout* aGroupLay = dynamic_cast<QGridLayout*>(myPolesGroupBox->layout());
153   ModuleBase_Tools::adjustMargins(aGroupLay);
154
155   int aNbPoles = (int)myPoles.size();
156
157   QString aPoleStr = tr("Pole %1");
158   aPoleStr = aPoleStr.arg(aNbPoles + 1);
159
160   QGroupBox* aPoleGroupBox = new QGroupBox(aPoleStr, myPolesGroupBox);
161   QFormLayout* aPoleLay = new QFormLayout(aPoleGroupBox);
162   ModuleBase_Tools::adjustMargins(aPoleLay);
163   aPoleLay->setSpacing(2);
164
165   myPoles.push_back(BSplinePoleWidgets());
166   BSplinePoleWidgets& aPoleWidgets = myPoles.back();
167
168   aPoleWidgets.myX = new ModuleBase_LabelValue(aPoleGroupBox, tr("X"));
169   aPoleLay->addRow(aPoleWidgets.myX);
170   aPoleWidgets.myY = new ModuleBase_LabelValue(aPoleGroupBox, tr("Y"));
171   aPoleLay->addRow(aPoleWidgets.myY);
172   aPoleWidgets.myWeight = new ModuleBase_ParamSpinBox(aPoleGroupBox);
173   aPoleWidgets.myWeight->setMinimum(0.0);
174   aPoleLay->addRow(tr("Weight") + " : ", aPoleWidgets.myWeight);
175
176   aGroupLay->addWidget(aPoleGroupBox, aNbPoles, 1);
177
178   // we should listen textChanged signal as valueChanged do not send when text is modified
179   connect(aPoleWidgets.myWeight, SIGNAL(textChanged(const QString&)),
180     this, SIGNAL(valuesChanged()));
181 }