Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[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 #include <HYDROData_DTM.h>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <LightApp_UpdateFlags.h>
37
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
40 #include <QColor>
41 #include <QApplication>
42
43
44 #include <OCCViewer_ViewManager.h>
45 #include <OCCViewer_ViewModel.h>
46 #include <OCCViewer_ViewWindow.h>
47 #include <gp_Ax1.hxx>
48 #include <gp_Ax2.hxx>
49 #include <gp_Ax3.hxx>
50 #include <gp_Vec.hxx>
51 #include <gp_Pnt.hxx>
52 #include <gp_Pln.hxx>
53 #include <gp.hxx>
54 #include <TopoDS_Face.hxx>
55 #include <TopoDS.hxx>
56 #include <BRepBuilderAPI_MakeFace.hxx>
57 #include <QSet>
58 #include <HYDROData_StreamLinearInterpolation.h>
59
60 void insertProfileInToOrder( const QString& theProfileName,
61                              const double&  theProfilePar,
62                              QStringList&   theProfiles,
63                              QList<double>& theProfileParams )
64 {
65   bool anIsInserted = false;
66   for ( int k = 0; k < theProfileParams.length(); ++k )
67   {
68     const double& aParam = theProfileParams.value( k );
69     if ( theProfilePar < aParam )
70     {
71       theProfiles.insert( k, theProfileName );
72       theProfileParams.insert( k, theProfilePar );
73       anIsInserted = true;
74       break;
75     }
76   }
77   
78   if ( !anIsInserted )
79   {
80     theProfiles << theProfileName;
81     theProfileParams << theProfilePar;
82   }
83 }
84
85 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
86 : HYDROGUI_Operation( theModule ), 
87   myIsEdit( theIsEdit ),
88   myPreviewPrs( NULL )
89 {
90   setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
91 }
92
93 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
94 {
95   erasePreview();
96 }
97
98 void HYDROGUI_StreamOp::startOperation()
99 {
100   HYDROGUI_Operation::startOperation();
101   int mode = 0; //DTM mode by def
102
103   if ( !myIsEdit || isApplyAndClose() )
104     myEditedObject.Nullify();
105   myHydAxis.clear();
106   myProfiles.clear();
107   myProfileParams.clear();
108
109   // Get panel and reset its state
110   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
111   aPanel->reset();
112
113   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
114   if ( myIsEdit )
115   {
116     if ( isApplyAndClose() )
117       myEditedObject =
118         Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
119     if ( !myEditedObject.IsNull() )
120     {          
121       mode = myEditedObject->GetInterpolationMethod();
122       anObjectName = myEditedObject->GetName();
123       //aPanel->setMode(mode);
124
125       // Hydraulic axis
126       Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
127       
128       TopoDS_Face aPlane;
129       HYDROData_Stream::BuildRefFace( aPlane ) ;
130       if ( !aHydraulicAxis.IsNull() )
131         myHydAxis = aHydraulicAxis->GetName();
132
133       // Stream profiles
134       HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
135       for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
136       {
137         Handle(HYDROData_Profile) aProfile = 
138           Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
139         if ( aProfile.IsNull() )
140           continue;
141
142         QString aProfileName = aProfile->GetName();        
143         myProfiles      << aProfileName;
144
145         if (!aHydraulicAxis.IsNull())
146         {
147           Standard_Real aProfilePar = 0.0;
148           HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
149           myProfileParams << aProfilePar;
150         }
151
152       }      
153
154     }
155   }
156
157   // Update the panel
158   // set the edited object name
159   aPanel->setObjectName( anObjectName );
160
161   // set the existing 2D polylines names to the panel
162   aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
163
164   aPanel->setLeftBankNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
165   aPanel->setRightBankNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
166
167   bool isBlocked = aPanel->blockSignals( true );
168   if (myIsEdit)
169   {
170     if (mode == 0)
171     {
172       aPanel->setDDZ( myEditedObject->GetDDZ() );
173     }
174     else
175     {
176       aPanel->setNbProfilePoints( myEditedObject->GetNbProfilePoints() );
177       aPanel->setLeftBankName(myEditedObject->GetLeftBank()->GetName());
178       aPanel->setRightBankName(myEditedObject->GetRightBank()->GetName());
179     }
180     aPanel->setSpatialStep( myEditedObject->GetSpatialStep() );
181   }  
182   
183   aPanel->setMode(mode);
184   aPanel->blockSignals( isBlocked );
185
186   // synchronize the panel state with the edited object state
187   updatePanelData();
188   // Create preview
189   createPreview();
190 }
191
192 void HYDROGUI_StreamOp::abortOperation()
193 {
194   erasePreview();
195   HYDROGUI_Operation::abortOperation();
196 }
197
198 void HYDROGUI_StreamOp::commitOperation()
199 {
200   erasePreview();
201   HYDROGUI_Operation::commitOperation();
202 }
203
204 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
205 {
206   HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
207
208   connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
209   connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ), 
210            this, SLOT( onRemoveProfiles( const QStringList& ) ) );
211   connect( aPanel, SIGNAL( AxisChanged( const QString& ) ), 
212            this, SLOT( onAxisChanged( const QString& ) ) );
213
214   connect( aPanel, SIGNAL( DDZValueChanged( double ) ),  this, SLOT( onDDZValueChanged( double ) ) );
215   connect( aPanel, SIGNAL( SSValueChanged( double ) ),  this, SLOT( onSSValueChanged( double ) ) );
216
217   connect( aPanel, SIGNAL( LeftBankChanged( const QString& ) ), 
218     this, SLOT( onLeftBankChanged( const QString& ) ) );
219
220   connect( aPanel, SIGNAL( RightBankChanged( const QString& ) ), 
221     this, SLOT( onRightBankChanged( const QString& ) ) );
222
223   connect( aPanel, SIGNAL( ProfilePointsChanged( int ) ),  this, SLOT( onProfilePointsChanged( int ) ) );
224
225   connect( aPanel, SIGNAL( ModeChanged( bool ) ), this, SLOT( onModeChanged( bool ) ) );
226
227   return aPanel;
228 }
229
230 void HYDROGUI_StreamOp::apply()
231 {
232   QApplication::setOverrideCursor( Qt::WaitCursor );
233
234   startDocOperation();
235
236   int anUpdateFlags = 0;
237   QString anErrorMsg;
238
239   bool aResult = false;
240   QStringList aBrowseObjectsEntries;
241
242   try
243   {
244     aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
245   }
246   catch ( Standard_Failure )
247   {
248     Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
249     anErrorMsg = aFailure->GetMessageString();
250     aResult = false;
251   }
252   catch ( ... )
253   {
254     aResult = false;
255   }
256   
257   QApplication::restoreOverrideCursor();
258
259   if ( aResult )
260   {
261     module()->update( anUpdateFlags );
262     commitDocOperation();
263     commit();
264     browseObjects( aBrowseObjectsEntries );
265   }
266   else
267   {
268     myEditedObject->Remove();
269     module()->setObjectRemoved( myEditedObject );  
270
271     if ( isToAbortOnApply() )
272       abortDocOperation();
273
274     abort();
275     SUIT_MessageBox::critical( module()->getApp()->desktop(),
276                                tr( "CREATE_STREAM_ERROR" ),
277                                anErrorMsg ); 
278
279   } 
280 }
281 //#include <HYDROData_StreamLinearInterpolation.h>
282 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
283                                       QString& theErrorMsg,
284                                       QStringList& theBrowseObjectsEntries )
285 {
286   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
287   if ( !aPanel )
288     return false;
289
290   // Check whether the object name is not empty
291   QString anObjectName = aPanel->getObjectName().simplified();
292   if ( anObjectName.isEmpty() )
293   {
294     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
295     return false;
296   }
297
298   // Check that there are no other objects with the same name in the document
299   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
300   {
301     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
302     if( !anObject.IsNull() )
303     {
304       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
305       return false;
306     }
307   }
308
309   if ( myEditedObject.IsNull() ) // Create new data model object
310     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
311
312   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
313     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
314  //if ( aHydAxis.IsNull() )
315  //{
316  //  theErrorMsg = tr( "AXIS_NOT_DEFINED" );
317  //  return false;
318  //}
319
320   // Check if at least 2 profiles is set
321   HYDROData_SequenceOfObjects aRefProfiles;
322   for ( int i = 0; i < myProfiles.length(); ++i )
323   {
324     QString aProfileName = myProfiles.value( i );
325
326     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
327       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
328     if ( !aProfile.IsNull() )
329       aRefProfiles.Append( aProfile );
330   }
331
332   if ( aRefProfiles.Length() < 2 )
333   {
334     theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
335     return false;
336   }
337
338   int InterpMethod = aPanel->getMode();
339
340   myEditedObject->SetInterpolationMethod(InterpMethod);
341
342   if (InterpMethod == 0)
343   {
344     //DTM
345     bool ToOrder = !aHydAxis.IsNull();
346     myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
347     myEditedObject->SetName( anObjectName );
348
349     if (!aHydAxis.IsNull())
350       myEditedObject->SetHydraulicAxis( aHydAxis );
351     myEditedObject->SetProfiles( aRefProfiles, false );
352     myEditedObject->SetDDZ( aPanel->getDDZ() );
353     myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
354   }
355   else if (InterpMethod == 1)
356   {
357     //LISM
358     myEditedObject->SetProfiles (aRefProfiles, false) ;
359     myEditedObject->SetName( anObjectName );
360     if (!aHydAxis.IsNull())
361       myEditedObject->SetHydraulicAxis( aHydAxis );
362     Handle(HYDROData_PolylineXY) aLeftBank = Handle(HYDROData_PolylineXY)::DownCast(
363       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getLeftBankName(), KIND_POLYLINEXY ) );
364     Handle(HYDROData_PolylineXY) aRightBank = Handle(HYDROData_PolylineXY)::DownCast(
365       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRightBankName(), KIND_POLYLINEXY ) );
366     //myEditedObject->Se
367     myEditedObject->SetLeftBank(aLeftBank);
368     myEditedObject->SetRightBank(aRightBank);
369
370     myEditedObject->SetNbProfilePoints( aPanel->getNbProfilePoints() );
371     myEditedObject->SetHaxStep( aPanel->getSpatialStep() );
372   }
373
374   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
375   {
376     myEditedObject->Update();
377     //NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>> warnings;
378     //myEditedObject->GetWarnings(warnings);
379     //QString totalWarning;
380    //for (NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>::Iterator it(warnings); it.More(); it.Next())
381    //{
382    //  const QSet<QString>& V = it.Value();
383    //  if (V.empty())
384    //    continue;
385    //  const Handle(HYDROData_Profile)& K = it.Key();
386    //  QString ProfName = K->GetName();
387    //  foreach (QString str, V)
388    //  {
389    //    totalWarning + "Profile: " + ProfName + ":  " + str + "\n";    
390    //  }
391    //}
392    //if (totalWarning != "")
393    //  SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), totalWarning);
394   }
395
396   if ( !myIsEdit )
397   {
398     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
399     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
400   }
401
402   // Erase preview
403   erasePreview();
404
405   // Show the object in case of creation mode of the operation
406   if( !myIsEdit ) {
407     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
408     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
409     theBrowseObjectsEntries.append( anEntry );
410   }
411
412   module()->setIsToUpdate( myEditedObject );
413
414   // Set update flags
415   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
416
417   return true;
418 }
419
420 void HYDROGUI_StreamOp::createPreview()
421 {
422   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
423   int mode = aPanel->getMode();
424
425   LightApp_Application* anApp = module()->getApp();
426   if ( !getPreviewManager() )
427   {
428     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
429       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
430   }
431
432   OCCViewer_ViewManager* aViewManager = getPreviewManager();
433   if ( aViewManager && !myPreviewPrs )
434   {
435     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
436     {
437       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
438       if ( !aCtx.IsNull() )
439       {
440         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
441
442         QColor aFillingColor = Qt::green;
443         QColor aBorderColor = Qt::transparent;
444         if ( !myEditedObject.IsNull() )
445         {
446           aFillingColor = myEditedObject->GetFillingColor();
447           aBorderColor = myEditedObject->GetBorderColor();
448         }
449
450         myPreviewPrs->setFillingColor( aFillingColor, false, false );
451         myPreviewPrs->setBorderColor( aBorderColor, false, false );
452       }
453     }
454   }
455
456   if ( !aViewManager || !myPreviewPrs )
457     return;
458
459   if (mode == 0)
460   {
461     Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
462       HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
463
464     HYDROData_SequenceOfObjects aRefProfiles;
465     //std::vector<Handle(HYDROData_Profile)> aRefProfiles;
466     int plen = myProfiles.length();
467     for ( int i = 0; i < plen; ++i )
468     {
469       QString aProfileName = myProfiles.value( i );
470
471       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
472         HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
473       if ( !aProfile.IsNull() )
474         aRefProfiles.Append( aProfile );
475     }
476
477     HYDROData_Stream::PrsDefinition aPrsDef;
478
479     TopoDS_Shape Out3dPres;
480     TopoDS_Shape Out2dPres;
481     TopoDS_Shape OutLeftB;
482     TopoDS_Shape OutRightB;
483     TopoDS_Shape OutInlet;
484     TopoDS_Shape OutOutlet;
485
486     double ddz = aPanel->getDDZ();
487     double ss = aPanel->getSpatialStep();
488
489     std::set<int> InvInd;
490
491 #ifdef _DEBUG
492     const int MAX_POINTS_IN_PREVIEW = 50000;
493 #else
494     const int MAX_POINTS_IN_PREVIEW = 500000;
495 #endif
496
497     HYDROData_Bathymetry::AltitudePoints points;
498
499     bool ProjStat = true;
500     NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>> warnings;
501     bool ToEstimateWarnings = false;
502     HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, points, Out3dPres, Out2dPres, OutLeftB, OutRightB,
503       OutInlet, OutOutlet, true, true, InvInd, MAX_POINTS_IN_PREVIEW, ProjStat, warnings, ToEstimateWarnings );
504
505     aPanel->clearAllBackgroundColorsForProfileList();
506     // for (std::set<int>::const_iterator it = InvInd.begin(); it != InvInd.end(); it++)
507     //   aPanel->setBackgroundColorForProfileList(*it, QColor(Qt::yellow));
508
509     aPrsDef.myInlet = OutInlet;
510     aPrsDef.myOutlet = OutOutlet;
511     aPrsDef.myLeftBank = OutLeftB;
512     aPrsDef.myRightBank = OutRightB;
513     if (ProjStat)
514       aPrsDef.myPrs2D = Out2dPres;
515     aPrsDef.myPrs3D = Out3dPres;
516
517     aPanel->clearWarnings();
518     if (!ProjStat)
519       aPanel->addWarning(tr("STREAM_PROJECTION_FAILED"));
520
521     for (NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>::Iterator it(warnings); it.More(); it.Next())
522     {
523       const QSet<QString>& V = it.Value();
524       if (V.empty())
525         continue;
526       const Handle(HYDROData_Profile)& K = it.Key();
527       QString ProfName = K->GetName();
528       foreach (QString str, V)
529       {
530         aPanel->addWarning("Profile: " + ProfName + ":  " + str);    
531       }
532
533       aPanel->setBackgroundColorForProfileList(ProfName, QColor(Qt::yellow));
534     }
535
536     myPreviewPrs->setShape( aPrsDef.myPrs2D );
537   }
538   else //mode == 1
539   {
540     aPanel->clearAllBackgroundColorsForProfileList();
541     aPanel->clearWarnings();
542     int nbProfPoints = aPanel->getNbProfilePoints();
543     double ss = aPanel->getSpatialStep();
544     QString axisName = aPanel->getAxisName();
545     QString leftBankName = aPanel->getLeftBankName();
546     QString rightBankName = aPanel->getRightBankName();
547     QList<Handle(HYDROData_Entity)> listProf = aPanel->getProfiles();
548     HYDROData_SequenceOfObjects seqProf;
549     foreach (Handle(HYDROData_Entity) ent, listProf)
550       seqProf.Append(ent);
551
552     Handle(HYDROData_PolylineXY) axis = Handle(HYDROData_PolylineXY)::DownCast(  HYDROGUI_Tool::FindObjectByName( module(), axisName, KIND_POLYLINEXY ) );
553     Handle(HYDROData_PolylineXY) LB = Handle(HYDROData_PolylineXY)::DownCast(  HYDROGUI_Tool::FindObjectByName( module(), leftBankName, KIND_POLYLINEXY ) );
554     Handle(HYDROData_PolylineXY) RB = Handle(HYDROData_PolylineXY)::DownCast(  HYDROGUI_Tool::FindObjectByName( module(), rightBankName, KIND_POLYLINEXY ) );
555     HYDROData_Bathymetry::AltitudePoints outBathypoints;
556
557     std::vector<std::string> warnings;
558     HYDROData_Stream::PrsDefinition prsDef;
559
560     HYDROData_StreamLinearInterpolation::Perform(seqProf, 500, 1, axis, LB, RB, outBathypoints, true, true, prsDef, &warnings); 
561     //TODO for prs3d??
562     myPreviewPrs->setShape( prsDef.myPrs2D );
563     for (int i=0;i<warnings.size();i++)
564       aPanel->addWarning(warnings[i].c_str());
565   }
566 }
567
568 void HYDROGUI_StreamOp::erasePreview()
569 {
570   if( myPreviewPrs )
571   {
572     delete myPreviewPrs;
573     myPreviewPrs = 0;
574   }
575 }
576
577 void HYDROGUI_StreamOp::onAddProfiles()
578 {
579   //TODO skip if mode == 1??
580   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
581     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
582   //if ( aHydAxis.IsNull() )
583    // return;
584
585   TopoDS_Face aPlane;
586   HYDROData_Stream::BuildRefFace( aPlane );
587
588   // Get the current profiles list
589   QStringList aCurrentProfiles = myProfiles;
590     
591   // Get the selected profiles ( in the Object Browser )
592   QStringList anInvalidProfiles;
593   QStringList anExistingProfiles;
594   QStringList aHasNoIntersectionProfiles;
595   QStringList aVerifiedProfiles;
596
597   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
598
599   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
600   {
601     Handle(HYDROData_Profile) aProfile = 
602       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
603     if ( aProfile.IsNull() )
604       continue;
605
606     QString aProfileName = aProfile->GetName();
607     Standard_Real aProfilePar = 0.0;
608
609     // Check the profile, if all is ok - add it to the list
610     if ( !aProfile->IsValid() )
611     {
612       // check whether the profile is valid
613       anInvalidProfiles << aProfileName;
614     }
615     else if ( aCurrentProfiles.contains( aProfileName ) )
616     {
617       // check whether the profile is already added
618       anExistingProfiles << aProfileName;
619     }
620     else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
621     {
622       // check whether the profile has intersection
623       aHasNoIntersectionProfiles << aProfileName;
624     }
625     else
626     {
627       // Insert profile in correct place
628       // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
629       insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
630       aVerifiedProfiles << aProfileName;
631     }
632   }
633  
634   // Show message box with the ignored profiles
635   if ( !anInvalidProfiles.isEmpty() ||
636        !anExistingProfiles.isEmpty() ||
637        !aHasNoIntersectionProfiles.isEmpty() )
638   {
639     QString aMessage = tr( "IGNORED_PROFILES" );
640     if ( !anInvalidProfiles.isEmpty() )
641     {
642       aMessage.append( "\n\n" );
643       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
644     }
645     if ( !anExistingProfiles.isEmpty() )
646     {
647       aMessage.append( "\n\n" );
648       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
649     }
650     if ( !aHasNoIntersectionProfiles.isEmpty() )
651     {
652       aMessage.append( "\n\n" );
653       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
654     }
655
656     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
657   }
658
659   if ( aVerifiedProfiles.isEmpty() )
660     return;
661
662   // Update the panel
663   updatePanelData();
664
665   // Update preview
666   createPreview();
667 }
668
669 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
670 {
671   QStringList aToRemove = theProfilesToRemove;
672   
673   aToRemove.removeDuplicates();
674   if ( aToRemove.isEmpty() )
675     return;
676
677   bool isRemoved = false;
678
679   // Remove profiles
680   for ( int i = 0; i < aToRemove.length(); ++i )
681   {
682     const QString& aProfileName = aToRemove.value( i );
683
684     int aProfileId = myProfiles.indexOf( aProfileName );
685     if ( aProfileId < 0 )
686       continue;
687
688     myProfiles.removeAt( aProfileId );
689     myProfileParams.removeAt( aProfileId );
690     isRemoved = true;
691   }
692
693   if ( isRemoved )
694   {
695     // Update the panel
696     updatePanelData();
697
698     // Update preview
699     createPreview();
700   }
701 }
702
703 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
704 {
705   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
706   if (!aPanel)
707     return;
708   if (aPanel->getMode() == 0)
709     createPreview();
710 }
711
712 void HYDROGUI_StreamOp::onSSValueChanged( double d )
713 {
714   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
715   if (!aPanel)
716     return;
717    createPreview();
718 }
719
720 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
721 {
722   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
723   if (!aPanel)
724     return;
725   // Get axis object   
726   Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
727     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
728
729   // Prepare data for intersection check
730   TopoDS_Face aPlane;
731   HYDROData_Stream::BuildRefFace( aPlane );
732   //{
733   //  SUIT_MessageBox::critical( module()->getApp()->desktop(), 
734   //                            tr( "BAD_SELECTED_POLYLINE_TLT" ),
735   //                            tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
736   //  // To restore the old axis
737   //  updatePanelData();
738   //  return;
739   //}
740
741   QStringList   aNewProfiles;
742   QList<double> aNewProfileParams;
743   QStringList   aHasNoIntersectionProfiles;
744
745   // Get list of profiles which do not intersect the axis
746   for ( int i = 0; i < myProfiles.length(); ++i )
747   {
748     QString aProfileName = myProfiles.value( i );
749
750     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
751       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
752     if ( aProfile.IsNull() )
753       continue;
754       
755     Standard_Real aProfilePar = 0.0;
756     if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
757     {
758       // Insert profile in correct place
759       insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
760     }
761     else
762     {
763       aHasNoIntersectionProfiles << aProfile->GetName();
764     }
765   }
766
767   // If there are profiles which don't intersect the new axis - show confirmation message box
768   bool isConfirmed = true;
769   if ( !aHasNoIntersectionProfiles.isEmpty() )
770   {
771     SUIT_MessageBox::StandardButtons aButtons = 
772       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
773
774     QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
775     isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
776                                              tr( "CONFIRMATION" ),
777                                              tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
778                                              aButtons, 
779                                              SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
780   }
781
782   // Check if the user has confirmed axis change
783   if ( !isConfirmed )
784   {
785     // To restore the old axis
786     updatePanelData();
787   }
788   else
789   {
790     // Update data
791     myHydAxis = theNewAxis;
792     myProfiles = aNewProfiles;
793     myProfileParams = aNewProfileParams;
794
795     // Update the panel
796     updatePanelData();
797
798     // Update preview
799     createPreview();
800   }
801 }
802
803 void HYDROGUI_StreamOp::onLeftBankChanged( const QString& theNewAxis )
804 {
805   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
806   if (!aPanel)
807     return;
808   if (aPanel->getMode() == 1)
809     createPreview();
810 }
811
812 void HYDROGUI_StreamOp::onRightBankChanged( const QString& theNewAxis )
813 {
814   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
815   if (!aPanel)
816     return;
817   if (aPanel->getMode() == 1)
818     createPreview();
819 }
820
821 void HYDROGUI_StreamOp::onProfilePointsChanged( int d )
822 {
823   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
824   if (!aPanel)
825     return;
826   if (aPanel->getMode() == 1)
827     createPreview();
828 }
829
830 void HYDROGUI_StreamOp::onModeChanged( bool mode )
831 {
832   createPreview();
833 }
834
835 void HYDROGUI_StreamOp::updatePanelData()
836 {
837   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
838   if ( !aPanel )
839     return;
840
841   aPanel->setAxisName( myHydAxis );
842   aPanel->setProfiles( myProfiles );
843
844 }