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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_ImportLandCoverMapDlg.h"
24 #include <QListWidget>
25 #include <QVBoxLayout>
26 #include <QToolButton>
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>
37 #include <QStackedWidget>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
40 #include <QSignalMapper>
42 HYDROGUI_ImportLandCoverMapDlg::HYDROGUI_ImportLandCoverMapDlg( HYDROGUI_Module* theModule, const QString& theTitle )
43 : HYDROGUI_Wizard( theModule, theTitle )
45 addPage( createPage1() );
46 addPage( createPage2() );
47 addPage( createPage3() );
48 myFirstPageState = false;
50 myUpdateViewerState = false;
53 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage1() {
54 QWizardPage* aPage = new QWizardPage( mainFrame() );
55 QFrame* aFrame = new QFrame( aPage );
57 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
59 myFileNameGroup = new QGroupBox( tr( "IMPORT_LANDCOVER_MAP_FROM_FILE" ) );
61 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
63 myFileName = new QLineEdit( myFileNameGroup );
64 myFileName->setReadOnly( true );
66 QToolButton* aBrowseBtn = new QToolButton( myFileNameGroup );
67 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
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 );
76 myObjectNameGroup = new QGroupBox( tr( "LANDCOVERMAP_NAME" ) );
78 QLabel* aLandcoverNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup );
79 myObjectName = new QLineEdit( myObjectNameGroup );
81 QBoxLayout* aLandcoverNameLayout = new QHBoxLayout( myObjectNameGroup );
82 aLandcoverNameLayout->setMargin( 5 );
83 aLandcoverNameLayout->setSpacing( 5 );
84 aLandcoverNameLayout->addWidget( aLandcoverNameLabel );
85 aLandcoverNameLayout->addWidget( myObjectName );
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 );
94 QBoxLayout* aPolygonsLayout = new QVBoxLayout;
95 aPolygonsLayout->addWidget( myPolygonsListWidget );
96 aPolygonsGroup->setLayout( aPolygonsLayout );
98 QVBoxLayout* aPageLayout = new QVBoxLayout;
99 myAttrCheckBox = new QCheckBox( tr("USE_DBF_AS_ST") );
100 myAttrCheckBox->setChecked(true);
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 );
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)));
119 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage2() {
120 QWizardPage* aPage = new QWizardPage( mainFrame() );
121 QFrame* aFrame = new QFrame( aPage );
123 myAvAttrLabel = new QLabel();
124 myAvAttrLabel->setText(tr("AV_ATTRS"));
126 myDBFAttrListWidget = new QListWidget( );
127 myDBFAttrListWidget->setSelectionMode( QListWidget::SingleSelection );
128 myDBFAttrListWidget->setEditTriggers( QListWidget::NoEditTriggers );
129 myDBFAttrListWidget->setViewMode( QListWidget::ListMode );
130 myDBFAttrListWidget->setSortingEnabled( false );
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 );
143 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage3() {
145 QWizardPage* aPage = new QWizardPage( mainFrame() );
146 QFrame* aFrame = new QFrame( aPage );
147 myCorrLabel = new QLabel();
148 myCorrLabel->setText(tr ("CORR"));
150 myCorrTableWidget = new QTableWidget();
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 );
163 HYDROGUI_ImportLandCoverMapDlg::~HYDROGUI_ImportLandCoverMapDlg()
165 myPolygonsListWidget->clear();
166 myDBFAttrListWidget->clear();
169 void HYDROGUI_ImportLandCoverMapDlg::reset()
171 myPolygonsListWidget->clear();
172 myDBFAttrListWidget->clear();
175 void HYDROGUI_ImportLandCoverMapDlg::setPolygonNames( const QStringList& theNames )
177 myPolygonsListWidget->clear();
178 myPolygonsListWidget->addItems( theNames );
181 void HYDROGUI_ImportLandCoverMapDlg::setAttributeNames( const QStringList& theAttrNames )
183 myDBFAttrListWidget->clear();
184 myDBFAttrListWidget->addItems( theAttrNames );
187 void HYDROGUI_ImportLandCoverMapDlg::setSelectedPolygonNames( const QStringList& theNames )
189 myPolygonsListWidget->clearSelection();
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 );
199 void HYDROGUI_ImportLandCoverMapDlg::onItemSelectionChanged()
201 int aCurIndex = getCurrentWizardIndex();
202 if (aCurIndex == 0 && getViewerState())
203 emit selectionChanged( getSelectedPolygonNames() );
206 QStringList HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonNames() const
208 QStringList aSelectedNames;
210 QList<QListWidgetItem*> aSelectedItems = myPolygonsListWidget->selectedItems();
211 foreach( const QListWidgetItem* anItem, aSelectedItems )
212 aSelectedNames << anItem->text();
214 return aSelectedNames;
217 void HYDROGUI_ImportLandCoverMapDlg::onBrowse()
219 QString aFilter( tr( "LANDCOVERMAP_FILTER" ) );
220 QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_LANDCOVER_MAP_FROM_FILE" ), true );
222 if( !aFileName.isEmpty() )
224 setFileName( aFileName );
225 emit FileSelected( aFileName );
229 void HYDROGUI_ImportLandCoverMapDlg::setObjectName( const QString& theName )
231 myObjectName->setText( theName );
232 myObjectNameGroup->setEnabled( !theName.isEmpty() || !myFileName->text().isEmpty() );
235 QString HYDROGUI_ImportLandCoverMapDlg::getObjectName() const
237 return myObjectName->text();
240 void HYDROGUI_ImportLandCoverMapDlg::setFileName( const QString& theFileName )
242 myFileName->setText( theFileName );
244 if ( !myObjectNameGroup->isEnabled() )
245 myObjectNameGroup->setEnabled( !theFileName.isEmpty() );
248 QString HYDROGUI_ImportLandCoverMapDlg::getFileName() const
250 return myFileName->text();
254 void HYDROGUI_ImportLandCoverMapDlg::FillCorrespondenceTable(const QStringList& theFirstColumn,
255 const QStringList& theSecondColumn,
256 const QVector<int> theDefCBIndices,
257 const QVector<QColor> theColors)
259 int aFCSize = theFirstColumn.size(); // == theDefCBIndices.size() !
260 int aSCSize = theSecondColumn.size();
262 myStrColors = theColors;
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()->setResizeMode( QHeaderView::ResizeToContents );
274 for (int i = 0; i < aFCSize; i++)
276 QTableWidgetItem* aTWI = new QTableWidgetItem();
277 aTWI->setText(theFirstColumn.at(i));
278 aTWI->setFlags(Qt::ItemIsUserCheckable);
279 myCorrTableWidget->setItem(i, 0, aTWI);
282 QSignalMapper* signalMap = new QSignalMapper();
283 for (int i = 0; i < aFCSize; i++)
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);
292 connect(signalMap, SIGNAL(mapped(int)), this, SLOT(onComboBoxColorChanged(int)));
295 for (int i = 0; i < aFCSize; i++)
297 QTableWidgetItem* aTWI = new QTableWidgetItem;
298 aTWI->setBackground(myStrColors[theDefCBIndices[i]]);
299 aTWI->setFlags(Qt::NoItemFlags);
300 myCorrTableWidget->setItem(i, 2, aTWI);
305 QString HYDROGUI_ImportLandCoverMapDlg::getSelectedFieldName() const
307 return myDBFAttrListWidget->selectedItems().first()->text();
310 void HYDROGUI_ImportLandCoverMapDlg::setFirstPageState(bool theState)
312 myFirstPageState = theState;
315 bool HYDROGUI_ImportLandCoverMapDlg::getFirstPageState() const
317 return myFirstPageState;
321 bool HYDROGUI_ImportLandCoverMapDlg::acceptCurrent() const
323 //Check the state of the current page
324 int aCurrPage = getCurrentWizardIndex();
329 //this method verifies first page
330 if (!CheckFirstPageFilling())
336 if (myDBFAttrListWidget->selectedItems().empty())
338 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), tr ("ATTRS_ISNT_SELECTED"));
345 if (this->isPolygonListEmpty())
347 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), tr("POLYGONS_ISNT_SELECTED"));
358 void HYDROGUI_ImportLandCoverMapDlg::getValAttr2StricklerTypeCorr(QStringList& theAttrValues, QStringList& theST)
360 int aRowCount = myCorrTableWidget->rowCount();
361 for (int i = 0; i < aRowCount; i++)
363 QTableWidgetItem* aTWI = myCorrTableWidget->item(i, 0);
364 theAttrValues.push_back(aTWI->text());
367 for (int i = 0; i < aRowCount; i++)
369 QWidget* aW = myCorrTableWidget->cellWidget(i, 1);
370 QComboBox* aCB = dynamic_cast<QComboBox*> (aW);
371 theST.push_back(aCB->currentText());
376 QVector<int> HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonIndices() const
378 QVector<int> aSelectedIndices;
380 QModelIndexList aSelectedItems = myPolygonsListWidget->selectionModel()->selectedIndexes();
381 foreach( QModelIndex index, aSelectedItems ) {
382 aSelectedIndices << index.row();
385 return aSelectedIndices;
388 void HYDROGUI_ImportLandCoverMapDlg::onAttrCBChecked( bool theState )
391 SetButtonsState ( false );
393 SetButtonsState ( true );
396 bool HYDROGUI_ImportLandCoverMapDlg::getAttrCheckBoxState() const
398 return myAttrCheckBox->isChecked();
401 void HYDROGUI_ImportLandCoverMapDlg::onComboBoxColorChanged(int theInd)
403 QComboBox* CB = qobject_cast<QComboBox*> (myCorrTableWidget->cellWidget(theInd, 1));
404 int aCurIndOfCB = CB->currentIndex();
405 myCorrTableWidget->item(theInd, 2)->setBackground(myStrColors[aCurIndOfCB]);
408 void HYDROGUI_ImportLandCoverMapDlg::setDbfState(bool theState)
410 myDbfState = theState;
413 bool HYDROGUI_ImportLandCoverMapDlg::getDbfState() const
418 int HYDROGUI_ImportLandCoverMapDlg::getCurrentWizardIndex() const
420 return wizard()->currentIndex();
423 void HYDROGUI_ImportLandCoverMapDlg::setViewerState(bool theState)
425 myUpdateViewerState = theState;
428 bool HYDROGUI_ImportLandCoverMapDlg::getViewerState() const
430 return myUpdateViewerState;
433 bool HYDROGUI_ImportLandCoverMapDlg::isPolygonListEmpty() const
435 return myPolygonsListWidget->selectedItems().empty();
438 bool HYDROGUI_ImportLandCoverMapDlg::CheckFirstPageFilling() const
440 if (!this->getFirstPageState())
442 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), tr("FILE_ISNT_CHOSEN"));
445 if (this->isPolygonListEmpty())
447 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), tr("POLYGONS_ISNT_SELECTED"));
450 if (this->getAttrCheckBoxState() && !this->getDbfState())
452 SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "DBF_LOAD_ERROR" ), tr("DBF_LOAD_ERR_MESS"));