Salome HOME
17.12.2013. Added Partition algorithm (draft version).
[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_Bathymetry) aRefBathymetry = myEditedObject->GetBathymetry();
89       if ( !aRefBathymetry.IsNull() )
90         aSelectedBathymetry = aRefBathymetry->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() || !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_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
189     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
190   if ( aZoneObj.IsNull() )
191     return false;
192
193   Handle(HYDROData_PolylineXY) aZonePolyline;
194   Handle(HYDROData_Bathymetry) aZoneBathymetry;
195
196   QString aPolylineName = aPanel->getPolylineName();
197   if ( !aPolylineName.isEmpty() )
198   {
199     aZonePolyline = Handle(HYDROData_PolylineXY)::DownCast(
200       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
201   }
202
203   QString aBathymetryName = aPanel->getBathymetryName();
204   if ( !aBathymetryName.isEmpty() )
205   {
206     aZoneBathymetry = Handle(HYDROData_Bathymetry)::DownCast(
207       HYDROGUI_Tool::FindObjectByName( module(), aBathymetryName, KIND_BATHYMETRY ) );
208   }
209
210   aZoneObj->SetName( anObjectName );
211
212   if ( !myIsEdit )
213   {
214     aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
215     aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
216   }
217
218   aZoneObj->SetPolyline( aZonePolyline );
219   aZoneObj->SetBathymetry( aZoneBathymetry );
220   aZoneObj->Update();
221
222   closePreview();
223
224   if( !myIsEdit )
225     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
226
227   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
228
229   return true;
230 }
231
232 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
233 {
234   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
235   if ( !aPanel )
236     return;
237
238   TopoDS_Wire aWire;
239   TopoDS_Shape aShape;
240
241   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
242     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
243   if ( !aPolyline.IsNull() )
244   {
245     aShape = aPolyline->GetShape();
246     if ( aShape.ShapeType() == TopAbs_WIRE ) {
247       aWire = TopoDS::Wire( aShape );
248     }
249   }
250
251   LightApp_Application* anApp = module()->getApp();
252   if ( !myViewManager )
253     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
254       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
255
256   if ( myViewManager && !myPreviewPrs )
257   {
258     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
259     {
260       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
261       if ( !aCtx.IsNull() )
262         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL );
263     }
264   }
265
266   if ( !myViewManager || !myPreviewPrs )
267     return;
268
269   QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
270   QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
271   if ( !myEditedObject.IsNull() ) {
272     aFillingColor = myEditedObject->GetFillingColor();
273     aBorderColor = myEditedObject->GetBorderColor();
274   }
275
276   myPreviewPrs->setFillingColor( aFillingColor, false, false );
277   myPreviewPrs->setBorderColor( aBorderColor, false, false );
278   if ( !aWire.IsNull() ) {
279     myPreviewPrs->setFace( aWire );
280   } else if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND ) {
281     TopoDS_Compound aCompound = TopoDS::Compound( aShape );
282     if ( !aCompound.IsNull() ) {
283       myPreviewPrs->setFaces( aCompound );
284     }
285   }
286 }
287
288 void HYDROGUI_ImmersibleZoneOp::closePreview()
289 {
290   if( myPreviewPrs )
291   {
292     delete myPreviewPrs;
293     myPreviewPrs = 0;
294   }
295 }