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