Salome HOME
Fix for the bug #255: VTK viewer is not updated after modification of objects.
[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   myObjects->clear();
139   myObjects->addItems( theNames );
140
141   updateControls();
142 }
143
144 void HYDROGUI_GeomObjectDlg::onModeActivated( int theMode )
145 {
146   updateControls();
147   updateObjectName();
148 }
149
150 QString HYDROGUI_GeomObjectDlg::getObjectName() const
151 {
152   return myObjectName->text();
153
154
155   QString aName;
156
157   if ( myModeButtons->checkedId() == ModifyExistentId ) {
158     aName = myObjects->currentText();
159   }
160
161   return aName;
162 }
163
164 QString HYDROGUI_GeomObjectDlg::getEditedObjectName() const
165 {
166   QString aName;
167
168   if ( myModeButtons->checkedId() == ModifyExistentId ) {
169     aName = myObjects->currentText();
170   }
171
172   return aName;
173 }
174
175 QString HYDROGUI_GeomObjectDlg::getFileName() const
176 {
177   return myFileName->text();
178 }
179
180 void HYDROGUI_GeomObjectDlg::onBrowse()
181 {
182   SUIT_FileDlg* aFileDlg = new SUIT_FileDlg( this, true );
183   aFileDlg->setWindowTitle( tr("IMPORT_OBSTACLE_FROM_FILE") );
184   aFileDlg->setFilter( tr("OBSTACLE_FILTER") );
185   if ( !lastUsedFilter.isEmpty() ) {
186     aFileDlg->selectFilter( lastUsedFilter );
187   }
188
189   if ( aFileDlg->exec() == QDialog::Accepted ) {
190     QString aFileName = aFileDlg->selectedFile();
191     lastUsedFilter = aFileDlg->selectedFilter();
192
193     if ( !aFileName.isEmpty() ) {
194       myFileName->setText( aFileName );
195       updateObjectName();
196     }
197
198     updateControls();
199   }
200 }
201
202 void HYDROGUI_GeomObjectDlg::reset()
203 {
204   if ( myFileSelectionEnabled ) {
205     myFileName->clear();
206   }
207   myObjectName->clear();
208
209   // Activate the creation mode
210   myModeButtons->button( CreateNewId )->setChecked( true );
211   onModeActivated( CreateNewId );
212
213   updateControls();
214 }
215
216 void HYDROGUI_GeomObjectDlg::onObjectSelectionChanged()
217 {
218   updateObjectName();
219 }
220
221 void HYDROGUI_GeomObjectDlg::updateControls()
222 {
223   myNameGroup->setEnabled( !myFileSelectionEnabled || 
224                            !myFileName->text().isEmpty() );
225   myModeGroup->setEnabled( myNameGroup->isEnabled() && 
226                            myObjects->count() > 0 );
227   myObjectsGroup->setEnabled( myModeButtons->checkedId() == ModifyExistentId );
228 }
229
230 void HYDROGUI_GeomObjectDlg::updateObjectName()
231 {
232   QString aName;
233
234   // Creation mode
235   int aMode = myModeButtons->checkedId();
236   if ( aMode == CreateNewId ) {
237     if ( myFileSelectionEnabled ) {
238       QString aFileName = myFileName->text();
239       if ( !aFileName.isEmpty() ) {
240         QFileInfo aFileInfo( aFileName );
241         aName = HYDROGUI_Tool::GenerateObjectName( 
242           module(), aFileInfo.baseName(), QStringList(), true );
243       }
244     } else {
245       aName = HYDROGUI_Tool::GenerateObjectName( 
246         module(), getDefaultName(), QStringList(), true );
247     }
248   } else if ( aMode == ModifyExistentId ) {
249       aName = getEditedObjectName();
250   }
251
252   myObjectName->setText( aName );
253 }
254
255 void HYDROGUI_GeomObjectDlg::setDefaultName( const QString& theName )
256 {
257   myDefaultName = theName;
258
259   updateObjectName();
260 }
261
262 QString HYDROGUI_GeomObjectDlg::getDefaultName()
263 {
264   return myDefaultName;
265 }