Salome HOME
SHP poly 2
[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 <SUIT_ResourceMgr.h>
28 #include <SUIT_Session.h>
29 #include <SUIT_FileDlg.h>
30
31
32 HYDROGUI_ImportLandCoverDlg::HYDROGUI_ImportLandCoverDlg( HYDROGUI_Module* theModule, const QString& theTitle )
33 : HYDROGUI_InputPanel( theModule, theTitle )
34 {
35   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
36
37   // Import bathymetry from file
38   myFileNameGroup = new QGroupBox( tr( "IMPORT_POLYGON_FROM_FILE" ) );
39
40   QLabel* aFileNameLabel = new QLabel( tr( "LANCOVER_NAME" ), myFileNameGroup );
41
42   myFileName = new QLineEdit( myFileNameGroup );
43   myFileName->setReadOnly( true );
44
45   QToolButton* aBrowseBtn = new QToolButton( myFileNameGroup );
46   aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
47
48   QBoxLayout* aFileNameLayout = new QHBoxLayout( myFileNameGroup );
49   aFileNameLayout->setMargin( 5 );
50   aFileNameLayout->setSpacing( 5 );
51   aFileNameLayout->addWidget( aFileNameLabel );
52   aFileNameLayout->addWidget( myFileName );
53   aFileNameLayout->addWidget( aBrowseBtn );
54
55   // Bathymetry name
56   myObjectNameGroup = new QGroupBox( tr( "FILE_NAME" ) );
57
58   QLabel* aBathymetryNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup );
59   myObjectName = new QLineEdit( myObjectNameGroup );
60
61   QBoxLayout* aBathymetryNameLayout = new QHBoxLayout( myObjectNameGroup );
62   aBathymetryNameLayout->setMargin( 5 );
63   aBathymetryNameLayout->setSpacing( 5 );
64   aBathymetryNameLayout->addWidget( aBathymetryNameLabel );
65   aBathymetryNameLayout->addWidget( myObjectName );
66
67   // List of recognized polylines
68   QGroupBox* aPolylinesGroup = new QGroupBox( tr( "FOUND_POLYGONS_OF_SHP_FILE" ), mainFrame() );
69   myPolylines = new QListWidget( aPolylinesGroup );
70   myPolylines->setSelectionMode( QListWidget::ExtendedSelection );
71   myPolylines->setEditTriggers( QListWidget::NoEditTriggers );
72   myPolylines->setViewMode( QListWidget::ListMode );
73   myPolylines->setSortingEnabled( false );
74
75   QBoxLayout* aPolylinesLayout = new QVBoxLayout;
76   aPolylinesLayout->addWidget( myPolylines );
77   aPolylinesGroup->setLayout( aPolylinesLayout );
78   
79   // Layout
80   addWidget( myFileNameGroup );
81   addWidget( myObjectNameGroup );  
82   addWidget( aPolylinesGroup );
83
84   // Conections
85   connect( myPolylines, SIGNAL( itemSelectionChanged() ), this, SLOT( onItemSelectionChanged() ) );
86   connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
87 }
88
89 HYDROGUI_ImportLandCoverDlg::~HYDROGUI_ImportLandCoverDlg()
90 {
91 }
92
93 void HYDROGUI_ImportLandCoverDlg::setImageName( const QString& theName )
94 {
95   myImageName->setText( theName );
96 }
97
98 void HYDROGUI_ImportLandCoverDlg::reset()
99 {
100   myImageName->clear();
101   myPolylines->clear();
102 }
103
104 void HYDROGUI_ImportLandCoverDlg::setPolylineNames( const QStringList& theNames )
105 {
106   myPolylines->clear();
107   myPolylines->addItems( theNames );
108 }
109
110 void HYDROGUI_ImportLandCoverDlg::removePolylineNames( const QStringList& theNames )
111 {
112   QList<QListWidgetItem*> aFoundItems;
113
114   foreach ( const QString& aName, theNames ) {
115     aFoundItems = myPolylines->findItems( aName, Qt::MatchExactly );
116     foreach ( QListWidgetItem* anItem, aFoundItems ) {
117       anItem = myPolylines->takeItem( myPolylines->row( anItem ) );
118       delete anItem;
119     }
120   }
121 }
122
123 void HYDROGUI_ImportLandCoverDlg::setSelectedPolylineNames( const QStringList& theNames )
124 {
125   myPolylines->clearSelection();
126
127   foreach( const QString aName, theNames ) {
128     QList<QListWidgetItem*> anItems = myPolylines->findItems( aName, Qt::MatchExactly );
129     if ( anItems.count() == 1 ) {
130       anItems.first()->setSelected( true );
131     }
132   }
133 }
134
135 void HYDROGUI_ImportLandCoverDlg::onItemSelectionChanged()
136 {
137   emit selectionChanged( getSelectedtPolylineNames() );
138 }
139
140 QStringList HYDROGUI_ImportLandCoverDlg::getSelectedtPolylineNames() const
141 {
142   QStringList aSelectedNames;
143
144   QList<QListWidgetItem*> aSelectedItems = myPolylines->selectedItems();
145   foreach( const QListWidgetItem* anItem, aSelectedItems ) {
146     aSelectedNames << anItem->text();
147   }
148
149   return aSelectedNames;
150 }
151
152
153 void HYDROGUI_ImportLandCoverDlg::onBrowse()
154 {
155   QString aFilter( tr( "LANDCOVER_FILTER" ) );
156   QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_LANDCOVER_FROM_FILE" ), true );
157
158   if( !aFileName.isEmpty() )
159   {
160     setFileName( aFileName );
161     emit FileSelected( aFileName );
162   }
163 }
164
165 void HYDROGUI_ImportLandCoverDlg::setObjectName( const QString& theName )
166 {
167   myObjectName->setText( theName );
168   myObjectNameGroup->setEnabled( !theName.isEmpty() || !myFileName->text().isEmpty() );
169 }
170
171 QString HYDROGUI_ImportLandCoverDlg::getObjectName() const
172 {
173   return myObjectName->text();
174 }
175
176 void HYDROGUI_ImportLandCoverDlg::setFileName( const QString& theFileName )
177 {
178   myFileName->setText( theFileName );
179
180   if ( !myObjectNameGroup->isEnabled() )
181     myObjectNameGroup->setEnabled( !theFileName.isEmpty() );
182 }
183
184 QString HYDROGUI_ImportLandCoverDlg::getFileName() const
185 {
186   return myFileName->text();
187 }