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;
51 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage1() {
52 QWizardPage* aPage = new QWizardPage( mainFrame() );
53 QFrame* aFrame = new QFrame( aPage );
55 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
57 myFileNameGroup = new QGroupBox( tr( "IMPORT_LANDCOVERMAP_FROM_FILE" ) );
59 QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
61 myFileName = new QLineEdit( myFileNameGroup );
62 myFileName->setReadOnly( true );
64 QToolButton* aBrowseBtn = new QToolButton( myFileNameGroup );
65 aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
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 );
74 myObjectNameGroup = new QGroupBox( tr( "LANDCOVERMAP_NAME" ) );
76 QLabel* aLandcoverNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup );
77 myObjectName = new QLineEdit( myObjectNameGroup );
79 QBoxLayout* aLandcoverNameLayout = new QHBoxLayout( myObjectNameGroup );
80 aLandcoverNameLayout->setMargin( 5 );
81 aLandcoverNameLayout->setSpacing( 5 );
82 aLandcoverNameLayout->addWidget( aLandcoverNameLabel );
83 aLandcoverNameLayout->addWidget( myObjectName );
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 );
92 QBoxLayout* aPolygonsLayout = new QVBoxLayout;
93 aPolygonsLayout->addWidget( myPolygons );
94 aPolygonsGroup->setLayout( aPolygonsLayout );
96 QVBoxLayout* aPageLayout = new QVBoxLayout;
97 myAttrCheckBox = new QCheckBox( "Use dBase attributes as a Strickler Types" ); //TODO replace with tr()
98 myAttrCheckBox->setChecked(true);
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 );
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)));
117 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage2() {
118 QWizardPage* aPage = new QWizardPage( mainFrame() );
119 QFrame* aFrame = new QFrame( aPage );
121 myAvAttrLabel = new QLabel();
122 myAvAttrLabel->setText("Available attributes");
124 myDBFAttr = new QListWidget( );
125 myDBFAttr->setSelectionMode( QListWidget::SingleSelection );
126 myDBFAttr->setEditTriggers( QListWidget::NoEditTriggers );
127 myDBFAttr->setViewMode( QListWidget::ListMode );
128 myDBFAttr->setSortingEnabled( false );
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 );
141 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage3() {
143 QWizardPage* aPage = new QWizardPage( mainFrame() );
144 QFrame* aFrame = new QFrame( aPage );
145 myCLabel = new QLabel();
146 myCLabel->setText("Correspondence");
148 myTableW = new QTableWidget();
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 );
161 HYDROGUI_ImportLandCoverMapDlg::~HYDROGUI_ImportLandCoverMapDlg()
165 void HYDROGUI_ImportLandCoverMapDlg::reset()
171 void HYDROGUI_ImportLandCoverMapDlg::setPolygonNames( const QStringList& theNames )
174 myPolygons->addItems( theNames );
177 void HYDROGUI_ImportLandCoverMapDlg::setAttributeNames( const QStringList& theAttrNames )
180 myDBFAttr->addItems( theAttrNames );
183 /*void HYDROGUI_ImportLandCoverMapDlg::removePolygonNames( const QStringList& theNames )
185 QList<QListWidgetItem*> aFoundItems;
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 ) );
196 void HYDROGUI_ImportLandCoverMapDlg::setSelectedPolygonNames( const QStringList& theNames )
198 myPolygons->clearSelection();
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 );
208 void HYDROGUI_ImportLandCoverMapDlg::onItemSelectionChanged()
210 emit selectionChanged( getSelectedPolygonNames() );
213 QStringList HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonNames() const
215 QStringList aSelectedNames;
217 QList<QListWidgetItem*> aSelectedItems = myPolygons->selectedItems();
218 foreach( const QListWidgetItem* anItem, aSelectedItems ) {
219 aSelectedNames << anItem->text();
222 return aSelectedNames;
227 void HYDROGUI_ImportLandCoverMapDlg::onBrowse()
229 QString aFilter( tr( "LANDCOVERMAP_FILTER" ) );
230 QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_LANDCOVERMAP_FROM_FILE" ), true );
232 if( !aFileName.isEmpty() )
234 setFileName( aFileName );
235 emit FileSelected( aFileName );
239 void HYDROGUI_ImportLandCoverMapDlg::setObjectName( const QString& theName )
241 myObjectName->setText( theName );
242 myObjectNameGroup->setEnabled( !theName.isEmpty() || !myFileName->text().isEmpty() );
245 QString HYDROGUI_ImportLandCoverMapDlg::getObjectName() const
247 return myObjectName->text();
250 void HYDROGUI_ImportLandCoverMapDlg::setFileName( const QString& theFileName )
252 myFileName->setText( theFileName );
254 if ( !myObjectNameGroup->isEnabled() )
255 myObjectNameGroup->setEnabled( !theFileName.isEmpty() );
258 QString HYDROGUI_ImportLandCoverMapDlg::getFileName() const
260 return myFileName->text();
264 void HYDROGUI_ImportLandCoverMapDlg::FillCorrTable(const QStringList& theFirstColumn,
265 const QStringList& theSecondColumn,
266 const QVector<QColor> theColors)
268 int FCSize = theFirstColumn.size();
269 int SCSize = theSecondColumn.size();
271 myStrColors = theColors;
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);
282 for (int i = 0; i < FCSize; i++)
284 QTableWidgetItem* aTWI = new QTableWidgetItem();
285 aTWI->setText(theFirstColumn.at(i));
286 aTWI->setFlags(Qt::ItemIsUserCheckable);
287 myTableW->setItem(i, 0, aTWI);
290 QSignalMapper* signalMap = new QSignalMapper();
291 for (int i = 0; i < FCSize; i++)
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);
299 connect(signalMap, SIGNAL(mapped(int)), this, SLOT(OnComboBoxColorChanged(int)));
301 for (int i = 0; i < FCSize; i++)
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]);
310 QString HYDROGUI_ImportLandCoverMapDlg::getSelectedFieldName() const
312 return myDBFAttr->selectedItems().first()->text();
315 void HYDROGUI_ImportLandCoverMapDlg::setFirstPageState(bool bState)
317 myFirstPageState = bState;
320 bool HYDROGUI_ImportLandCoverMapDlg::acceptCurrent() const
322 //Check the state of curent page
323 int CurrPage = wizard()->currentIndex();
328 if (!myFirstPageState)
330 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), "File isn't chosen");
333 if (myPolygons->selectedItems().empty())
335 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "LCM_IMPORT_WARNING" ), "Polygons isn't selected");
342 if (myDBFAttr->selectedItems().empty())
353 void HYDROGUI_ImportLandCoverMapDlg::GetAttribute2StricklerCorr(QStringList& AttrValues, QStringList& ST)
355 int aRowCount = myTableW->rowCount();
356 for (int i = 0; i < aRowCount; i++)
358 QTableWidgetItem* aTWI = myTableW->item(i, 0);
359 AttrValues.push_back(aTWI->text());
362 for (int i = 0; i < aRowCount; i++)
364 QWidget* aW = myTableW->cellWidget(i, 1);
365 QComboBox* aCB = dynamic_cast<QComboBox*> (aW);
366 ST.push_back(aCB->currentText());
371 QVector<int> HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonIndices() const
373 QVector<int> aSelectedIndices;
375 QModelIndexList aSelectedItems = myPolygons->selectionModel()->selectedIndexes();
376 foreach( QModelIndex index, aSelectedItems ) {
377 aSelectedIndices << index.row();
380 return aSelectedIndices;
383 void HYDROGUI_ImportLandCoverMapDlg::onAttrCBChecked( bool theState )
386 SetButtonsState ( false );
388 SetButtonsState ( true );
391 bool HYDROGUI_ImportLandCoverMapDlg::getAttrCheckBoxState() const
393 return myAttrCheckBox->isChecked();
396 void HYDROGUI_ImportLandCoverMapDlg::OnComboBoxColorChanged(int theInd)
398 QComboBox* CB = qobject_cast<QComboBox *> (myTableW->cellWidget(theInd, 1));
399 int CurIndOfCB = CB->currentIndex();
400 myTableW->item(theInd, 2)->setBackground(myStrColors[CurIndOfCB]);