Salome HOME
merge BR_LAND_COVER_MAP
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_GeomObjectDlg.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_GeomObjectDlg.h"
20
21 #include "HYDROGUI_Tool2.h"
22
23 #include <SUIT_FileDlg.h>
24 #include <SUIT_ResourceMgr.h>
25 #include <SUIT_Session.h>
26
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QComboBox>
30 #include <QToolButton>
31 #include <QRadioButton>
32 #include <QButtonGroup>
33 #include <QGroupBox>
34 #include <QLayout>
35
36 static QString lastUsedFilter;
37
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 ),
43   myDefaultName( "" )
44 {
45   // Get resource manager
46   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
47
48   // File selector
49   QGroupBox* aFileNameGroup = 0;
50   myFileName = 0;
51   if ( myFileSelectionEnabled ) {
52     aFileNameGroup = new QGroupBox( tr( "GET_SHAPE_FROM_FILE" ), this );
53
54     QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), aFileNameGroup );
55
56     myFileName = new QLineEdit( aFileNameGroup );
57     myFileName->setReadOnly( true );
58
59     QToolButton* aBrowseBtn = new QToolButton( aFileNameGroup );
60     aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
61
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 );
68
69     connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
70   }
71
72   // Obstacle name
73   myNameGroup = new QGroupBox( tr( "OBJECT_NAME" ).arg( theObjectTypeName ), this );
74
75   QLabel* aNameLabel = new QLabel( tr( "NAME" ), myNameGroup );
76   myObjectName = new QLineEdit( myNameGroup );
77
78   QBoxLayout* anNameLayout = new QHBoxLayout( myNameGroup );
79   anNameLayout->setMargin( 5 );
80   anNameLayout->setSpacing( 5 );
81   anNameLayout->addWidget( aNameLabel );
82   anNameLayout->addWidget( myObjectName );
83
84   // Mode selector (create/edit)
85   myModeGroup = new QGroupBox( tr( "MODE" ), this );
86
87   QRadioButton* aNewRB = new QRadioButton( tr( "CREATE_NEW" ), mainFrame() );
88   QRadioButton* aModifyRB = new QRadioButton( tr( "MODIFY" ), mainFrame() );
89
90   myModeButtons = new QButtonGroup( mainFrame() );
91   myModeButtons->addButton( aNewRB, CreateNewId );
92   myModeButtons->addButton( aModifyRB, ModifyExistentId );
93
94   QBoxLayout* aModeSelectorLayout = new QHBoxLayout( myModeGroup );
95   aModeSelectorLayout->setMargin( 5 );
96   aModeSelectorLayout->setSpacing( 5 );
97   aModeSelectorLayout->addWidget( aNewRB );
98   aModeSelectorLayout->addWidget( aModifyRB );
99
100   // Existing obstacles selector
101   myObjectsGroup = new QGroupBox( tr( "OBJECT_TO_EDIT" ).arg( theObjectTypeName ), this );
102
103   QLabel* anObjectLabel = new QLabel( theObjectTypeName, myObjectsGroup );
104
105   myObjects = new QComboBox( mainFrame() );
106   myObjects->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
107  
108   QBoxLayout* anObjectsSelectorLayout = new QHBoxLayout( myObjectsGroup );
109   anObjectsSelectorLayout->setMargin( 5 );
110   anObjectsSelectorLayout->setSpacing( 5 );
111   anObjectsSelectorLayout->addWidget( anObjectLabel );
112   anObjectsSelectorLayout->addWidget( myObjects );
113
114   // Layout
115   if ( aFileNameGroup ) {
116     addWidget( aFileNameGroup );
117   }
118   addWidget( myNameGroup );
119   addWidget( myModeGroup );
120   addWidget( myObjectsGroup );
121   addStretch();
122
123   // Connect signals and slots
124   connect( myModeButtons, SIGNAL( buttonClicked( int ) ), this, SLOT( onModeActivated( int ) ) );
125   connect( myObjects, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onObjectSelectionChanged( ) ) );
126 }
127
128 HYDROGUI_GeomObjectDlg::~HYDROGUI_GeomObjectDlg()
129 {
130 }
131
132 void HYDROGUI_GeomObjectDlg::setObjectNames( const QStringList& theNames )
133 {
134   bool isBlocked = myObjects->blockSignals( true );
135   myObjects->clear();
136   myObjects->addItems( theNames );
137   myObjects->blockSignals( isBlocked );
138
139   updateControls();
140 }
141
142 void HYDROGUI_GeomObjectDlg::onModeActivated( int theMode )
143 {
144   updateControls();
145   updateObjectName();
146 }
147
148 QString HYDROGUI_GeomObjectDlg::getObjectName() const
149 {
150   return myObjectName->text();
151
152
153   QString aName;
154
155   if ( myModeButtons->checkedId() == ModifyExistentId ) {
156     aName = myObjects->currentText();
157   }
158
159   return aName;
160 }
161
162 QString HYDROGUI_GeomObjectDlg::getEditedObjectName() const
163 {
164   QString aName;
165
166   if ( myModeButtons->checkedId() == ModifyExistentId ) {
167     aName = myObjects->currentText();
168   }
169
170   return aName;
171 }
172
173 QString HYDROGUI_GeomObjectDlg::getFileName() const
174 {
175   return myFileName->text();
176 }
177
178 void HYDROGUI_GeomObjectDlg::onBrowse()
179 {
180   SUIT_FileDlg* aFileDlg = new SUIT_FileDlg( this, true );
181   aFileDlg->setWindowTitle( tr("IMPORT_OBSTACLE_FROM_FILE") );
182   aFileDlg->setFilter( tr("OBSTACLE_FILTER") );
183   if ( !lastUsedFilter.isEmpty() ) {
184     aFileDlg->selectFilter( lastUsedFilter );
185   }
186
187   if ( aFileDlg->exec() == QDialog::Accepted ) {
188     QString aFileName = aFileDlg->selectedFile();
189     lastUsedFilter = aFileDlg->selectedFilter();
190
191     if ( !aFileName.isEmpty() ) {
192       myFileName->setText( aFileName );
193       updateObjectName();
194     }
195
196     updateControls();
197   }
198 }
199
200 void HYDROGUI_GeomObjectDlg::reset()
201 {
202   myDefaultName.clear();
203   if ( myFileSelectionEnabled ) {
204     myFileName->clear();
205   }
206   myObjectName->clear();
207
208   // Activate the creation mode
209   myModeButtons->button( CreateNewId )->setChecked( true );
210   onModeActivated( CreateNewId );
211
212   updateControls();
213 }
214
215 void HYDROGUI_GeomObjectDlg::onObjectSelectionChanged()
216 {
217   updateObjectName();
218 }
219
220 void HYDROGUI_GeomObjectDlg::updateControls()
221 {
222   myNameGroup->setEnabled( !myFileSelectionEnabled || 
223                            !myFileName->text().isEmpty() );
224   myModeGroup->setEnabled( myNameGroup->isEnabled() && 
225                            myObjects->count() > 0 );
226   myObjectsGroup->setEnabled( myModeButtons->checkedId() == ModifyExistentId );
227 }
228
229 void HYDROGUI_GeomObjectDlg::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 = getEditedObjectName();
249   }
250
251   myObjectName->setText( aName );
252 }
253
254 void HYDROGUI_GeomObjectDlg::setDefaultName( const QString& theName )
255 {
256   myDefaultName = theName;
257
258   updateObjectName();
259 }
260
261 QString HYDROGUI_GeomObjectDlg::getDefaultName()
262 {
263   return myDefaultName;
264 }