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