Salome HOME
Merge branch 'BR_MULTI_BATHS' into HEAD
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_StreamOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include <HYDROGUI_DataObject.h>
23 #include "HYDROGUI_Shape.h"
24 #include "HYDROGUI_StreamDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_Tool2.h"
27 #include "HYDROGUI_UpdateFlags.h"
28
29 #include <HYDROData_Document.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Profile.h>
32
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
36
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_Desktop.h>
39
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
43 #include <gp_Ax1.hxx>
44 #include <gp_Ax2.hxx>
45 #include <gp_Ax3.hxx>
46 #include <gp_Vec.hxx>
47 #include <gp_Pnt.hxx>
48 #include <gp_Pln.hxx>
49 #include <gp.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS.hxx>
52 #include <BRepBuilderAPI_MakeFace.hxx>
53
54 void insertProfileInToOrder( const QString& theProfileName,
55                              const double&  theProfilePar,
56                              QStringList&   theProfiles,
57                              QList<double>& theProfileParams )
58 {
59   bool anIsInserted = false;
60   for ( int k = 0; k < theProfileParams.length(); ++k )
61   {
62     const double& aParam = theProfileParams.value( k );
63     if ( theProfilePar < aParam )
64     {
65       theProfiles.insert( k, theProfileName );
66       theProfileParams.insert( k, theProfilePar );
67       anIsInserted = true;
68       break;
69     }
70   }
71   
72   if ( !anIsInserted )
73   {
74     theProfiles << theProfileName;
75     theProfileParams << theProfilePar;
76   }
77 }
78
79 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
80 : HYDROGUI_Operation( theModule ), 
81   myIsEdit( theIsEdit ),
82   myPreviewPrs( NULL )
83 {
84   setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
85 }
86
87 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
88 {
89   erasePreview();
90 }
91
92 void HYDROGUI_StreamOp::startOperation()
93 {
94   HYDROGUI_Operation::startOperation();
95
96   if ( !myIsEdit || isApplyAndClose() )
97     myEditedObject.Nullify();
98   myHydAxis.clear();
99   myProfiles.clear();
100   myProfileParams.clear();
101
102   // Get panel and reset its state
103   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
104   aPanel->reset();
105
106   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
107   if ( myIsEdit )
108   {
109     if ( isApplyAndClose() )
110       myEditedObject =
111         Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
112     if ( !myEditedObject.IsNull() )
113     {
114       anObjectName = myEditedObject->GetName();
115
116       // Hydraulic axis
117       Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
118       if ( !aHydraulicAxis.IsNull() )
119       {
120         myHydAxis = aHydraulicAxis->GetName();
121
122         TopoDS_Face aPlane;
123         if ( HYDROData_Stream::BuildFace( aHydraulicAxis, aPlane ) )
124         {
125           // Stream profiles
126           HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
127           for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
128           {
129             Handle(HYDROData_Profile) aProfile = 
130               Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
131             if ( aProfile.IsNull() )
132               continue;
133
134             QString aProfileName = aProfile->GetName();
135
136             Standard_Real aProfilePar = 0.0;
137             HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
138
139             myProfiles      << aProfileName;
140             myProfileParams << aProfilePar;
141           }
142         }
143       }
144     }
145   }
146
147   // Update the panel
148   // set the edited object name
149   aPanel->setObjectName( anObjectName );
150
151   // set the existing 2D polylines names to the panel
152   aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
153
154   // synchronize the panel state with the edited object state
155   updatePanelData();
156
157   // Create preview
158   createPreview();
159 }
160
161 void HYDROGUI_StreamOp::abortOperation()
162 {
163   erasePreview();
164   HYDROGUI_Operation::abortOperation();
165 }
166
167 void HYDROGUI_StreamOp::commitOperation()
168 {
169   erasePreview();
170   HYDROGUI_Operation::commitOperation();
171 }
172
173 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
174 {
175   HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
176
177   connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
178   connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ), 
179            this, SLOT( onRemoveProfiles( const QStringList& ) ) );
180   connect( aPanel, SIGNAL( AxisChanged( const QString& ) ), 
181            this, SLOT( onAxisChanged( const QString& ) ) );
182
183   return aPanel;
184 }
185
186 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
187                                       QString& theErrorMsg,
188                                       QStringList& theBrowseObjectsEntries )
189 {
190   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
191   if ( !aPanel )
192     return false;
193
194   // Check whether the object name is not empty
195   QString anObjectName = aPanel->getObjectName().simplified();
196   if ( anObjectName.isEmpty() )
197   {
198     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
199     return false;
200   }
201
202   // Check that there are no other objects with the same name in the document
203   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
204   {
205     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
206     if( !anObject.IsNull() )
207     {
208       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
209       return false;
210     }
211   }
212
213   if ( myEditedObject.IsNull() ) // Create new data model object
214     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
215
216   // Check if the axis is set
217   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
218     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
219   if ( aHydAxis.IsNull() )
220   {
221     theErrorMsg = tr( "AXIS_NOT_DEFINED" );
222     return false;
223   }
224
225   // Check if at least 2 profiles is set
226   HYDROData_SequenceOfObjects aRefProfiles;
227   for ( int i = 0; i < myProfiles.length(); ++i )
228   {
229     QString aProfileName = myProfiles.value( i );
230
231     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
232       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
233     if ( !aProfile.IsNull() )
234       aRefProfiles.Append( aProfile );
235   }
236
237   if ( aRefProfiles.Length() < 2 )
238   {
239     theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
240     return false;
241   }
242
243   // Set the object name
244   myEditedObject->SetName( anObjectName );
245
246   myEditedObject->SetHydraulicAxis( aHydAxis );
247   myEditedObject->SetProfiles( aRefProfiles, false );
248
249   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
250     myEditedObject->Update();
251
252   if ( !myIsEdit )
253   {
254     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
255     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
256   }
257
258   // Erase preview
259   erasePreview();
260
261   // Show the object in case of creation mode of the operation
262   if( !myIsEdit ) {
263     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
264     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
265     theBrowseObjectsEntries.append( anEntry );
266   }
267
268   module()->setIsToUpdate( myEditedObject );
269
270   // Set update flags
271   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
272
273   return true;
274 }
275
276 void HYDROGUI_StreamOp::createPreview()
277 {
278   LightApp_Application* anApp = module()->getApp();
279   if ( !getPreviewManager() )
280   {
281     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
282                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
283   }
284
285   OCCViewer_ViewManager* aViewManager = getPreviewManager();
286   if ( aViewManager && !myPreviewPrs )
287   {
288     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
289     {
290       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
291       if ( !aCtx.IsNull() )
292       {
293         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
294
295         QColor aFillingColor = Qt::green;
296         QColor aBorderColor = Qt::transparent;
297         if ( !myEditedObject.IsNull() )
298         {
299           aFillingColor = myEditedObject->GetFillingColor();
300           aBorderColor = myEditedObject->GetBorderColor();
301         }
302
303         myPreviewPrs->setFillingColor( aFillingColor, false, false );
304         myPreviewPrs->setBorderColor( aBorderColor, false, false );
305       }
306     }
307   }
308
309   if ( !aViewManager || !myPreviewPrs )
310     return;
311
312   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
313     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
314
315   HYDROData_SequenceOfObjects aRefProfiles;
316   for ( int i = 0; i < myProfiles.length(); ++i )
317   {
318     QString aProfileName = myProfiles.value( i );
319
320     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
321       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
322     if ( !aProfile.IsNull() )
323       aRefProfiles.Append( aProfile );
324   }
325
326   HYDROData_Stream::PrsDefinition aPrsDef;
327   if ( !HYDROData_Stream::CreatePresentations( aHydAxis, aRefProfiles, aPrsDef ) )
328   {
329     erasePreview();
330     return;
331   }
332
333   myPreviewPrs->setShape( aPrsDef.myPrs2D );
334 }
335
336 void HYDROGUI_StreamOp::erasePreview()
337 {
338   if( myPreviewPrs )
339   {
340     delete myPreviewPrs;
341     myPreviewPrs = 0;
342   }
343 }
344
345 void HYDROGUI_StreamOp::onAddProfiles()
346 {
347   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
348     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
349   if ( aHydAxis.IsNull() )
350     return;
351
352   TopoDS_Face aPlane;
353   if ( !HYDROData_Stream::BuildFace( aHydAxis, aPlane ) )
354     return;
355
356   // Get the current profiles list
357   QStringList aCurrentProfiles = myProfiles;
358     
359   // Get the selected profiles ( in the Object Browser )
360   QStringList anInvalidProfiles;
361   QStringList anExistingProfiles;
362   QStringList aHasNoIntersectionProfiles;
363   QStringList aVerifiedProfiles;
364
365   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
366
367   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
368   {
369     Handle(HYDROData_Profile) aProfile = 
370       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
371     if ( aProfile.IsNull() )
372       continue;
373
374     QString aProfileName = aProfile->GetName();
375     Standard_Real aProfilePar = 0.0;
376
377     // Check the profile, if all is ok - add it to the list
378     if ( !aProfile->IsValid() )
379     {
380       // check whether the profile is valid
381       anInvalidProfiles << aProfileName;
382     }
383     else if ( aCurrentProfiles.contains( aProfileName ) )
384     {
385       // check whether the profile is already added
386       anExistingProfiles << aProfileName;
387     }
388     else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
389     {
390       // check whether the profile has intersection
391       aHasNoIntersectionProfiles << aProfileName;
392     }
393     else
394     {
395       // Insert profile in correct place
396       insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
397       aVerifiedProfiles << aProfileName;
398     }
399   }
400  
401   // Show message box with the ignored profiles
402   if ( !anInvalidProfiles.isEmpty() ||
403        !anExistingProfiles.isEmpty() ||
404        !aHasNoIntersectionProfiles.isEmpty() )
405   {
406     QString aMessage = tr( "IGNORED_PROFILES" );
407     if ( !anInvalidProfiles.isEmpty() )
408     {
409       aMessage.append( "\n\n" );
410       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
411     }
412     if ( !anExistingProfiles.isEmpty() )
413     {
414       aMessage.append( "\n\n" );
415       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
416     }
417     if ( !aHasNoIntersectionProfiles.isEmpty() )
418     {
419       aMessage.append( "\n\n" );
420       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
421     }
422
423     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
424   }
425
426   if ( aVerifiedProfiles.isEmpty() )
427     return;
428
429   // Update the panel
430   updatePanelData();
431
432   // Update preview
433   createPreview();
434 }
435
436 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
437 {
438   QStringList aToRemove = theProfilesToRemove;
439   
440   aToRemove.removeDuplicates();
441   if ( aToRemove.isEmpty() )
442     return;
443
444   bool isRemoved = false;
445
446   // Remove profiles
447   for ( int i = 0; i < aToRemove.length(); ++i )
448   {
449     const QString& aProfileName = aToRemove.value( i );
450
451     int aProfileId = myProfiles.indexOf( aProfileName );
452     if ( aProfileId < 0 )
453       continue;
454
455     myProfiles.removeAt( aProfileId );
456     myProfileParams.removeAt( aProfileId );
457     isRemoved = true;
458   }
459
460   if ( isRemoved )
461   {
462     // Update the panel
463     updatePanelData();
464
465     // Update preview
466     createPreview();
467   }
468 }
469
470 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
471 {
472   // Get axis object   
473   Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
474     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
475
476   // Prepare data for intersection check
477   TopoDS_Face aPlane;
478   if ( !HYDROData_Stream::BuildFace( aNewAxis, aPlane ) )
479   {
480     SUIT_MessageBox::critical( module()->getApp()->desktop(), 
481                               tr( "BAD_SELECTED_POLYLINE_TLT" ),
482                               tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
483     // To restore the old axis
484     updatePanelData();
485     return;
486   }
487
488   QStringList   aNewProfiles;
489   QList<double> aNewProfileParams;
490   QStringList   aHasNoIntersectionProfiles;
491
492   // Get list of profiles which do not intersect the axis
493   for ( int i = 0; i < myProfiles.length(); ++i )
494   {
495     QString aProfileName = myProfiles.value( i );
496
497     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
498       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
499     if ( aProfile.IsNull() )
500       continue;
501       
502     Standard_Real aProfilePar = 0.0;
503     if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
504     {
505       // Insert profile in correct place
506       insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
507     }
508     else
509     {
510       aHasNoIntersectionProfiles << aProfile->GetName();
511     }
512   }
513
514   // If there are profiles which don't intersect the new axis - show confirmation message box
515   bool isConfirmed = true;
516   if ( !aHasNoIntersectionProfiles.isEmpty() )
517   {
518     SUIT_MessageBox::StandardButtons aButtons = 
519       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
520
521     QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
522     isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
523                                              tr( "CONFIRMATION" ),
524                                              tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
525                                              aButtons, 
526                                              SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
527   }
528
529   // Check if the user has confirmed axis change
530   if ( !isConfirmed )
531   {
532     // To restore the old axis
533     updatePanelData();
534   }
535   else
536   {
537     // Update data
538     myHydAxis = theNewAxis;
539     myProfiles = aNewProfiles;
540     myProfileParams = aNewProfileParams;
541
542     // Update the panel
543     updatePanelData();
544
545     // Update preview
546     createPreview();
547   }
548 }
549
550 void HYDROGUI_StreamOp::updatePanelData()
551 {
552   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
553   if ( !aPanel )
554     return;
555
556   aPanel->setAxisName( myHydAxis );
557   aPanel->setProfiles( myProfiles );
558 }