1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_GeomObjectDlg.h"
21 #include "HYDROGUI_Tool2.h"
23 #include <SUIT_FileDlg.h>
24 #include <SUIT_ResourceMgr.h>
25 #include <SUIT_Session.h>
30 #include <QToolButton>
31 #include <QRadioButton>
32 #include <QButtonGroup>
36 static QString lastUsedFilter;
38 HYDROGUI_GeomObjectDlg::HYDROGUI_GeomObjectDlg( HYDROGUI_Module* theModule, const QString& theTitle,
39 const QString& theObjectTypeName,
40 const bool theIsToEnableFileSelection )
41 : HYDROGUI_InputPanel( theModule, theTitle ),
42 myFileSelectionEnabled( theIsToEnableFileSelection ),
45 // Get resource manager
46 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
49 QGroupBox* aFileNameGroup = 0;
51 if ( myFileSelectionEnabled ) {
52 aFileNameGroup = new QGroupBox( tr( "GET_SHAPE_FROM_FILE" ), this );
54 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
56 myFileName = new QLineEdit( aFileNameGroup );
57 myFileName->setReadOnly( true );
59 QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
60 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
62 QBoxLayout* aFileNameLayout = new QHBoxLayout( aFileNameGroup );
63 aFileNameLayout->setMargin( 5 );
64 aFileNameLayout->setSpacing( 5 );
65 aFileNameLayout->addWidget( aFileNameLabel );
66 aFileNameLayout->addWidget( myFileName );
67 aFileNameLayout->addWidget( aBrowseBtn );
69 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
73 myNameGroup = new QGroupBox( tr( "OBJECT_NAME" ).arg( theObjectTypeName ), this );
75 QLabel* aNameLabel = new QLabel( tr( "NAME" ), myNameGroup );
76 myObjectName = new QLineEdit( myNameGroup );
78 QBoxLayout* anNameLayout = new QHBoxLayout( myNameGroup );
79 anNameLayout->setMargin( 5 );
80 anNameLayout->setSpacing( 5 );
81 anNameLayout->addWidget( aNameLabel );
82 anNameLayout->addWidget( myObjectName );
84 // Mode selector (create/edit)
85 myModeGroup = new QGroupBox( tr( "MODE" ), this );
87 QRadioButton* aNewRB = new QRadioButton( tr( "CREATE_NEW" ), mainFrame() );
88 QRadioButton* aModifyRB = new QRadioButton( tr( "MODIFY" ), mainFrame() );
90 myModeButtons = new QButtonGroup( mainFrame() );
91 myModeButtons->addButton( aNewRB, CreateNewId );
92 myModeButtons->addButton( aModifyRB, ModifyExistentId );
94 QBoxLayout* aModeSelectorLayout = new QHBoxLayout( myModeGroup );
95 aModeSelectorLayout->setMargin( 5 );
96 aModeSelectorLayout->setSpacing( 5 );
97 aModeSelectorLayout->addWidget( aNewRB );
98 aModeSelectorLayout->addWidget( aModifyRB );
100 // Existing obstacles selector
101 myObjectsGroup = new QGroupBox( tr( "OBJECT_TO_EDIT" ).arg( theObjectTypeName ), this );
103 QLabel* anObjectLabel = new QLabel( theObjectTypeName, myObjectsGroup );
105 myObjects = new QComboBox( mainFrame() );
106 myObjects->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
108 QBoxLayout* anObjectsSelectorLayout = new QHBoxLayout( myObjectsGroup );
109 anObjectsSelectorLayout->setMargin( 5 );
110 anObjectsSelectorLayout->setSpacing( 5 );
111 anObjectsSelectorLayout->addWidget( anObjectLabel );
112 anObjectsSelectorLayout->addWidget( myObjects );
115 if ( aFileNameGroup ) {
116 addWidget( aFileNameGroup );
118 addWidget( myNameGroup );
119 addWidget( myModeGroup );
120 addWidget( myObjectsGroup );
123 // Connect signals and slots
124 connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
125 connect( myObjects, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onObjectSelectionChanged( ) ) );
128 HYDROGUI_GeomObjectDlg::~HYDROGUI_GeomObjectDlg()
132 void HYDROGUI_GeomObjectDlg::setObjectNames( const QStringList& theNames )
134 bool isBlocked = myObjects->blockSignals( true );
136 myObjects->addItems( theNames );
137 myObjects->blockSignals( isBlocked );
142 void HYDROGUI_GeomObjectDlg::onModeActivated( int theMode )
148 QString HYDROGUI_GeomObjectDlg::getObjectName() const
150 return myObjectName->text();
155 if ( myModeButtons->checkedId() == ModifyExistentId ) {
156 aName = myObjects->currentText();
162 QString HYDROGUI_GeomObjectDlg::getEditedObjectName() const
166 if ( myModeButtons->checkedId() == ModifyExistentId ) {
167 aName = myObjects->currentText();
173 QString HYDROGUI_GeomObjectDlg::getFileName() const
175 return myFileName->text();
178 void HYDROGUI_GeomObjectDlg::onBrowse()
180 SUIT_FileDlg* aFileDlg = new SUIT_FileDlg( this, true );
181 aFileDlg->setWindowTitle( tr("IMPORT_OBSTACLE_FROM_FILE") );
182 aFileDlg->setNameFilter( tr("OBSTACLE_FILTER") );
183 if ( !lastUsedFilter.isEmpty() ) {
184 aFileDlg->selectNameFilter( lastUsedFilter );
187 if ( aFileDlg->exec() == QDialog::Accepted ) {
188 QString aFileName = aFileDlg->selectedFile();
189 lastUsedFilter = aFileDlg->selectedNameFilter();
191 if ( !aFileName.isEmpty() ) {
192 myFileName->setText( aFileName );
200 void HYDROGUI_GeomObjectDlg::reset()
202 myDefaultName.clear();
203 if ( myFileSelectionEnabled ) {
206 myObjectName->clear();
208 // Activate the creation mode
209 myModeButtons->button( CreateNewId )->setChecked( true );
210 onModeActivated( CreateNewId );
215 void HYDROGUI_GeomObjectDlg::onObjectSelectionChanged()
220 void HYDROGUI_GeomObjectDlg::updateControls()
222 myNameGroup->setEnabled( !myFileSelectionEnabled ||
223 !myFileName->text().isEmpty() );
224 myModeGroup->setEnabled( myNameGroup->isEnabled() &&
225 myObjects->count() > 0 );
226 myObjectsGroup->setEnabled( myModeButtons->checkedId() == ModifyExistentId );
229 void HYDROGUI_GeomObjectDlg::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 = getEditedObjectName();
251 myObjectName->setText( aName );
254 void HYDROGUI_GeomObjectDlg::setDefaultName( const QString& theName )
256 myDefaultName = theName;
261 QString HYDROGUI_GeomObjectDlg::getDefaultName()
263 return myDefaultName;