Salome HOME
Still an error on ZLayers related to edit CalculationCase The call to createViewManag...
[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& e )
247   {
248     anErrorMsg = e.GetMessageString();
249     aResult = false;
250   }
251   catch ( ... )
252   {
253     aResult = false;
254   }
255   
256   QApplication::restoreOverrideCursor();
257
258   if ( aResult )
259   {
260     module()->update( anUpdateFlags );
261     commitDocOperation();
262     commit();
263     browseObjects( aBrowseObjectsEntries );
264   }
265   else
266   {
267     myEditedObject->Remove();
268     module()->setObjectRemoved( myEditedObject );  
269
270     if ( isToAbortOnApply() )
271       abortDocOperation();
272
273     abort();
274     SUIT_MessageBox::critical( module()->getApp()->desktop(),
275                                tr( "CREATE_STREAM_ERROR" ),
276                                anErrorMsg ); 
277
278   } 
279 }
280 //#include <HYDROData_StreamLinearInterpolation.h>
281 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
282                                       QString& theErrorMsg,
283                                       QStringList& theBrowseObjectsEntries )
284 {
285   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
286   if ( !aPanel )
287     return false;
288
289   // Check whether the object name is not empty
290   QString anObjectName = aPanel->getObjectName().simplified();
291   if ( anObjectName.isEmpty() )
292   {
293     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
294     return false;
295   }
296
297   // Check that there are no other objects with the same name in the document
298   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
299   {
300     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
301     if( !anObject.IsNull() )
302     {
303       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
304       return false;
305     }
306   }
307
308   if ( myEditedObject.IsNull() ) // Create new data model object
309     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
310
311   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
312     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
313  //if ( aHydAxis.IsNull() )
314  //{
315  //  theErrorMsg = tr( "AXIS_NOT_DEFINED" );
316  //  return false;
317  //}
318
319   // Check if at least 2 profiles is set
320   HYDROData_SequenceOfObjects aRefProfiles;
321   for ( int i = 0; i < myProfiles.length(); ++i )
322   {
323     QString aProfileName = myProfiles.value( i );
324
325     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
326       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
327     if ( !aProfile.IsNull() )
328       aRefProfiles.Append( aProfile );
329   }
330
331   if ( aRefProfiles.Length() < 2 )
332   {
333     theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
334     return false;
335   }
336
337   int InterpMethod = aPanel->getMode();
338
339   myEditedObject->SetInterpolationMethod(InterpMethod);
340
341   if (InterpMethod == 0)
342   {
343     //DTM
344     bool ToOrder = !aHydAxis.IsNull();
345     myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
346     myEditedObject->SetName( anObjectName );
347
348     if (!aHydAxis.IsNull())
349       myEditedObject->SetHydraulicAxis( aHydAxis );
350     myEditedObject->SetProfiles( aRefProfiles, false );
351     myEditedObject->SetDDZ( aPanel->getDDZ() );
352     myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
353   }
354   else if (InterpMethod == 1)
355   {
356     //LISM
357     myEditedObject->SetProfiles (aRefProfiles, false) ;
358     myEditedObject->SetName( anObjectName );
359     if (!aHydAxis.IsNull())
360       myEditedObject->SetHydraulicAxis( aHydAxis );
361     Handle(HYDROData_PolylineXY) aLeftBank = Handle(HYDROData_PolylineXY)::DownCast(
362       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getLeftBankName(), KIND_POLYLINEXY ) );
363     Handle(HYDROData_PolylineXY) aRightBank = Handle(HYDROData_PolylineXY)::DownCast(
364       HYDROGUI_Tool::FindObjectByName( module(), aPanel->getRightBankName(), KIND_POLYLINEXY ) );
365     //myEditedObject->Se
366     myEditedObject->SetLeftBank(aLeftBank);
367     myEditedObject->SetRightBank(aRightBank);
368
369     myEditedObject->SetNbProfilePoints( aPanel->getNbProfilePoints() );
370     myEditedObject->SetHaxStep( aPanel->getSpatialStep() );
371   }
372
373   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
374   {
375     myEditedObject->Update();
376     //NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>> warnings;
377     //myEditedObject->GetWarnings(warnings);
378     //QString totalWarning;
379    //for (NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>::Iterator it(warnings); it.More(); it.Next())
380    //{
381    //  const QSet<QString>& V = it.Value();
382    //  if (V.empty())
383    //    continue;
384    //  const Handle(HYDROData_Profile)& K = it.Key();
385    //  QString ProfName = K->GetName();
386    //  foreach (QString str, V)
387    //  {
388    //    totalWarning + "Profile: " + ProfName + ":  " + str + "\n";    
389    //  }
390    //}
391    //if (totalWarning != "")
392    //  SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), totalWarning);
393   }
394
395   if ( !myIsEdit )
396   {
397     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
398     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
399   }
400
401   // Erase preview
402   erasePreview();
403
404   // Show the object in case of creation mode of the operation
405   if( !myIsEdit ) {
406     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
407     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
408     theBrowseObjectsEntries.append( anEntry );
409   }
410
411   module()->setIsToUpdate( myEditedObject );
412
413   // Set update flags
414   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
415
416   return true;
417 }
418
419 void HYDROGUI_StreamOp::createPreview()
420 {
421   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
422   int mode = aPanel->getMode();
423
424   LightApp_Application* anApp = module()->getApp();
425   if ( !getPreviewManager() )
426   {
427     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
428       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
429   }
430
431   OCCViewer_ViewManager* aViewManager = getPreviewManager();
432   if ( aViewManager && !myPreviewPrs )
433   {
434     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
435     {
436       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
437       if ( !aCtx.IsNull() )
438       {
439         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
440
441         QColor aFillingColor = Qt::green;
442         QColor aBorderColor = Qt::transparent;
443         if ( !myEditedObject.IsNull() )
444         {
445           aFillingColor = myEditedObject->GetFillingColor();
446           aBorderColor = myEditedObject->GetBorderColor();
447         }
448
449         myPreviewPrs->setFillingColor( aFillingColor, false, false );
450         myPreviewPrs->setBorderColor( aBorderColor, false, false );
451       }
452     }
453   }
454
455   if ( !aViewManager || !myPreviewPrs )
456     return;
457
458   if (mode == 0)
459   {
460     Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
461       HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
462
463     HYDROData_SequenceOfObjects aRefProfiles;
464     //std::vector<Handle(HYDROData_Profile)> aRefProfiles;
465     int plen = myProfiles.length();
466     for ( int i = 0; i < plen; ++i )
467     {
468       QString aProfileName = myProfiles.value( i );
469
470       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
471         HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
472       if ( !aProfile.IsNull() )
473         aRefProfiles.Append( aProfile );
474     }
475
476     HYDROData_Stream::PrsDefinition aPrsDef;
477
478     TopoDS_Shape Out3dPres;
479     TopoDS_Shape Out2dPres;
480     TopoDS_Shape OutLeftB;
481     TopoDS_Shape OutRightB;
482     TopoDS_Shape OutInlet;
483     TopoDS_Shape OutOutlet;
484
485     double ddz = aPanel->getDDZ();
486     double ss = aPanel->getSpatialStep();
487
488     std::set<int> InvInd;
489
490 #ifdef _DEBUG
491     const int MAX_POINTS_IN_PREVIEW = 50000;
492 #else
493     const int MAX_POINTS_IN_PREVIEW = 500000;
494 #endif
495
496     HYDROData_Bathymetry::AltitudePoints points;
497
498     bool ProjStat = true;
499     NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>> warnings;
500     bool ToEstimateWarnings = false;
501     HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, points, Out3dPres, Out2dPres, OutLeftB, OutRightB,
502       OutInlet, OutOutlet, true, true, InvInd, MAX_POINTS_IN_PREVIEW, ProjStat, warnings, ToEstimateWarnings );
503
504     aPanel->clearAllBackgroundColorsForProfileList();
505     // for (std::set<int>::const_iterator it = InvInd.begin(); it != InvInd.end(); it++)
506     //   aPanel->setBackgroundColorForProfileList(*it, QColor(Qt::yellow));
507
508     aPrsDef.myInlet = OutInlet;
509     aPrsDef.myOutlet = OutOutlet;
510     aPrsDef.myLeftBank = OutLeftB;
511     aPrsDef.myRightBank = OutRightB;
512     if (ProjStat)
513       aPrsDef.myPrs2D = Out2dPres;
514     aPrsDef.myPrs3D = Out3dPres;
515
516     aPanel->clearWarnings();
517     if (!ProjStat)
518       aPanel->addWarning(tr("STREAM_PROJECTION_FAILED"));
519
520     QVector<QString> warningsStr;
521
522     for (NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>::Iterator it(warnings); it.More(); it.Next())
523     {
524       QSet<QString> V = it.Value();
525       if (V.empty())
526         continue;   
527       const Handle(HYDROData_Profile)& K = it.Key();
528       QString ProfName = K->GetName();
529       foreach (QString str, V)
530       {
531         warningsStr.append(ProfName + ":  " + str);
532         //aPanel->addWarning(ProfName + ":  " + str);    
533       }
534
535       aPanel->setBackgroundColorForProfileList(ProfName, QColor(Qt::yellow));
536     }
537
538     std::sort(warningsStr.begin(), warningsStr.end());
539     for (int k=0;k<warningsStr.size();k++)
540       aPanel->addWarning(warningsStr[k]);
541
542     myPreviewPrs->setShape( aPrsDef.myPrs2D );
543   }
544   else //mode == 1
545   {
546     aPanel->clearAllBackgroundColorsForProfileList();
547     aPanel->clearWarnings();
548     int nbProfPoints = aPanel->getNbProfilePoints();
549     double ss = aPanel->getSpatialStep();
550     QString axisName = aPanel->getAxisName();
551     QString leftBankName = aPanel->getLeftBankName();
552     QString rightBankName = aPanel->getRightBankName();
553     QList<Handle(HYDROData_Entity)> listProf = aPanel->getProfiles();
554     HYDROData_SequenceOfObjects seqProf;
555     foreach (Handle(HYDROData_Entity) ent, listProf)
556       seqProf.Append(ent);
557
558     Handle(HYDROData_PolylineXY) axis = Handle(HYDROData_PolylineXY)::DownCast(  HYDROGUI_Tool::FindObjectByName( module(), axisName, KIND_POLYLINEXY ) );
559     Handle(HYDROData_PolylineXY) LB = Handle(HYDROData_PolylineXY)::DownCast(  HYDROGUI_Tool::FindObjectByName( module(), leftBankName, KIND_POLYLINEXY ) );
560     Handle(HYDROData_PolylineXY) RB = Handle(HYDROData_PolylineXY)::DownCast(  HYDROGUI_Tool::FindObjectByName( module(), rightBankName, KIND_POLYLINEXY ) );
561     HYDROData_Bathymetry::AltitudePoints outBathypoints;
562
563     std::vector<std::string> warnings;
564     HYDROData_Stream::PrsDefinition prsDef;
565
566     HYDROData_StreamLinearInterpolation::Perform(seqProf, 500, 1, axis, LB, RB, outBathypoints, true, true, prsDef, &warnings); 
567     //TODO for prs3d??
568     myPreviewPrs->setShape( prsDef.myPrs2D );
569     for (int i=0;i<warnings.size();i++)
570       aPanel->addWarning(warnings[i].c_str());
571   }
572 }
573
574 void HYDROGUI_StreamOp::erasePreview()
575 {
576   if( myPreviewPrs )
577   {
578     delete myPreviewPrs;
579     myPreviewPrs = 0;
580   }
581 }
582
583 void HYDROGUI_StreamOp::onAddProfiles()
584 {
585   //TODO skip if mode == 1??
586   Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
587     HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
588   //if ( aHydAxis.IsNull() )
589    // return;
590
591   TopoDS_Face aPlane;
592   HYDROData_Stream::BuildRefFace( aPlane );
593
594   // Get the current profiles list
595   QStringList aCurrentProfiles = myProfiles;
596     
597   // Get the selected profiles ( in the Object Browser )
598   QStringList anInvalidProfiles;
599   QStringList anExistingProfiles;
600   QStringList aHasNoIntersectionProfiles;
601   QStringList aVerifiedProfiles;
602
603   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
604
605   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
606   {
607     Handle(HYDROData_Profile) aProfile = 
608       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
609     if ( aProfile.IsNull() )
610       continue;
611
612     QString aProfileName = aProfile->GetName();
613     Standard_Real aProfilePar = 0.0;
614
615     // Check the profile, if all is ok - add it to the list
616     if ( !aProfile->IsValid() )
617     {
618       // check whether the profile is valid
619       anInvalidProfiles << aProfileName;
620     }
621     else if ( aCurrentProfiles.contains( aProfileName ) )
622     {
623       // check whether the profile is already added
624       anExistingProfiles << aProfileName;
625     }
626     else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
627     {
628       // check whether the profile has intersection
629       aHasNoIntersectionProfiles << aProfileName;
630     }
631     else
632     {
633       // Insert profile in correct place
634       // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
635       insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
636       aVerifiedProfiles << aProfileName;
637     }
638   }
639  
640   // Show message box with the ignored profiles
641   if ( !anInvalidProfiles.isEmpty() ||
642        !anExistingProfiles.isEmpty() ||
643        !aHasNoIntersectionProfiles.isEmpty() )
644   {
645     QString aMessage = tr( "IGNORED_PROFILES" );
646     if ( !anInvalidProfiles.isEmpty() )
647     {
648       aMessage.append( "\n\n" );
649       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
650     }
651     if ( !anExistingProfiles.isEmpty() )
652     {
653       aMessage.append( "\n\n" );
654       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
655     }
656     if ( !aHasNoIntersectionProfiles.isEmpty() )
657     {
658       aMessage.append( "\n\n" );
659       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
660     }
661
662     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
663   }
664
665   if ( aVerifiedProfiles.isEmpty() )
666     return;
667
668   // Update the panel
669   updatePanelData();
670
671   // Update preview
672   createPreview();
673 }
674
675 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
676 {
677   QStringList aToRemove = theProfilesToRemove;
678   
679   aToRemove.removeDuplicates();
680   if ( aToRemove.isEmpty() )
681     return;
682
683   bool isRemoved = false;
684
685   // Remove profiles
686   for ( int i = 0; i < aToRemove.length(); ++i )
687   {
688     const QString& aProfileName = aToRemove.value( i );
689
690     int aProfileId = myProfiles.indexOf( aProfileName );
691     if ( aProfileId < 0 )
692       continue;
693
694     myProfiles.removeAt( aProfileId );
695     myProfileParams.removeAt( aProfileId );
696     isRemoved = true;
697   }
698
699   if ( isRemoved )
700   {
701     // Update the panel
702     updatePanelData();
703
704     // Update preview
705     createPreview();
706   }
707 }
708
709 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
710 {
711   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
712   if (!aPanel)
713     return;
714   if (aPanel->getMode() == 0)
715     createPreview();
716 }
717
718 void HYDROGUI_StreamOp::onSSValueChanged( double d )
719 {
720   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
721   if (!aPanel)
722     return;
723    createPreview();
724 }
725
726 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
727 {
728   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
729   if (!aPanel)
730     return;
731   // Get axis object   
732   Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
733     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
734
735   // Prepare data for intersection check
736   TopoDS_Face aPlane;
737   HYDROData_Stream::BuildRefFace( aPlane );
738   //{
739   //  SUIT_MessageBox::critical( module()->getApp()->desktop(), 
740   //                            tr( "BAD_SELECTED_POLYLINE_TLT" ),
741   //                            tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
742   //  // To restore the old axis
743   //  updatePanelData();
744   //  return;
745   //}
746
747   QStringList   aNewProfiles;
748   QList<double> aNewProfileParams;
749   QStringList   aHasNoIntersectionProfiles;
750
751   // Get list of profiles which do not intersect the axis
752   for ( int i = 0; i < myProfiles.length(); ++i )
753   {
754     QString aProfileName = myProfiles.value( i );
755
756     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
757       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
758     if ( aProfile.IsNull() )
759       continue;
760       
761     Standard_Real aProfilePar = 0.0;
762     if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
763     {
764       // Insert profile in correct place
765       insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
766     }
767     else
768     {
769       aHasNoIntersectionProfiles << aProfile->GetName();
770     }
771   }
772
773   // If there are profiles which don't intersect the new axis - show confirmation message box
774   bool isConfirmed = true;
775   if ( !aHasNoIntersectionProfiles.isEmpty() )
776   {
777     SUIT_MessageBox::StandardButtons aButtons = 
778       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
779
780     QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
781     isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
782                                              tr( "CONFIRMATION" ),
783                                              tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
784                                              aButtons, 
785                                              SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
786   }
787
788   // Check if the user has confirmed axis change
789   if ( !isConfirmed )
790   {
791     // To restore the old axis
792     updatePanelData();
793   }
794   else
795   {
796     // Update data
797     myHydAxis = theNewAxis;
798     myProfiles = aNewProfiles;
799     myProfileParams = aNewProfileParams;
800
801     // Update the panel
802     updatePanelData();
803
804     // Update preview
805     createPreview();
806   }
807 }
808
809 void HYDROGUI_StreamOp::onLeftBankChanged( const QString& theNewAxis )
810 {
811   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
812   if (!aPanel)
813     return;
814   if (aPanel->getMode() == 1)
815     createPreview();
816 }
817
818 void HYDROGUI_StreamOp::onRightBankChanged( const QString& theNewAxis )
819 {
820   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
821   if (!aPanel)
822     return;
823   if (aPanel->getMode() == 1)
824     createPreview();
825 }
826
827 void HYDROGUI_StreamOp::onProfilePointsChanged( int d )
828 {
829   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
830   if (!aPanel)
831     return;
832   if (aPanel->getMode() == 1)
833     createPreview();
834 }
835
836 void HYDROGUI_StreamOp::onModeChanged( bool mode )
837 {
838   createPreview();
839 }
840
841 void HYDROGUI_StreamOp::updatePanelData()
842 {
843   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
844   if ( !aPanel )
845     return;
846
847   aPanel->setAxisName( myHydAxis );
848   aPanel->setProfiles( myProfiles );
849
850 }