Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[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 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
40 #include <QSignalMapper>
41
42 HYDROGUI_ImportLandCoverMapDlg::HYDROGUI_ImportLandCoverMapDlg( HYDROGUI_Module* theModule, const QString& theTitle )
43 : HYDROGUI_Wizard( theModule, theTitle )
44 {
45   addPage( createPage1() );
46   addPage( createPage2() );
47   addPage( createPage3() );
48   myFirstPageState = false;
49   myDbfState = false;
50   myUpdateViewerState = false;
51 }
52
53 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage1() {
54   QWizardPage* aPage = new QWizardPage( mainFrame() );
55   QFrame* aFrame = new QFrame( aPage );
56
57   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
58
59   myFileNameGroup = new QGroupBox( tr( "IMPORT_LANDCOVER_MAP_FROM_FILE" ) );
60
61   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
62
63   myFileName = new QLineEdit( myFileNameGroup );
64   myFileName->setReadOnly( true );
65
66   QToolButton* aBrowseBtn = new QToolButton( myFileNameGroup );
67   aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
68
69   QBoxLayout* aFileNameLayout = new QHBoxLayout( myFileNameGroup );
70   aFileNameLayout->setMargin( 5 );
71   aFileNameLayout->setSpacing( 5 );
72   aFileNameLayout->addWidget( aFileNameLabel );
73   aFileNameLayout->addWidget( myFileName );
74   aFileNameLayout->addWidget( aBrowseBtn );
75     
76   myObjectNameGroup = new QGroupBox( tr( "LANDCOVERMAP_NAME" ) );
77
78   QLabel* aLandcoverNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup );
79   myObjectName = new QLineEdit( myObjectNameGroup );
80
81   QBoxLayout* aLandcoverNameLayout = new QHBoxLayout( myObjectNameGroup );
82   aLandcoverNameLayout->setMargin( 5 );
83   aLandcoverNameLayout->setSpacing( 5 );
84   aLandcoverNameLayout->addWidget( aLandcoverNameLabel );
85   aLandcoverNameLayout->addWidget( myObjectName );
86
87   QGroupBox* aPolygonsGroup = new QGroupBox( tr( "FOUND_POLYGONS" ) );
88   myPolygonsListWidget = new QListWidget( aPolygonsGroup );
89   myPolygonsListWidget->setSelectionMode( QListWidget::ExtendedSelection );
90   myPolygonsListWidget->setEditTriggers( QListWidget::NoEditTriggers );
91   myPolygonsListWidget->setViewMode( QListWidget::ListMode );
92   myPolygonsListWidget->setSortingEnabled( false );
93
94   QBoxLayout* aPolygonsLayout = new QVBoxLayout;
95   aPolygonsLayout->addWidget( myPolygonsListWidget );
96   aPolygonsGroup->setLayout( aPolygonsLayout );
97
98   QVBoxLayout* aPageLayout = new QVBoxLayout;
99   myAttrCheckBox = new QCheckBox( tr("USE_DBF_AS_ST") );
100   myAttrCheckBox->setChecked(true);
101
102   // Layout
103   aPageLayout->setMargin( 5 );
104   aPageLayout->setSpacing( 5 );
105   aPageLayout->addWidget( myFileNameGroup );
106   aPageLayout->addWidget( myAttrCheckBox );
107   aPageLayout->addWidget( myObjectNameGroup );
108   aPageLayout->addWidget( aPolygonsGroup );
109   aPage->setLayout( aPageLayout );
110
111   // Conections
112   connect( myPolygonsListWidget, SIGNAL( itemSelectionChanged() ), this, SLOT( onItemSelectionChanged() ) );
113   connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) ); 
114   connect( myAttrCheckBox, SIGNAL(clicked(bool)), this, SLOT(onAttrCBChecked(bool)));
115
116   return aPage;
117 }
118
119 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage2() {
120   QWizardPage* aPage = new QWizardPage( mainFrame() );
121   QFrame* aFrame = new QFrame( aPage );
122
123   myAvAttrLabel = new QLabel();
124   myAvAttrLabel->setText(tr("AV_ATTRS"));
125   
126   myDBFAttrListWidget = new QListWidget( );
127   myDBFAttrListWidget->setSelectionMode( QListWidget::SingleSelection );
128   myDBFAttrListWidget->setEditTriggers( QListWidget::NoEditTriggers );
129   myDBFAttrListWidget->setViewMode( QListWidget::ListMode );
130   myDBFAttrListWidget->setSortingEnabled( false );
131
132   // Layout
133   QVBoxLayout* aPageLayout = new QVBoxLayout;
134   aPageLayout->setMargin( 5 );
135   aPageLayout->setSpacing( 5 );
136   aPageLayout->addWidget( myAvAttrLabel );
137   aPageLayout->addWidget( myDBFAttrListWidget );
138   aPage->setLayout( aPageLayout );
139
140   return aPage;
141 }
142
143 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage3() {
144
145   QWizardPage* aPage = new QWizardPage( mainFrame() );
146   QFrame* aFrame = new QFrame( aPage );
147   myCorrLabel = new QLabel();
148   myCorrLabel->setText(tr ("CORR"));
149   
150   myCorrTableWidget = new QTableWidget();
151
152   // Layout
153   QVBoxLayout* aPageLayout = new QVBoxLayout;
154   aPageLayout->setMargin( 5 );
155   aPageLayout->setSpacing( 5 );
156   aPageLayout->addWidget( myCorrLabel );
157   aPageLayout->addWidget( myCorrTableWidget );
158   aPage->setLayout( aPageLayout );
159   return aPage;
160 }
161
162
163 HYDROGUI_ImportLandCoverMapDlg::~HYDROGUI_ImportLandCoverMapDlg()
164 {
165   myPolygonsListWidget->clear();
166   myDBFAttrListWidget->clear();
167 }
168
169 void HYDROGUI_ImportLandCoverMapDlg::reset()
170 {
171   myPolygonsListWidget->clear();
172   myDBFAttrListWidget->clear();
173 }
174
175 void HYDROGUI_ImportLandCoverMapDlg::setPolygonNames( const QStringList& theNames )
176 {
177   myPolygonsListWidget->clear();
178   myPolygonsListWidget->addItems( theNames );
179 }
180
181 void HYDROGUI_ImportLandCoverMapDlg::setAttributeNames( const QStringList& theAttrNames )
182 {
183   myDBFAttrListWidget->clear();
184   myDBFAttrListWidget->addItems( theAttrNames );
185 }
186
187 void HYDROGUI_ImportLandCoverMapDlg::setSelectedPolygonNames( const QStringList& theNames )
188 {
189   myPolygonsListWidget->clearSelection();
190
191   foreach( const QString aName, theNames ) {
192     QList<QListWidgetItem*> anItems = myPolygonsListWidget->findItems( aName, Qt::MatchExactly );
193     if ( anItems.count() == 1 ) {
194       anItems.first()->setSelected( true );
195     }
196   }
197 }
198
199 void HYDROGUI_ImportLandCoverMapDlg::onItemSelectionChanged()
200
201   int aCurIndex = getCurrentWizardIndex();
202   if (aCurIndex == 0 && getViewerState())
203     emit selectionChanged( getSelectedPolygonNames() );
204 }
205
206 QStringList HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonNames() const
207 {
208   QStringList aSelectedNames;
209
210   QList<QListWidgetItem*> aSelectedItems = myPolygonsListWidget->selectedItems();
211   foreach( const QListWidgetItem* anItem, aSelectedItems )
212     aSelectedNames << anItem->text();
213
214   return aSelectedNames;
215 }
216
217 void HYDROGUI_ImportLandCoverMapDlg::onBrowse()
218 {
219   QString aFilter( tr( "LANDCOVERMAP_FILTER" ) );
220   QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_LANDCOVER_MAP_FROM_FILE" ), true );
221
222   if( !aFileName.isEmpty() )
223   {
224     setFileName( aFileName );
225     emit FileSelected( aFileName );
226   }
227 }
228
229 void HYDROGUI_ImportLandCoverMapDlg::setObjectName( const QString& theName )
230 {
231   myObjectName->setText( theName );
232   myObjectNameGroup->setEnabled( !theName.isEmpty() || !myFileName->text().isEmpty() );
233 }
234
235 QString HYDROGUI_ImportLandCoverMapDlg::getObjectName() const
236 {
237   return myObjectName->text();
238 }
239
240 void HYDROGUI_ImportLandCoverMapDlg::setFileName( const QString& theFileName )
241 {
242   myFileName->setText( theFileName );
243
244   if ( !myObjectNameGroup->isEnabled() )
245     myObjectNameGroup->setEnabled( !theFileName.isEmpty() );
246 }
247
248 QString HYDROGUI_ImportLandCoverMapDlg::getFileName() const
249 {
250   return myFileName->text();
251 }
252
253
254 void HYDROGUI_ImportLandCoverMapDlg::FillCorrespondenceTable(const QStringList& theFirstColumn, 
255                                                    const QStringList& theSecondColumn,
256                                                    const QVector<int> theDefCBIndices,
257                                                    const QVector<QColor> theColors)
258 {
259   int aFCSize = theFirstColumn.size();  // == theDefCBIndices.size() !
260   int aSCSize = theSecondColumn.size(); 
261
262   myStrColors = theColors;
263
264   myCorrTableWidget->setRowCount(aFCSize);
265   myCorrTableWidget->setColumnCount(3);
266   QTableWidgetItem* aHeader_1 = new QTableWidgetItem(QString(tr("TABLE_H1")), QTableWidgetItem::Type);
267   QTableWidgetItem* aHeader_2 = new QTableWidgetItem(QString(tr("TABLE_H2")), QTableWidgetItem::Type);
268   QTableWidgetItem* aHeader_3 = new QTableWidgetItem(QString(tr("TABLE_H3")), QTableWidgetItem::Type);
269   myCorrTableWidget->setHorizontalHeaderItem(0, aHeader_1);
270   myCorrTableWidget->setHorizontalHeaderItem(1, aHeader_2);
271   myCorrTableWidget->setHorizontalHeaderItem(2, aHeader_3);
272   myCorrTableWidget->horizontalHeader()->setSectionResizeMode( QHeaderView::ResizeToContents );
273   //
274   for (int i = 0; i < aFCSize; i++)
275   {
276     QTableWidgetItem* aTWI = new QTableWidgetItem();
277     aTWI->setText(theFirstColumn.at(i));
278     aTWI->setFlags(Qt::ItemIsUserCheckable);
279     myCorrTableWidget->setItem(i, 0, aTWI);
280   }
281   //
282   QSignalMapper* signalMap = new QSignalMapper();
283   for (int i = 0; i < aFCSize; i++)
284   {
285     QComboBox* aCB = new QComboBox();
286     connect(aCB, SIGNAL(currentIndexChanged(int)), signalMap, SLOT(map()));
287     signalMap->setMapping(aCB, i);
288     aCB->addItems(theSecondColumn);
289     aCB->setCurrentIndex(theDefCBIndices[i]);
290     myCorrTableWidget->setCellWidget(i, 1, aCB);
291   }
292   connect(signalMap, SIGNAL(mapped(int)), this, SLOT(onComboBoxColorChanged(int)));
293
294   //
295   for (int i = 0; i < aFCSize; i++)
296   {
297     QTableWidgetItem* aTWI = new QTableWidgetItem;
298     aTWI->setBackground(myStrColors[theDefCBIndices[i]]);
299     aTWI->setFlags(Qt::NoItemFlags);
300     myCorrTableWidget->setItem(i, 2, aTWI);
301   }
302
303 }
304
305 QString HYDROGUI_ImportLandCoverMapDlg::getSelectedFieldName() const
306 {
307   return myDBFAttrListWidget->selectedItems().first()->text();
308 }
309
310 void HYDROGUI_ImportLandCoverMapDlg::setFirstPageState(bool theState)
311 {
312   myFirstPageState = theState;
313 }
314
315 bool HYDROGUI_ImportLandCoverMapDlg::getFirstPageState() const
316 {
317   return myFirstPageState;
318 }
319
320
321 bool HYDROGUI_ImportLandCoverMapDlg::acceptCurrent() const
322 {
323   //Check the state of the current page
324   int aCurrPage = getCurrentWizardIndex();
325   switch ( aCurrPage )
326   {
327     case 0:
328     {
329       //this method verifies first page
330       if (!CheckFirstPageFilling())
331         return false;
332       break;
333     }
334     case 1:
335     {
336       if (myDBFAttrListWidget->selectedItems().empty())
337       {
338         SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), tr ("ATTRS_ISNT_SELECTED"));
339         return false;
340       }
341       break;
342     }
343     case 2: //last page
344     {
345       if (this->isPolygonListEmpty())
346       {
347         SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), tr("POLYGONS_ISNT_SELECTED"));
348         return false;
349       }
350       break;
351     }
352     default:
353      return false;
354   }
355   return true;
356 }
357
358 void HYDROGUI_ImportLandCoverMapDlg::getValAttr2StricklerTypeCorr(QStringList& theAttrValues, QStringList& theST)
359 {
360   int aRowCount = myCorrTableWidget->rowCount();
361   for (int i = 0; i < aRowCount; i++)
362   {
363     QTableWidgetItem* aTWI = myCorrTableWidget->item(i, 0);
364     theAttrValues.push_back(aTWI->text());
365   }
366
367   for (int i = 0; i < aRowCount; i++)
368   {
369     QWidget* aW = myCorrTableWidget->cellWidget(i, 1);
370     QComboBox* aCB = dynamic_cast<QComboBox*> (aW);
371     theST.push_back(aCB->currentText());
372   }
373 }
374
375
376 QVector<int> HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonIndices() const
377 {
378   QVector<int> aSelectedIndices;
379
380   QModelIndexList aSelectedItems = myPolygonsListWidget->selectionModel()->selectedIndexes();
381   foreach( QModelIndex index, aSelectedItems ) {
382     aSelectedIndices << index.row();
383   }
384
385   return aSelectedIndices;
386 }
387
388 void HYDROGUI_ImportLandCoverMapDlg::onAttrCBChecked( bool theState )
389 {
390   if (theState)
391     SetButtonsState ( false );
392   else
393     SetButtonsState ( true );
394 }
395
396 bool HYDROGUI_ImportLandCoverMapDlg::getAttrCheckBoxState() const
397 {
398   return myAttrCheckBox->isChecked();
399 }
400
401 void HYDROGUI_ImportLandCoverMapDlg::onComboBoxColorChanged(int theInd)
402 {
403   QComboBox* CB = qobject_cast<QComboBox*> (myCorrTableWidget->cellWidget(theInd, 1));
404   int aCurIndOfCB = CB->currentIndex();
405   myCorrTableWidget->item(theInd, 2)->setBackground(myStrColors[aCurIndOfCB]);
406 }
407
408 void HYDROGUI_ImportLandCoverMapDlg::setDbfState(bool theState)
409 {
410   myDbfState = theState;
411 }
412
413 bool HYDROGUI_ImportLandCoverMapDlg::getDbfState() const
414 {
415   return myDbfState;
416 }
417
418 void HYDROGUI_ImportLandCoverMapDlg::setDbfRecordsNb(int theRecordsNbs)
419 {
420   myDbfRecordsNbs = theRecordsNbs;
421 }
422
423 int HYDROGUI_ImportLandCoverMapDlg::getDbfRecordsNb() const
424 {
425   return myDbfRecordsNbs;
426 }
427
428 int HYDROGUI_ImportLandCoverMapDlg::getCurrentWizardIndex() const
429 {
430   return wizard()->currentIndex();
431 }
432
433 void HYDROGUI_ImportLandCoverMapDlg::setViewerState(bool theState)
434 {
435   myUpdateViewerState = theState;
436 }
437
438 bool HYDROGUI_ImportLandCoverMapDlg::getViewerState() const
439 {
440   return myUpdateViewerState;
441 }
442
443 bool HYDROGUI_ImportLandCoverMapDlg::isPolygonListEmpty() const
444 {
445   return myPolygonsListWidget->selectedItems().empty();
446 }
447
448 bool HYDROGUI_ImportLandCoverMapDlg::CheckFirstPageFilling() const
449 {
450   if (!this->getFirstPageState())
451   {
452     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), tr("FILE_ISNT_CHOSEN"));
453     return false;
454   }
455   if (this->isPolygonListEmpty())
456   {
457     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), tr("POLYGONS_ISNT_SELECTED"));
458     return false;
459   }
460   if (this->getAttrCheckBoxState() && !this->getDbfState())
461   {
462      SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "DBF_LOAD_ERROR" ), tr("DBF_LOAD_ERR_MESS"));
463      return false;
464   }
465   if (this->getAttrCheckBoxState() && this->getDbfRecordsNb() == 0)
466   {
467      SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "DBF_NB_RECORDS_IS_NULL" ), tr("DBF_NB_RECORDS_IS_NULL_MESS"));
468      return false;
469   }
470   return true;
471 }