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