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