Salome HOME
updated copyright message
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetPointInput.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 L"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(tr("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(tr("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(tr("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::wstring 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.toStdWString();
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::wstring 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::wstring 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::wstring 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->setText(L"", L"", L"");
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::wstring aXText = aAttr->textX();
173       if (aXText.empty()) {
174         myXSpin->setValue(aAttr->x());
175       }
176       else {
177         myXSpin->setText(QString::fromStdWString(aXText));
178       }
179       std::wstring aYText = aAttr->textY();
180       if (aYText.empty()) {
181         myYSpin->setValue(aAttr->y());
182       }
183       else {
184         myYSpin->setText(QString::fromStdWString(aYText));
185       }
186       std::wstring aZText = aAttr->textZ();
187       if (aZText.empty()) {
188         myZSpin->setValue(aAttr->z());
189       }
190       else {
191         myZSpin->setText(QString::fromStdWString(aZText));
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     setValueState(Stored);
201     return true;
202   }
203   return false;
204 }
205
206 //********************************************************************
207 void ModuleBase_WidgetPointInput::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
208 {
209   theModuleSelectionModes = -1;
210   theModes << TopAbs_VERTEX;
211 }
212
213 //********************************************************************
214 QIntList ModuleBase_WidgetPointInput::shapeTypes() const
215 {
216   QIntList aList;
217   aList << TopAbs_VERTEX;
218   return aList;
219 }
220
221 //********************************************************************
222 bool ModuleBase_WidgetPointInput
223 ::setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
224   const bool theToValidate)
225 {
226   if (theValues.size() == 1) {
227     GeomShapePtr aShape = theValues.first()->shape();
228     if (aShape.get() && aShape->isVertex()) {
229       GeomVertexPtr aVertex(new GeomAPI_Vertex(aShape));
230       GeomPointPtr aPnt = aVertex->point();
231       myXSpin->setValue(aPnt->x());
232       myYSpin->setValue(aPnt->y());
233       myZSpin->setValue(aPnt->z());
234       return true;
235     }
236   }
237   return false;
238 }
239
240 //********************************************************************
241 bool ModuleBase_WidgetPointInput::processEnter()
242 {
243   bool isModified = getValueState() == ModifiedInPP;
244   if (isModified) {
245     emit valuesChanged();
246   }
247   return isModified;
248 }