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