Salome HOME
Bug #350: multiply creation of obstacles problem.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_GeomObjectDlg.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_GeomObjectDlg.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_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( "" )
48 {
49   // Get resource manager
50   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
51
52   // File selector
53   QGroupBox* aFileNameGroup = 0;
54   myFileName = 0;
55   if ( myFileSelectionEnabled ) {
56     aFileNameGroup = new QGroupBox( tr( "GET_SHAPE_FROM_FILE" ), this );
57
58     QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
59
60     myFileName = new QLineEdit( aFileNameGroup );
61     myFileName->setReadOnly( true );
62
63     QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
64     aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
65
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 );
72
73     connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
74   }
75
76   // Obstacle name
77   myNameGroup = new QGroupBox( tr( "OBJECT_NAME" ).arg( theObjectTypeName ), this );
78
79   QLabel* aNameLabel = new QLabel( tr( "NAME" ), myNameGroup );
80   myObjectName = new QLineEdit( myNameGroup );
81
82   QBoxLayout* anNameLayout = new QHBoxLayout( myNameGroup );
83   anNameLayout->setMargin( 5 );
84   anNameLayout->setSpacing( 5 );
85   anNameLayout->addWidget( aNameLabel );
86   anNameLayout->addWidget( myObjectName );
87
88   // Mode selector (create/edit)
89   myModeGroup = new QGroupBox( tr( "MODE" ), this );
90
91   QRadioButton* aNewRB = new QRadioButton( tr( "CREATE_NEW" ), mainFrame() );
92   QRadioButton* aModifyRB = new QRadioButton( tr( "MODIFY" ), mainFrame() );
93
94   myModeButtons = new QButtonGroup( mainFrame() );
95   myModeButtons->addButton( aNewRB, CreateNewId );
96   myModeButtons->addButton( aModifyRB, ModifyExistentId );
97
98   QBoxLayout* aModeSelectorLayout = new QHBoxLayout( myModeGroup );
99   aModeSelectorLayout->setMargin( 5 );
100   aModeSelectorLayout->setSpacing( 5 );
101   aModeSelectorLayout->addWidget( aNewRB );
102   aModeSelectorLayout->addWidget( aModifyRB );
103
104   // Existing obstacles selector
105   myObjectsGroup = new QGroupBox( tr( "OBJECT_TO_EDIT" ).arg( theObjectTypeName ), this );
106
107   QLabel* anObjectLabel = new QLabel( theObjectTypeName, myObjectsGroup );
108
109   myObjects = new QComboBox( mainFrame() );
110   myObjects->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
111  
112   QBoxLayout* anObjectsSelectorLayout = new QHBoxLayout( myObjectsGroup );
113   anObjectsSelectorLayout->setMargin( 5 );
114   anObjectsSelectorLayout->setSpacing( 5 );
115   anObjectsSelectorLayout->addWidget( anObjectLabel );
116   anObjectsSelectorLayout->addWidget( myObjects );
117
118   // Layout
119   if ( aFileNameGroup ) {
120     addWidget( aFileNameGroup );
121   }
122   addWidget( myNameGroup );
123   addWidget( myModeGroup );
124   addWidget( myObjectsGroup );
125   addStretch();
126
127   // Connect signals and slots
128   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
129   connect( myObjects, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onObjectSelectionChanged( ) ) );
130 }
131
132 HYDROGUI_GeomObjectDlg::~HYDROGUI_GeomObjectDlg()
133 {
134 }
135
136 void HYDROGUI_GeomObjectDlg::setObjectNames( const QStringList& theNames )
137 {
138   bool isBlocked = myObjects->blockSignals( true );
139   myObjects->clear();
140   myObjects->addItems( theNames );
141   myObjects->blockSignals( isBlocked );
142
143   updateControls();
144 }
145
146 void HYDROGUI_GeomObjectDlg::onModeActivated( int theMode )
147 {
148   updateControls();
149   updateObjectName();
150 }
151
152 QString HYDROGUI_GeomObjectDlg::getObjectName() const
153 {
154   return myObjectName->text();
155
156
157   QString aName;
158
159   if ( myModeButtons->checkedId() == ModifyExistentId ) {
160     aName = myObjects->currentText();
161   }
162
163   return aName;
164 }
165
166 QString HYDROGUI_GeomObjectDlg::getEditedObjectName() const
167 {
168   QString aName;
169
170   if ( myModeButtons->checkedId() == ModifyExistentId ) {
171     aName = myObjects->currentText();
172   }
173
174   return aName;
175 }
176
177 QString HYDROGUI_GeomObjectDlg::getFileName() const
178 {
179   return myFileName->text();
180 }
181
182 void HYDROGUI_GeomObjectDlg::onBrowse()
183 {
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 );
189   }
190
191   if ( aFileDlg->exec() == QDialog::Accepted ) {
192     QString aFileName = aFileDlg->selectedFile();
193     lastUsedFilter = aFileDlg->selectedFilter();
194
195     if ( !aFileName.isEmpty() ) {
196       myFileName->setText( aFileName );
197       updateObjectName();
198     }
199
200     updateControls();
201   }
202 }
203
204 void HYDROGUI_GeomObjectDlg::reset()
205 {
206   myDefaultName.clear();
207   if ( myFileSelectionEnabled ) {
208     myFileName->clear();
209   }
210   myObjectName->clear();
211
212   // Activate the creation mode
213   myModeButtons->button( CreateNewId )->setChecked( true );
214   onModeActivated( CreateNewId );
215
216   updateControls();
217 }
218
219 void HYDROGUI_GeomObjectDlg::onObjectSelectionChanged()
220 {
221   updateObjectName();
222 }
223
224 void HYDROGUI_GeomObjectDlg::updateControls()
225 {
226   myNameGroup->setEnabled( !myFileSelectionEnabled || 
227                            !myFileName->text().isEmpty() );
228   myModeGroup->setEnabled( myNameGroup->isEnabled() && 
229                            myObjects->count() > 0 );
230   myObjectsGroup->setEnabled( myModeButtons->checkedId() == ModifyExistentId );
231 }
232
233 void HYDROGUI_GeomObjectDlg::updateObjectName()
234 {
235   QString aName;
236
237   // Creation mode
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 );
246       }
247     } else {
248       aName = HYDROGUI_Tool::GenerateObjectName( 
249         module(), getDefaultName(), QStringList(), true );
250     }
251   } else if ( aMode == ModifyExistentId ) {
252       aName = getEditedObjectName();
253   }
254
255   myObjectName->setText( aName );
256 }
257
258 void HYDROGUI_GeomObjectDlg::setDefaultName( const QString& theName )
259 {
260   myDefaultName = theName;
261
262   updateObjectName();
263 }
264
265 QString HYDROGUI_GeomObjectDlg::getDefaultName()
266 {
267   return myDefaultName;
268 }