Salome HOME
770357923d04fed894acf46b5382f5b7044c71cf
[modules/visu.git] / src / VISUGUI / VisuGUI_NameDlg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : VisuGUI_NameDlg.cxx
24 //  Author : Vadim SANDLER
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "VisuGUI_NameDlg.h"
29 #include "VisuGUI.h"
30
31 #include <SUIT_Session.h>
32 #include <SUIT_Application.h>
33 #include <SUIT_Desktop.h>
34 #include <SUIT_Tools.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_ResourceMgr.h>
37
38 #include <LightApp_Application.h>
39
40 #include <QGroupBox>
41 #include <QLabel>
42 #include <QLineEdit>
43 #include <QPushButton>
44 #include <QLayout>
45 #include <QKeyEvent>
46 using namespace std;
47
48 /*!
49   Constructor
50 */
51 VisuGUI_NameDlg::VisuGUI_NameDlg( QWidget* parent )
52     : QDialog( parent ? parent : SUIT_Session::session()->activeApplication()->desktop(), 
53                Qt::WindowTitleHint | Qt::WindowSystemMenuHint )//,WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
54 {
55   setWindowTitle( tr("TLT_RENAME") );
56   setSizeGripEnabled( TRUE );
57   setModal( true );
58
59   QVBoxLayout* topLayout = new QVBoxLayout( this );
60   topLayout->setMargin( 11 ); topLayout->setSpacing( 6 );
61
62   /***************************************************************/
63   QGroupBox* GroupC1 = new QGroupBox( this );  
64   //GroupC1->setColumnLayout(0, Qt::Vertical );
65   //GroupC1->layout()->setMargin( 0 ); GroupC1->layout()->setSpacing( 0 );
66   QHBoxLayout* GroupC1Layout = new QHBoxLayout( GroupC1 );
67   GroupC1Layout->setAlignment( Qt::AlignTop );
68   GroupC1Layout->setMargin( 11 ); GroupC1Layout->setSpacing( 6 );
69   
70   QLabel* TextLabel = new QLabel( tr( "NAME_LBL" ), GroupC1 );
71   GroupC1Layout->addWidget( TextLabel );
72   
73   myLineEdit = new QLineEdit( GroupC1 );
74   myLineEdit->setMinimumSize( 250, 0 );
75   GroupC1Layout->addWidget( myLineEdit );
76
77   /***************************************************************/
78   QGroupBox* GroupButtons = new QGroupBox( this );
79   //GroupButtons->setColumnLayout(0, Qt::Vertical );
80   //GroupButtons->layout()->setMargin( 0 ); GroupButtons->layout()->setSpacing( 0 ); 
81   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
82   GroupButtonsLayout->setAlignment( Qt::AlignTop );
83   GroupButtonsLayout->setMargin( 11 ); GroupButtonsLayout->setSpacing( 6 );
84   
85   myButtonOk = new QPushButton( GroupButtons );
86   myButtonOk->setText( tr( "BUT_OK"  ) );
87   myButtonOk->setAutoDefault( TRUE ); myButtonOk->setDefault( TRUE );
88   GroupButtonsLayout->addWidget( myButtonOk );
89
90   GroupButtonsLayout->addStretch();
91   
92   myButtonCancel = new QPushButton( GroupButtons );
93   myButtonCancel->setText( tr( "BUT_CANCEL"  ) );
94   myButtonCancel->setAutoDefault( TRUE );
95   GroupButtonsLayout->addWidget( myButtonCancel );
96
97   myButtonHelp = new QPushButton( GroupButtons );
98   myButtonHelp->setText( tr( "BUT_HELP"  ) );
99   myButtonHelp->setAutoDefault( TRUE );
100   GroupButtonsLayout->addWidget( myButtonHelp );
101   /***************************************************************/
102   
103   topLayout->addWidget( GroupC1 );
104   topLayout->addWidget( GroupButtons );
105   
106   // signals and slots connections
107   connect( myButtonOk,     SIGNAL( clicked() ), this, SLOT( accept() ) );
108   connect( myButtonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
109   connect( myButtonHelp,   SIGNAL( clicked() ), this, SLOT( onHelp() ) );
110   
111   /* Move widget on the botton right corner of main widget */
112   SUIT_Tools::centerWidget( this, parent );
113 }
114
115 /*!
116   Destructor
117 */
118 VisuGUI_NameDlg::~VisuGUI_NameDlg()
119 {
120 }
121
122 /*!
123   Sets name
124 */
125 void VisuGUI_NameDlg::setName( const QString& name )
126 {
127   myLineEdit->setText( name );
128   myLineEdit->end(false);
129   myLineEdit->home(true);
130 }
131
132 /*!
133   Returns name entered by user
134 */
135 QString VisuGUI_NameDlg::name()
136 {
137   return myLineEdit->text();
138 }
139
140 void VisuGUI_NameDlg::accept()
141 {
142   if ( name().trimmed().isEmpty() )
143     return;
144   QDialog::accept();
145 }
146
147 void VisuGUI_NameDlg::onHelp()
148 {
149   QString aHelpFileName = "viewing_3d_presentations_page.html#rename_anchor";
150   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
151   if (app) {
152     VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>( app->activeModule() );
153     app->onHelpContextModule(aVisuGUI ? app->moduleName(aVisuGUI->moduleName()) : QString(""), aHelpFileName);
154   }
155   else {
156     QString platform;
157 #ifdef WIN32
158     platform = "winapplication";
159 #else
160     platform = "application";
161 #endif
162     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
163                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
164                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName) );
165   }
166 }
167
168 /*!
169   Creates modal <Rename> dialog and returns name entered [ static ]
170 */
171 QString VisuGUI_NameDlg::getName( QWidget* parent, const QString& oldName )
172 {
173   QString n;
174   VisuGUI_NameDlg* dlg = new VisuGUI_NameDlg( parent );
175   if ( !oldName.isNull() )
176     dlg->setName( oldName );
177   if ( dlg->exec() == QDialog::Accepted )
178     n = dlg->name();
179   delete dlg;
180   return n;
181 }
182
183 void VisuGUI_NameDlg::keyPressEvent( QKeyEvent* e )
184 {
185   QDialog::keyPressEvent( e );
186   if ( e->isAccepted() )
187     return;
188
189   if ( e->key() == Qt::Key_F1 )
190     {
191       e->accept();
192       onHelp();
193     }
194 }