1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModuleBase_WidgetPointInput.h"
22 #include "ModuleBase_Tools.h"
23 #include "ModuleBase_ParamSpinBox.h"
24 #include "ModuleBase_ViewerPrs.h"
26 #include <GeomAPI_Vertex.h>
27 #include <GeomAPI_Pnt.h>
29 #include <GeomDataAPI_Point.h>
31 #include <Config_WidgetAPI.h>
32 #include <Config_Keywords.h>
34 #include <QFormLayout>
38 #define ERR_STRING "ERROR"
40 ModuleBase_WidgetPointInput::ModuleBase_WidgetPointInput(QWidget* theParent,
41 ModuleBase_IWorkshop* theWorkshop,
42 const Config_WidgetAPI* theData)
43 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
45 myDefaultValue[0] = 0;
46 myDefaultValue[1] = 0;
47 myDefaultValue[2] = 0;
48 bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
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();
59 QFormLayout* aMainlayout = new QFormLayout(this);
60 ModuleBase_Tools::adjustMargins(aMainlayout);
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);
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);
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);
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()));
91 ModuleBase_WidgetPointInput::~ModuleBase_WidgetPointInput()
97 //********************************************************************
98 QList<QWidget*> ModuleBase_WidgetPointInput::getControls() const
100 QList<QWidget*> aList;
101 aList.append(myXSpin);
102 aList.append(myYSpin);
103 aList.append(myZSpin);
107 std::string getParmText(ModuleBase_ParamSpinBox* theSpin, FeaturePtr& theParam)
109 QString aText = theSpin->text();
110 if (aText.contains('=')) {
111 if (!theParam.get()) {
112 theParam = ModuleBase_Tools::createParameter(aText);
113 if (!theParam.get()) {
118 ModuleBase_Tools::editParameter(theParam, aText);
120 aText = aText.split('=').at(0);
122 return aText.toStdString();
125 //********************************************************************
126 bool ModuleBase_WidgetPointInput::storeValueCustom()
128 AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
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);
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);
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);
153 aAttr->setText(aXText, aYText, aZText);
155 aAttr->setValue(myXSpin->value(), myYSpin->value(), myZSpin->value());
158 aAttr->setValue(myDefaultValue[0], myDefaultValue[1], myDefaultValue[2]);
160 updateObject(myFeature);
166 //********************************************************************
167 bool ModuleBase_WidgetPointInput::restoreValueCustom()
169 AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
171 if (aAttr->isInitialized()) {
172 std::string aXText = aAttr->textX();
173 if (aXText.empty()) {
174 myXSpin->setValue(aAttr->x());
177 myXSpin->setText(aXText.c_str());
179 std::string aYText = aAttr->textY();
180 if (aYText.empty()) {
181 myYSpin->setValue(aAttr->y());
184 myYSpin->setText(aYText.c_str());
186 std::string aZText = aAttr->textZ();
187 if (aZText.empty()) {
188 myZSpin->setValue(aAttr->z());
191 myZSpin->setText(aZText.c_str());
195 aAttr->setValue(myDefaultValue[0], myDefaultValue[1], myDefaultValue[2]);
196 myXSpin->setValue(myDefaultValue[0]);
197 myYSpin->setValue(myDefaultValue[1]);
198 myZSpin->setValue(myDefaultValue[2]);
205 //********************************************************************
206 void ModuleBase_WidgetPointInput::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
208 theModuleSelectionModes = -1;
209 theModes << TopAbs_VERTEX;
212 //********************************************************************
213 QIntList ModuleBase_WidgetPointInput::shapeTypes() const
216 aList << TopAbs_VERTEX;
220 //********************************************************************
221 bool ModuleBase_WidgetPointInput
222 ::setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
223 const bool theToValidate)
225 if (theValues.size() == 1) {
226 GeomShapePtr aShape = theValues.first()->shape();
227 if (aShape->isVertex()) {
228 GeomVertexPtr aVertex(new GeomAPI_Vertex(aShape));
229 GeomPointPtr aPnt = aVertex->point();
230 myXSpin->setValue(aPnt->x());
231 myYSpin->setValue(aPnt->y());
232 myZSpin->setValue(aPnt->z());
239 //********************************************************************
240 bool ModuleBase_WidgetPointInput::processEnter()
242 bool isModified = getValueState() == ModifiedInPP;
244 emit valuesChanged();