Salome HOME
b85cfb80a3f283f0a9029555f5cfe3dbcad33d8a
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamOp.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_StreamOp.h"
24
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_Shape.h"
27 #include "HYDROGUI_StreamDlg.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30
31 #include <HYDROData_Document.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_Profile.h>
34
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
38
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Desktop.h>
41
42 #include <OCCViewer_ViewManager.h>
43 #include <OCCViewer_ViewModel.h>
44 #include <OCCViewer_ViewWindow.h>
45 #include <gp_Ax1.hxx>
46 #include <gp_Ax2.hxx>
47 #include <gp_Ax3.hxx>
48 #include <gp_Vec.hxx>
49 #include <gp_Pnt.hxx>
50 #include <gp_Pln.hxx>
51 #include <gp.hxx>
52 #include <TopoDS_Face.hxx>
53 #include <TopoDS.hxx>
54 #include <BRepBuilderAPI_MakeFace.hxx>
55 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
56 : HYDROGUI_Operation( theModule ), 
57   myIsEdit( theIsEdit ),
58   myPreviewPrs( NULL )
59 {
60   setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
61 }
62
63 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
64 {
65   erasePreview();
66 }
67
68 void HYDROGUI_StreamOp::startOperation()
69 {
70   HYDROGUI_Operation::startOperation();
71
72   // We start operation in the document
73   startDocOperation();
74
75   // Get panel and reset its state
76   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
77   aPanel->reset();
78
79   // Get/create the edited object
80   if( myIsEdit ) {
81     myEditedObject = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
82     if ( !myEditedObject.IsNull() && myEditedObject->IsMustBeUpdated() ) {
83       myEditedObject->Update();
84     }
85   } else {
86     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
87   }
88
89   // Get the edited object name
90   QString anObjectName; 
91   if ( !myIsEdit ) {
92     anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
93   } else if ( !myEditedObject.IsNull() ) {
94     anObjectName = myEditedObject->GetName();
95   }
96
97   // Update the panel
98   // set the edited object name
99   aPanel->setObjectName( anObjectName );
100   // set the existing 2D polylines names to the panel
101   aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
102   // synchronize the panel state with the edited object state
103   updatePanelData();
104
105   // Create preview
106   createPreview();
107 }
108
109 void HYDROGUI_StreamOp::abortOperation()
110 {
111   erasePreview();
112   abortDocOperation();
113
114   HYDROGUI_Operation::abortOperation();
115 }
116
117 void HYDROGUI_StreamOp::commitOperation()
118 {
119   erasePreview();
120
121   HYDROGUI_Operation::commitOperation();
122 }
123
124 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
125 {
126   HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
127
128   connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
129   connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ), 
130            this, SLOT( onRemoveProfiles( const QStringList& ) ) );
131   connect( aPanel, SIGNAL( AxisChanged( const QString& ) ), 
132            this, SLOT( onAxisChanged( const QString& ) ) );
133
134   return aPanel;
135 }
136
137 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
138                                       QString& theErrorMsg )
139 {
140   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
141   if ( !aPanel || myEditedObject.IsNull() ) {
142     return false;
143   }
144
145   // Check whether the object name is not empty
146   QString anObjectName = aPanel->getObjectName().simplified();
147   if ( anObjectName.isEmpty() ) {
148     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
149     return false;
150   }
151
152   // Check that there are no other objects with the same name in the document
153   if( myEditedObject->GetName() != anObjectName )
154   {
155     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
156     if( !anObject.IsNull() ) {
157       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
158       return false;
159     }
160   }
161
162   // Check if the axis is set
163   Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
164   if ( aHydraulicAxis.IsNull() ) {
165     theErrorMsg = tr( "AXIS_NOT_DEFINED" );
166     return false;
167   }
168
169   // Check if at least 2 profiles is set
170   HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
171   if ( aProfiles.Length() < 2 ) {
172     theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
173     return false;
174   }
175
176   // Set the object name
177   myEditedObject->SetName( anObjectName );
178
179   if ( !myIsEdit )
180   {
181     myEditedObject->SetFillingColor( HYDROData_Stream::DefaultFillingColor() );
182     myEditedObject->SetBorderColor( HYDROData_Stream::DefaultBorderColor() );
183   }
184
185   // Erase preview
186   erasePreview();
187
188   // Show the object in case of creation mode of the operation
189   if( !myIsEdit ) {
190     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
191   }
192
193   module()->setIsToUpdate( myEditedObject );
194
195   // Set update flags
196   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
197
198   return true;
199 }
200
201 void HYDROGUI_StreamOp::createPreview()
202 {
203   if ( myEditedObject.IsNull() ) {
204     return;
205   }
206
207   LightApp_Application* anApp = module()->getApp();
208   if ( !getPreviewManager() )
209     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
210                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
211
212   OCCViewer_ViewManager* aViewManager = getPreviewManager();
213   if ( aViewManager && !myPreviewPrs ) {
214     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
215       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
216       if ( !aCtx.IsNull() ) {
217         myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject, getPreviewZLayer() );
218       }
219     }
220   }
221
222   if ( myPreviewPrs ) {
223     myPreviewPrs->update( true );
224   }
225 }
226
227 void HYDROGUI_StreamOp::erasePreview()
228 {
229   if( myPreviewPrs ) {
230     delete myPreviewPrs;
231     myPreviewPrs = 0;
232   }
233 }
234
235 void HYDROGUI_StreamOp::onAddProfiles()
236 {
237   if ( myEditedObject.IsNull() ) {
238     return;
239   }
240
241   // Get the current profiles list
242   HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
243
244   // TODO: to be optimized
245   QStringList aCurrentProfiles;
246   for( int i = 1, n = aProfiles.Length(); i <= n; i++ ) {
247     Handle(HYDROData_Profile) aProfile =
248       Handle(HYDROData_Profile)::DownCast( aProfiles.Value( i ) );
249     if ( !aProfile.IsNull() ) {
250       aCurrentProfiles << aProfile->GetName();
251     }
252   }
253     
254   // Get the selected profiles ( in the Object Browser )
255   QStringList anInvalidProfiles, anExistingProfiles, aHasNoIntersectionProfiles;
256
257   HYDROData_SequenceOfObjects aVerifiedProfiles;
258   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
259   Handle(HYDROData_PolylineXY) aHydAxis = myEditedObject->GetHydraulicAxis();
260   if ( aHydAxis.IsNull() )
261     return; 
262   TopoDS_Face aPlane;
263   if(!myEditedObject->BuildFace(aHydAxis, aPlane))
264     return;
265   Standard_Real aPar(.0);
266   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ ) {
267     Handle(HYDROData_Profile) aProfile = 
268       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
269     if ( !aProfile.IsNull() ) {
270       QString aProfileName = aProfile->GetName();
271
272       // Check the profile, if all is ok - add it to the list
273       if ( !aProfile->IsValid() ) { // check whether the profile is valid
274         anInvalidProfiles << aProfileName;
275       } else if ( aCurrentProfiles.contains( aProfileName ) ) { // check whether the profile is already added
276         anExistingProfiles << aProfileName;
277       } else if ( !myEditedObject->HasIntersection( aProfile, aPlane, aPar ) ) {  // check whether the profile has intersection
278         aHasNoIntersectionProfiles << aProfileName;
279       } else {
280         aVerifiedProfiles.Append( aProfile );
281       }
282     }
283   }
284  
285   // Show message box with the ignored profiles
286   if ( !anInvalidProfiles.isEmpty() || !anExistingProfiles.isEmpty() ||
287        !aHasNoIntersectionProfiles.isEmpty() ) {
288     QString aMessage = tr( "IGNORED_PROFILES" );
289     if ( !anInvalidProfiles.isEmpty() ) {
290       aMessage.append( "\n\n" );
291       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
292     }
293     if ( !anExistingProfiles.isEmpty() ) {
294       aMessage.append( "\n\n" );
295       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
296     }
297     if ( !aHasNoIntersectionProfiles.isEmpty() ) {
298       aMessage.append( "\n\n" );
299       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
300     }
301
302     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
303   }
304
305   // Update the stream object
306   for( int i = 1, n = aVerifiedProfiles.Length(); i <= n; i++ ) {
307     Handle(HYDROData_Profile) aProfile =
308       Handle(HYDROData_Profile)::DownCast( aVerifiedProfiles.Value( i ) );
309     myEditedObject->AddProfile( aProfile );
310   }
311   myEditedObject->UpdatePrs();
312
313   // Update the panel
314   updatePanelData();
315
316   // Update preview
317   createPreview();
318 }
319
320 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
321 {
322   if ( myEditedObject.IsNull() ) {
323     return;
324   }
325
326   bool isRemoved = false;
327
328   // Take the Object Browser selection into account
329   HYDROData_SequenceOfObjects aSelectedObjects = HYDROGUI_Tool::GetSelectedObjects( module() ); 
330   for( int i = 1, n = aSelectedObjects.Length(); i <= n; i++ ) {
331     Handle(HYDROData_Profile) aProfile = 
332       Handle(HYDROData_Profile)::DownCast( aSelectedObjects.Value( i ) );
333     if ( !aProfile.IsNull() && !theProfilesToRemove.contains(aProfile->GetName()) ) {
334       if ( myEditedObject->RemoveProfile( aProfile ) ) {
335         isRemoved = true;
336       }
337     }
338   }
339
340   // Remove profiles
341   foreach( const QString& aProfileName, theProfilesToRemove ) {
342     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
343       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
344     if ( myEditedObject->RemoveProfile( aProfile ) ) {
345       isRemoved = true;
346     }
347   }
348
349   if ( isRemoved ) {
350     // Update the edited stream object
351     myEditedObject->UpdatePrs();
352
353     // Update the panel
354     updatePanelData();
355
356     // Update preview
357     createPreview();
358   }
359 }
360
361 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
362 {
363   if ( myEditedObject.IsNull() ) {
364     return;
365   }
366
367   // Get axis object   
368   Handle(HYDROData_PolylineXY) anAxis = Handle(HYDROData_PolylineXY)::DownCast(
369     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
370
371   // Prepare data for intersection check
372   TopoDS_Face aPlane;
373   if ( !myEditedObject->BuildFace(anAxis, aPlane) ) {
374     SUIT_MessageBox::critical( module()->getApp()->desktop(), 
375                               tr( "BAD_SELECTED_POLYLINE_TLT" ),
376                               tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
377     // To restore the old axis
378     updatePanelData();
379     return;
380   }
381
382   Standard_Real aPar(.0);
383
384   // Get list of profiles which do not intersect the axis
385   QStringList aHasNoIntersectionProfiles;
386   HYDROData_SequenceOfObjects aCurrentProfiles = myEditedObject->GetProfiles();
387   for( int anIndex = 1, aLength = aCurrentProfiles.Length(); anIndex <= aLength; anIndex++ ) {
388     Handle(HYDROData_Profile) aProfile =
389       Handle(HYDROData_Profile)::DownCast( aCurrentProfiles.Value( anIndex ) );
390     if ( !aProfile.IsNull() ) {
391       if ( !HYDROData_Stream::HasIntersection( anAxis, aProfile, aPlane, aPar ) ) {
392         aHasNoIntersectionProfiles << aProfile->GetName();
393       }
394     }
395   }
396
397   // If there are profiles which don't intersect the new axis - show confirmation message box
398   bool isConfirmed = true;
399   if ( !aHasNoIntersectionProfiles.isEmpty() ) {
400     SUIT_MessageBox::StandardButtons aButtons = 
401       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
402
403     QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
404     isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
405                                              tr( "CONFIRMATION" ),
406                                              tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
407                                              aButtons, 
408                                              SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
409   }
410
411   // Check if the user has confirmed axis change
412   if ( !isConfirmed ) {
413     // To restore the old axis
414     updatePanelData();
415   } else {
416     // Set axis
417     myEditedObject->SetHydraulicAxis( anAxis );
418     myEditedObject->UpdatePrs();
419
420     // Update the panel
421     updatePanelData();
422
423     // Update preview
424     createPreview();
425   }
426 }
427
428 void HYDROGUI_StreamOp::updatePanelData()
429 {
430   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
431   if ( !aPanel || myEditedObject.IsNull() ) {
432     return;
433   }
434
435   // Hydraulic axis
436   Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
437   if ( !aHydraulicAxis.IsNull() ) {
438     aPanel->setAxisName( aHydraulicAxis->GetName() );
439   } else {
440     aPanel->setAxisName( "" );
441   }
442
443   // Stream profiles
444   QStringList aProfiles;
445
446   HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
447   for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i ) {
448     Handle(HYDROData_Profile) aProfile = 
449       Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
450     if ( !aProfile.IsNull() ) {
451       QString aProfileName = aProfile->GetName();
452       aProfiles << aProfileName;      
453     }
454   }
455
456   aPanel->setProfiles( aProfiles );
457 }