Salome HOME
8c6300f88c288edfe196aa2092085e7ceec4dfcf
[modules/visu.git] / src / VISUGUI / VisuGUI_NameDlg.cxx
1 //  Copyright (C) 2007-2008  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 //  File   : VisuGUI_NameDlg.cxx
23 //  Author : Vadim SANDLER
24 //  Module : SALOME
25 //  $Header$
26 //
27 #include "VisuGUI_NameDlg.h"
28 #include "VisuGUI.h"
29
30 #include <SUIT_Session.h>
31 #include <SUIT_Application.h>
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Tools.h>
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_ResourceMgr.h>
36
37 #include <LightApp_Application.h>
38
39 #include <QGroupBox>
40 #include <QLabel>
41 #include <QLineEdit>
42 #include <QPushButton>
43 #include <QLayout>
44 #include <QKeyEvent>
45 using namespace std;
46
47 /*!
48   Constructor
49 */
50 VisuGUI_NameDlg::VisuGUI_NameDlg( QWidget* parent )
51     : QDialog( parent ? parent : SUIT_Session::session()->activeApplication()->desktop(), 
52                Qt::WindowTitleHint | Qt::WindowSystemMenuHint )//,WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
53 {
54   setWindowTitle( tr("TLT_RENAME") );
55   setSizeGripEnabled( TRUE );
56   setModal( true );
57
58   QVBoxLayout* topLayout = new QVBoxLayout( this );
59   topLayout->setMargin( 11 ); topLayout->setSpacing( 6 );
60
61   /***************************************************************/
62   QGroupBox* GroupC1 = new QGroupBox( this );  
63   //GroupC1->setColumnLayout(0, Qt::Vertical );
64   //GroupC1->layout()->setMargin( 0 ); GroupC1->layout()->setSpacing( 0 );
65   QHBoxLayout* GroupC1Layout = new QHBoxLayout( GroupC1 );
66   GroupC1Layout->setAlignment( Qt::AlignTop );
67   GroupC1Layout->setMargin( 11 ); GroupC1Layout->setSpacing( 6 );
68   
69   QLabel* TextLabel = new QLabel( tr( "NAME_LBL" ), GroupC1 );
70   GroupC1Layout->addWidget( TextLabel );
71   
72   myLineEdit = new QLineEdit( GroupC1 );
73   myLineEdit->setMinimumSize( 250, 0 );
74   GroupC1Layout->addWidget( myLineEdit );
75
76   /***************************************************************/
77   QGroupBox* GroupButtons = new QGroupBox( this );
78   //GroupButtons->setColumnLayout(0, Qt::Vertical );
79   //GroupButtons->layout()->setMargin( 0 ); GroupButtons->layout()->setSpacing( 0 ); 
80   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
81   GroupButtonsLayout->setAlignment( Qt::AlignTop );
82   GroupButtonsLayout->setMargin( 11 ); GroupButtonsLayout->setSpacing( 6 );
83   
84   myButtonOk = new QPushButton( GroupButtons );
85   myButtonOk->setText( tr( "BUT_OK"  ) );
86   myButtonOk->setAutoDefault( TRUE ); myButtonOk->setDefault( TRUE );
87   GroupButtonsLayout->addWidget( myButtonOk );
88
89   GroupButtonsLayout->addStretch();
90   
91   myButtonCancel = new QPushButton( GroupButtons );
92   myButtonCancel->setText( tr( "BUT_CANCEL"  ) );
93   myButtonCancel->setAutoDefault( TRUE );
94   GroupButtonsLayout->addWidget( myButtonCancel );
95
96   myButtonHelp = new QPushButton( GroupButtons );
97   myButtonHelp->setText( tr( "BUT_HELP"  ) );
98   myButtonHelp->setAutoDefault( TRUE );
99   GroupButtonsLayout->addWidget( myButtonHelp );
100   /***************************************************************/
101   
102   topLayout->addWidget( GroupC1 );
103   topLayout->addWidget( GroupButtons );
104   
105   // signals and slots connections
106   connect( myButtonOk,     SIGNAL( clicked() ), this, SLOT( accept() ) );
107   connect( myButtonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
108   connect( myButtonHelp,   SIGNAL( clicked() ), this, SLOT( onHelp() ) );
109   
110   /* Move widget on the botton right corner of main widget */
111   SUIT_Tools::centerWidget( this, parent );
112 }
113
114 /*!
115   Destructor
116 */
117 VisuGUI_NameDlg::~VisuGUI_NameDlg()
118 {
119 }
120
121 /*!
122   Sets name
123 */
124 void VisuGUI_NameDlg::setName( const QString& name )
125 {
126   myLineEdit->setText( name );
127   myLineEdit->end(false);
128   myLineEdit->home(true);
129 }
130
131 /*!
132   Returns name entered by user
133 */
134 QString VisuGUI_NameDlg::name()
135 {
136   return myLineEdit->text();
137 }
138
139 void VisuGUI_NameDlg::accept()
140 {
141   if ( name().trimmed().isEmpty() )
142     return;
143   QDialog::accept();
144 }
145
146 void VisuGUI_NameDlg::onHelp()
147 {
148   QString aHelpFileName = "viewing_3d_presentations_page.html#rename_anchor";
149   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
150   if (app) {
151     VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>( app->activeModule() );
152     app->onHelpContextModule(aVisuGUI ? app->moduleName(aVisuGUI->moduleName()) : QString(""), aHelpFileName);
153   }
154   else {
155     QString platform;
156 #ifdef WIN32
157     platform = "winapplication";
158 #else
159     platform = "application";
160 #endif
161     SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
162                              QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
163                              arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName) );
164   }
165 }
166
167 /*!
168   Creates modal <Rename> dialog and returns name entered [ static ]
169 */
170 QString VisuGUI_NameDlg::getName( QWidget* parent, const QString& oldName )
171 {
172   QString n;
173   VisuGUI_NameDlg* dlg = new VisuGUI_NameDlg( parent );
174   if ( !oldName.isNull() )
175     dlg->setName( oldName );
176   if ( dlg->exec() == QDialog::Accepted )
177     n = dlg->name();
178   delete dlg;
179   return n;
180 }
181
182 void VisuGUI_NameDlg::keyPressEvent( QKeyEvent* e )
183 {
184   QDialog::keyPressEvent( e );
185   if ( e->isAccepted() )
186     return;
187
188   if ( e->key() == Qt::Key_F1 )
189     {
190       e->accept();
191       onHelp();
192     }
193 }