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