Salome HOME
9ff544a07de019c164ab9fb35f6d10cc9f4b1fee
[modules/gui.git] / src / Plot2d / Plot2d_FitDataDlg.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA 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.
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 //  File   : Plot2d_FitDataDlg.cxx
20 //  Author : Vadim SANDLER
21 //  Module : SALOME
22 //  $Header$
23
24 #include "Plot2d_FitDataDlg.h"
25 #include <qlabel.h>
26 #include <qlayout.h>
27 #include <qvalidator.h>
28 #include <qpushbutton.h>
29 #include <qradiobutton.h>
30 #include <qbuttongroup.h>
31 #include <qlineedit.h>
32
33 #define SPACING_SIZE      6
34 #define MARGIN_SIZE       11
35 #define MIN_EDIT_SIZE     100
36
37 /*!
38   Constructor 
39 */
40 Plot2d_FitDataDlg::Plot2d_FitDataDlg( QWidget* parent, bool secondAxisY )
41      : QDialog( parent ? parent : 0,
42          "Plot2d_FitDataDlg", 
43          true, 
44          WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
45          myY2MinEdit( 0 ), myY2MaxEdit( 0 ), mySecondAxisY( secondAxisY )
46
47 {
48   setCaption( tr( "FIT_DATA_TLT" ) );
49   setSizeGripEnabled( TRUE );
50   QGridLayout* topLayout = new QGridLayout( this ); 
51   topLayout->setSpacing( SPACING_SIZE );
52   topLayout->setMargin( MARGIN_SIZE );
53
54   // 'Range' group
55   myRangeGrp = new QButtonGroup( this );
56   myRangeGrp->setColumnLayout( 0, Qt::Vertical );
57   myRangeGrp->layout()->setSpacing( 0 );
58   myRangeGrp->layout()->setMargin( 0 );
59   QGridLayout* aGridLayout = new QGridLayout( myRangeGrp->layout() );
60   aGridLayout->setAlignment( Qt::AlignTop );
61   aGridLayout->setMargin( MARGIN_SIZE );
62   aGridLayout->setSpacing( SPACING_SIZE );
63
64   myModeAllRB  = new QRadioButton( tr( "FIT_ALL" ),        myRangeGrp );
65   myModeHorRB  = new QRadioButton( tr( "FIT_HORIZONTAL" ), myRangeGrp );
66   myModeVerRB  = new QRadioButton( tr( "FIT_VERTICAL" ),   myRangeGrp );
67
68   QDoubleValidator* aValidator = new QDoubleValidator( this );
69   myXMinEdit = new QLineEdit( myRangeGrp );
70   myXMinEdit->setValidator( aValidator );
71   myXMinEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
72   myXMinEdit->setMinimumSize( MIN_EDIT_SIZE, 0 );
73   myXMinEdit->setText( "0.0" );
74
75   myYMinEdit = new QLineEdit( myRangeGrp );
76   myYMinEdit->setValidator( aValidator );
77   myYMinEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
78   myYMinEdit->setMinimumSize( MIN_EDIT_SIZE, 0 );
79   myYMinEdit->setText( "0.0" );
80
81   myXMaxEdit = new QLineEdit( myRangeGrp );
82   myXMaxEdit->setValidator( aValidator );
83   myXMaxEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
84   myXMaxEdit->setMinimumSize( MIN_EDIT_SIZE, 0 );
85   myXMaxEdit->setText( "0.0" );
86
87   myYMaxEdit = new QLineEdit( myRangeGrp );
88   myYMaxEdit->setValidator( aValidator );
89   myYMaxEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
90   myYMaxEdit->setMinimumSize( MIN_EDIT_SIZE, 0 );
91   myYMaxEdit->setText( "0.0" );
92
93   if (mySecondAxisY) {
94     myY2MinEdit = new QLineEdit( myRangeGrp );
95     myY2MinEdit->setValidator( aValidator );
96     myY2MinEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
97     myY2MinEdit->setMinimumSize( MIN_EDIT_SIZE, 0 );
98     myY2MinEdit->setText( "0.0" );
99
100     myY2MaxEdit = new QLineEdit( myRangeGrp );
101     myY2MaxEdit->setValidator( aValidator );
102     myY2MaxEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
103     myY2MaxEdit->setMinimumSize( MIN_EDIT_SIZE, 0 );
104     myY2MaxEdit->setText( "0.0" );
105   }
106
107   QFrame* aHLine = new QFrame( myRangeGrp );
108   aHLine->setFrameStyle( QFrame::HLine | QFrame::Sunken );
109
110   QHBoxLayout* aModeLayout = new QHBoxLayout;
111   aModeLayout->setMargin( 0 );
112   aModeLayout->setSpacing( SPACING_SIZE );
113   aModeLayout->addWidget( myModeAllRB );
114   aModeLayout->addWidget( myModeHorRB );
115   aModeLayout->addWidget( myModeVerRB );
116
117   QLabel* horLab = new QLabel( tr( "HORIZONTAL_AXIS" ), myRangeGrp );
118   QLabel* verLab = new QLabel( tr( "VERTICAL_AXIS" ), myRangeGrp );
119   if (mySecondAxisY)
120     verLab->setText( tr( "VERTICAL_LEFT_AXIS" ) );
121
122   QFont font = horLab->font(); font.setBold( true );
123   horLab->setFont( font ); verLab->setFont( font );
124
125   aGridLayout->addMultiCellLayout( aModeLayout,    0, 0, 0, 4 );
126   aGridLayout->addMultiCellWidget( aHLine,         1, 1, 0, 4 );
127   aGridLayout->addWidget         ( horLab,         2,    0 );
128   aGridLayout->addWidget         ( new QLabel( tr( "MIN_VALUE_LAB" ), myRangeGrp ), 
129                                                    2,    1 );
130   aGridLayout->addWidget         ( myXMinEdit,     2,    2 );
131   aGridLayout->addWidget         ( new QLabel( tr( "MAX_VALUE_LAB" ), myRangeGrp ), 
132                                                    2,    3 );
133   aGridLayout->addWidget         ( myXMaxEdit,     2,    4 );
134   aGridLayout->addWidget         ( verLab,         3,    0 );
135   aGridLayout->addWidget         ( new QLabel( tr( "MIN_VALUE_LAB" ), myRangeGrp ), 
136                                                    3,    1 );
137   aGridLayout->addWidget         ( myYMinEdit,     3,    2 );
138   aGridLayout->addWidget         ( new QLabel( tr( "MAX_VALUE_LAB" ), myRangeGrp ), 
139                                                    3,    3 );
140   aGridLayout->addWidget         ( myYMaxEdit,     3,    4 );
141
142   if (mySecondAxisY) {
143     QLabel* ver2Lab = new QLabel(tr( "VERTICAL_RIGHT_AXIS" ), myRangeGrp );
144     ver2Lab->setFont( font );
145     aGridLayout->addWidget       ( ver2Lab,        4,    0 );
146     aGridLayout->addWidget       ( new QLabel( tr( "MIN_VALUE_LAB" ), myRangeGrp ), 
147                                                    4,    1 );
148     aGridLayout->addWidget       ( myY2MinEdit,    4,    2 );
149     aGridLayout->addWidget       ( new QLabel( tr( "MAX_VALUE_LAB" ), myRangeGrp ), 
150                                                    4,    3 );
151     aGridLayout->addWidget       ( myY2MaxEdit,    4,    4 );
152   }
153
154   // OK/Cancel buttons
155   myOkBtn = new QPushButton( tr( "BUT_OK" ), this, "buttonOk" );
156   myOkBtn->setAutoDefault( TRUE );
157   myOkBtn->setDefault( TRUE );
158   myCancelBtn = new QPushButton(  tr( "BUT_CANCEL" ), this, "buttonCancel" );
159   myCancelBtn->setAutoDefault( TRUE );
160
161   topLayout->addMultiCellWidget( myRangeGrp, 0, 0, 0, 2 );
162   topLayout->addWidget( myOkBtn, 1, 0 );
163   topLayout->setColStretch( 1, 5 );
164   topLayout->addWidget( myCancelBtn, 1, 2 );
165
166   // connect signals
167   connect( myOkBtn,      SIGNAL( clicked() ),      this, SLOT( accept() ) );
168   connect( myCancelBtn,  SIGNAL( clicked() ),      this, SLOT( reject() ) );
169   connect( myRangeGrp,   SIGNAL( clicked( int ) ), this, SLOT( onModeChanged( int ) ) );
170
171   // initial state
172   myModeAllRB->setChecked( true );
173   onModeChanged( 0 );
174 }
175
176 /*!
177   Sets range
178 */
179 void Plot2d_FitDataDlg::setRange( const double xMin, 
180                                   const double xMax,
181                                   const double yMin,
182                                   const double yMax,
183                                   const double y2Min,
184                                   const double y2Max) 
185 {
186   myXMinEdit->setText( QString::number( xMin ) );
187   myXMaxEdit->setText( QString::number( xMax ) );
188   myYMinEdit->setText( QString::number( yMin ) );
189   myYMaxEdit->setText( QString::number( yMax ) );
190   if (mySecondAxisY) {
191     myY2MinEdit->setText( QString::number( y2Min ) );
192     myY2MaxEdit->setText( QString::number( y2Max ) );
193   }
194 }
195
196 /*!
197   Gets range, returns mode (see getMode())
198 */
199 int Plot2d_FitDataDlg::getRange( double& xMin, 
200                                  double& xMax,
201                                  double& yMin,
202                                  double& yMax,
203                                  double& y2Min,
204                                  double& y2Max) 
205 {
206   xMin = myXMinEdit->text().toDouble();
207   xMax = myXMaxEdit->text().toDouble();
208   yMin = myYMinEdit->text().toDouble();
209   yMax = myYMaxEdit->text().toDouble();
210   if (mySecondAxisY) {
211     y2Min = myY2MinEdit->text().toDouble();
212     y2Max = myY2MaxEdit->text().toDouble();
213   }
214   else {
215     y2Min = 0;
216     y2Max = 0;
217   }
218   int myMode = 0;
219   if ( myModeAllRB->isChecked() )
220     myMode = 0;
221   if ( myModeHorRB->isChecked() )
222     myMode = 1;
223   if ( myModeVerRB->isChecked() )
224     myMode = 2;
225   return myMode;
226 }
227
228 /*!
229   Gets mode : 0 - Fit all; 1 - Fit horizontal, 2 - Fit vertical
230 */
231 int Plot2d_FitDataDlg::getMode() 
232 {
233   int myMode = 0;
234   if ( myModeAllRB->isChecked() )
235     myMode = 0;
236   if ( myModeHorRB->isChecked() )
237     myMode = 1;
238   if ( myModeVerRB->isChecked() )
239     myMode = 2;
240   return myMode;
241 }
242
243 /*!
244   Called when range mode changed
245 */
246 void Plot2d_FitDataDlg::onModeChanged(int mode) 
247 {
248   bool badFocus;
249   switch( mode ) {
250   case 0: // fit all mode
251     myXMinEdit->setEnabled(true);
252     myXMaxEdit->setEnabled(true);
253     myYMinEdit->setEnabled(true);
254     myYMaxEdit->setEnabled(true);
255     if (mySecondAxisY) {
256       myY2MinEdit->setEnabled(true);
257       myY2MaxEdit->setEnabled(true);
258     }
259     break;
260   case 1: // fit horizontal mode
261     badFocus = myYMinEdit->hasFocus() || myYMaxEdit->hasFocus();
262     myXMinEdit->setEnabled(true);
263     myXMaxEdit->setEnabled(true);
264     myYMinEdit->setEnabled(false);
265     myYMaxEdit->setEnabled(false);
266     if (mySecondAxisY) {
267       myY2MinEdit->setEnabled(false);
268       myY2MaxEdit->setEnabled(false);
269     }
270     if (badFocus)
271       myXMinEdit->setFocus();
272     break;
273   case 2: // fit vertical mode
274     badFocus = myXMinEdit->hasFocus() || myXMaxEdit->hasFocus();
275     myXMinEdit->setEnabled(false);
276     myXMaxEdit->setEnabled(false);
277     myYMinEdit->setEnabled(true);
278     myYMaxEdit->setEnabled(true);
279     if (mySecondAxisY) {
280       myY2MinEdit->setEnabled(true);
281       myY2MaxEdit->setEnabled(true);
282     }
283     if (badFocus)
284       myYMinEdit->setFocus();
285     break;
286   }
287 }
288