Salome HOME
bug #237: fatal error on profile
[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   myDefaultName( tr("DEFAULT_OBSTACLE_NAME") )
47 {
48   // Get resource manager
49   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
50
51   // File selector
52   QGroupBox* aFileNameGroup = 0;
53   myFileName = 0;
54   if ( myFileSelectionEnabled ) {
55     aFileNameGroup = new QGroupBox( tr( "GET_SHAPE_FROM_FILE" ), this );
56
57     QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
58
59     myFileName = new QLineEdit( aFileNameGroup );
60     myFileName->setReadOnly( true );
61
62     QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
63     aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
64
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 );
71
72     connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
73   }
74
75   // Obstacle name
76   myObstacleNameGroup = new QGroupBox( tr( "OBSTACLE_NAME" ), this );
77
78   QLabel* anObstacleNameLabel = new QLabel( tr( "NAME" ), myObstacleNameGroup );
79   myObstacleName = new QLineEdit( myObstacleNameGroup );
80
81   QBoxLayout* anObstacleNameLayout = new QHBoxLayout( myObstacleNameGroup );
82   anObstacleNameLayout->setMargin( 5 );
83   anObstacleNameLayout->setSpacing( 5 );
84   anObstacleNameLayout->addWidget( anObstacleNameLabel );
85   anObstacleNameLayout->addWidget( myObstacleName );
86
87   // Mode selector (create/edit)
88   myModeGroup = new QGroupBox( tr( "MODE" ), this );
89
90   QRadioButton* aNewObstacleRB = new QRadioButton( tr( "CREATE_NEW_OBSTACLE" ), mainFrame() );
91   QRadioButton* aModifyObstacleRB = new QRadioButton( tr( "MODIFY_OBSTACLE" ), mainFrame() );
92
93   myModeButtons = new QButtonGroup( mainFrame() );
94   myModeButtons->addButton( aNewObstacleRB, CreateNewId );
95   myModeButtons->addButton( aModifyObstacleRB, ModifyExistentId );
96
97   QBoxLayout* aModeSelectorLayout = new QHBoxLayout( myModeGroup );
98   aModeSelectorLayout->setMargin( 5 );
99   aModeSelectorLayout->setSpacing( 5 );
100   aModeSelectorLayout->addWidget( aNewObstacleRB );
101   aModeSelectorLayout->addWidget( aModifyObstacleRB );
102
103   // Existing obstacles selector
104   myObstaclesGroup = new QGroupBox( tr( "OBSTACLE_TO_EDIT" ), this );
105
106   QLabel* anObstacleLabel = new QLabel( tr( "OBSTACLE" ), myObstaclesGroup );
107
108   myObstacles = new QComboBox( mainFrame() );
109   myObstacles->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
110  
111   QBoxLayout* anObstaclesSelectorLayout = new QHBoxLayout( myObstaclesGroup );
112   anObstaclesSelectorLayout->setMargin( 5 );
113   anObstaclesSelectorLayout->setSpacing( 5 );
114   anObstaclesSelectorLayout->addWidget( anObstacleLabel );
115   anObstaclesSelectorLayout->addWidget( myObstacles );
116
117   // Layout
118   if ( aFileNameGroup ) {
119     addWidget( aFileNameGroup );
120   }
121   addWidget( myObstacleNameGroup );
122   addWidget( myModeGroup );
123   addWidget( myObstaclesGroup );
124   addStretch();
125
126   // Connect signals and slots
127   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
128   connect( myObstacles, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onObstacleSelectionChanged( ) ) );
129 }
130
131 HYDROGUI_ObstacleDlg::~HYDROGUI_ObstacleDlg()
132 {
133 }
134
135 void HYDROGUI_ObstacleDlg::setObstacleNames( const QStringList& theObstacles )
136 {
137   myObstacles->clear();
138   myObstacles->addItems( theObstacles );
139
140   updateControls();
141 }
142
143 void HYDROGUI_ObstacleDlg::onModeActivated( int theMode )
144 {
145   updateControls();
146   updateObjectName();
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       updateObjectName();
195     }
196
197     updateControls();
198   }
199 }
200
201 void HYDROGUI_ObstacleDlg::reset()
202 {
203   if ( myFileSelectionEnabled ) {
204     myFileName->clear();
205   }
206   myObstacleName->clear();
207
208   // Activate the creation mode
209   myModeButtons->button( CreateNewId )->setChecked( true );
210   onModeActivated( CreateNewId );
211
212   updateControls();
213 }
214
215 void HYDROGUI_ObstacleDlg::onObstacleSelectionChanged()
216 {
217   updateObjectName();
218 }
219
220 void HYDROGUI_ObstacleDlg::updateControls()
221 {
222   myObstacleNameGroup->setEnabled( !myFileSelectionEnabled || 
223                                    !myFileName->text().isEmpty() );
224   myModeGroup->setEnabled( myObstacleNameGroup->isEnabled() && 
225                            myObstacles->count() > 0 );
226   myObstaclesGroup->setEnabled( myModeButtons->checkedId() == ModifyExistentId );
227 }
228
229 void HYDROGUI_ObstacleDlg::updateObjectName()
230 {
231   QString aName;
232
233   // Creation mode
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 );
242       }
243     } else {
244       aName = HYDROGUI_Tool::GenerateObjectName( 
245         module(), getDefaultName(), QStringList(), true );
246     }
247   } else if ( aMode == ModifyExistentId ) {
248       aName = getEditedObstacleName();
249   }
250
251   myObstacleName->setText( aName );
252 }
253
254 void HYDROGUI_ObstacleDlg::setDefaultName( const QString& theName )
255 {
256   myDefaultName = theName;
257
258   updateObjectName();
259 }
260
261 QString HYDROGUI_ObstacleDlg::getDefaultName()
262 {
263   return myDefaultName;
264 }