Salome HOME
Merging with V7_main branch.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ObstacleDlg.cxx
1 // Copyright (C) 2007-2013  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
23 #include "HYDROGUI_ObstacleDlg.h"
24
25 #include "HYDROGUI_Tool.h"
26
27 #include <SUIT_FileDlg.h>
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_Session.h>
30
31 #include <QLabel>
32 #include <QLineEdit>
33 #include <QComboBox>
34 #include <QToolButton>
35 #include <QRadioButton>
36 #include <QButtonGroup>
37 #include <QGroupBox>
38 #include <QLayout>
39
40 static QString lastUsedFilter;
41
42 HYDROGUI_ObstacleDlg::HYDROGUI_ObstacleDlg( HYDROGUI_Module* theModule, const QString& theTitle,
43                                             const bool theIsToEnableFileSelection )
44 : HYDROGUI_InputPanel( theModule, theTitle ),
45   myFileSelectionEnabled ( theIsToEnableFileSelection )
46 {
47   // Get resource manager
48   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
49
50   // File selector
51   QGroupBox* aFileNameGroup = 0;
52   myFileName = 0;
53   if ( myFileSelectionEnabled ) {
54     aFileNameGroup = new QGroupBox( tr( "GET_SHAPE_FROM_FILE" ), this );
55
56     QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
57
58     myFileName = new QLineEdit( aFileNameGroup );
59     myFileName->setReadOnly( true );
60
61     QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
62     aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
63
64     QBoxLayout* aFileNameLayout = new QHBoxLayout( aFileNameGroup );
65     aFileNameLayout->setMargin( 5 );
66     aFileNameLayout->setSpacing( 5 );
67     aFileNameLayout->addWidget( aFileNameLabel );
68     aFileNameLayout->addWidget( myFileName );
69     aFileNameLayout->addWidget( aBrowseBtn );
70
71     connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
72   }
73
74   // Obstacle name
75   myObstacleNameGroup = new QGroupBox( tr( "OBSTACLE_NAME" ), this );
76
77   QLabel* anObstacleNameLabel = new QLabel( tr( "NAME" ), myObstacleNameGroup );
78   myObstacleName = new QLineEdit( myObstacleNameGroup );
79
80   QBoxLayout* anObstacleNameLayout = new QHBoxLayout( myObstacleNameGroup );
81   anObstacleNameLayout->setMargin( 5 );
82   anObstacleNameLayout->setSpacing( 5 );
83   anObstacleNameLayout->addWidget( anObstacleNameLabel );
84   anObstacleNameLayout->addWidget( myObstacleName );
85
86   myObstacleNameGroup->hide(); //TODO to be enabled?
87
88   // Mode selector (create/edit)
89   myModeGroup = new QGroupBox( tr( "MODE" ), this );
90
91   QRadioButton* aNewObstacleRB = new QRadioButton( tr( "CREATE_NEW_OBSTACLE" ), mainFrame() );
92   QRadioButton* aModifyObstacleRB = new QRadioButton( tr( "MODIFY_OBSTACLE" ), mainFrame() );
93
94   myModeButtons = new QButtonGroup( mainFrame() );
95   myModeButtons->addButton( aNewObstacleRB, CreateNewId );
96   myModeButtons->addButton( aModifyObstacleRB, ModifyExistentId );
97
98   QBoxLayout* aModeSelectorLayout = new QHBoxLayout( myModeGroup );
99   aModeSelectorLayout->setMargin( 5 );
100   aModeSelectorLayout->setSpacing( 5 );
101   aModeSelectorLayout->addWidget( aNewObstacleRB );
102   aModeSelectorLayout->addWidget( aModifyObstacleRB );
103
104   // Existing obstacles selector
105   myObstaclesGroup = new QGroupBox( tr( "OBSTACLE_TO_EDIT" ), this );
106
107   QLabel* anObstacleLabel = new QLabel( tr( "OBSTACLE" ), myObstaclesGroup );
108
109   myObstacles = new QComboBox( mainFrame() );
110   myObstacles->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
111  
112   QBoxLayout* anObstaclesSelectorLayout = new QHBoxLayout( myObstaclesGroup );
113   anObstaclesSelectorLayout->setMargin( 5 );
114   anObstaclesSelectorLayout->setSpacing( 5 );
115   anObstaclesSelectorLayout->addWidget( anObstacleLabel );
116   anObstaclesSelectorLayout->addWidget( myObstacles );
117
118   // Layout
119   if ( aFileNameGroup ) {
120     addWidget( aFileNameGroup );
121   }
122   addWidget( myObstacleNameGroup );
123   addWidget( myModeGroup );
124   addWidget( myObstaclesGroup );
125   addStretch();
126
127   // Connect signals and slots
128   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
129   connect( myObstacles, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onObstacleSelectionChanged( ) ) );
130 }
131
132 HYDROGUI_ObstacleDlg::~HYDROGUI_ObstacleDlg()
133 {
134 }
135
136 void HYDROGUI_ObstacleDlg::setObstacleNames( const QStringList& theObstacles )
137 {
138   myObstacles->clear();
139   myObstacles->addItems( theObstacles );
140
141   UpdateControls();
142 }
143
144 void HYDROGUI_ObstacleDlg::onModeActivated( int theMode )
145 {
146   UpdateControls();
147 }
148
149 QString HYDROGUI_ObstacleDlg::getObstacleName() const
150 {
151   return myObstacleName->text();
152
153
154   QString aName;
155
156   if ( myModeButtons->checkedId() == ModifyExistentId ) {
157     aName = myObstacles->currentText();
158   }
159
160   return aName;
161 }
162
163 QString HYDROGUI_ObstacleDlg::getEditedObstacleName() const
164 {
165   QString aName;
166
167   if ( myModeButtons->checkedId() == ModifyExistentId ) {
168     aName = myObstacles->currentText();
169   }
170
171   return aName;
172 }
173
174 QString HYDROGUI_ObstacleDlg::getFileName() const
175 {
176   return myFileName->text();
177 }
178
179 void HYDROGUI_ObstacleDlg::onBrowse()
180 {
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 );
186   }
187
188   if ( aFileDlg->exec() == QDialog::Accepted ) {
189     QString aFileName = aFileDlg->selectedFile();
190     lastUsedFilter = aFileDlg->selectedFilter();
191
192     if ( !aFileName.isEmpty() ) {
193       myFileName->setText( aFileName );
194
195       QFileInfo aFileInfo( aFileName );
196       QString aName = HYDROGUI_Tool::GenerateObjectName( module(), aFileInfo.baseName() );
197       myObstacleName->setText( aName );
198     }
199
200     UpdateControls();
201   }
202 }
203
204 void HYDROGUI_ObstacleDlg::reset()
205 {
206   if ( myFileSelectionEnabled ) {
207     myFileName->clear();
208   }
209   myObstacleName->clear();
210
211   // Activate the creation mode
212   myModeButtons->button( CreateNewId )->setChecked( true );
213   onModeActivated( CreateNewId );
214
215   UpdateControls();
216 }
217
218 void HYDROGUI_ObstacleDlg::onObstacleSelectionChanged()
219 {
220   if ( myObstacleName->text().isEmpty() ) {
221     myObstacleName->setText( myObstacles->currentText() );
222   }
223 }
224
225 void HYDROGUI_ObstacleDlg::UpdateControls()
226 {
227   myObstacleNameGroup->setEnabled( !myFileSelectionEnabled || 
228                                    !myFileName->text().isEmpty() );
229   myModeGroup->setEnabled( myObstacleNameGroup->isEnabled() && 
230                            myObstacles->count() > 0 );
231   myObstaclesGroup->setEnabled( myModeButtons->checkedId() == ModifyExistentId );
232 }