]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ImportLandCoverMapDlg.cxx
Salome HOME
LCM // Import/Export of SHP p.3
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportLandCoverMapDlg.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_ImportLandCoverMapDlg.h"
20
21 #include <QGroupBox>
22 #include <QLabel>
23 #include <QLineEdit>
24 #include <QListWidget>
25 #include <QVBoxLayout>
26 #include <QToolButton>
27 #include <QCheckBox>
28 #include <SUIT_ResourceMgr.h>
29 #include <SUIT_Session.h>
30 #include <SUIT_FileDlg.h>
31 #include <HYDROGUI_Module.h>
32 #include <SUIT_MessageBox.h>
33 #include <LightApp_Application.h>
34 #include <QTableWidget>
35 #include <QHeaderView>
36 #include <QComboBox>
37 #include <QStackedWidget>
38
39 HYDROGUI_ImportLandCoverMapDlg::HYDROGUI_ImportLandCoverMapDlg( HYDROGUI_Module* theModule, const QString& theTitle )
40 : HYDROGUI_Wizard( theModule, theTitle )
41 {
42   addPage( createPage1() );
43   addPage( createPage2() );
44   addPage( createPage3() );
45   myFirstPageState = false;
46 }
47
48 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage1() {
49   QWizardPage* aPage = new QWizardPage( mainFrame() );
50   QFrame* aFrame = new QFrame( aPage );
51
52   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
53
54   myFileNameGroup = new QGroupBox( tr( "IMPORT_LANDCOVERMAP_FROM_FILE" ) );
55
56   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
57
58   myFileName = new QLineEdit( myFileNameGroup );
59   myFileName->setReadOnly( true );
60
61   QToolButton* aBrowseBtn = new QToolButton( myFileNameGroup );
62   aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
63
64   QBoxLayout* aFileNameLayout = new QHBoxLayout( myFileNameGroup );
65   aFileNameLayout->setMargin( 5 );
66   aFileNameLayout->setSpacing( 5 );
67   aFileNameLayout->addWidget( aFileNameLabel );
68   aFileNameLayout->addWidget( myFileName );
69   aFileNameLayout->addWidget( aBrowseBtn );
70
71   myObjectNameGroup = new QGroupBox( tr( "LANDCOVERMAP_NAME" ) );
72
73   QLabel* aLandcoverNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup );
74   myObjectName = new QLineEdit( myObjectNameGroup );
75
76   QBoxLayout* aLandcoverNameLayout = new QHBoxLayout( myObjectNameGroup );
77   aLandcoverNameLayout->setMargin( 5 );
78   aLandcoverNameLayout->setSpacing( 5 );
79   aLandcoverNameLayout->addWidget( aLandcoverNameLabel );
80   aLandcoverNameLayout->addWidget( myObjectName );
81
82   QGroupBox* aPolygonsGroup = new QGroupBox( tr( "FOUNDED_POLYGONS" ) );
83   myPolygons = new QListWidget( aPolygonsGroup );
84   myPolygons->setSelectionMode( QListWidget::ExtendedSelection );
85   myPolygons->setEditTriggers( QListWidget::NoEditTriggers );
86   myPolygons->setViewMode( QListWidget::ListMode );
87   myPolygons->setSortingEnabled( false );
88
89   QBoxLayout* aPolygonsLayout = new QVBoxLayout;
90   aPolygonsLayout->addWidget( myPolygons );
91   aPolygonsGroup->setLayout( aPolygonsLayout );
92
93   // Layout
94   QVBoxLayout* aPageLayout = new QVBoxLayout;
95   aPageLayout->setMargin( 5 );
96   aPageLayout->setSpacing( 5 );
97   aPageLayout->addWidget( myFileNameGroup );
98   aPageLayout->addWidget( myObjectNameGroup );
99   aPageLayout->addWidget( aPolygonsGroup );
100   aPage->setLayout( aPageLayout );
101
102   // Conections
103   connect( myPolygons, SIGNAL( itemSelectionChanged() ), this, SLOT( onItemSelectionChanged() ) );
104   connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) ); 
105
106   return aPage;
107 }
108
109 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage2() {
110   QWizardPage* aPage = new QWizardPage( mainFrame() );
111   QFrame* aFrame = new QFrame( aPage );
112
113   myAttrNameGroup = new QGroupBox( "Available attributes" );
114  
115   QCheckBox* anAttrCheckBox = new QCheckBox( "Use dBase attributes as a Strickler Types", myAttrNameGroup );
116
117   QBoxLayout* aFileNameLayout = new QHBoxLayout( myAttrNameGroup );
118   aFileNameLayout->setMargin( 5 );
119   aFileNameLayout->setSpacing( 5 );
120   aFileNameLayout->addWidget( anAttrCheckBox );
121
122
123   myDBFAttr = new QListWidget( aPage );
124   myDBFAttr->setSelectionMode( QListWidget::SingleSelection );
125   myDBFAttr->setEditTriggers( QListWidget::NoEditTriggers );
126   myDBFAttr->setViewMode( QListWidget::ListMode );
127   myDBFAttr->setSortingEnabled( false );
128
129   // Layout
130   QVBoxLayout* aPageLayout = new QVBoxLayout;
131   aPageLayout->setMargin( 5 );
132   aPageLayout->setSpacing( 5 );
133   aPageLayout->addWidget( myAttrNameGroup );
134   aPageLayout->addWidget( myDBFAttr );
135   aPage->setLayout( aPageLayout );
136   return aPage;
137 }
138
139 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage3() {
140
141   QWizardPage* aPage = new QWizardPage( mainFrame() );
142   QFrame* aFrame = new QFrame( aPage );
143   myCLabel = new QLabel();
144   myCLabel->setText("Correspondence");
145   
146   myTableW = new QTableWidget();
147
148   // Layout
149   QVBoxLayout* aPageLayout = new QVBoxLayout;
150   aPageLayout->setMargin( 5 );
151   aPageLayout->setSpacing( 5 );
152   aPageLayout->addWidget( myCLabel );
153   aPageLayout->addWidget( myTableW );
154   aPage->setLayout( aPageLayout );
155   return aPage;
156 }
157
158
159 HYDROGUI_ImportLandCoverMapDlg::~HYDROGUI_ImportLandCoverMapDlg()
160 {
161 }
162
163 void HYDROGUI_ImportLandCoverMapDlg::reset()
164 {
165   myPolygons->clear();
166   myDBFAttr->clear();
167 }
168
169 void HYDROGUI_ImportLandCoverMapDlg::setPolygonNames( const QStringList& theNames )
170 {
171   myPolygons->clear();
172   myPolygons->addItems( theNames );
173 }
174
175 void HYDROGUI_ImportLandCoverMapDlg::setAttributeNames( const QStringList& theAttrNames )
176 {
177   myDBFAttr->clear();
178   myDBFAttr->addItems( theAttrNames );
179 }
180
181 /*void HYDROGUI_ImportLandCoverMapDlg::removePolygonNames( const QStringList& theNames )
182 {
183   QList<QListWidgetItem*> aFoundItems;
184
185   foreach ( const QString& aName, theNames ) {
186     aFoundItems = myPolygons->findItems( aName, Qt::MatchExactly );
187     foreach ( QListWidgetItem* anItem, aFoundItems ) {
188       anItem = myPolygons->takeItem( myPolygons->row( anItem ) );
189       delete anItem;
190     }
191   }
192 }*/
193
194 void HYDROGUI_ImportLandCoverMapDlg::setSelectedPolygonNames( const QStringList& theNames )
195 {
196   myPolygons->clearSelection();
197
198   foreach( const QString aName, theNames ) {
199     QList<QListWidgetItem*> anItems = myPolygons->findItems( aName, Qt::MatchExactly );
200     if ( anItems.count() == 1 ) {
201       anItems.first()->setSelected( true );
202     }
203   }
204 }
205
206 void HYDROGUI_ImportLandCoverMapDlg::onItemSelectionChanged()
207 {
208   emit selectionChanged( getSelectedPolygonNames() );
209 }
210
211 QStringList HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonNames() const
212 {
213   QStringList aSelectedNames;
214
215   QList<QListWidgetItem*> aSelectedItems = myPolygons->selectedItems();
216   foreach( const QListWidgetItem* anItem, aSelectedItems ) {
217     aSelectedNames << anItem->text();
218   }
219
220   return aSelectedNames;
221 }
222
223
224
225 void HYDROGUI_ImportLandCoverMapDlg::onBrowse()
226 {
227   QString aFilter( tr( "LANDCOVERMAP_FILTER" ) );
228   QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_LANDCOVERMAP_FROM_FILE" ), true );
229
230   if( !aFileName.isEmpty() )
231   {
232     setFileName( aFileName );
233     emit FileSelected( aFileName );
234   }
235 }
236
237 void HYDROGUI_ImportLandCoverMapDlg::setObjectName( const QString& theName )
238 {
239   myObjectName->setText( theName );
240   myObjectNameGroup->setEnabled( !theName.isEmpty() || !myFileName->text().isEmpty() );
241 }
242
243 QString HYDROGUI_ImportLandCoverMapDlg::getObjectName() const
244 {
245   return myObjectName->text();
246 }
247
248 void HYDROGUI_ImportLandCoverMapDlg::setFileName( const QString& theFileName )
249 {
250   myFileName->setText( theFileName );
251
252   if ( !myObjectNameGroup->isEnabled() )
253     myObjectNameGroup->setEnabled( !theFileName.isEmpty() );
254 }
255
256 QString HYDROGUI_ImportLandCoverMapDlg::getFileName() const
257 {
258   return myFileName->text();
259 }
260
261
262 void HYDROGUI_ImportLandCoverMapDlg::FillCorrTable(const QStringList& theFirstColumn, const QStringList& theSecondColumn)
263 {
264   int FCSize = theFirstColumn.size();
265   int SCSize = theSecondColumn.size();
266  
267   myTableW->setRowCount(FCSize);
268   myTableW->setColumnCount(2);
269
270   for (int i = 0; i < FCSize; i++)
271   {
272     QTableWidgetItem* aTWI = new QTableWidgetItem();
273     aTWI->setText(theFirstColumn.at(i));
274     aTWI->setFlags(Qt::ItemIsUserCheckable);
275     myTableW->setItem(i, 0, aTWI);
276   }
277   
278   for (int i = 0; i < FCSize; i++)
279   {
280     QComboBox* aCB = new QComboBox();
281     aCB->addItems(theSecondColumn);
282     myTableW->setCellWidget(i, 1, aCB);
283   }
284 }
285
286 QString HYDROGUI_ImportLandCoverMapDlg::getSelectedFieldName() const
287 {
288   return myDBFAttr->selectedItems().first()->text();
289 }
290
291 void HYDROGUI_ImportLandCoverMapDlg::setFirstPageState(bool bState)
292 {
293   myFirstPageState = bState;
294 }
295
296 bool HYDROGUI_ImportLandCoverMapDlg::acceptCurrent() const
297 {
298   //Check the state of curent page
299   int CurrPage = wizard()->currentIndex();
300   switch ( CurrPage )
301   {
302     case 0:
303     {
304       if (!myFirstPageState)
305         return false;
306       break;
307     }
308     case 1:
309     {
310       if (this->myDBFAttr->selectedItems().empty())
311         return false;
312       break;
313     }
314     default:
315      return false;
316   }
317   return true;
318
319 }
320
321 void HYDROGUI_ImportLandCoverMapDlg::GetAttribute2StricklerCorr(QStringList& AttrValues, QStringList& ST)
322 {
323   int aRowCount = myTableW->rowCount();
324   for (int i = 0; i < aRowCount; i++)
325   {
326     QTableWidgetItem* aTWI = myTableW->item(i, 0);
327     AttrValues.push_back(aTWI->text());
328   }
329
330   for (int i = 0; i < aRowCount; i++)
331   {
332     QWidget* aW = myTableW->cellWidget(i, 1);
333     QComboBox* aCB = dynamic_cast<QComboBox*> (aW);
334     ST.push_back(aCB->currentText());
335   }
336 }
337
338
339 QVector<int> HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonIndices() const
340 {
341   QVector<int> aSelectedIndices;
342
343   QModelIndexList aSelectedItems = myPolygons->selectionModel()->selectedIndexes();
344   foreach( QModelIndex index, aSelectedItems ) {
345     aSelectedIndices << index.row();
346   }
347
348   return aSelectedIndices;
349 }