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