1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ObstacleDlg.h"
25 #include "HYDROGUI_Tool.h"
27 #include <SUIT_FileDlg.h>
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_Session.h>
34 #include <QToolButton>
35 #include <QRadioButton>
36 #include <QButtonGroup>
40 static QString lastUsedFilter;
42 HYDROGUI_ObstacleDlg::HYDROGUI_ObstacleDlg( HYDROGUI_Module* theModule, const QString& theTitle,
43 const bool theIsToEnableFileSelection )
44 : HYDROGUI_InputPanel( theModule, theTitle ),
45 myFileSelectionEnabled ( theIsToEnableFileSelection ),
46 myDefaultName( tr("DEFAULT_OBSTACLE_NAME") )
48 // Get resource manager
49 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
52 QGroupBox* aFileNameGroup = 0;
54 if ( myFileSelectionEnabled ) {
55 aFileNameGroup = new QGroupBox( tr( "GET_SHAPE_FROM_FILE" ), this );
57 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
59 myFileName = new QLineEdit( aFileNameGroup );
60 myFileName->setReadOnly( true );
62 QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
63 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
65 QBoxLayout* aFileNameLayout = new QHBoxLayout( aFileNameGroup );
66 aFileNameLayout->setMargin( 5 );
67 aFileNameLayout->setSpacing( 5 );
68 aFileNameLayout->addWidget( aFileNameLabel );
69 aFileNameLayout->addWidget( myFileName );
70 aFileNameLayout->addWidget( aBrowseBtn );
72 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
76 myObstacleNameGroup = new QGroupBox( tr( "OBSTACLE_NAME" ), this );
78 QLabel* anObstacleNameLabel = new QLabel( tr( "NAME" ), myObstacleNameGroup );
79 myObstacleName = new QLineEdit( myObstacleNameGroup );
81 QBoxLayout* anObstacleNameLayout = new QHBoxLayout( myObstacleNameGroup );
82 anObstacleNameLayout->setMargin( 5 );
83 anObstacleNameLayout->setSpacing( 5 );
84 anObstacleNameLayout->addWidget( anObstacleNameLabel );
85 anObstacleNameLayout->addWidget( myObstacleName );
87 // Mode selector (create/edit)
88 myModeGroup = new QGroupBox( tr( "MODE" ), this );
90 QRadioButton* aNewObstacleRB = new QRadioButton( tr( "CREATE_NEW_OBSTACLE" ), mainFrame() );
91 QRadioButton* aModifyObstacleRB = new QRadioButton( tr( "MODIFY_OBSTACLE" ), mainFrame() );
93 myModeButtons = new QButtonGroup( mainFrame() );
94 myModeButtons->addButton( aNewObstacleRB, CreateNewId );
95 myModeButtons->addButton( aModifyObstacleRB, ModifyExistentId );
97 QBoxLayout* aModeSelectorLayout = new QHBoxLayout( myModeGroup );
98 aModeSelectorLayout->setMargin( 5 );
99 aModeSelectorLayout->setSpacing( 5 );
100 aModeSelectorLayout->addWidget( aNewObstacleRB );
101 aModeSelectorLayout->addWidget( aModifyObstacleRB );
103 // Existing obstacles selector
104 myObstaclesGroup = new QGroupBox( tr( "OBSTACLE_TO_EDIT" ), this );
106 QLabel* anObstacleLabel = new QLabel( tr( "OBSTACLE" ), myObstaclesGroup );
108 myObstacles = new QComboBox( mainFrame() );
109 myObstacles->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
111 QBoxLayout* anObstaclesSelectorLayout = new QHBoxLayout( myObstaclesGroup );
112 anObstaclesSelectorLayout->setMargin( 5 );
113 anObstaclesSelectorLayout->setSpacing( 5 );
114 anObstaclesSelectorLayout->addWidget( anObstacleLabel );
115 anObstaclesSelectorLayout->addWidget( myObstacles );
118 if ( aFileNameGroup ) {
119 addWidget( aFileNameGroup );
121 addWidget( myObstacleNameGroup );
122 addWidget( myModeGroup );
123 addWidget( myObstaclesGroup );
126 // Connect signals and slots
127 connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
128 connect( myObstacles, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onObstacleSelectionChanged( ) ) );
131 HYDROGUI_ObstacleDlg::~HYDROGUI_ObstacleDlg()
135 void HYDROGUI_ObstacleDlg::setObstacleNames( const QStringList& theObstacles )
137 myObstacles->clear();
138 myObstacles->addItems( theObstacles );
143 void HYDROGUI_ObstacleDlg::onModeActivated( int theMode )
149 QString HYDROGUI_ObstacleDlg::getObstacleName() const
151 return myObstacleName->text();
156 if ( myModeButtons->checkedId() == ModifyExistentId ) {
157 aName = myObstacles->currentText();
163 QString HYDROGUI_ObstacleDlg::getEditedObstacleName() const
167 if ( myModeButtons->checkedId() == ModifyExistentId ) {
168 aName = myObstacles->currentText();
174 QString HYDROGUI_ObstacleDlg::getFileName() const
176 return myFileName->text();
179 void HYDROGUI_ObstacleDlg::onBrowse()
181 SUIT_FileDlg* aFileDlg = new SUIT_FileDlg( this, true );
182 aFileDlg->setWindowTitle( tr("IMPORT_OBSTACLE_FROM_FILE") );
183 aFileDlg->setFilter( tr("OBSTACLE_FILTER") );
184 if ( !lastUsedFilter.isEmpty() ) {
185 aFileDlg->selectFilter( lastUsedFilter );
188 if ( aFileDlg->exec() == QDialog::Accepted ) {
189 QString aFileName = aFileDlg->selectedFile();
190 lastUsedFilter = aFileDlg->selectedFilter();
192 if ( !aFileName.isEmpty() ) {
193 myFileName->setText( aFileName );
201 void HYDROGUI_ObstacleDlg::reset()
203 if ( myFileSelectionEnabled ) {
206 myObstacleName->clear();
208 // Activate the creation mode
209 myModeButtons->button( CreateNewId )->setChecked( true );
210 onModeActivated( CreateNewId );
215 void HYDROGUI_ObstacleDlg::onObstacleSelectionChanged()
220 void HYDROGUI_ObstacleDlg::updateControls()
222 myObstacleNameGroup->setEnabled( !myFileSelectionEnabled ||
223 !myFileName->text().isEmpty() );
224 myModeGroup->setEnabled( myObstacleNameGroup->isEnabled() &&
225 myObstacles->count() > 0 );
226 myObstaclesGroup->setEnabled( myModeButtons->checkedId() == ModifyExistentId );
229 void HYDROGUI_ObstacleDlg::updateObjectName()
234 int aMode = myModeButtons->checkedId();
235 if ( aMode == CreateNewId ) {
236 if ( myFileSelectionEnabled ) {
237 QString aFileName = myFileName->text();
238 if ( !aFileName.isEmpty() ) {
239 QFileInfo aFileInfo( aFileName );
240 aName = HYDROGUI_Tool::GenerateObjectName(
241 module(), aFileInfo.baseName(), QStringList(), true );
244 aName = HYDROGUI_Tool::GenerateObjectName(
245 module(), getDefaultName(), QStringList(), true );
247 } else if ( aMode == ModifyExistentId ) {
248 aName = getEditedObstacleName();
251 myObstacleName->setText( aName );
254 void HYDROGUI_ObstacleDlg::setDefaultName( const QString& theName )
256 myDefaultName = theName;
261 QString HYDROGUI_ObstacleDlg::getDefaultName()
263 return myDefaultName;