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