Salome HOME
#refs 327 - Polyline is not shown during creation
[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 <SUIT_MessageBox.h>
43 #include <SUIT_Desktop.h>
44
45 #include <TopoDS.hxx>
46 #include <TopoDS_Wire.hxx>
47
48 #include <QApplication>
49
50 HYDROGUI_ImmersibleZoneOp::HYDROGUI_ImmersibleZoneOp( HYDROGUI_Module* theModule,
51                                                       const bool theIsEdit )
52 : HYDROGUI_Operation( theModule ),
53   myIsEdit( theIsEdit ),
54   myPreviewPrs( 0 )
55 {
56   setName( theIsEdit ? tr( "EDIT_IMMERSIBLE_ZONE" ) : tr( "CREATE_IMMERSIBLE_ZONE" ) );
57 }
58
59 HYDROGUI_ImmersibleZoneOp::~HYDROGUI_ImmersibleZoneOp()
60 {
61   closePreview();
62 }
63
64 void HYDROGUI_ImmersibleZoneOp::startOperation()
65 {
66   HYDROGUI_Operation::startOperation();
67
68   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
69   if ( !aPanel )
70     return;
71
72   aPanel->blockSignals( true );
73
74   aPanel->reset();
75
76   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_IMMERSIBLE_ZONE_NAME" ) );
77
78   QString     aSelectedPolyline, aSelectedBathymetry;
79   QStringList aSelectedBathymetries;
80
81   if ( myIsEdit )
82   {
83     myEditedObject = Handle(HYDROData_ImmersibleZone)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
84     if( !myEditedObject.IsNull() )
85     {
86       anObjectName = myEditedObject->GetName();
87
88       Handle(HYDROData_PolylineXY) aRefPolyline = myEditedObject->GetPolyline();
89       if ( !aRefPolyline.IsNull() )
90         aSelectedPolyline = aRefPolyline->GetName();
91
92       Handle(HYDROData_IAltitudeObject) aRefAltitude = myEditedObject->GetAltitudeObject();
93       if ( !aRefAltitude.IsNull() )
94         aSelectedBathymetry = aRefAltitude->GetName();
95     }
96   }
97
98   // collect information about existing closed polylines
99   QStringList aPolylines;
100
101   HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
102   for ( ; anIter.More(); anIter.Next() )
103   {
104     Handle(HYDROData_PolylineXY) aPolylineObj = 
105       Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
106     if ( aPolylineObj.IsNull() )//TODO: || !aPolylineObj->IsClosed() )
107       continue;
108
109     QString aPolylineName = aPolylineObj->GetName();
110     if ( aPolylineName.isEmpty() )
111       continue;
112
113     aPolylines.append( aPolylineName );
114   }
115
116   // collect information about existing bathymetries
117   QStringList aBathymetries;
118
119   anIter = HYDROData_Iterator( doc(), KIND_BATHYMETRY );
120   for ( ; anIter.More(); anIter.Next() )
121   {
122     Handle(HYDROData_Bathymetry) aBathymetryObj = 
123       Handle(HYDROData_Bathymetry)::DownCast( anIter.Current() );
124     if ( aBathymetryObj.IsNull() )
125       continue;
126
127     QString aBathymetryName = aBathymetryObj->GetName();
128     if ( aBathymetryName.isEmpty() )
129       continue;
130
131     aBathymetries.append( aBathymetryName );
132   }
133   
134   aPanel->setObjectName( anObjectName );
135
136   aPanel->setPolylineNames( aPolylines );
137   aPanel->setBathymetryNames( aBathymetries );
138
139   aPanel->blockSignals( false );
140
141   aPanel->setPolylineName( aSelectedPolyline );
142   aPanel->setBathymetryName( aSelectedBathymetry );
143 }
144
145 void HYDROGUI_ImmersibleZoneOp::abortOperation()
146 {
147   closePreview();
148
149   HYDROGUI_Operation::abortOperation();
150 }
151
152 void HYDROGUI_ImmersibleZoneOp::commitOperation()
153 {
154   closePreview();
155
156   HYDROGUI_Operation::commitOperation();
157 }
158
159 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
160 {
161   HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
162   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
163            this,   SLOT( onCreatePreview( const QString& ) ) );
164   return aPanel;
165 }
166
167 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
168                                               QString& theErrorMsg )
169 {
170   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
171   if ( !aPanel )
172     return false;
173
174   QString anObjectName = aPanel->getObjectName().simplified();
175   if ( anObjectName.isEmpty() )
176   {
177     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
178     return false;
179   }
180
181   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
182   {
183     // check that there are no other objects with the same name in the document
184     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
185     if( !anObject.IsNull() )
186     {
187       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
188       return false;
189     }
190   }
191
192   Handle(HYDROData_PolylineXY) aZonePolyline;
193   Handle(HYDROData_Bathymetry) aZoneBathymetry;
194
195   QString aPolylineName = aPanel->getPolylineName();
196   if ( !aPolylineName.isEmpty() )
197   {
198     aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
199       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
200   }
201
202   QString aBathymetryName = aPanel->getBathymetryName();
203   if ( !aBathymetryName.isEmpty() )
204   {
205     aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
206       HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
207   }
208
209
210   if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
211   {
212     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
213     return false;
214   }
215
216   Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
217     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
218
219   aZoneObj->SetName( anObjectName );
220
221   if ( !myIsEdit )
222   {
223     aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
224     aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
225   }
226
227   aZoneObj->SetPolyline( aZonePolyline );
228   aZoneObj->SetAltitudeObject( aZoneBathymetry );
229   aZoneObj->Update();
230
231   closePreview();
232
233   if( !myIsEdit )
234     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
235
236   module()->setIsToUpdate( aZoneObj );
237
238   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
239
240   return true;
241 }
242
243 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
244 {
245   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
246   if ( !aPanel )
247     return;
248
249   QApplication::setOverrideCursor( Qt::WaitCursor );
250   TopoDS_Shape aZoneShape;
251
252   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
253     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
254   if ( !aPolyline.IsNull() )
255   {
256     aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
257     if( aZoneShape.IsNull() )
258       printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
259   }
260
261   LightApp_Application* anApp = module()->getApp();
262   if ( !getPreviewManager() )
263     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
264                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
265   OCCViewer_ViewManager* aViewManager = getPreviewManager();
266   if ( aViewManager && !myPreviewPrs )
267   {
268     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
269     {
270       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
271       if ( !aCtx.IsNull() )
272         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
273     }
274   }
275
276   if ( aViewManager && myPreviewPrs )
277   {
278     QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
279     QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
280     if ( !myEditedObject.IsNull() ) {
281       aFillingColor = myEditedObject->GetFillingColor();
282       aBorderColor = myEditedObject->GetBorderColor();
283     }
284
285     myPreviewPrs->setFillingColor( aFillingColor, false, false );
286     myPreviewPrs->setBorderColor( aBorderColor, false, false );
287     TopoDS_Face aFace;
288     if( !aZoneShape.IsNull() )
289       aFace = TopoDS::Face( aZoneShape );
290     myPreviewPrs->setFace( aFace );
291   }
292
293   QApplication::restoreOverrideCursor();
294 }
295
296 void HYDROGUI_ImmersibleZoneOp::closePreview()
297 {
298   if( myPreviewPrs )
299   {
300     delete myPreviewPrs;
301     myPreviewPrs = 0;
302   }
303 }