Salome HOME
The bathymetry is changed to their base altitude class for geometry objects (Bug...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImmersibleZoneOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ImmersibleZoneOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_ImmersibleZoneDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Shape.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Bathymetry.h>
33 #include <HYDROData_Iterator.h>
34 #include <HYDROData_PolylineXY.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
41
42 #include <TopoDS.hxx>
43 #include <TopoDS_Wire.hxx>
44
45 HYDROGUI_ImmersibleZoneOp::HYDROGUI_ImmersibleZoneOp( HYDROGUI_Module* theModule,
46                                                       const bool theIsEdit )
47 : HYDROGUI_Operation( theModule ),
48   myIsEdit( theIsEdit ),
49   myViewManager( 0 ),
50   myPreviewPrs( 0 )
51 {
52   setName( theIsEdit ? tr( "EDIT_IMMERSIBLE_ZONE" ) : tr( "CREATE_IMMERSIBLE_ZONE" ) );
53 }
54
55 HYDROGUI_ImmersibleZoneOp::~HYDROGUI_ImmersibleZoneOp()
56 {
57   closePreview();
58 }
59
60 void HYDROGUI_ImmersibleZoneOp::startOperation()
61 {
62   HYDROGUI_Operation::startOperation();
63
64   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
65   if ( !aPanel )
66     return;
67
68   aPanel->blockSignals( true );
69
70   aPanel->reset();
71
72   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMMERSIBLE_ZONE_NAME" ) );
73
74   QString     aSelectedPolyline, aSelectedBathymetry;
75   QStringList aSelectedBathymetries;
76
77   if ( myIsEdit )
78   {
79     myEditedObject = Handle(HYDROData_ImmersibleZone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
80     if( !myEditedObject.IsNull() )
81     {
82       anObjectName = myEditedObject->GetName();
83
84       Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline();
85       if ( !aRefPolyline.IsNull() )
86         aSelectedPolyline = aRefPolyline->GetName();
87
88       Handle(HYDROData_IAltitudeObject) aRefAltitude = myEditedObject->GetAltitudeObject();
89       if ( !aRefAltitude.IsNull() )
90         aSelectedBathymetry = aRefAltitude->GetName();
91     }
92   }
93
94   // collect information about existing closed polylines
95   QStringList aPolylines;
96
97   HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
98   for ( ; anIter.More(); anIter.Next() )
99   {
100     Handle(HYDROData_PolylineXY) aPolylineObj = 
101       Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
102     if ( aPolylineObj.IsNull() )//TODO: || !aPolylineObj->IsClosed() )
103       continue;
104
105     QString aPolylineName = aPolylineObj->GetName();
106     if ( aPolylineName.isEmpty() )
107       continue;
108
109     aPolylines.append( aPolylineName );
110   }
111
112   // collect information about existing bathymetries
113   QStringList aBathymetries;
114
115   anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
116   for ( ; anIter.More(); anIter.Next() )
117   {
118     Handle(HYDROData_Bathymetry) aBathymetryObj = 
119       Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
120     if ( aBathymetryObj.IsNull() )
121       continue;
122
123     QString aBathymetryName = aBathymetryObj->GetName();
124     if ( aBathymetryName.isEmpty() )
125       continue;
126
127     aBathymetries.append( aBathymetryName );
128   }
129   
130   aPanel->setObjectName( anObjectName );
131
132   aPanel->setPolylineNames( aPolylines );
133   aPanel->setBathymetryNames( aBathymetries );
134
135   aPanel->blockSignals( false );
136
137   aPanel->setPolylineName( aSelectedPolyline );
138   aPanel->setBathymetryName( aSelectedBathymetry );
139 }
140
141 void HYDROGUI_ImmersibleZoneOp::abortOperation()
142 {
143   closePreview();
144
145   HYDROGUI_Operation::abortOperation();
146 }
147
148 void HYDROGUI_ImmersibleZoneOp::commitOperation()
149 {
150   closePreview();
151
152   HYDROGUI_Operation::commitOperation();
153 }
154
155 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
156 {
157   HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
158   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
159            this,   SLOT( onCreatePreview( const QString& ) ) );
160   return aPanel;
161 }
162
163 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
164                                               QString& theErrorMsg )
165 {
166   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
167   if ( !aPanel )
168     return false;
169
170   QString anObjectName = aPanel->getObjectName().simplified();
171   if ( anObjectName.isEmpty() )
172   {
173     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
174     return false;
175   }
176
177   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
178   {
179     // check that there are no other objects with the same name in the document
180     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
181     if( !anObject.IsNull() )
182     {
183       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
184       return false;
185     }
186   }
187
188   Handle(HYDROData_PolylineXY) aZonePolyline;
189   Handle(HYDROData_Bathymetry) aZoneBathymetry;
190
191   QString aPolylineName = aPanel->getPolylineName();
192   if ( !aPolylineName.isEmpty() )
193   {
194     aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
195       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
196   }
197
198   QString aBathymetryName = aPanel->getBathymetryName();
199   if ( !aBathymetryName.isEmpty() )
200   {
201     aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
202       HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
203   }
204
205
206   if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
207   {
208     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
209     return false;
210   }
211
212   Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
213     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
214
215   aZoneObj->SetName( anObjectName );
216
217   if ( !myIsEdit )
218   {
219     aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
220     aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
221   }
222
223   aZoneObj->SetPolyline( aZonePolyline );
224   aZoneObj->SetAltitudeObject( aZoneBathymetry );
225   aZoneObj->Update();
226
227   closePreview();
228
229   if( !myIsEdit )
230     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
231
232   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
233
234   return true;
235 }
236
237 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
238 {
239   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
240   if ( !aPanel )
241     return;
242
243   TopoDS_Shape aZoneShape;
244
245   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
246     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
247   if ( !aPolyline.IsNull() )
248   {
249     aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
250   }
251
252   LightApp_Application* anApp = module()->getApp();
253   if ( !myViewManager )
254     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
255       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
256
257   if ( myViewManager && !myPreviewPrs )
258   {
259     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
260     {
261       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
262       if ( !aCtx.IsNull() )
263         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL );
264     }
265   }
266
267   if ( !myViewManager || !myPreviewPrs )
268     return;
269
270   QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
271   QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
272   if ( !myEditedObject.IsNull() ) {
273     aFillingColor = myEditedObject->GetFillingColor();
274     aBorderColor = myEditedObject->GetBorderColor();
275   }
276
277   myPreviewPrs->setFillingColor( aFillingColor, false, false );
278   myPreviewPrs->setBorderColor( aBorderColor, false, false );
279   TopoDS_Face aFace;
280   if( !aZoneShape.IsNull() )
281     aFace = TopoDS::Face( aZoneShape );
282   myPreviewPrs->setFace( aFace );
283 }
284
285 void HYDROGUI_ImmersibleZoneOp::closePreview()
286 {
287   if( myPreviewPrs )
288   {
289     delete myPreviewPrs;
290     myPreviewPrs = 0;
291   }
292 }