Salome HOME
Style changes.
[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
56 void insertProfileInToOrder( const QString& theProfileName,
57                              const double&  theProfilePar,
58                              QStringList&   theProfiles,
59                              QList<double>& theProfileParams )
60 {
61   bool anIsInserted = false;
62   for ( int k = 0; k < theProfileParams.length(); ++k )
63   {
64     const double& aParam = theProfileParams.value( k );
65     if ( theProfilePar < aParam )
66     {
67       theProfiles.insert( k, theProfileName );
68       theProfileParams.insert( k, theProfilePar );
69       anIsInserted = true;
70       break;
71     }
72   }
73   
74   if ( !anIsInserted )
75   {
76     theProfiles << theProfileName;
77     theProfileParams << theProfilePar;
78   }
79 }
80
81 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
82 : HYDROGUI_Operation( theModule ), 
83   myIsEdit( theIsEdit ),
84   myPreviewPrs( NULL )
85 {
86   setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
87 }
88
89 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
90 {
91   erasePreview();
92 }
93
94 void HYDROGUI_StreamOp::startOperation()
95 {
96   HYDROGUI_Operation::startOperation();
97
98   myEditedObject.Nullify();
99   myHydAxis.clear();
100   myProfiles.clear();
101   myProfileParams.clear();
102
103   // Get panel and reset its state
104   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
105   aPanel->reset();
106
107   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
108   if ( myIsEdit )
109   {
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 {
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   }
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 }