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