Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_LandCoverMapDlg.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_LandCoverMapDlg.h"
20
21 #include "HYDROGUI_Operations.h"
22 #include "HYDROGUI_StricklerTypeComboBox.h"
23
24 #include <HYDROData_Object.h>
25 #include <HYDROData_PolylineXY.h>
26
27 #include <QLineEdit>
28 #include <QComboBox>
29 #include <QGroupBox>
30 #include <QLabel>
31 #include <QLayout>
32
33 HYDROGUI_LandCoverMapDlg::HYDROGUI_LandCoverMapDlg( HYDROGUI_Module* theModule,
34                                                     const QString& theTitle,
35                                                     const int theOperationId )
36 : HYDROGUI_InputPanel( theModule, theTitle ),
37   HYDROGUI_ObjComboBoxFilter(),
38   myOperationId( theOperationId )
39 {
40   // Land Cover Map name
41   myObjectNameGroup = new QGroupBox( tr( "LAND_COVER_MAP_NAME" ), mainFrame() );
42
43   myObjectNameCreate = new QLineEdit( myObjectNameGroup );
44   myObjectNameEdit = new QComboBox( myObjectNameGroup );
45   myObjectNameEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
46
47   QBoxLayout* aNameLayout = new QHBoxLayout( myObjectNameGroup );
48   aNameLayout->setMargin( 5 );
49   aNameLayout->setSpacing( 5 );
50   aNameLayout->addWidget( new QLabel( tr( "NAME" ), myObjectNameGroup ) );
51   aNameLayout->addWidget( myObjectNameCreate );
52   aNameLayout->addWidget( myObjectNameEdit );
53
54   myParamGroup = new QGroupBox( tr( "LAND_COVER_MAP_PARAMETERS" ), mainFrame() );
55   QGridLayout* aParamLayout = new QGridLayout( myParamGroup );
56   aParamLayout->setMargin( 5 );
57   aParamLayout->setSpacing( 10 );
58   
59   // Polyline/Face name
60   myPolylinesFacesLabel = new QLabel( tr( "LAND_COVER_MAP_POLYLINE_FACE" ) );
61   aParamLayout->addWidget( myPolylinesFacesLabel, 0, 0, 1, 1 );
62   aParamLayout->addWidget( myPolylinesFaces = new HYDROGUI_ObjComboBox( theModule, "", KIND_UNKNOWN, myParamGroup ), 0, 1, 1, 1 );
63   myPolylinesFaces->setObjectFilter( this );
64
65   // Strickler type name
66   myStricklerTypesLabel = new QLabel( tr( "LAND_COVER_MAP_STRICKLER_TYPE" ), myParamGroup );
67   myStricklerTypes = new HYDROGUI_StricklerTypeComboBox( theModule, "", myParamGroup );
68   aParamLayout->addWidget( myStricklerTypesLabel, 1, 0, 1, 1 );
69   aParamLayout->addWidget( myStricklerTypes, 1, 1, 1, 1 );
70
71   // Common
72   addWidget( myObjectNameGroup );
73   addWidget( myParamGroup );
74
75   addStretch();
76
77   // Connect signals and slots
78   connect( myObjectNameEdit, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onLandCoverMapChanged() ) );
79   connect( myPolylinesFaces, SIGNAL( objectSelected( const QString& ) ), this, SLOT( onPolylineFaceChanged( const QString& ) ) );
80
81   updateState( true );
82
83 }
84
85 HYDROGUI_LandCoverMapDlg::~HYDROGUI_LandCoverMapDlg()
86 {
87 }
88
89 void HYDROGUI_LandCoverMapDlg::reset()
90 {
91   bool isBlocked = blockSignals( true );
92
93   myPolylinesFaces->reset();
94
95   blockSignals( isBlocked );
96
97   updateState();
98 }
99
100 void HYDROGUI_LandCoverMapDlg::setObjectNames( const QStringList& theNames )
101 {
102   bool isBlocked = blockSignals( true );
103
104   myObjectNameEdit->clear();
105   myObjectNameEdit->addItems( theNames );
106
107   blockSignals( isBlocked );
108 }
109
110 void HYDROGUI_LandCoverMapDlg::setObjectName( const QString& theName )
111 {
112   if ( myObjectNameCreate->isVisible() )
113     myObjectNameCreate->setText( theName );
114   else
115     myObjectNameEdit->setCurrentIndex( myObjectNameEdit->findText( theName ) );
116 }
117
118 QString HYDROGUI_LandCoverMapDlg::getObjectName() const
119 {
120   if ( myObjectNameCreate->isVisible() )
121     return myObjectNameCreate->text();
122   return myObjectNameEdit->currentText();
123 }
124
125 QString HYDROGUI_LandCoverMapDlg::getPolylineFaceName() const
126 {
127   return myPolylinesFaces->selectedObject();
128 }
129
130 void HYDROGUI_LandCoverMapDlg::setPolylineFaceName( const QString& theName )
131 {
132   myPolylinesFaces->setSelectedObject( theName );
133 }
134
135 Handle(HYDROData_Entity) HYDROGUI_LandCoverMapDlg::getPolylineFace() const
136 {
137   return myPolylinesFaces->GetObject();
138 }
139
140 void HYDROGUI_LandCoverMapDlg::setSelectedStricklerTypeName( const QString& theName )
141 {
142   myStricklerTypes->setSelectedStricklerTypeName( theName );
143 }
144
145 QString HYDROGUI_LandCoverMapDlg::getSelectedStricklerTypeName() const
146 {
147   return myStricklerTypes->getSelectedStricklerTypeName();
148 }
149
150 bool HYDROGUI_LandCoverMapDlg::isOk( const Handle(HYDROData_Entity)& theEntity ) const
151 {
152   Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast(theEntity);
153   Handle(HYDROData_Object) anObject2d3dPrs = Handle(HYDROData_Object)::DownCast(theEntity);  
154   return ( !anObject2d3dPrs.IsNull() ||
155            ( !aPolylineXY.IsNull() && ( ( myOperationId == CreateLandCoverMapId || myOperationId == AddLandCoverId ) && aPolylineXY->IsClosed() ||
156                                         myOperationId == SplitLandCoverId ) ) );
157 }
158
159 void HYDROGUI_LandCoverMapDlg::onLandCoverMapChanged()
160 {
161   if ( signalsBlocked() )
162     return;
163
164   reset();
165
166   emit landCoverMapChanged( getObjectName() );
167 }
168
169 void HYDROGUI_LandCoverMapDlg::onPolylineFaceChanged( const QString& )
170 {
171   updateState();
172   // TODO: select chosen polyline/face in the 3D viewer, if it is not selected yet (i.e. object was chosen not in the viewer, but in combo-box)
173 }
174
175 void HYDROGUI_LandCoverMapDlg::updateState( bool theInitialConfigure )
176 {
177   if ( theInitialConfigure )
178   {
179     myObjectNameCreate->setVisible( myOperationId == CreateLandCoverMapId );
180     myObjectNameEdit->setVisible( myOperationId != CreateLandCoverMapId );
181
182     myParamGroup->setVisible( myOperationId != RemoveLandCoverId );
183
184     bool aShowPolylinesFacesCtrls = ( myOperationId == CreateLandCoverMapId ||
185                                       myOperationId == AddLandCoverId ||
186                                       myOperationId == SplitLandCoverId );
187     myPolylinesFacesLabel->setVisible( aShowPolylinesFacesCtrls );
188     myPolylinesFaces->setVisible( aShowPolylinesFacesCtrls );
189
190     bool aShowStricklerTypesCtrls = ( myOperationId == CreateLandCoverMapId ||
191                                       myOperationId == AddLandCoverId ||
192                                       myOperationId == MergeLandCoverId ||
193                                       myOperationId == ChangeLandCoverTypeId );
194     myStricklerTypesLabel->setVisible( aShowStricklerTypesCtrls );
195     myStricklerTypes->setVisible( aShowStricklerTypesCtrls );
196   }
197   else
198   {
199     bool anEmptyObjectName = getObjectName().isEmpty();
200
201     bool anEmptyPolylineFaceName = false;
202     if ( myPolylinesFaces->isVisible() )
203       anEmptyPolylineFaceName = getPolylineFaceName().isEmpty();
204
205     setApplyEnabled( !anEmptyObjectName && !anEmptyPolylineFaceName );
206   }
207 }