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