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