Salome HOME
refs #327 - Polyline is not shown during creation
[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   myViewManager( 0 ),
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   // 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() )//TODO: || !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_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
211   if ( HYDROData_ImmersibleZone::generateTopShape( aZonePolyline ).IsNull() )
212   {
213     theErrorMsg = tr( "ZONE_OBJECT_CANNOT_BE_CREATED" );
214     return false;
215   }
216
217   Handle(HYDROData_ImmersibleZone) aZoneObj = myIsEdit ? myEditedObject :
218     Handle(HYDROData_ImmersibleZone)::DownCast( doc()->CreateObject( KIND_IMMERSIBLE_ZONE ) );
219
220   aZoneObj->SetName( anObjectName );
221
222   if ( !myIsEdit )
223   {
224     aZoneObj->SetFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
225     aZoneObj->SetBorderColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
226   }
227
228   aZoneObj->SetPolyline( aZonePolyline );
229   aZoneObj->SetAltitudeObject( aZoneBathymetry );
230   aZoneObj->Update();
231
232   closePreview();
233
234   if( !myIsEdit )
235     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aZoneObj, true );
236
237   module()->setIsToUpdate( aZoneObj );
238
239   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
240
241   return true;
242 }
243
244 void HYDROGUI_ImmersibleZoneOp::onCreatePreview( const QString& thePolylineName )
245 {
246   HYDROGUI_ImmersibleZoneDlg* aPanel = ::qobject_cast<HYDROGUI_ImmersibleZoneDlg*>( inputPanel() );
247   if ( !aPanel )
248     return;
249
250   QApplication::setOverrideCursor( Qt::WaitCursor );
251   TopoDS_Shape aZoneShape;
252
253   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
254     HYDROGUI_Tool::FindObjectByName( module(), thePolylineName, KIND_POLYLINEXY ) );
255   if ( !aPolyline.IsNull() )
256   {
257     aZoneShape = HYDROData_ImmersibleZone::generateTopShape( aPolyline );
258     if( aZoneShape.IsNull() )
259       printErrorMessage( tr( "ZONE_OBJECT_CANNOT_BE_CREATED" ) );
260   }
261
262   LightApp_Application* anApp = module()->getApp();
263   if ( !myViewManager )
264     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
265       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
266
267   if ( myViewManager && !myPreviewPrs )
268   {
269     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
270     {
271       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
272       if ( !aCtx.IsNull() )
273         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL );
274     }
275   }
276
277   if ( myViewManager && myPreviewPrs )
278   {
279     QColor aFillingColor = HYDROData_ImmersibleZone::DefaultFillingColor();
280     QColor aBorderColor = HYDROData_ImmersibleZone::DefaultBorderColor();
281     if ( !myEditedObject.IsNull() ) {
282       aFillingColor = myEditedObject->GetFillingColor();
283       aBorderColor = myEditedObject->GetBorderColor();
284     }
285
286     myPreviewPrs->setFillingColor( aFillingColor, false, false );
287     myPreviewPrs->setBorderColor( aBorderColor, false, false );
288     TopoDS_Face aFace;
289     if( !aZoneShape.IsNull() )
290       aFace = TopoDS::Face( aZoneShape );
291     myPreviewPrs->setFace( aFace );
292   }
293
294   QApplication::restoreOverrideCursor();
295 }
296
297 void HYDROGUI_ImmersibleZoneOp::closePreview()
298 {
299   if( myPreviewPrs )
300   {
301     delete myPreviewPrs;
302     myPreviewPrs = 0;
303   }
304 }