Salome HOME
bb15571e86080ecce96c968471f54c295ac6db2f
[modules/geom.git] / src / CurveCreator / CurveCreator_NewPointDlg.cxx
1 // Copyright (C) 2013-2014  CEA/DEN, EDF R&D, OPEN CASCADE
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "CurveCreator_NewPointDlg.h"
21
22 #include <QGridLayout>
23 #include <QVBoxLayout>
24 #include <QLabel>
25 #include <QDoubleSpinBox>
26 #include <QDialogButtonBox>
27 #include <QDoubleValidator>
28 #include <QRegExpValidator>
29 #include <QAbstractButton>
30 #include <QPushButton>
31 #include <QLocale>
32
33 CurveCreator_NewPointDlg::CurveCreator_NewPointDlg(CurveCreator::Dimension theDim, QWidget *parent) :
34   QWidget(parent), myX(NULL), myY(NULL), myZ(NULL), myIsEdit(false), myDim(theDim),
35   myIsInstantSketchingEnabled(false)
36 {
37   QString aTitle = QString(tr("ADD_NEW_POINT"));
38   setWindowTitle(aTitle);
39
40   QFrame* aFrame = new QFrame( this );
41   QVBoxLayout* aLayout = new QVBoxLayout( aFrame );
42
43   QFrame* aCoordFrame = new QFrame( aFrame );
44   QGridLayout* aCoordLayout = new QGridLayout( aCoordFrame );
45
46   QLabel* aLbl = new QLabel( tr("X_COORD"), this);
47   myX = new QDoubleSpinBox(this);
48   aCoordLayout->addWidget(aLbl, 0, 0);
49   aCoordLayout->addWidget(myX, 0, 1 );
50
51   aLbl = new QLabel( tr("Y_COORD"), this);
52   myY = new QDoubleSpinBox(this);
53   aCoordLayout->addWidget(aLbl, 1, 0 );
54   aCoordLayout->addWidget(myY, 1, 1 );
55
56   myZLabel = new QLabel( tr("Z_COORD"), this);
57   myZ = new QDoubleSpinBox(this);
58   aCoordLayout->addWidget(myZLabel, 2,0 );
59   aCoordLayout->addWidget(myZ, 2,1 );
60
61   if( theDim != CurveCreator::Dim3d ){
62     myZ->hide();
63     myZLabel->hide();
64   }
65
66   myBtnFrame = new QFrame( aFrame );
67   QHBoxLayout* aBtnsLayout = new QHBoxLayout( myBtnFrame );
68
69   myAddBtn = new QPushButton( tr( "ADD_BTN" ), myBtnFrame );
70   myCancelBtn = new QPushButton( tr( "CANCEL" ), myBtnFrame );
71
72   connect( myCancelBtn, SIGNAL( clicked() ), this, SIGNAL( cancelPoint() ) );
73
74   aBtnsLayout->addWidget( myAddBtn );
75   aBtnsLayout->addStretch( 1 );
76   aBtnsLayout->addWidget( myCancelBtn );
77
78   aLayout->addWidget( aCoordFrame, 0 );
79   aLayout->addWidget( myBtnFrame, 1 );
80
81   clear();
82   updateTitle();
83 }
84
85 void CurveCreator_NewPointDlg::setSectionName( const QString& theName )
86 {
87   mySectionName = theName;
88   updateTitle();
89 }
90
91 void CurveCreator_NewPointDlg::setEditMode( bool isEdit )
92 {
93   myIsEdit = isEdit;
94   if( myIsEdit ){
95     myAddBtn->setText(tr("OK"));
96     myAddBtn->disconnect( SIGNAL( clicked() ) );
97     connect( myAddBtn, SIGNAL( clicked() ), this, SIGNAL( modifyPoint() ) );
98   }
99   else{
100     myAddBtn->setText(tr("ADD_BTN"));
101     myAddBtn->disconnect( SIGNAL( clicked() ) );
102     connect( myAddBtn, SIGNAL( clicked() ), this, SIGNAL( addPoint() ) );
103   }
104   updateTitle();
105 }
106
107 void CurveCreator_NewPointDlg::updateTitle()
108 {
109   QString aTitle;
110   if( !myIsEdit ){
111     if( mySectionName.isEmpty() ){
112       aTitle = tr("ADD_NEW_POINT");
113     }
114     else{
115       aTitle = QString(tr("ADD_NEW_POINT_TO_%1")).arg(mySectionName);
116     }
117   }
118   else{
119     aTitle = tr("SET_POINT_COORDINATES");
120   }
121   setWindowTitle(aTitle);
122 }
123
124 CurveCreator::Coordinates CurveCreator_NewPointDlg::getCoordinates() const
125 {
126   CurveCreator::Coordinates aCoords;
127   double anX = myX->value();
128   aCoords.push_back(anX);
129   double anY = myY->value();
130   aCoords.push_back(anY);
131   if( myDim == CurveCreator::Dim3d ){
132     double aZ = myZ->value();
133     aCoords.push_back(aZ);
134   }
135   return aCoords;
136 }
137
138 void CurveCreator_NewPointDlg::clear()
139 {
140   initSpinBox(myX);
141   initSpinBox(myY);
142   initSpinBox(myZ);
143 }
144
145 void CurveCreator_NewPointDlg::setDimension(CurveCreator::Dimension theDim)
146 {
147   if( theDim == CurveCreator::Dim2d ){
148     myZ->hide();
149     myZLabel->hide();
150   }
151   else{
152     myZ->show();
153     myZLabel->show();
154   }
155 }
156
157 void CurveCreator_NewPointDlg::setCoordinates( const CurveCreator::Coordinates& theCoords )
158 {
159   double anX = theCoords[0];
160   myX->setValue(anX);
161   double anY = theCoords[1];
162   myY->setValue(anY);
163   if( theCoords.size() == 3 ){
164     double aZ = theCoords[2];
165     myZ->setValue(aZ);
166   }
167   if( isInstantSketchingEnabled() )
168     emit addPoint();
169 }
170
171 bool CurveCreator_NewPointDlg::isInstantSketchingEnabled() const
172 {
173   return myIsInstantSketchingEnabled;
174 }
175
176 void CurveCreator_NewPointDlg::setInstantSketchingEnabled( const bool theState )
177 {
178   myIsInstantSketchingEnabled = theState;
179 }
180
181 //=======================================================================
182 // function: initSpinBox
183 // purpose:
184 //=======================================================================
185 void CurveCreator_NewPointDlg::initSpinBox(QDoubleSpinBox *theSpinBox)
186 {
187   const double aCoordMin  = -1.e+15;
188   const double aCoordMax  = 1.e+15;
189   const double aStep      = 10;
190   const int    aPrecision = 6;
191
192   theSpinBox->setDecimals( qAbs( aPrecision ) );
193   theSpinBox->setRange(aCoordMin, aCoordMax);
194   theSpinBox->setSingleStep(aStep);
195   theSpinBox->setValue(0.0);
196 }