Salome HOME
NRI : Merge from V1_2.
[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 using namespace std;
13 #include "SALOMEGUI_SetValueDlg.h"
14 #include "QAD_Tools.h"
15
16 #include <qgroupbox.h>
17 #include <qlabel.h>
18 #include <qlineedit.h>
19 #include <qpushbutton.h>
20 #include <qlayout.h>
21 #include <qvalidator.h>
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 /*!
123   Gets string value
124 */
125 QString SALOMEGUI_SetValueDlg::getString ( const QString& caption, 
126                                            const QString& label, 
127                                            const QString& oldValue,
128                                            bool*          ok, 
129                                            QWidget*       parent )
130 {
131   QString v = QString::null;
132   SALOMEGUI_SetValueDlg* dlg = new SALOMEGUI_SetValueDlg( parent );
133   dlg->setCaption( caption );
134   dlg->setLabel( label );
135   if ( !oldValue.isNull() )
136     dlg->setValue( oldValue );
137   int result = dlg->exec();
138   if ( result == QDialog::Accepted ) 
139     v = dlg->value();
140   if ( ok ) 
141     *ok = result == QDialog::Accepted;
142   delete dlg;
143   return v;
144 }
145
146 /*!
147   Gets integer value
148 */
149 int SALOMEGUI_SetValueDlg::getInteger( const QString& caption, 
150                                        const QString& label, 
151                                        const int      oldValue,
152                                        bool*          ok, 
153                                        QWidget*       parent )
154 {
155   int v = 0;
156   SALOMEGUI_SetValueDlg* dlg = new SALOMEGUI_SetValueDlg( parent );
157   dlg->setCaption( caption );
158   dlg->setLabel( label );
159   dlg->setValidator( new QIntValidator( dlg ) );
160   dlg->setValue( QString::number( oldValue ) );
161   int result = dlg->exec();
162   if ( result == QDialog::Accepted ) 
163     v = dlg->value().toInt();
164   if ( ok ) 
165     *ok = result == QDialog::Accepted;
166   delete dlg;
167   return v;
168 }
169
170 /*!
171   Gets double value
172 */
173 double SALOMEGUI_SetValueDlg::getDouble ( const QString& caption, 
174                                           const QString& label, 
175                                           const double   oldValue,
176                                           bool*          ok, 
177                                           QWidget*       parent )
178 {
179   double v = 0;
180   SALOMEGUI_SetValueDlg* dlg = new SALOMEGUI_SetValueDlg( parent );
181   dlg->setCaption( caption );
182   dlg->setLabel( label );
183   dlg->setValidator( new QDoubleValidator( dlg ) );
184   dlg->setValue( QString::number( oldValue ) );
185   int result = dlg->exec();
186   if ( result == QDialog::Accepted ) 
187     v = dlg->value().toDouble();
188   if ( ok ) 
189     *ok = result == QDialog::Accepted;
190   delete dlg;
191   return v;
192 }