Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[modules/kernel.git] / src / SALOMEGUI / SALOMEGUI_SetValueDlg.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SALOMEGUI_SetValueDlg.cxx
8 //  Author : Vadim SANDLER
9 //  Module : SALOME
10 //  $Header$
11
12 #include "SALOMEGUI_SetValueDlg.h"
13 #include "QAD_Tools.h"
14
15 #include <qgroupbox.h>
16 #include <qlabel.h>
17 #include <qlineedit.h>
18 #include <qpushbutton.h>
19 #include <qlayout.h>
20 #include <qvalidator.h>
21 using namespace std;
22
23 #define MARGIN_SIZE     11
24 #define SPACING_SIZE    6
25 /*!
26   Constructor
27 */
28 SALOMEGUI_SetValueDlg::SALOMEGUI_SetValueDlg( QWidget* parent )
29     : QDialog( parent, "SALOMEGUI_SetValueDlg", true, 
30                WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
31 {
32   setCaption( tr("TLT_SETVALUE") );
33   setSizeGripEnabled( TRUE );
34   
35   QVBoxLayout* topLayout = new QVBoxLayout( this );
36   topLayout->setMargin( MARGIN_SIZE ); topLayout->setSpacing( SPACING_SIZE );
37
38   /***************************************************************/
39   QGroupBox* TopGroup = new QGroupBox( this, "TopGroup" );
40   TopGroup->setColumnLayout(0, Qt::Vertical );
41   TopGroup->layout()->setMargin( 0 ); TopGroup->layout()->setSpacing( 0 );
42   QHBoxLayout* TopGroupLayout = new QHBoxLayout( TopGroup->layout() );
43   TopGroupLayout->setAlignment( Qt::AlignTop );
44   TopGroupLayout->setMargin( MARGIN_SIZE ); TopGroupLayout->setSpacing( SPACING_SIZE );
45   
46   myLabel = new QLabel( tr( "VALUE_LBL" ), TopGroup, "myLabel" );
47   myLineEdit = new QLineEdit( TopGroup, "myLineEdit" );
48   myLineEdit->setMinimumSize( 250, 0 );
49
50   TopGroupLayout->addWidget( myLabel );
51   TopGroupLayout->addWidget( myLineEdit );
52   
53   /***************************************************************/
54   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
55   GroupButtons->setColumnLayout(0, Qt::Vertical );
56   GroupButtons->layout()->setMargin( 0 ); GroupButtons->layout()->setSpacing( 0 ); 
57   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons->layout() );
58   GroupButtonsLayout->setAlignment( Qt::AlignTop );
59   GroupButtonsLayout->setMargin( MARGIN_SIZE ); GroupButtonsLayout->setSpacing( SPACING_SIZE );
60   
61   myButtonOk = new QPushButton( tr( "BUT_OK"  ), GroupButtons, "myButtonOk" );
62   myButtonOk->setAutoDefault( TRUE ); myButtonOk->setDefault( TRUE );
63   myButtonCancel = new QPushButton( tr( "BUT_CANCEL"  ), GroupButtons, "myButtonCancel" );
64   myButtonCancel->setAutoDefault( TRUE );
65
66   GroupButtonsLayout->addWidget( myButtonOk );
67   GroupButtonsLayout->addStretch();
68   GroupButtonsLayout->addWidget( myButtonCancel );
69   /***************************************************************/
70   
71   topLayout->addWidget( TopGroup );
72   topLayout->addWidget( GroupButtons );
73   
74   // signals and slots connections
75   connect( myButtonOk,     SIGNAL( clicked() ), this, SLOT( accept() ) );
76   connect( myButtonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
77   
78   /* Move widget on the botton right corner of main widget */
79   QAD_Tools::centerWidget( this, parent );
80 }
81
82 /*!
83   Destructor
84 */
85 SALOMEGUI_SetValueDlg::~SALOMEGUI_SetValueDlg()
86 {
87 }
88
89 /*!
90   Sets value
91 */
92 void SALOMEGUI_SetValueDlg::setValue( const QString& value )
93 {
94   myLineEdit->setText( value );
95   myLineEdit->selectAll();
96 }
97
98 /*!
99   Returns value entered by user
100 */
101 QString SALOMEGUI_SetValueDlg::value()
102 {
103   return myLineEdit->text();
104 }
105
106 /*!
107   Sets validator
108 */
109 void SALOMEGUI_SetValueDlg::setValidator( QValidator* v )
110 {
111   myLineEdit->setValidator( v );
112 }
113
114 /*!
115   Sets label text
116 */
117 void SALOMEGUI_SetValueDlg::setLabel( const QString& label )
118 {
119   myLabel->setText( label );
120 }
121
122 void SALOMEGUI_SetValueDlg::accept()
123 {
124   const QValidator* v = myLineEdit->validator();
125   if ( v ) {
126     if ( v->inherits( "QIntValidator" ) ) {
127       bool b;
128       int val = myLineEdit->text().toInt( &b );
129       const QIntValidator* iv = dynamic_cast<const QIntValidator*>(v);
130       if ( !b || val < iv->bottom() || val > iv->top())
131         return;
132     }
133   }
134   QDialog::accept();
135 }
136
137 /*!
138   Gets string value
139 */
140 QString SALOMEGUI_SetValueDlg::getString ( const QString& caption, 
141                                            const QString& label, 
142                                            const QString& oldValue,
143                                            bool*          ok, 
144                                            QWidget*       parent )
145 {
146   QString v = QString::null;
147   SALOMEGUI_SetValueDlg* dlg = new SALOMEGUI_SetValueDlg( parent );
148   dlg->setCaption( caption );
149   dlg->setLabel( label );
150   if ( !oldValue.isNull() )
151     dlg->setValue( oldValue );
152   int result = dlg->exec();
153   if ( result == QDialog::Accepted ) 
154     v = dlg->value();
155   if ( ok ) 
156     *ok = result == QDialog::Accepted;
157   delete dlg;
158   return v;
159 }
160
161 /*!
162   Gets integer value
163 */
164 int SALOMEGUI_SetValueDlg::getInteger( const QString& caption, 
165                                        const QString& label, 
166                                        const int      oldValue,
167                                        bool*          ok, 
168                                        QWidget*       parent )
169 {
170   int v = 0;
171   SALOMEGUI_SetValueDlg* dlg = new SALOMEGUI_SetValueDlg( parent );
172   dlg->setCaption( caption );
173   dlg->setLabel( label );
174   dlg->setValidator( new QIntValidator( dlg ) );
175   dlg->setValue( QString::number( oldValue ) );
176   int result = dlg->exec();
177   if ( result == QDialog::Accepted ) 
178     v = dlg->value().toInt();
179   if ( ok ) 
180     *ok = result == QDialog::Accepted;
181   delete dlg;
182   return v;
183 }
184
185 /*!
186   Gets integer value
187 */
188 int SALOMEGUI_SetValueDlg::getInteger( const QString& caption, 
189                                        const QString& label, 
190                                        int            bottom,
191                                        int            top,
192                                        const int      oldValue,
193                                        bool*          ok, 
194                                        QWidget*       parent )
195 {
196   int v = 0;
197   SALOMEGUI_SetValueDlg* dlg = new SALOMEGUI_SetValueDlg( parent );
198   dlg->setCaption( caption );
199   dlg->setLabel( label );
200   dlg->setValidator( new QIntValidator( bottom, top, dlg ) );
201   dlg->setValue( QString::number( oldValue ) );
202   int result = dlg->exec();
203   if ( result == QDialog::Accepted ) 
204     v = dlg->value().toInt();
205   if ( ok ) 
206     *ok = result == QDialog::Accepted;
207   delete dlg;
208   return v;
209 }
210
211 /*!
212   Gets double value
213 */
214 double SALOMEGUI_SetValueDlg::getDouble ( const QString& caption, 
215                                           const QString& label, 
216                                           const double   oldValue,
217                                           bool*          ok, 
218                                           QWidget*       parent )
219 {
220   double v = 0;
221   SALOMEGUI_SetValueDlg* dlg = new SALOMEGUI_SetValueDlg( parent );
222   dlg->setCaption( caption );
223   dlg->setLabel( label );
224   dlg->setValidator( new QDoubleValidator( dlg ) );
225   dlg->setValue( QString::number( oldValue ) );
226   int result = dlg->exec();
227   if ( result == QDialog::Accepted ) 
228     v = dlg->value().toDouble();
229   if ( ok ) 
230     *ok = result == QDialog::Accepted;
231   delete dlg;
232   return v;
233 }