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_GeomObjectDlg.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_GeomObjectDlg::HYDROGUI_GeomObjectDlg( HYDROGUI_Module* theModule, const QString& theTitle,
43 const QString& theObjectTypeName,
44 const bool theIsToEnableFileSelection )
45 : HYDROGUI_InputPanel( theModule, theTitle ),
46 myFileSelectionEnabled( theIsToEnableFileSelection ),
47 myDefaultName( theObjectTypeName )
49 // Get resource manager
50 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
53 QGroupBox* aFileNameGroup = 0;
55 if ( myFileSelectionEnabled ) {
56 aFileNameGroup = new QGroupBox( tr( "GET_SHAPE_FROM_FILE" ), this );
58 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
60 myFileName = new QLineEdit( aFileNameGroup );
61 myFileName->setReadOnly( true );
63 QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
64 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
66 QBoxLayout* aFileNameLayout = new QHBoxLayout( aFileNameGroup );
67 aFileNameLayout->setMargin( 5 );
68 aFileNameLayout->setSpacing( 5 );
69 aFileNameLayout->addWidget( aFileNameLabel );
70 aFileNameLayout->addWidget( myFileName );
71 aFileNameLayout->addWidget( aBrowseBtn );
73 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
77 myNameGroup = new QGroupBox( tr( "OBJECT_NAME" ).arg( theObjectTypeName ), this );
79 QLabel* aNameLabel = new QLabel( tr( "NAME" ), myNameGroup );
80 myObjectName = new QLineEdit( myNameGroup );
82 QBoxLayout* anNameLayout = new QHBoxLayout( myNameGroup );
83 anNameLayout->setMargin( 5 );
84 anNameLayout->setSpacing( 5 );
85 anNameLayout->addWidget( aNameLabel );
86 anNameLayout->addWidget( myObjectName );
88 // Mode selector (create/edit)
89 myModeGroup = new QGroupBox( tr( "MODE" ), this );
91 QRadioButton* aNewRB = new QRadioButton( tr( "CREATE_NEW" ), mainFrame() );
92 QRadioButton* aModifyRB = new QRadioButton( tr( "MODIFY" ), mainFrame() );
94 myModeButtons = new QButtonGroup( mainFrame() );
95 myModeButtons->addButton( aNewRB, CreateNewId );
96 myModeButtons->addButton( aModifyRB, ModifyExistentId );
98 QBoxLayout* aModeSelectorLayout = new QHBoxLayout( myModeGroup );
99 aModeSelectorLayout->setMargin( 5 );
100 aModeSelectorLayout->setSpacing( 5 );
101 aModeSelectorLayout->addWidget( aNewRB );
102 aModeSelectorLayout->addWidget( aModifyRB );
104 // Existing obstacles selector
105 myObjectsGroup = new QGroupBox( tr( "OBJECT_TO_EDIT" ).arg( theObjectTypeName ), this );
107 QLabel* anObjectLabel = new QLabel( theObjectTypeName, myObjectsGroup );
109 myObjects = new QComboBox( mainFrame() );
110 myObjects->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
112 QBoxLayout* anObjectsSelectorLayout = new QHBoxLayout( myObjectsGroup );
113 anObjectsSelectorLayout->setMargin( 5 );
114 anObjectsSelectorLayout->setSpacing( 5 );
115 anObjectsSelectorLayout->addWidget( anObjectLabel );
116 anObjectsSelectorLayout->addWidget( myObjects );
119 if ( aFileNameGroup ) {
120 addWidget( aFileNameGroup );
122 addWidget( myNameGroup );
123 addWidget( myModeGroup );
124 addWidget( myObjectsGroup );
127 // Connect signals and slots
128 connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
129 connect( myObjects, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onObjectSelectionChanged( ) ) );
132 HYDROGUI_GeomObjectDlg::~HYDROGUI_GeomObjectDlg()
136 void HYDROGUI_GeomObjectDlg::setObjectNames( const QStringList& theNames )
139 myObjects->addItems( theNames );
144 void HYDROGUI_GeomObjectDlg::onModeActivated( int theMode )
150 QString HYDROGUI_GeomObjectDlg::getObjectName() const
152 return myObjectName->text();
157 if ( myModeButtons->checkedId() == ModifyExistentId ) {
158 aName = myObjects->currentText();
164 QString HYDROGUI_GeomObjectDlg::getEditedObjectName() const
168 if ( myModeButtons->checkedId() == ModifyExistentId ) {
169 aName = myObjects->currentText();
175 QString HYDROGUI_GeomObjectDlg::getFileName() const
177 return myFileName->text();
180 void HYDROGUI_GeomObjectDlg::onBrowse()
182 SUIT_FileDlg* aFileDlg = new SUIT_FileDlg( this, true );
183 aFileDlg->setWindowTitle( tr("IMPORT_OBSTACLE_FROM_FILE") );
184 aFileDlg->setFilter( tr("OBSTACLE_FILTER") );
185 if ( !lastUsedFilter.isEmpty() ) {
186 aFileDlg->selectFilter( lastUsedFilter );
189 if ( aFileDlg->exec() == QDialog::Accepted ) {
190 QString aFileName = aFileDlg->selectedFile();
191 lastUsedFilter = aFileDlg->selectedFilter();
193 if ( !aFileName.isEmpty() ) {
194 myFileName->setText( aFileName );
202 void HYDROGUI_GeomObjectDlg::reset()
204 if ( myFileSelectionEnabled ) {
207 myObjectName->clear();
209 // Activate the creation mode
210 myModeButtons->button( CreateNewId )->setChecked( true );
211 onModeActivated( CreateNewId );
216 void HYDROGUI_GeomObjectDlg::onObjectSelectionChanged()
221 void HYDROGUI_GeomObjectDlg::updateControls()
223 myNameGroup->setEnabled( !myFileSelectionEnabled ||
224 !myFileName->text().isEmpty() );
225 myModeGroup->setEnabled( myNameGroup->isEnabled() &&
226 myObjects->count() > 0 );
227 myObjectsGroup->setEnabled( myModeButtons->checkedId() == ModifyExistentId );
230 void HYDROGUI_GeomObjectDlg::updateObjectName()
235 int aMode = myModeButtons->checkedId();
236 if ( aMode == CreateNewId ) {
237 if ( myFileSelectionEnabled ) {
238 QString aFileName = myFileName->text();
239 if ( !aFileName.isEmpty() ) {
240 QFileInfo aFileInfo( aFileName );
241 aName = HYDROGUI_Tool::GenerateObjectName(
242 module(), aFileInfo.baseName(), QStringList(), true );
245 aName = HYDROGUI_Tool::GenerateObjectName(
246 module(), getDefaultName(), QStringList(), true );
248 } else if ( aMode == ModifyExistentId ) {
249 aName = getEditedObjectName();
252 myObjectName->setText( aName );
255 void HYDROGUI_GeomObjectDlg::setDefaultName( const QString& theName )
257 myDefaultName = theName;
262 QString HYDROGUI_GeomObjectDlg::getDefaultName()
264 return myDefaultName;