]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_BSplineWidget.cpp
Salome HOME
Fix coding style problems.
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_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 <SketchPlugin_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 SketchPlugin_BSplineWidget::SketchPlugin_BSplineWidget(
39     QWidget* theParent,
40     const Config_WidgetAPI* theData)
41   : ModuleBase_ModelWidget(theParent, theData)
42 {
43   QVBoxLayout* aMainLayout = new QVBoxLayout(this);
44
45   // GroupBox to keep widgets for B-spline poles and weights
46   myPolesGroupBox = new QGroupBox(tr("Poles and weights"), theParent);
47   aMainLayout->addWidget(myPolesGroupBox);
48   // layout of GroupBox
49   QGridLayout* aGroupLayout = new QGridLayout(myPolesGroupBox);
50   aGroupLayout->setSpacing(4);
51   aGroupLayout->setColumnStretch(1, 1);
52
53   restoreValueCustom();
54 }
55
56 void SketchPlugin_BSplineWidget::setFeature(const FeaturePtr& theFeature,
57                                             const bool theToStoreValue,
58                                             const bool isUpdateFlushed)
59 {
60   ModuleBase_ModelWidget::setFeature(theFeature, theToStoreValue, isUpdateFlushed);
61   restoreValueCustom();
62 }
63
64 void SketchPlugin_BSplineWidget::deactivate()
65 {
66   ModuleBase_ModelWidget::deactivate();
67   storeValueCustom();
68 }
69
70
71 QList<QWidget*> SketchPlugin_BSplineWidget::getControls() const
72 {
73   QList<QWidget*> aControls;
74   std::list<BSplinePoleWidgets>::const_iterator anIt = myPoles.begin();
75   for (; anIt != myPoles.end(); ++anIt) {
76     aControls.append(anIt->myX);
77     aControls.append(anIt->myY);
78     aControls.append(anIt->myWeight);
79   }
80   return aControls;
81 }
82
83 void SketchPlugin_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 SketchPlugin_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   return true;
120 }
121
122 bool SketchPlugin_BSplineWidget::restoreValueCustom()
123 {
124   if (!myFeature)
125     return false;
126
127   DataPtr aData = myFeature->data();
128
129   AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
130       aData->attribute(SketchPlugin_BSpline::POLES_ID()));
131   AttributeDoubleArrayPtr aWeights = aData->realArray(SketchPlugin_BSpline::WEIGHTS_ID());
132
133   while (myPoles.size() < aPoles->size())
134     addPoleWidget();
135
136   std::list<BSplinePoleWidgets>::iterator anIt = myPoles.begin();
137   for (int anIndex = 0; anIt != myPoles.end(); ++anIt, ++anIndex) {
138     GeomPnt2dPtr aPoint = aPoles->pnt(anIndex);
139     anIt->myX->setValue(aPoint->x());
140     anIt->myY->setValue(aPoint->y());
141     anIt->myWeight->setValue(aWeights->value(anIndex));
142   }
143
144   return true;
145 }
146
147 void SketchPlugin_BSplineWidget::addPoleWidget()
148 {
149   QGridLayout* aGroupLay = dynamic_cast<QGridLayout*>(myPolesGroupBox->layout());
150
151   int aNbPoles = (int)myPoles.size();
152
153   QString aPoleStr = tr("Pole %1");
154   aPoleStr = aPoleStr.arg(aNbPoles + 1);
155
156   QGroupBox* aPoleGroupBox = new QGroupBox(aPoleStr, myPolesGroupBox);
157   QFormLayout* aPoleLay = new QFormLayout(aPoleGroupBox);
158   ModuleBase_Tools::adjustMargins(aPoleLay);
159   aPoleLay->setSpacing(2);
160
161   myPoles.push_back(BSplinePoleWidgets());
162   BSplinePoleWidgets& aPoleWidgets = myPoles.back();
163
164   aPoleWidgets.myX = new ModuleBase_LabelValue(aPoleGroupBox, tr("X"));
165   aPoleLay->addRow(aPoleWidgets.myX);
166   aPoleWidgets.myY = new ModuleBase_LabelValue(aPoleGroupBox, tr("Y"));
167   aPoleLay->addRow(aPoleWidgets.myY);
168   aPoleWidgets.myWeight = new ModuleBase_ParamSpinBox(aPoleGroupBox);
169   aPoleWidgets.myWeight->setMinimum(0.0);
170   aPoleLay->addRow(tr("Weight") + " : ", aPoleWidgets.myWeight);
171
172   aGroupLay->addWidget(aPoleGroupBox, aNbPoles, 1);
173
174   // we should listen textChanged signal as valueChanged do not send when text is modified
175   connect(aPoleWidgets.myWeight, SIGNAL(textChanged(const QString&)),
176           this, SIGNAL(valuesModified()));
177 }