Salome HOME
1c41a1ab5010f733087bca48a5212dc131c2b57c
[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, ModuleBase_IWorkshop* theWorkshop,
38   const Config_WidgetAPI* theData)
39   : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
40 {
41   bool aAcceptVariables = theData->getBooleanAttribute(DOUBLE_WDG_ACCEPT_EXPRESSIONS, true);
42
43   QFormLayout* aMainlayout = new QFormLayout(this);
44   ModuleBase_Tools::adjustMargins(aMainlayout);
45
46   myXSpin = new ModuleBase_ParamSpinBox(this);
47   myXSpin->setAcceptVariables(aAcceptVariables);
48   myXSpin->setToolTip("X coordinate");
49   myXSpin->setValue(0);
50   QLabel* aXLbl = new QLabel(this);
51   aXLbl->setPixmap(QPixmap(":pictures/x_size.png"));
52   aMainlayout->addRow(aXLbl, myXSpin);
53
54   myYSpin = new ModuleBase_ParamSpinBox(this);
55   myYSpin->setAcceptVariables(aAcceptVariables);
56   myYSpin->setToolTip("Y coordinate");
57   myYSpin->setValue(0);
58   QLabel* aYLbl = new QLabel(this);
59   aYLbl->setPixmap(QPixmap(":pictures/y_size.png"));
60   aMainlayout->addRow(aYLbl, myYSpin);
61
62   myZSpin = new ModuleBase_ParamSpinBox(this);
63   myZSpin->setAcceptVariables(aAcceptVariables);
64   myZSpin->setToolTip("Z coordinate");
65   myZSpin->setValue(0);
66   QLabel* aZLbl = new QLabel(this);
67   aZLbl->setPixmap(QPixmap(":pictures/z_size.png"));
68   aMainlayout->addRow(aZLbl, myZSpin);
69 }
70
71 ModuleBase_WidgetPointInput::~ModuleBase_WidgetPointInput()
72 {
73
74 }
75
76
77 //********************************************************************
78 QList<QWidget*> ModuleBase_WidgetPointInput::getControls() const
79 {
80   QList<QWidget*> aList;
81   aList.append(myXSpin);
82   aList.append(myYSpin);
83   aList.append(myZSpin);
84   return aList;
85 }
86
87 //********************************************************************
88 bool ModuleBase_WidgetPointInput::storeValueCustom()
89 {
90   AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
91   if (aAttr.get()) {
92     if (myXSpin->hasVariable() || myYSpin->hasVariable() || myZSpin->hasVariable()) {
93       aAttr->setText(myXSpin->text().toStdString(),
94         myYSpin->text().toStdString(), myZSpin->text().toStdString());
95     } else {
96       aAttr->setValue(myXSpin->value(), myYSpin->value(), myZSpin->value());
97     }
98     return true;
99   }
100   return false;
101 }
102
103 //********************************************************************
104 bool ModuleBase_WidgetPointInput::restoreValueCustom()
105 {
106   AttributePointPtr aAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(attribute());
107   if (aAttr.get()) {
108     std::string aXText = aAttr->textX();
109     if (aXText.empty()) {
110       myXSpin->setValue(aAttr->x());
111     } else {
112       myXSpin->setText(aXText.c_str());
113     }
114     std::string aYText = aAttr->textY();
115     if (aYText.empty()) {
116       myYSpin->setValue(aAttr->y());
117     } else {
118       myYSpin->setText(aYText.c_str());
119     }
120     std::string aZText = aAttr->textZ();
121     if (aZText.empty()) {
122       myZSpin->setValue(aAttr->z());
123     } else {
124       myZSpin->setText(aZText.c_str());
125     }
126     return true;
127   }
128   return false;
129 }
130
131 //********************************************************************
132 void ModuleBase_WidgetPointInput::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
133 {
134   theModuleSelectionModes = -1;
135   theModes << TopAbs_VERTEX;
136 }
137
138 //********************************************************************
139 QIntList ModuleBase_WidgetPointInput::shapeTypes() const
140 {
141   QIntList aList;
142   aList << TopAbs_VERTEX;
143   return aList;
144 }
145
146 //********************************************************************
147 bool ModuleBase_WidgetPointInput
148 ::setSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
149 {
150   GeomShapePtr aShape = thePrs->shape();
151   if (aShape->isVertex()) {
152     GeomVertexPtr aVertex(new GeomAPI_Vertex(aShape));
153     GeomPointPtr aPnt = aVertex->point();
154     myXSpin->setValue(aPnt->x());
155     myYSpin->setValue(aPnt->y());
156     myZSpin->setValue(aPnt->z());
157     return true;
158   }
159   return false;
160 }