Salome HOME
Minor changes.
[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   aPanel->setObjectName( anObjectName );
99   aPanel->setPolylineNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
100   aPanel->setBathymetryNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_BATHYMETRY ) );
101
102   aPanel->blockSignals( false );
103
104   aPanel->setPolylineName( aSelectedPolyline );
105   aPanel->setBathymetryName( aSelectedBathymetry );
106 }
107
108 void HYDROGUI_ImmersibleZoneOp::abortOperation()
109 {
110   closePreview();
111
112   HYDROGUI_Operation::abortOperation();
113 }
114
115 void HYDROGUI_ImmersibleZoneOp::commitOperation()
116 {
117   closePreview();
118
119   HYDROGUI_Operation::commitOperation();
120 }
121
122 HYDROGUI_InputPanel* HYDROGUI_ImmersibleZoneOp::createInputPanel() const
123 {
124   HYDROGUI_ImmersibleZoneDlg* aPanel = new HYDROGUI_ImmersibleZoneDlg( module(), getName() );
125   connect( aPanel, SIGNAL( CreatePreview( const QString& ) ),
126            this,   SLOT( onCreatePreview( const QString& ) ) );
127   return aPanel;
128 }
129
130 bool HYDROGUI_ImmersibleZoneOp::processApply( int& theUpdateFlags,
131                                               QString& theErrorMsg )
132 {
133   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
134   if ( !aPanel )
135     return false;
136
137   QString anObjectName = aPanel->getObjectName().simplified();
138   if ( anObjectName.isEmpty() )
139   {
140     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
141     return false;
142   }
143
144   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
145   {
146     // check that there are no other objects with the same name in the document
147     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
148     if( !anObject.IsNull() )
149     {
150       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
151       return false;
152     }
153   }
154
155   Handle(HYDROData_PolylineXY) aZonePolyline;
156   Handle(HYDROData_Bathymetry) aZoneBathymetry;
157
158   QString aPolylineName = aPanel->getPolylineName();
159   if ( !aPolylineName.isEmpty() )
160   {
161     aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
162       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
163   }
164
165   QString aBathymetryName = aPanel->getBathymetryName();
166   if ( !aBathymetryName.isEmpty() )
167   {
168     aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
169       HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
170   }
171
172
173   if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
174   {
175     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
176     return false;
177   }
178
179   Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
180     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
181
182   aZoneObj->SetName( anObjectName );
183
184   if ( !myIsEdit )
185   {
186     aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
187     aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
188   }
189
190   aZoneObj->SetPolyline( aZonePolyline );
191   aZoneObj->SetAltitudeObject( aZoneBathymetry );
192   aZoneObj->Update();
193
194   closePreview();
195
196   if( !myIsEdit )
197     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
198
199   module()->setIsToUpdate( aZoneObj );
200
201   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
202
203   return true;
204 }
205
206 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
207 {
208   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
209   if ( !aPanel )
210     return;
211
212   QApplication::setOverrideCursor( Qt::WaitCursor );
213   TopoDS_Shape aZoneShape;
214
215   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
216     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
217   if ( !aPolyline.IsNull() )
218   {
219     aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
220     if( aZoneShape.IsNull() )
221       printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
222   }
223
224   LightApp_Application* anApp = module()->getApp();
225   if ( !getPreviewManager() )
226     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
227                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
228   OCCViewer_ViewManager* aViewManager = getPreviewManager();
229   if ( aViewManager && !myPreviewPrs )
230   {
231     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
232     {
233       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
234       if ( !aCtx.IsNull() )
235         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
236     }
237   }
238
239   if ( aViewManager && myPreviewPrs )
240   {
241     QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
242     QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
243     if ( !myEditedObject.IsNull() ) {
244       aFillingColor = myEditedObject->GetFillingColor();
245       aBorderColor = myEditedObject->GetBorderColor();
246     }
247
248     myPreviewPrs->setFillingColor( aFillingColor, false, false );
249     myPreviewPrs->setBorderColor( aBorderColor, false, false );
250     TopoDS_Face aFace;
251     if( !aZoneShape.IsNull() )
252       aFace = TopoDS::Face( aZoneShape );
253     myPreviewPrs->setFace( aFace );
254   }
255
256   QApplication::restoreOverrideCursor();
257 }
258
259 void HYDROGUI_ImmersibleZoneOp::closePreview()
260 {
261   if( myPreviewPrs )
262   {
263     delete myPreviewPrs;
264     myPreviewPrs = 0;
265   }
266 }