Salome HOME
refs #1278: by default objects in study are not expanded
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportBathymetryDlg.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_ImportBathymetryDlg.h"
20
21 #include "HYDROGUI_Tool.h"
22
23 #include <SUIT_FileDlg.h>
24 #include <SUIT_ResourceMgr.h>
25 #include <SUIT_Session.h>
26
27 #include <QListWidget>
28 #include <QGroupBox>
29 #include <QLabel>
30 #include <QLayout>
31 #include <QLineEdit>
32 #include <QPicture>
33 #include <QPushButton>
34 #include <QCheckBox>
35
36 HYDROGUI_ImportBathymetryDlg::HYDROGUI_ImportBathymetryDlg( HYDROGUI_Module* theModule, const QString& theTitle )
37 : HYDROGUI_InputPanel( theModule, theTitle ), myIsEdit ( false )
38 {
39   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
40
41   // Import bathymetry from file
42   myFileNameGroup = new QGroupBox( tr( "IMPORT_BATHYMETRY_FROM_FILE" ) );
43
44   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAMES" ), myFileNameGroup );
45
46   myFileNames = new QListWidget( myFileNameGroup );
47   myFileNames->setSelectionMode( QAbstractItemView::ExtendedSelection);
48   //myFileNames->viewport()->setAttribute( Qt::WA_TransparentForMouseEvents );
49   //myFileNames->setFocusPolicy(Qt::FocusPolicy::NoFocus);
50
51   QPushButton* aBrowseBtn = new QPushButton( myFileNameGroup );
52   aBrowseBtn->setText("Add files(s)");
53   aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
54
55   QPushButton* aRemoveBtn = new QPushButton( myFileNameGroup );
56   aRemoveBtn->setText("Remove selected file(s)");
57
58   QBoxLayout* aFileNameLayout = new QVBoxLayout( myFileNameGroup );
59
60   myFuseIntoOne = new QCheckBox();
61   myFuseIntoOne->setText("Import as one bathymetry object (fuse the geometry data)");
62   myFuseIntoOne->setEnabled( false );
63
64   aFileNameLayout->setMargin( 5 );
65   aFileNameLayout->setSpacing( 5 );
66   aFileNameLayout->addWidget( aFileNameLabel );  
67   aFileNameLayout->addWidget( aBrowseBtn );
68   aFileNameLayout->addWidget( aRemoveBtn );
69   aFileNameLayout->addWidget( myFileNames );
70   aFileNameLayout->addWidget( myFuseIntoOne );
71
72   // Bathymetry name
73   myObjectNameGroup = new QGroupBox( tr( "BATHYMETRY_NAME" ) );
74
75   QLabel* aBathymetryNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup );
76   myObjectName = new QLineEdit( myObjectNameGroup );
77
78   QBoxLayout* aBathymetryNameLayout = new QHBoxLayout( myObjectNameGroup );
79   aBathymetryNameLayout->setMargin( 5 );
80   aBathymetryNameLayout->setSpacing( 5 );
81   aBathymetryNameLayout->addWidget( aBathymetryNameLabel );
82   aBathymetryNameLayout->addWidget( myObjectName );
83
84   myInvertAltitudes = new QCheckBox( tr( "INVERT_BATHYMETRY_ALTITUDES" ), this );
85
86   // Common
87   addWidget( myFileNameGroup );
88   addWidget( myObjectNameGroup );
89   addWidget( myInvertAltitudes );
90
91   addStretch();
92
93   connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
94   connect( aRemoveBtn, SIGNAL( clicked() ), this, SLOT( onRemove() ) );
95   connect( myFuseIntoOne, SIGNAL( stateChanged(int) ), this, SLOT( onStateChanged(int) ) );
96 }
97
98 HYDROGUI_ImportBathymetryDlg::~HYDROGUI_ImportBathymetryDlg()
99 {
100 }
101
102 void HYDROGUI_ImportBathymetryDlg::reset()
103 {
104   myFileNames->clear();
105   myObjectName->clear();
106   myObjectNameGroup->setEnabled( false );
107   myFuseIntoOne->setEnabled( false );
108 }
109
110 void HYDROGUI_ImportBathymetryDlg::SetEditMode(bool isEdit)
111 {
112   myIsEdit = isEdit;
113 }
114
115 void HYDROGUI_ImportBathymetryDlg::setObjectName( const QString& theName )
116 {
117   myObjectName->setText( theName );
118   bool ObjNameState = myObjectName->isEnabled();
119   myObjectNameGroup->setEnabled( !theName.isEmpty() || myFileNames->count() > 0 );
120   myObjectName->setEnabled(ObjNameState);
121   myFuseIntoOne->setEnabled( myFileNames->count() > 1 );
122 }
123
124 QString HYDROGUI_ImportBathymetryDlg::getObjectName() const
125 {
126   return myObjectName->text();
127 }
128
129 void HYDROGUI_ImportBathymetryDlg::addFileNames( const QStringList& theFileNames )
130 {
131   //myFileNames->clear();
132   QSet<QString> exfilenames = getFileNames().toSet();
133   QStringList newFileNames;
134   foreach (QString str, theFileNames)
135     if (!exfilenames.contains(str))
136       newFileNames.append(str);
137   myFileNames->addItems( newFileNames );
138   UpdateCheckBoxStates();
139 }
140
141 void HYDROGUI_ImportBathymetryDlg::removeFileNames( const QList<QListWidgetItem*>& theFileNamesToRem )
142 {
143   foreach (QListWidgetItem* item, theFileNamesToRem)
144     delete myFileNames->takeItem(myFileNames->row(item));
145   UpdateCheckBoxStates();
146 }
147
148
149 void HYDROGUI_ImportBathymetryDlg::UpdateCheckBoxStates()
150 {
151   if (myFuseIntoOne->isChecked() || myFileNames->count() == 1 )
152   {
153     myObjectNameGroup->setEnabled( true );
154     myObjectName->setEnabled( true );
155   }
156   else
157   {
158     myObjectNameGroup->setEnabled( false );
159     myObjectName->setEnabled( false );
160   }
161   //
162   if (myFileNames->count() <= 1 || myIsEdit)
163     myFuseIntoOne->setEnabled( false );
164   else
165     myFuseIntoOne->setEnabled( true );
166
167   QString anObjectName = getObjectName().simplified();
168   
169   QStringList aFileNames = getFileNames();
170   if ( anObjectName.isEmpty() )
171   {
172     if (!aFileNames.empty())
173     {
174       anObjectName = aFileNames[0];
175       if ( !anObjectName.isEmpty() )
176         anObjectName = QFileInfo( anObjectName ).baseName();
177     }
178     setObjectName( anObjectName );
179   }
180
181 }
182
183 QStringList HYDROGUI_ImportBathymetryDlg::getFileNames() const
184 {
185   QStringList stritems;
186   for(int i = 0; i < myFileNames->count(); ++i)
187   {
188     QListWidgetItem* item = myFileNames->item(i);
189     stritems << item->text();
190   }
191   return stritems;
192 }
193
194 void HYDROGUI_ImportBathymetryDlg::setInvertAltitudes( const bool theIsInvert )
195 {
196   myInvertAltitudes->setChecked( theIsInvert );
197 }
198
199 bool HYDROGUI_ImportBathymetryDlg::isInvertAltitudes() const
200 {
201   return myInvertAltitudes->isChecked();
202 }
203
204 void HYDROGUI_ImportBathymetryDlg::setFuseIntoOneOptionChecked( bool isFIO )
205 {
206   myFuseIntoOne->setChecked( isFIO );
207 }
208
209 bool HYDROGUI_ImportBathymetryDlg::isFuseIntoOneOptionChecked() const
210 {
211   return myFuseIntoOne->isChecked();
212 }
213
214 void HYDROGUI_ImportBathymetryDlg::setFuseIntoOneOptionEnabled(bool enabled)
215 {
216   myFuseIntoOne->setEnabled(enabled);
217 }
218
219 bool HYDROGUI_ImportBathymetryDlg::isFuseIntoOneOptionEnabled() const
220 {
221   return myFuseIntoOne->isEnabled();
222 }
223
224 void HYDROGUI_ImportBathymetryDlg::onBrowse()
225 {
226   QString aFilter( tr( "BATHYMETRY_FILTER" ) );
227   QStringList aFileNames = SUIT_FileDlg::getOpenFileNames( this, "", aFilter, tr( "IMPORT_BATHYMETRY_FROM_FILE" ), true );
228
229   if( !aFileNames.isEmpty() )
230   {
231     addFileNames( aFileNames );
232     emit FileSelected( aFileNames );
233   }
234 }
235
236 void HYDROGUI_ImportBathymetryDlg::onRemove()
237 {
238   QList<QListWidgetItem*> selected = myFileNames->selectedItems();
239
240   removeFileNames(selected);
241 }
242
243 void HYDROGUI_ImportBathymetryDlg::onStateChanged (int state)
244 {
245   if (getFileNames().count() > 1)
246   {
247     if (state == Qt::Checked)
248     {
249       myObjectName->setEnabled(true);
250       myObjectNameGroup->setEnabled(true);
251     }
252     else if (state == Qt::Unchecked)
253     {
254       myObjectName->setEnabled(false);
255       myObjectNameGroup->setEnabled(false);
256     }
257   }
258 }
259
260