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