Salome HOME
++ shapefil
[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   myFileNameGroup = new QGroupBox( tr( "IMPORT_LANDCOVER_FROM_FILE" ) );
38
39   QLabel* aFileNameLabel = new QLabel( tr( "FILE_NAME" ), myFileNameGroup );
40
41   myFileName = new QLineEdit( myFileNameGroup );
42   myFileName->setReadOnly( true );
43
44   QToolButton* aBrowseBtn = new QToolButton( myFileNameGroup );
45   aBrowseBtn->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "BROWSE_ICO" ) ) );
46
47   QBoxLayout* aFileNameLayout = new QHBoxLayout( myFileNameGroup );
48   aFileNameLayout->setMargin( 5 );
49   aFileNameLayout->setSpacing( 5 );
50   aFileNameLayout->addWidget( aFileNameLabel );
51   aFileNameLayout->addWidget( myFileName );
52   aFileNameLayout->addWidget( aBrowseBtn );
53
54   myObjectNameGroup = new QGroupBox( tr( "LANDCOVER_NAME" ) );
55
56   QLabel* aLandcoverNameLabel = new QLabel( tr( "NAME" ), myObjectNameGroup );
57   myObjectName = new QLineEdit( myObjectNameGroup );
58
59   QBoxLayout* aLandcoverNameLayout = new QHBoxLayout( myObjectNameGroup );
60   aLandcoverNameLayout->setMargin( 5 );
61   aLandcoverNameLayout->setSpacing( 5 );
62   aLandcoverNameLayout->addWidget( aLandcoverNameLabel );
63   aLandcoverNameLayout->addWidget( myObjectName );
64
65   QGroupBox* aPolygonsGroup = new QGroupBox( tr( "FOUNDED_POLYGONS" ), mainFrame() );
66   myPolygons = new QListWidget( aPolygonsGroup );
67   myPolygons->setSelectionMode( QListWidget::ExtendedSelection );
68   myPolygons->setEditTriggers( QListWidget::NoEditTriggers );
69   myPolygons->setViewMode( QListWidget::ListMode );
70   myPolygons->setSortingEnabled( false );
71
72   QBoxLayout* aPolygonsLayout = new QVBoxLayout;
73   aPolygonsLayout->addWidget( myPolygons );
74   aPolygonsGroup->setLayout( aPolygonsLayout );
75   
76   // Layout
77   addWidget( myFileNameGroup );
78   addWidget( myObjectNameGroup );  
79   addWidget( aPolygonsGroup );
80
81   // Conections
82   connect( myPolygons, SIGNAL( itemSelectionChanged() ), this, SLOT( onItemSelectionChanged() ) );
83   connect( aBrowseBtn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
84 }
85
86 HYDROGUI_ImportLandCoverDlg::~HYDROGUI_ImportLandCoverDlg()
87 {
88 }
89
90 void HYDROGUI_ImportLandCoverDlg::reset()
91 {
92   myPolygons->clear();
93 }
94
95 void HYDROGUI_ImportLandCoverDlg::setPolygonNames( const QStringList& theNames )
96 {
97   myPolygons->clear();
98   myPolygons->addItems( theNames );
99 }
100
101 void HYDROGUI_ImportLandCoverDlg::removePolygonNames( const QStringList& theNames )
102 {
103   QList<QListWidgetItem*> aFoundItems;
104
105   foreach ( const QString& aName, theNames ) {
106     aFoundItems = myPolygons->findItems( aName, Qt::MatchExactly );
107     foreach ( QListWidgetItem* anItem, aFoundItems ) {
108       anItem = myPolygons->takeItem( myPolygons->row( anItem ) );
109       delete anItem;
110     }
111   }
112 }
113
114 void HYDROGUI_ImportLandCoverDlg::setSelectedPolygonNames( const QStringList& theNames )
115 {
116   myPolygons->clearSelection();
117
118   foreach( const QString aName, theNames ) {
119     QList<QListWidgetItem*> anItems = myPolygons->findItems( aName, Qt::MatchExactly );
120     if ( anItems.count() == 1 ) {
121       anItems.first()->setSelected( true );
122     }
123   }
124 }
125
126 void HYDROGUI_ImportLandCoverDlg::onItemSelectionChanged()
127 {
128   emit selectionChanged( getSelectedPolygonNames() );
129 }
130
131 QStringList HYDROGUI_ImportLandCoverDlg::getSelectedPolygonNames() const
132 {
133   QStringList aSelectedNames;
134
135   QList<QListWidgetItem*> aSelectedItems = myPolygons->selectedItems();
136   foreach( const QListWidgetItem* anItem, aSelectedItems ) {
137     aSelectedNames << anItem->text();
138   }
139
140   return aSelectedNames;
141 }
142
143
144 void HYDROGUI_ImportLandCoverDlg::onBrowse()
145 {
146   QString aFilter( tr( "LANDCOVER_FILTER" ) );
147   QString aFileName = SUIT_FileDlg::getFileName( this, "", aFilter, tr( "IMPORT_LANDCOVER_FROM_FILE" ), true );
148
149   if( !aFileName.isEmpty() )
150   {
151     setFileName( aFileName );
152     emit FileSelected( aFileName );
153   }
154 }
155
156 void HYDROGUI_ImportLandCoverDlg::setObjectName( const QString& theName )
157 {
158   myObjectName->setText( theName );
159   myObjectNameGroup->setEnabled( !theName.isEmpty() || !myFileName->text().isEmpty() );
160 }
161
162 QString HYDROGUI_ImportLandCoverDlg::getObjectName() const
163 {
164   return myObjectName->text();
165 }
166
167 void HYDROGUI_ImportLandCoverDlg::setFileName( const QString& theFileName )
168 {
169   myFileName->setText( theFileName );
170
171   if ( !myObjectNameGroup->isEnabled() )
172     myObjectNameGroup->setEnabled( !theFileName.isEmpty() );
173 }
174
175 QString HYDROGUI_ImportLandCoverDlg::getFileName() const
176 {
177   return myFileName->text();
178 }