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;
52 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage1() {
53 QWizardPage* aPage = new QWizardPage( mainFrame() );
54 QFrame* aFrame = new QFrame( aPage );
56 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
58 myFileNameGroup = new QGroupBox( tr( "IMPORT_LANDCOVERMAP_FROM_FILE" ) );
60 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
62 myFileName = new QLineEdit( myFileNameGroup );
63 myFileName->setReadOnly( true );
65 QToolButton* aBrowseBtn = new QToolButton( myFileNameGroup );
66 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
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 );
75 myObjectNameGroup = new QGroupBox( tr( "LANDCOVERMAP_NAME" ) );
77 QLabel* aLandcoverNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup );
78 myObjectName = new QLineEdit( myObjectNameGroup );
80 QBoxLayout* aLandcoverNameLayout = new QHBoxLayout( myObjectNameGroup );
81 aLandcoverNameLayout->setMargin( 5 );
82 aLandcoverNameLayout->setSpacing( 5 );
83 aLandcoverNameLayout->addWidget( aLandcoverNameLabel );
84 aLandcoverNameLayout->addWidget( myObjectName );
86 QGroupBox* aPolygonsGroup = new QGroupBox( tr( "FOUNDED_POLYGONS" ) );
87 myPolygons = new QListWidget( aPolygonsGroup );
88 myPolygons->setSelectionMode( QListWidget::ExtendedSelection );
89 myPolygons->setEditTriggers( QListWidget::NoEditTriggers );
90 myPolygons->setViewMode( QListWidget::ListMode );
91 myPolygons->setSortingEnabled( false );
93 QBoxLayout* aPolygonsLayout = new QVBoxLayout;
94 aPolygonsLayout->addWidget( myPolygons );
95 aPolygonsGroup->setLayout( aPolygonsLayout );
97 QVBoxLayout* aPageLayout = new QVBoxLayout;
98 myAttrCheckBox = new QCheckBox( "Use dBase attributes as a Strickler Types" ); //TODO replace with tr()
99 myAttrCheckBox->setChecked(true);
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 );
111 connect( myPolygons, SIGNAL( itemSelectionChanged() ), this, SLOT( onItemSelectionChanged() ) );
112 connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
113 connect( myAttrCheckBox, SIGNAL(clicked(bool)), this, SLOT(onAttrCBChecked(bool)));
118 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage2() {
119 QWizardPage* aPage = new QWizardPage( mainFrame() );
120 QFrame* aFrame = new QFrame( aPage );
122 myAvAttrLabel = new QLabel();
123 myAvAttrLabel->setText("Available attributes");
125 myDBFAttr = new QListWidget( );
126 myDBFAttr->setSelectionMode( QListWidget::SingleSelection );
127 myDBFAttr->setEditTriggers( QListWidget::NoEditTriggers );
128 myDBFAttr->setViewMode( QListWidget::ListMode );
129 myDBFAttr->setSortingEnabled( false );
132 QVBoxLayout* aPageLayout = new QVBoxLayout;
133 aPageLayout->setMargin( 5 );
134 aPageLayout->setSpacing( 5 );
135 aPageLayout->addWidget( myAvAttrLabel );
136 aPageLayout->addWidget( myDBFAttr );
137 aPage->setLayout( aPageLayout );
142 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage3() {
144 QWizardPage* aPage = new QWizardPage( mainFrame() );
145 QFrame* aFrame = new QFrame( aPage );
146 myCLabel = new QLabel();
147 myCLabel->setText("Correspondence");
149 myTableW = new QTableWidget();
152 QVBoxLayout* aPageLayout = new QVBoxLayout;
153 aPageLayout->setMargin( 5 );
154 aPageLayout->setSpacing( 5 );
155 aPageLayout->addWidget( myCLabel );
156 aPageLayout->addWidget( myTableW );
157 aPage->setLayout( aPageLayout );
162 HYDROGUI_ImportLandCoverMapDlg::~HYDROGUI_ImportLandCoverMapDlg()
168 void HYDROGUI_ImportLandCoverMapDlg::reset()
174 void HYDROGUI_ImportLandCoverMapDlg::setPolygonNames( const QStringList& theNames )
177 myPolygons->addItems( theNames );
180 void HYDROGUI_ImportLandCoverMapDlg::setAttributeNames( const QStringList& theAttrNames )
183 myDBFAttr->addItems( theAttrNames );
186 /*void HYDROGUI_ImportLandCoverMapDlg::removePolygonNames( const QStringList& theNames )
188 QList<QListWidgetItem*> aFoundItems;
190 foreach ( const QString& aName, theNames ) {
191 aFoundItems = myPolygons->findItems( aName, Qt::MatchExactly );
192 foreach ( QListWidgetItem* anItem, aFoundItems ) {
193 anItem = myPolygons->takeItem( myPolygons->row( anItem ) );
199 void HYDROGUI_ImportLandCoverMapDlg::setSelectedPolygonNames( const QStringList& theNames )
201 myPolygons->clearSelection();
203 foreach( const QString aName, theNames ) {
204 QList<QListWidgetItem*> anItems = myPolygons->findItems( aName, Qt::MatchExactly );
205 if ( anItems.count() == 1 ) {
206 anItems.first()->setSelected( true );
211 void HYDROGUI_ImportLandCoverMapDlg::onItemSelectionChanged()
213 emit selectionChanged( getSelectedPolygonNames() );
216 QStringList HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonNames() const
218 QStringList aSelectedNames;
220 QList<QListWidgetItem*> aSelectedItems = myPolygons->selectedItems();
221 foreach( const QListWidgetItem* anItem, aSelectedItems )
222 aSelectedNames << anItem->text();
224 return aSelectedNames;
229 void HYDROGUI_ImportLandCoverMapDlg::onBrowse()
231 QString aFilter( tr( "LANDCOVERMAP_FILTER" ) );
232 QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_LANDCOVERMAP_FROM_FILE" ), true );
234 if( !aFileName.isEmpty() )
236 setFileName( aFileName );
237 emit FileSelected( aFileName );
241 void HYDROGUI_ImportLandCoverMapDlg::setObjectName( const QString& theName )
243 myObjectName->setText( theName );
244 myObjectNameGroup->setEnabled( !theName.isEmpty() || !myFileName->text().isEmpty() );
247 QString HYDROGUI_ImportLandCoverMapDlg::getObjectName() const
249 return myObjectName->text();
252 void HYDROGUI_ImportLandCoverMapDlg::setFileName( const QString& theFileName )
254 myFileName->setText( theFileName );
256 if ( !myObjectNameGroup->isEnabled() )
257 myObjectNameGroup->setEnabled( !theFileName.isEmpty() );
260 QString HYDROGUI_ImportLandCoverMapDlg::getFileName() const
262 return myFileName->text();
266 void HYDROGUI_ImportLandCoverMapDlg::FillCorrTable(const QStringList& theFirstColumn,
267 const QStringList& theSecondColumn,
268 const QVector<QColor> theColors)
270 int FCSize = theFirstColumn.size();
271 int SCSize = theSecondColumn.size();
273 myStrColors = theColors;
275 myTableW->setRowCount(FCSize);
276 myTableW->setColumnCount(3);
277 QTableWidgetItem* Header_1 = new QTableWidgetItem(QString("Attribute"), QTableWidgetItem::Type);
278 QTableWidgetItem* Header_2 = new QTableWidgetItem(QString("Strickler Type"), QTableWidgetItem::Type);
279 QTableWidgetItem* Header_3 = new QTableWidgetItem(QString("Color"), QTableWidgetItem::Type);
280 myTableW->setHorizontalHeaderItem(0, Header_1);
281 myTableW->setHorizontalHeaderItem(1, Header_2);
282 myTableW->setHorizontalHeaderItem(2, Header_3);
284 for (int i = 0; i < FCSize; i++)
286 QTableWidgetItem* aTWI = new QTableWidgetItem();
287 aTWI->setText(theFirstColumn.at(i));
288 aTWI->setFlags(Qt::ItemIsUserCheckable);
289 myTableW->setItem(i, 0, aTWI);
292 QSignalMapper* signalMap = new QSignalMapper();
293 for (int i = 0; i < FCSize; i++)
295 QComboBox* aCB = new QComboBox();
296 connect(aCB, SIGNAL(currentIndexChanged(int)), signalMap, SLOT(map()));
297 signalMap->setMapping(aCB, i);
298 aCB->addItems(theSecondColumn);
299 myTableW->setCellWidget(i, 1, aCB);
301 connect(signalMap, SIGNAL(mapped(int)), this, SLOT(OnComboBoxColorChanged(int)));
303 for (int i = 0; i < FCSize; i++)
305 myTableW->setItem(i, 2, new QTableWidgetItem);
306 //take the first color from array; this color corresponds to the first item in the combo box
307 myTableW->item(i, 2)->setBackground(myStrColors[0]);
312 QString HYDROGUI_ImportLandCoverMapDlg::getSelectedFieldName() const
314 return myDBFAttr->selectedItems().first()->text();
317 void HYDROGUI_ImportLandCoverMapDlg::setFirstPageState(bool bState)
319 myFirstPageState = bState;
322 bool HYDROGUI_ImportLandCoverMapDlg::acceptCurrent() const
324 //Check the state of the current page
325 int CurrPage = wizard()->currentIndex();
330 if (!myFirstPageState)
332 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), "File isn't chosen");
335 if (myPolygons->selectedItems().empty())
337 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), "Polygons isn't selected");
340 if (getAttrCheckBoxState() && !getDbfState())
342 SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "DBF_LOAD_ERROR" ), "Cant open DBF file or it's corrupted");
349 if (myDBFAttr->selectedItems().empty())
359 void HYDROGUI_ImportLandCoverMapDlg::GetAttribute2StricklerCorr(QStringList& AttrValues, QStringList& ST)
361 int aRowCount = myTableW->rowCount();
362 for (int i = 0; i < aRowCount; i++)
364 QTableWidgetItem* aTWI = myTableW->item(i, 0);
365 AttrValues.push_back(aTWI->text());
368 for (int i = 0; i < aRowCount; i++)
370 QWidget* aW = myTableW->cellWidget(i, 1);
371 QComboBox* aCB = dynamic_cast<QComboBox*> (aW);
372 ST.push_back(aCB->currentText());
377 QVector<int> HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonIndices() const
379 QVector<int> aSelectedIndices;
381 QModelIndexList aSelectedItems = myPolygons->selectionModel()->selectedIndexes();
382 foreach( QModelIndex index, aSelectedItems ) {
383 aSelectedIndices << index.row();
386 return aSelectedIndices;
389 void HYDROGUI_ImportLandCoverMapDlg::onAttrCBChecked( bool theState )
392 SetButtonsState ( false );
394 SetButtonsState ( true );
397 bool HYDROGUI_ImportLandCoverMapDlg::getAttrCheckBoxState() const
399 return myAttrCheckBox->isChecked();
402 void HYDROGUI_ImportLandCoverMapDlg::OnComboBoxColorChanged(int theInd)
404 QComboBox* CB = qobject_cast<QComboBox*> (myTableW->cellWidget(theInd, 1));
405 int CurIndOfCB = CB->currentIndex();
406 myTableW->item(theInd, 2)->setBackground(myStrColors[CurIndOfCB]);
409 void HYDROGUI_ImportLandCoverMapDlg::setDbfState(bool _state)
414 bool HYDROGUI_ImportLandCoverMapDlg::getDbfState() const