1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModuleBase_WidgetPointInput.h"
21 #include "ModuleBase_Tools.h"
22 #include "ModuleBase_ParamSpinBox.h"
23 #include "ModuleBase_ViewerPrs.h"
25 #include <GeomAPI_Vertex.h>
26 #include <GeomAPI_Pnt.h>
28 #include <GeomDataAPI_Point.h>
30 #include <Config_WidgetAPI.h>
31 #include <Config_Keywords.h>
33 #include <QFormLayout>
37 #define ERR_STRING "ERROR"
39 ModuleBase_WidgetPointInput::ModuleBase_WidgetPointInput(QWidget* theParent,
40 ModuleBase_IWorkshop* theWorkshop,
41 const Config_WidgetAPI* theData)
42 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
44 myDefaultValue[0] = 0;
45 myDefaultValue[1] = 0;
46 myDefaultValue[2] = 0;
47 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
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();
58 QFormLayout* aMainlayout = new QFormLayout(this);
59 ModuleBase_Tools::adjustMargins(aMainlayout);
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);
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);
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);
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()));
90 ModuleBase_WidgetPointInput::~ModuleBase_WidgetPointInput()
96 //********************************************************************
97 QList<QWidget*> ModuleBase_WidgetPointInput::getControls() const
99 QList<QWidget*> aList;
100 aList.append(myXSpin);
101 aList.append(myYSpin);
102 aList.append(myZSpin);
106 std::string getParmText(ModuleBase_ParamSpinBox* theSpin, FeaturePtr& theParam)
108 QString aText = theSpin->text();
109 if (aText.contains('=')) {
110 if (!theParam.get()) {
111 theParam = ModuleBase_Tools::createParameter(aText);
112 if (!theParam.get()) {
117 ModuleBase_Tools::editParameter(theParam, aText);
119 aText = aText.split('=').at(0);
121 return aText.toStdString();
124 //********************************************************************
125 bool ModuleBase_WidgetPointInput::storeValueCustom()
127 AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
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);
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);
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);
152 aAttr->setText(aXText, aYText, aZText);
154 aAttr->setValue(myXSpin->value(), myYSpin->value(), myZSpin->value());
157 aAttr->setValue(myDefaultValue[0], myDefaultValue[1], myDefaultValue[2]);
159 updateObject(myFeature);
165 //********************************************************************
166 bool ModuleBase_WidgetPointInput::restoreValueCustom()
168 AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
170 if (aAttr->isInitialized()) {
171 std::string aXText = aAttr->textX();
172 if (aXText.empty()) {
173 myXSpin->setValue(aAttr->x());
176 myXSpin->setText(aXText.c_str());
178 std::string aYText = aAttr->textY();
179 if (aYText.empty()) {
180 myYSpin->setValue(aAttr->y());
183 myYSpin->setText(aYText.c_str());
185 std::string aZText = aAttr->textZ();
186 if (aZText.empty()) {
187 myZSpin->setValue(aAttr->z());
190 myZSpin->setText(aZText.c_str());
194 aAttr->setValue(myDefaultValue[0], myDefaultValue[1], myDefaultValue[2]);
195 myXSpin->setValue(myDefaultValue[0]);
196 myYSpin->setValue(myDefaultValue[1]);
197 myZSpin->setValue(myDefaultValue[2]);
204 //********************************************************************
205 void ModuleBase_WidgetPointInput::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
207 theModuleSelectionModes = -1;
208 theModes << TopAbs_VERTEX;
211 //********************************************************************
212 QIntList ModuleBase_WidgetPointInput::shapeTypes() const
215 aList << TopAbs_VERTEX;
219 //********************************************************************
220 bool ModuleBase_WidgetPointInput
221 ::setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
222 const bool theToValidate)
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());
238 //********************************************************************
239 bool ModuleBase_WidgetPointInput::processEnter()
241 bool isModified = getValueState() == ModifiedInPP;
243 emit valuesChanged();