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