Salome HOME
Issue #2666: Create parameter on parametrical point control input
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetPointInput.cpp
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 #include "ModuleBase_WidgetPointInput.h"
22 #include "ModuleBase_Tools.h"
23 #include "ModuleBase_ParamSpinBox.h"
24 #include "ModuleBase_ViewerPrs.h"
25
26 #include <GeomAPI_Vertex.h>
27 #include <GeomAPI_Pnt.h>
28
29 #include <GeomDataAPI_Point.h>
30
31 #include <Config_WidgetAPI.h>
32 #include <Config_Keywords.h>
33
34 #include <QFormLayout>
35 #include <QLabel>
36
37
38 #define ERR_STRING "ERROR"
39
40 ModuleBase_WidgetPointInput::ModuleBase_WidgetPointInput(QWidget* theParent,
41   ModuleBase_IWorkshop* theWorkshop,
42   const Config_WidgetAPI* theData)
43   : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
44 {
45   myDefaultValue[0] = 0;
46   myDefaultValue[1] = 0;
47   myDefaultValue[2] = 0;
48   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
49
50   std::string aDefValuesStr = theData->getProperty(ATTR_DEFAULT);
51   if (!aDefValuesStr.empty()) {
52     QString aDefVal(aDefValuesStr.c_str());
53     QStringList aStrArray = aDefVal.split(';', QString::SkipEmptyParts);
54     if (aStrArray.length() == 3) {
55       for (int i = 0; i < 3; i++)
56         myDefaultValue[i] = aStrArray.at(i).toDouble();
57     }
58   }
59   QFormLayout* aMainlayout = new QFormLayout(this);
60   ModuleBase_Tools::adjustMargins(aMainlayout);
61
62   myXSpin = new ModuleBase_ParamSpinBox(this);
63   myXSpin->setAcceptVariables(aAcceptVariables);
64   myXSpin->setToolTip("X coordinate");
65   myXSpin->setValue(myDefaultValue[0]);
66   QLabel* aXLbl = new QLabel(this);
67   aXLbl->setPixmap(QPixmap(":pictures/x_size.png"));
68   aMainlayout->addRow(aXLbl, myXSpin);
69
70   myYSpin = new ModuleBase_ParamSpinBox(this);
71   myYSpin->setAcceptVariables(aAcceptVariables);
72   myYSpin->setToolTip("Y coordinate");
73   myYSpin->setValue(myDefaultValue[1]);
74   QLabel* aYLbl = new QLabel(this);
75   aYLbl->setPixmap(QPixmap(":pictures/y_size.png"));
76   aMainlayout->addRow(aYLbl, myYSpin);
77
78   myZSpin = new ModuleBase_ParamSpinBox(this);
79   myZSpin->setAcceptVariables(aAcceptVariables);
80   myZSpin->setToolTip("Z coordinate");
81   myZSpin->setValue(myDefaultValue[2]);
82   QLabel* aZLbl = new QLabel(this);
83   aZLbl->setPixmap(QPixmap(":pictures/z_size.png"));
84   aMainlayout->addRow(aZLbl, myZSpin);
85
86   connect(myXSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
87   connect(myYSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
88   connect(myZSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
89 }
90
91 ModuleBase_WidgetPointInput::~ModuleBase_WidgetPointInput()
92 {
93
94 }
95
96
97 //********************************************************************
98 QList<QWidget*> ModuleBase_WidgetPointInput::getControls() const
99 {
100   QList<QWidget*> aList;
101   aList.append(myXSpin);
102   aList.append(myYSpin);
103   aList.append(myZSpin);
104   return aList;
105 }
106
107 std::string getParmText(ModuleBase_ParamSpinBox* theSpin, FeaturePtr& theParam)
108 {
109   QString aText = theSpin->text();
110   if (aText.contains('=')) {
111     if (!theParam.get()) {
112       theParam = ModuleBase_Tools::createParameter(aText);
113       if (!theParam.get()) {
114         return ERR_STRING;
115       }
116     }
117     else {
118       ModuleBase_Tools::editParameter(theParam, aText);
119     }
120     aText = aText.split('=').at(0);
121   }
122   return aText.toStdString();
123 }
124
125 //********************************************************************
126 bool ModuleBase_WidgetPointInput::storeValueCustom()
127 {
128   AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
129   if (aAttr.get()) {
130     if (aAttr->isInitialized()) {
131       if (myXSpin->hasVariable() || myYSpin->hasVariable() || myZSpin->hasVariable()) {
132         std::string aXText = getParmText(myXSpin, myXParam);
133         if (aXText == ERR_STRING) {
134           aAttr->setExpressionError(0, "Parameter cannot be created");
135           aAttr->setExpressionInvalid(0, true);
136           updateObject(myFeature);
137           return false;
138         }
139         std::string aYText = getParmText(myYSpin, myYParam);
140         if (aYText == ERR_STRING) {
141           aAttr->setExpressionError(1, "Parameter cannot be created");
142           aAttr->setExpressionInvalid(1, true);
143           updateObject(myFeature);
144           return false;
145         }
146         std::string aZText = getParmText(myZSpin, myZParam);
147         if (aZText == ERR_STRING) {
148           aAttr->setExpressionError(2, "Parameter cannot be created");
149           aAttr->setExpressionInvalid(2, true);
150           updateObject(myFeature);
151           return false;
152         }
153         aAttr->setText(aXText, aYText, aZText);
154       } else {
155         aAttr->setValue(myXSpin->value(), myYSpin->value(), myZSpin->value());
156       }
157     } else {
158       aAttr->setValue(myDefaultValue[0], myDefaultValue[1], myDefaultValue[2]);
159     }
160     updateObject(myFeature);
161     return true;
162   }
163   return false;
164 }
165
166 //********************************************************************
167 bool ModuleBase_WidgetPointInput::restoreValueCustom()
168 {
169   AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
170   if (aAttr.get()) {
171     if (aAttr->isInitialized()) {
172       std::string aXText = aAttr->textX();
173       if (aXText.empty()) {
174         myXSpin->setValue(aAttr->x());
175       }
176       else {
177         myXSpin->setText(aXText.c_str());
178       }
179       std::string aYText = aAttr->textY();
180       if (aYText.empty()) {
181         myYSpin->setValue(aAttr->y());
182       }
183       else {
184         myYSpin->setText(aYText.c_str());
185       }
186       std::string aZText = aAttr->textZ();
187       if (aZText.empty()) {
188         myZSpin->setValue(aAttr->z());
189       }
190       else {
191         myZSpin->setText(aZText.c_str());
192       }
193     }
194     else {
195       aAttr->setValue(myDefaultValue[0], myDefaultValue[1], myDefaultValue[2]);
196       myXSpin->setValue(myDefaultValue[0]);
197       myYSpin->setValue(myDefaultValue[1]);
198       myZSpin->setValue(myDefaultValue[2]);
199     }
200     return true;
201   }
202   return false;
203 }
204
205 //********************************************************************
206 void ModuleBase_WidgetPointInput::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
207 {
208   theModuleSelectionModes = -1;
209   theModes << TopAbs_VERTEX;
210 }
211
212 //********************************************************************
213 QIntList ModuleBase_WidgetPointInput::shapeTypes() const
214 {
215   QIntList aList;
216   aList << TopAbs_VERTEX;
217   return aList;
218 }
219
220 //********************************************************************
221 bool ModuleBase_WidgetPointInput
222 ::setSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
223 {
224   GeomShapePtr aShape = thePrs->shape();
225   if (aShape->isVertex()) {
226     GeomVertexPtr aVertex(new GeomAPI_Vertex(aShape));
227     GeomPointPtr aPnt = aVertex->point();
228     myXSpin->setValue(aPnt->x());
229     myYSpin->setValue(aPnt->y());
230     myZSpin->setValue(aPnt->z());
231     return true;
232   }
233   return false;
234 }
235
236 //********************************************************************
237 bool ModuleBase_WidgetPointInput::processEnter()
238 {
239   bool isModified = getValueState() == ModifiedInPP;
240   if (isModified) {
241     emit valuesChanged();
242   }
243   return isModified;
244 }