]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetPoint2D.cpp
Salome HOME
Use validators to set import file formats
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetPoint2D.cpp
1 // File:        ModuleBase_WidgetPoint2D.cpp
2 // Created:     25 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <ModuleBase_WidgetPoint2D.h>
6 #include <ModuleBase_WidgetValueFeature.h>
7 #include <ModuleBase_DoubleSpinBox.h>
8 #include <ModuleBase_Tools.h>
9
10 #include <Config_Keywords.h>
11 #include <Config_WidgetAPI.h>
12
13 #include <Events_Loop.h>
14 #include <ModelAPI_Events.h>
15
16 #include <ModelAPI_Feature.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Object.h>
19 #include <GeomDataAPI_Point2D.h>
20 #include <GeomAPI_Pnt2d.h>
21
22 #include <QGroupBox>
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QEvent>
26 #include <QKeyEvent>
27
28 #include <cfloat>
29 #include <climits>
30
31 ModuleBase_WidgetPoint2D::ModuleBase_WidgetPoint2D(QWidget* theParent,
32                                                    const Config_WidgetAPI* theData,
33                                                    const std::string& theParentId)
34     : ModuleBase_ModelWidget(theParent, theData, theParentId)
35 {
36   myOptionParam = theData->getProperty(PREVIOUS_FEATURE_PARAM);
37   QString aPageName = QString::fromStdString(theData->getProperty(CONTAINER_PAGE_NAME));
38   myGroupBox = new QGroupBox(aPageName, theParent);
39   myGroupBox->setFlat(false);
40
41   QGridLayout* aGroupLay = new QGridLayout(myGroupBox);
42   ModuleBase_Tools::adjustMargins(aGroupLay);
43   aGroupLay->setColumnStretch(1, 1);
44   {
45     QLabel* aLabel = new QLabel(myGroupBox);
46     aLabel->setText("X");
47     aLabel->setPixmap(QPixmap(":pictures/x_point.png"));
48     aGroupLay->addWidget(aLabel, 0, 0);
49
50     myXSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
51     myXSpin->setMinimum(-DBL_MAX);
52     myXSpin->setMaximum(DBL_MAX);
53     myXSpin->setToolTip("X");
54     aGroupLay->addWidget(myXSpin, 0, 1);
55
56     connect(myXSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
57   }
58   {
59     QLabel* aLabel = new QLabel(myGroupBox);
60     aLabel->setText("Y");
61     aLabel->setPixmap(QPixmap(":pictures/y_point.png"));
62     aGroupLay->addWidget(aLabel, 1, 0);
63
64     myYSpin = new ModuleBase_DoubleSpinBox(myGroupBox);
65     myYSpin->setMinimum(-DBL_MAX);
66     myYSpin->setMaximum(DBL_MAX);
67     myYSpin->setToolTip("X");
68     aGroupLay->addWidget(myYSpin, 1, 1);
69
70     connect(myYSpin, SIGNAL(valueChanged(double)), this, SIGNAL(valuesChanged()));
71   }
72 }
73
74 ModuleBase_WidgetPoint2D::~ModuleBase_WidgetPoint2D()
75 {
76 }
77
78 bool ModuleBase_WidgetPoint2D::setValue(ModuleBase_WidgetValue* theValue)
79 {
80   bool isDone = false;
81   if (theValue) {
82     ModuleBase_WidgetValueFeature* aFeatureValue =
83         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
84     if (aFeatureValue) {
85       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = aFeatureValue->point();
86       if (aPoint) {
87         setPoint(aPoint);
88         isDone = true;
89       }
90     }
91   }
92   return isDone;
93 }
94
95 void ModuleBase_WidgetPoint2D::setPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
96 {
97
98   bool isBlocked = this->blockSignals(true);
99   myXSpin->setValue(thePoint->x());
100   myYSpin->setValue(thePoint->y());
101   this->blockSignals(isBlocked);
102
103   emit valuesChanged();
104 }
105
106 bool ModuleBase_WidgetPoint2D::storeValue() const
107 {
108   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
109   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
110       aData->attribute(attributeID()));
111
112   ModuleBase_WidgetPoint2D* that = (ModuleBase_WidgetPoint2D*) this;
113   bool isBlocked = that->blockSignals(true);
114   aPoint->setValue(myXSpin->value(), myYSpin->value());
115   updateObject(myFeature);
116   that->blockSignals(isBlocked);
117
118   return true;
119 }
120
121 bool ModuleBase_WidgetPoint2D::restoreValue()
122 {
123   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
124   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
125       aData->attribute(attributeID()));
126
127   bool isBlocked = this->blockSignals(true);
128   myXSpin->setValue(aPoint->x());
129   myYSpin->setValue(aPoint->y());
130   this->blockSignals(isBlocked);
131   return true;
132 }
133
134 QWidget* ModuleBase_WidgetPoint2D::getControl() const
135 {
136   return myGroupBox;
137 }
138
139 QList<QWidget*> ModuleBase_WidgetPoint2D::getControls() const
140 {
141   QList<QWidget*> aControls;
142   aControls.push_back(myXSpin);
143   aControls.push_back(myYSpin);
144
145   return aControls;
146 }
147
148 bool ModuleBase_WidgetPoint2D::initFromPrevious(ObjectPtr theObject)
149 {
150   if (myOptionParam.length() == 0)
151     return false;
152   boost::shared_ptr<ModelAPI_Data> aData = theObject->data();
153   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154       aData->attribute(myOptionParam));
155   if (aPoint) {
156     bool isBlocked = this->blockSignals(true);
157     myXSpin->setValue(aPoint->x());
158     myYSpin->setValue(aPoint->y());
159     this->blockSignals(isBlocked);
160
161     emit valuesChanged();
162     emit storedPoint2D(theObject, myOptionParam);
163     return true;
164   }
165   return false;
166 }