1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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 ),
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 )
138 bool isBlocked = myObjects->blockSignals( true );
140 myObjects->addItems( theNames );
141 myObjects->blockSignals( isBlocked );
146 void HYDROGUI_GeomObjectDlg::onModeActivated( int theMode )
152 QString HYDROGUI_GeomObjectDlg::getObjectName() const
154 return myObjectName->text();
159 if ( myModeButtons->checkedId() == ModifyExistentId ) {
160 aName = myObjects->currentText();
166 QString HYDROGUI_GeomObjectDlg::getEditedObjectName() const
170 if ( myModeButtons->checkedId() == ModifyExistentId ) {
171 aName = myObjects->currentText();
177 QString HYDROGUI_GeomObjectDlg::getFileName() const
179 return myFileName->text();
182 void HYDROGUI_GeomObjectDlg::onBrowse()
184 SUIT_FileDlg* aFileDlg = new SUIT_FileDlg( this, true );
185 aFileDlg->setWindowTitle( tr("IMPORT_OBSTACLE_FROM_FILE") );
186 aFileDlg->setFilter( tr("OBSTACLE_FILTER") );
187 if ( !lastUsedFilter.isEmpty() ) {
188 aFileDlg->selectFilter( lastUsedFilter );
191 if ( aFileDlg->exec() == QDialog::Accepted ) {
192 QString aFileName = aFileDlg->selectedFile();
193 lastUsedFilter = aFileDlg->selectedFilter();
195 if ( !aFileName.isEmpty() ) {
196 myFileName->setText( aFileName );
204 void HYDROGUI_GeomObjectDlg::reset()
206 myDefaultName.clear();
207 if ( myFileSelectionEnabled ) {
210 myObjectName->clear();
212 // Activate the creation mode
213 myModeButtons->button( CreateNewId )->setChecked( true );
214 onModeActivated( CreateNewId );
219 void HYDROGUI_GeomObjectDlg::onObjectSelectionChanged()
224 void HYDROGUI_GeomObjectDlg::updateControls()
226 myNameGroup->setEnabled( !myFileSelectionEnabled ||
227 !myFileName->text().isEmpty() );
228 myModeGroup->setEnabled( myNameGroup->isEnabled() &&
229 myObjects->count() > 0 );
230 myObjectsGroup->setEnabled( myModeButtons->checkedId() == ModifyExistentId );
233 void HYDROGUI_GeomObjectDlg::updateObjectName()
238 int aMode = myModeButtons->checkedId();
239 if ( aMode == CreateNewId ) {
240 if ( myFileSelectionEnabled ) {
241 QString aFileName = myFileName->text();
242 if ( !aFileName.isEmpty() ) {
243 QFileInfo aFileInfo( aFileName );
244 aName = HYDROGUI_Tool::GenerateObjectName(
245 module(), aFileInfo.baseName(), QStringList(), true );
248 aName = HYDROGUI_Tool::GenerateObjectName(
249 module(), getDefaultName(), QStringList(), true );
251 } else if ( aMode == ModifyExistentId ) {
252 aName = getEditedObjectName();
255 myObjectName->setText( aName );
258 void HYDROGUI_GeomObjectDlg::setDefaultName( const QString& theName )
260 myDefaultName = theName;
265 QString HYDROGUI_GeomObjectDlg::getDefaultName()
267 return myDefaultName;