Salome HOME
Fix a regression
[modules/visu.git] / src / VISUGUI / VisuGUI_NameDlg.cxx
1 //  SALOME VisuGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : VisuGUI_NameDlg.cxx
8 //  Author : Vadim SANDLER
9 //  Module : SALOME
10 //  $Header$
11
12 #include "VisuGUI_NameDlg.h"
13
14 #include <SUIT_Session.h>
15 #include <SUIT_Application.h>
16 #include <SUIT_Desktop.h>
17 #include <SUIT_Tools.h>
18
19 #include <qgroupbox.h>
20 #include <qlabel.h>
21 #include <qlineedit.h>
22 #include <qpushbutton.h>
23 #include <qlayout.h>
24 using namespace std;
25
26 /*!
27   Constructor
28 */
29 VisuGUI_NameDlg::VisuGUI_NameDlg( QWidget* parent )
30     : QDialog( parent ? parent : SUIT_Session::session()->activeApplication()->desktop(), 
31                "VisuGUI_NameDlg", 
32                true, 
33                WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
34 {
35   setCaption( tr("TLT_RENAME") );
36   setSizeGripEnabled( TRUE );
37   
38   QVBoxLayout* topLayout = new QVBoxLayout( this );
39   topLayout->setMargin( 11 ); topLayout->setSpacing( 6 );
40
41   /***************************************************************/
42   QGroupBox* GroupC1 = new QGroupBox( this, "GroupC1" );
43   GroupC1->setColumnLayout(0, Qt::Vertical );
44   GroupC1->layout()->setMargin( 0 ); GroupC1->layout()->setSpacing( 0 );
45   QHBoxLayout* GroupC1Layout = new QHBoxLayout( GroupC1->layout() );
46   GroupC1Layout->setAlignment( Qt::AlignTop );
47   GroupC1Layout->setMargin( 11 ); GroupC1Layout->setSpacing( 6 );
48   
49   QLabel* TextLabel = new QLabel( GroupC1, "TextLabel1" );
50   TextLabel->setText( tr( "NAME_LBL" ) );
51   GroupC1Layout->addWidget( TextLabel );
52   
53   myLineEdit = new QLineEdit( GroupC1, "LineEdit1" );
54   myLineEdit->setMinimumSize( 250, 0 );
55   GroupC1Layout->addWidget( myLineEdit );
56   
57   /***************************************************************/
58   QGroupBox* GroupButtons = new QGroupBox( this, "GroupButtons" );
59   GroupButtons->setColumnLayout(0, Qt::Vertical );
60   GroupButtons->layout()->setMargin( 0 ); GroupButtons->layout()->setSpacing( 0 ); 
61   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons->layout() );
62   GroupButtonsLayout->setAlignment( Qt::AlignTop );
63   GroupButtonsLayout->setMargin( 11 ); GroupButtonsLayout->setSpacing( 6 );
64   
65   myButtonOk = new QPushButton( GroupButtons, "buttonOk" );
66   myButtonOk->setText( tr( "BUT_OK"  ) );
67   myButtonOk->setAutoDefault( TRUE ); myButtonOk->setDefault( TRUE );
68   GroupButtonsLayout->addWidget( myButtonOk );
69
70   GroupButtonsLayout->addStretch();
71   
72   myButtonCancel = new QPushButton( GroupButtons, "buttonCancel" );
73   myButtonCancel->setText( tr( "BUT_CANCEL"  ) );
74   myButtonCancel->setAutoDefault( TRUE );
75   GroupButtonsLayout->addWidget( myButtonCancel );
76   /***************************************************************/
77   
78   topLayout->addWidget( GroupC1 );
79   topLayout->addWidget( GroupButtons );
80   
81   // signals and slots connections
82   connect( myButtonOk,     SIGNAL( clicked() ), this, SLOT( accept() ) );
83   connect( myButtonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
84   
85   /* Move widget on the botton right corner of main widget */
86   SUIT_Tools::centerWidget( this, parent );
87 }
88
89 /*!
90   Destructor
91 */
92 VisuGUI_NameDlg::~VisuGUI_NameDlg()
93 {
94 }
95
96 /*!
97   Sets name
98 */
99 void VisuGUI_NameDlg::setName( const QString& name )
100 {
101   myLineEdit->setText( name );
102   myLineEdit->end(false);
103   myLineEdit->home(true);
104 }
105
106 /*!
107   Returns name entered by user
108 */
109 QString VisuGUI_NameDlg::name()
110 {
111   return myLineEdit->text();
112 }
113
114 void VisuGUI_NameDlg::accept()
115 {
116   if ( name().stripWhiteSpace().isEmpty() )
117     return;
118   QDialog::accept();
119 }
120
121 /*!
122   Creates modal <Rename> dialog and returns name entered [ static ]
123 */
124 QString VisuGUI_NameDlg::getName( QWidget* parent, const QString& oldName )
125 {
126   QString n;
127   VisuGUI_NameDlg* dlg = new VisuGUI_NameDlg( parent );
128   if ( !oldName.isNull() )
129     dlg->setName( oldName );
130   if ( dlg->exec() == QDialog::Accepted ) 
131     n = dlg->name();
132   delete dlg;
133   return n;
134 }