Salome HOME
Issue #2507: Bug fix
[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 ModuleBase_WidgetPointInput::ModuleBase_WidgetPointInput(QWidget* theParent,
38   ModuleBase_IWorkshop* theWorkshop,
39   const Config_WidgetAPI* theData)
40   : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
41 {
42   myDefaultValue[0] = 0;
43   myDefaultValue[1] = 0;
44   myDefaultValue[2] = 0;
45   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
46
47   std::string aDefValuesStr = theData->getProperty(ATTR_DEFAULT);
48   if (!aDefValuesStr.empty()) {
49     QString aDefVal(aDefValuesStr.c_str());
50     QStringList aStrArray = aDefVal.split(';', QString::SkipEmptyParts);
51     if (aStrArray.length() == 3) {
52       for (int i = 0; i < 3; i++)
53         myDefaultValue[i] = aStrArray.at(i).toDouble();
54     }
55   }
56   QFormLayout* aMainlayout = new QFormLayout(this);
57   ModuleBase_Tools::adjustMargins(aMainlayout);
58
59   myXSpin = new ModuleBase_ParamSpinBox(this);
60   myXSpin->setAcceptVariables(aAcceptVariables);
61   myXSpin->setToolTip("X coordinate");
62   myXSpin->setValue(myDefaultValue[0]);
63   QLabel* aXLbl = new QLabel(this);
64   aXLbl->setPixmap(QPixmap(":pictures/x_size.png"));
65   aMainlayout->addRow(aXLbl, myXSpin);
66
67   myYSpin = new ModuleBase_ParamSpinBox(this);
68   myYSpin->setAcceptVariables(aAcceptVariables);
69   myYSpin->setToolTip("Y coordinate");
70   myYSpin->setValue(myDefaultValue[1]);
71   QLabel* aYLbl = new QLabel(this);
72   aYLbl->setPixmap(QPixmap(":pictures/y_size.png"));
73   aMainlayout->addRow(aYLbl, myYSpin);
74
75   myZSpin = new ModuleBase_ParamSpinBox(this);
76   myZSpin->setAcceptVariables(aAcceptVariables);
77   myZSpin->setToolTip("Z coordinate");
78   myZSpin->setValue(myDefaultValue[2]);
79   QLabel* aZLbl = new QLabel(this);
80   aZLbl->setPixmap(QPixmap(":pictures/z_size.png"));
81   aMainlayout->addRow(aZLbl, myZSpin);
82
83   connect(myXSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
84   connect(myYSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
85   connect(myZSpin, SIGNAL(textChanged(const QString&)), this, SIGNAL(valuesModified()));
86 }
87
88 ModuleBase_WidgetPointInput::~ModuleBase_WidgetPointInput()
89 {
90
91 }
92
93
94 //********************************************************************
95 QList<QWidget*> ModuleBase_WidgetPointInput::getControls() const
96 {
97   QList<QWidget*> aList;
98   aList.append(myXSpin);
99   aList.append(myYSpin);
100   aList.append(myZSpin);
101   return aList;
102 }
103
104 //********************************************************************
105 bool ModuleBase_WidgetPointInput::storeValueCustom()
106 {
107   AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
108   if (aAttr.get()) {
109     if (aAttr->isInitialized()) {
110       if (myXSpin->hasVariable() || myYSpin->hasVariable() || myZSpin->hasVariable()) {
111         aAttr->setText(myXSpin->text().toStdString(),
112           myYSpin->text().toStdString(), myZSpin->text().toStdString());
113       } else {
114         aAttr->setValue(myXSpin->value(), myYSpin->value(), myZSpin->value());
115       }
116     } else {
117       aAttr->setValue(myDefaultValue[0], myDefaultValue[1], myDefaultValue[2]);
118     }
119     updateObject(myFeature);
120     return true;
121   }
122   return false;
123 }
124
125 //********************************************************************
126 bool ModuleBase_WidgetPointInput::restoreValueCustom()
127 {
128   AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
129   if (aAttr.get()) {
130     if (aAttr->isInitialized()) {
131       std::string aXText = aAttr->textX();
132       if (aXText.empty()) {
133         myXSpin->setValue(aAttr->x());
134       }
135       else {
136         myXSpin->setText(aXText.c_str());
137       }
138       std::string aYText = aAttr->textY();
139       if (aYText.empty()) {
140         myYSpin->setValue(aAttr->y());
141       }
142       else {
143         myYSpin->setText(aYText.c_str());
144       }
145       std::string aZText = aAttr->textZ();
146       if (aZText.empty()) {
147         myZSpin->setValue(aAttr->z());
148       }
149       else {
150         myZSpin->setText(aZText.c_str());
151       }
152     }
153     else {
154       aAttr->setValue(myDefaultValue[0], myDefaultValue[1], myDefaultValue[2]);
155       myXSpin->setValue(myDefaultValue[0]);
156       myYSpin->setValue(myDefaultValue[1]);
157       myZSpin->setValue(myDefaultValue[2]);
158     }
159     return true;
160   }
161   return false;
162 }
163
164 //********************************************************************
165 void ModuleBase_WidgetPointInput::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
166 {
167   theModuleSelectionModes = -1;
168   theModes << TopAbs_VERTEX;
169 }
170
171 //********************************************************************
172 QIntList ModuleBase_WidgetPointInput::shapeTypes() const
173 {
174   QIntList aList;
175   aList << TopAbs_VERTEX;
176   return aList;
177 }
178
179 //********************************************************************
180 bool ModuleBase_WidgetPointInput
181 ::setSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
182 {
183   GeomShapePtr aShape = thePrs->shape();
184   if (aShape->isVertex()) {
185     GeomVertexPtr aVertex(new GeomAPI_Vertex(aShape));
186     GeomPointPtr aPnt = aVertex->point();
187     myXSpin->setValue(aPnt->x());
188     myYSpin->setValue(aPnt->y());
189     myZSpin->setValue(aPnt->z());
190     return true;
191   }
192   return false;
193 }
194
195 //********************************************************************
196 bool ModuleBase_WidgetPointInput::processEnter()
197 {
198   bool isModified = getValueState() == ModifiedInPP;
199   if (isModified) {
200     emit valuesChanged();
201   }
202   return isModified;
203 }