Salome HOME
LCM // Import/Export of SHP p.1
[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
32 #include <QTableWidget>
33 #include <QHeaderView>
34 #include <QComboBox>
35
36 HYDROGUI_ImportLandCoverMapDlg::HYDROGUI_ImportLandCoverMapDlg( HYDROGUI_Module* theModule, const QString& theTitle )
37 : HYDROGUI_Wizard( theModule, theTitle )
38 {
39   addPage( createPage1() );
40   addPage( createPage2() );
41   addPage( createPage3() );
42 }
43
44 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage1() {
45   QWizardPage* aPage = new QWizardPage( mainFrame() );
46   QFrame* aFrame = new QFrame( aPage );
47
48   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
49
50   myFileNameGroup = new QGroupBox( tr( "IMPORT_LANDCOVERMAP_FROM_FILE" ) );
51
52   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
53
54   myFileName = new QLineEdit( myFileNameGroup );
55   myFileName->setReadOnly( true );
56
57   QToolButton* aBrowseBtn = new QToolButton( myFileNameGroup );
58   aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
59
60   QBoxLayout* aFileNameLayout = new QHBoxLayout( myFileNameGroup );
61   aFileNameLayout->setMargin( 5 );
62   aFileNameLayout->setSpacing( 5 );
63   aFileNameLayout->addWidget( aFileNameLabel );
64   aFileNameLayout->addWidget( myFileName );
65   aFileNameLayout->addWidget( aBrowseBtn );
66
67   myObjectNameGroup = new QGroupBox( tr( "LANDCOVERMAP_NAME" ) );
68
69   QLabel* aLandcoverNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup );
70   myObjectName = new QLineEdit( myObjectNameGroup );
71
72   QBoxLayout* aLandcoverNameLayout = new QHBoxLayout( myObjectNameGroup );
73   aLandcoverNameLayout->setMargin( 5 );
74   aLandcoverNameLayout->setSpacing( 5 );
75   aLandcoverNameLayout->addWidget( aLandcoverNameLabel );
76   aLandcoverNameLayout->addWidget( myObjectName );
77
78   QGroupBox* aPolygonsGroup = new QGroupBox( tr( "FOUNDED_POLYGONS" ) );
79   myPolygons = new QListWidget( aPolygonsGroup );
80   myPolygons->setSelectionMode( QListWidget::ExtendedSelection );
81   myPolygons->setEditTriggers( QListWidget::NoEditTriggers );
82   myPolygons->setViewMode( QListWidget::ListMode );
83   myPolygons->setSortingEnabled( false );
84
85   QBoxLayout* aPolygonsLayout = new QVBoxLayout;
86   aPolygonsLayout->addWidget( myPolygons );
87   aPolygonsGroup->setLayout( aPolygonsLayout );
88
89   // Layout
90   QVBoxLayout* aPageLayout = new QVBoxLayout;
91   aPageLayout->setMargin( 5 );
92   aPageLayout->setSpacing( 5 );
93   aPageLayout->addWidget( myFileNameGroup );
94   aPageLayout->addWidget( myObjectNameGroup );
95   aPageLayout->addWidget( aPolygonsGroup );
96   aPage->setLayout( aPageLayout );
97
98   // Conections
99   connect( myPolygons, SIGNAL( itemSelectionChanged() ), this, SLOT( onItemSelectionChanged() ) );
100   connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) ); 
101
102   return aPage;
103 }
104
105 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage2() {
106   QWizardPage* aPage = new QWizardPage( mainFrame() );
107   QFrame* aFrame = new QFrame( aPage );
108
109   myAttrNameGroup = new QGroupBox( "Available attributes" );
110  
111   QCheckBox* anAttrCheckBox = new QCheckBox( "Use dBase attributes as a Strickler Types", myAttrNameGroup );
112
113   QBoxLayout* aFileNameLayout = new QHBoxLayout( myAttrNameGroup );
114   aFileNameLayout->setMargin( 5 );
115   aFileNameLayout->setSpacing( 5 );
116   aFileNameLayout->addWidget( anAttrCheckBox );
117
118
119   myDBFAttr = new QListWidget( aPage );
120   myDBFAttr->setSelectionMode( QListWidget::ExtendedSelection );
121   myDBFAttr->setEditTriggers( QListWidget::NoEditTriggers );
122   myDBFAttr->setViewMode( QListWidget::ListMode );
123   myDBFAttr->setSortingEnabled( false );
124
125   // Layout
126   QVBoxLayout* aPageLayout = new QVBoxLayout;
127   aPageLayout->setMargin( 5 );
128   aPageLayout->setSpacing( 5 );
129   aPageLayout->addWidget( myAttrNameGroup );
130   aPageLayout->addWidget( myDBFAttr );
131   aPage->setLayout( aPageLayout );
132   return aPage;
133 }
134
135 QWizardPage* HYDROGUI_ImportLandCoverMapDlg::createPage3() {
136
137   QWizardPage* aPage = new QWizardPage( mainFrame() );
138   QFrame* aFrame = new QFrame( aPage );
139   myCLabel = new QLabel();
140   myCLabel->setText("Correspondence");
141   
142   myTableW = new QTableWidget();
143
144   myTableW->setRowCount(5);
145   myTableW->setColumnCount(2);
146
147   QStringList list;
148   list << "1" << "2" << "3";
149
150   for (int i = 0; i < 5; i++)
151     for (int j = 0; j < 2; j++)
152     {
153       QComboBox* CB = new QComboBox();
154       CB->addItems(list);
155       myTableW->setCellWidget(i,j,CB);
156     }
157
158   // Layout
159   QVBoxLayout* aPageLayout = new QVBoxLayout;
160   aPageLayout->setMargin( 5 );
161   aPageLayout->setSpacing( 5 );
162   aPageLayout->addWidget( myCLabel );
163   aPageLayout->addWidget( myTableW );
164   aPage->setLayout( aPageLayout );
165   return aPage;
166 }
167
168
169 HYDROGUI_ImportLandCoverMapDlg::~HYDROGUI_ImportLandCoverMapDlg()
170 {
171 }
172
173 void HYDROGUI_ImportLandCoverMapDlg::reset()
174 {
175   myPolygons->clear();
176 }
177
178 void HYDROGUI_ImportLandCoverMapDlg::setPolygonNames( const QStringList& theNames )
179 {
180   myPolygons->clear();
181   myPolygons->addItems( theNames );
182 }
183
184 void HYDROGUI_ImportLandCoverMapDlg::removePolygonNames( const QStringList& theNames )
185 {
186   QList<QListWidgetItem*> aFoundItems;
187
188   foreach ( const QString& aName, theNames ) {
189     aFoundItems = myPolygons->findItems( aName, Qt::MatchExactly );
190     foreach ( QListWidgetItem* anItem, aFoundItems ) {
191       anItem = myPolygons->takeItem( myPolygons->row( anItem ) );
192       delete anItem;
193     }
194   }
195 }
196
197 void HYDROGUI_ImportLandCoverMapDlg::setSelectedPolygonNames( const QStringList& theNames )
198 {
199   myPolygons->clearSelection();
200
201   foreach( const QString aName, theNames ) {
202     QList<QListWidgetItem*> anItems = myPolygons->findItems( aName, Qt::MatchExactly );
203     if ( anItems.count() == 1 ) {
204       anItems.first()->setSelected( true );
205     }
206   }
207 }
208
209 void HYDROGUI_ImportLandCoverMapDlg::onItemSelectionChanged()
210 {
211   emit selectionChanged( getSelectedPolygonNames() );
212 }
213
214 QStringList HYDROGUI_ImportLandCoverMapDlg::getSelectedPolygonNames() const
215 {
216   QStringList aSelectedNames;
217
218   QList<QListWidgetItem*> aSelectedItems = myPolygons->selectedItems();
219   foreach( const QListWidgetItem* anItem, aSelectedItems ) {
220     aSelectedNames << anItem->text();
221   }
222
223   return aSelectedNames;
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