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