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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_StreamOp.h"
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"
29 #include <HYDROData_Document.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Profile.h>
32 #include <HYDROData_DTM.h>
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <LightApp_UpdateFlags.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Desktop.h>
41 #include <QApplication>
44 #include <OCCViewer_ViewManager.h>
45 #include <OCCViewer_ViewModel.h>
46 #include <OCCViewer_ViewWindow.h>
54 #include <TopoDS_Face.hxx>
56 #include <BRepBuilderAPI_MakeFace.hxx>
59 void insertProfileInToOrder( const QString& theProfileName,
60 const double& theProfilePar,
61 QStringList& theProfiles,
62 QList<double>& theProfileParams )
64 bool anIsInserted = false;
65 for ( int k = 0; k < theProfileParams.length(); ++k )
67 const double& aParam = theProfileParams.value( k );
68 if ( theProfilePar < aParam )
70 theProfiles.insert( k, theProfileName );
71 theProfileParams.insert( k, theProfilePar );
79 theProfiles << theProfileName;
80 theProfileParams << theProfilePar;
84 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
85 : HYDROGUI_Operation( theModule ),
86 myIsEdit( theIsEdit ),
89 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
92 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
97 void HYDROGUI_StreamOp::startOperation()
99 HYDROGUI_Operation::startOperation();
101 if ( !myIsEdit || isApplyAndClose() )
102 myEditedObject.Nullify();
105 myProfileParams.clear();
107 // Get panel and reset its state
108 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
111 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
114 if ( isApplyAndClose() )
116 Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
117 if ( !myEditedObject.IsNull() )
119 anObjectName = myEditedObject->GetName();
122 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
125 HYDROData_Stream::BuildRefFace( aPlane ) ;
126 if ( !aHydraulicAxis.IsNull() )
127 myHydAxis = aHydraulicAxis->GetName();
130 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
131 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
133 Handle(HYDROData_Profile) aProfile =
134 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
135 if ( aProfile.IsNull() )
138 QString aProfileName = aProfile->GetName();
139 myProfiles << aProfileName;
141 if (!aHydraulicAxis.IsNull())
143 Standard_Real aProfilePar = 0.0;
144 HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
145 myProfileParams << aProfilePar;
154 // set the edited object name
155 aPanel->setObjectName( anObjectName );
157 // set the existing 2D polylines names to the panel
158 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
162 aPanel->setDDZ( myEditedObject->GetDDZ() );
163 aPanel->setSpatialStep( myEditedObject->GetSpatialStep() );
166 // synchronize the panel state with the edited object state
173 void HYDROGUI_StreamOp::abortOperation()
176 HYDROGUI_Operation::abortOperation();
179 void HYDROGUI_StreamOp::commitOperation()
182 HYDROGUI_Operation::commitOperation();
185 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
187 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
189 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
190 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
191 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
192 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
193 this, SLOT( onAxisChanged( const QString& ) ) );
195 connect( aPanel, SIGNAL( DDZValueChanged( double ) ), this, SLOT( onDDZValueChanged( double ) ) );
196 connect( aPanel, SIGNAL( SSValueChanged( double ) ), this, SLOT( onSSValueChanged( double ) ) );
200 void HYDROGUI_StreamOp::apply()
202 QApplication::setOverrideCursor( Qt::WaitCursor );
206 int anUpdateFlags = 0;
209 bool aResult = false;
210 QStringList aBrowseObjectsEntries;
214 aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
216 catch ( Standard_Failure )
218 Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
219 anErrorMsg = aFailure->GetMessageString();
227 QApplication::restoreOverrideCursor();
231 module()->update( anUpdateFlags );
232 commitDocOperation();
234 browseObjects( aBrowseObjectsEntries );
238 myEditedObject->Remove();
239 module()->setObjectRemoved( myEditedObject );
241 if ( isToAbortOnApply() )
245 SUIT_MessageBox::critical( module()->getApp()->desktop(),
246 tr( "CREATE_STREAM_ERROR" ),
252 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
253 QString& theErrorMsg,
254 QStringList& theBrowseObjectsEntries )
256 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
260 // Check whether the object name is not empty
261 QString anObjectName = aPanel->getObjectName().simplified();
262 if ( anObjectName.isEmpty() )
264 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
268 // Check that there are no other objects with the same name in the document
269 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
271 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
272 if( !anObject.IsNull() )
274 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
279 if ( myEditedObject.IsNull() ) // Create new data model object
280 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
282 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
283 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
284 //if ( aHydAxis.IsNull() )
286 // theErrorMsg = tr( "AXIS_NOT_DEFINED" );
290 // Check if at least 2 profiles is set
291 HYDROData_SequenceOfObjects aRefProfiles;
292 for ( int i = 0; i < myProfiles.length(); ++i )
294 QString aProfileName = myProfiles.value( i );
296 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
297 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
298 if ( !aProfile.IsNull() )
299 aRefProfiles.Append( aProfile );
302 if ( aRefProfiles.Length() < 2 )
304 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
310 if (aHydAxis.IsNull())
313 myEditedObject->SetProfiles (aRefProfiles, ToOrder) ;
314 myEditedObject->SetName( anObjectName );
316 if (!aHydAxis.IsNull())
317 myEditedObject->SetHydraulicAxis( aHydAxis );
318 myEditedObject->SetProfiles( aRefProfiles, false );
319 myEditedObject->SetDDZ( aPanel->getDDZ() );
320 myEditedObject->SetSpatialStep( aPanel->getSpatialStep() );
322 if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
324 myEditedObject->Update();
325 NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>> warnings;
326 myEditedObject->GetWarnings(warnings);
327 QString totalWarning;
328 //for (NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>::Iterator it(warnings); it.More(); it.Next())
330 // const QSet<QString>& V = it.Value();
333 // const Handle(HYDROData_Profile)& K = it.Key();
334 // QString ProfName = K->GetName();
335 // foreach (QString str, V)
337 // totalWarning + "Profile: " + ProfName + ": " + str + "\n";
340 //if (totalWarning != "")
341 // SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), totalWarning);
346 myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
347 myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
353 // Show the object in case of creation mode of the operation
355 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
356 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
357 theBrowseObjectsEntries.append( anEntry );
360 module()->setIsToUpdate( myEditedObject );
363 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
368 void HYDROGUI_StreamOp::createPreview()
370 LightApp_Application* anApp = module()->getApp();
371 if ( !getPreviewManager() )
373 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
374 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
377 OCCViewer_ViewManager* aViewManager = getPreviewManager();
378 if ( aViewManager && !myPreviewPrs )
380 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
382 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
383 if ( !aCtx.IsNull() )
385 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
387 QColor aFillingColor = Qt::green;
388 QColor aBorderColor = Qt::transparent;
389 if ( !myEditedObject.IsNull() )
391 aFillingColor = myEditedObject->GetFillingColor();
392 aBorderColor = myEditedObject->GetBorderColor();
395 myPreviewPrs->setFillingColor( aFillingColor, false, false );
396 myPreviewPrs->setBorderColor( aBorderColor, false, false );
401 if ( !aViewManager || !myPreviewPrs )
404 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
405 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
407 HYDROData_SequenceOfObjects aRefProfiles;
408 //std::vector<Handle(HYDROData_Profile)> aRefProfiles;
409 int plen = myProfiles.length();
410 for ( int i = 0; i < plen; ++i )
412 QString aProfileName = myProfiles.value( i );
414 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
415 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
416 if ( !aProfile.IsNull() )
417 aRefProfiles.Append( aProfile );
420 HYDROData_Stream::PrsDefinition aPrsDef;
422 TopoDS_Shape Out3dPres;
423 TopoDS_Shape Out2dPres;
424 TopoDS_Shape OutLeftB;
425 TopoDS_Shape OutRightB;
426 TopoDS_Shape OutInlet;
427 TopoDS_Shape OutOutlet;
429 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
430 double ddz = aPanel->getDDZ();
431 double ss = aPanel->getSpatialStep();
433 std::set<int> InvInd;
436 const int MAX_POINTS_IN_PREVIEW = 50000;
438 const int MAX_POINTS_IN_PREVIEW = 500000;
441 HYDROData_Bathymetry::AltitudePoints points;
443 bool ProjStat = true;
444 NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>> warnings;
445 bool ToEstimateWarnings = false;
446 HYDROData_DTM::CreateProfilesFromDTM( aRefProfiles, ddz, ss, points, Out3dPres, Out2dPres, OutLeftB, OutRightB,
447 OutInlet, OutOutlet, true, true, InvInd, MAX_POINTS_IN_PREVIEW, ProjStat, warnings, ToEstimateWarnings );
449 aPanel->clearAllBackgroundColorsForProfileList();
450 // for (std::set<int>::const_iterator it = InvInd.begin(); it != InvInd.end(); it++)
451 // aPanel->setBackgroundColorForProfileList(*it, QColor(Qt::yellow));
453 aPrsDef.myInlet = OutInlet;
454 aPrsDef.myOutlet = OutOutlet;
455 aPrsDef.myLeftBank = OutLeftB;
456 aPrsDef.myRightBank = OutRightB;
458 aPrsDef.myPrs2D = Out2dPres;
459 aPrsDef.myPrs3D = Out3dPres;
461 aPanel->clearWarnings();
463 aPanel->addWarning(tr("STREAM_PROJECTION_FAILED"));
465 for (NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>::Iterator it(warnings); it.More(); it.Next())
467 const QSet<QString>& V = it.Value();
470 const Handle(HYDROData_Profile)& K = it.Key();
471 QString ProfName = K->GetName();
472 foreach (QString str, V)
474 aPanel->addWarning("Profile: " + ProfName + ": " + str);
477 aPanel->setBackgroundColorForProfileList(ProfName, QColor(Qt::yellow));
480 myPreviewPrs->setShape( aPrsDef.myPrs2D );
483 void HYDROGUI_StreamOp::erasePreview()
492 void HYDROGUI_StreamOp::onAddProfiles()
494 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
495 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
496 //if ( aHydAxis.IsNull() )
500 HYDROData_Stream::BuildRefFace( aPlane );
502 // Get the current profiles list
503 QStringList aCurrentProfiles = myProfiles;
505 // Get the selected profiles ( in the Object Browser )
506 QStringList anInvalidProfiles;
507 QStringList anExistingProfiles;
508 QStringList aHasNoIntersectionProfiles;
509 QStringList aVerifiedProfiles;
511 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
513 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
515 Handle(HYDROData_Profile) aProfile =
516 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
517 if ( aProfile.IsNull() )
520 QString aProfileName = aProfile->GetName();
521 Standard_Real aProfilePar = 0.0;
523 // Check the profile, if all is ok - add it to the list
524 if ( !aProfile->IsValid() )
526 // check whether the profile is valid
527 anInvalidProfiles << aProfileName;
529 else if ( aCurrentProfiles.contains( aProfileName ) )
531 // check whether the profile is already added
532 anExistingProfiles << aProfileName;
534 else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
536 // check whether the profile has intersection
537 aHasNoIntersectionProfiles << aProfileName;
541 // Insert profile in correct place
542 // if hidr axis is null => the params (myProfileParams) will be igrored. So ordering will be the same as in the aSelectedProfiles
543 insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
544 aVerifiedProfiles << aProfileName;
548 // Show message box with the ignored profiles
549 if ( !anInvalidProfiles.isEmpty() ||
550 !anExistingProfiles.isEmpty() ||
551 !aHasNoIntersectionProfiles.isEmpty() )
553 QString aMessage = tr( "IGNORED_PROFILES" );
554 if ( !anInvalidProfiles.isEmpty() )
556 aMessage.append( "\n\n" );
557 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
559 if ( !anExistingProfiles.isEmpty() )
561 aMessage.append( "\n\n" );
562 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
564 if ( !aHasNoIntersectionProfiles.isEmpty() )
566 aMessage.append( "\n\n" );
567 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
570 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
573 if ( aVerifiedProfiles.isEmpty() )
583 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
585 QStringList aToRemove = theProfilesToRemove;
587 aToRemove.removeDuplicates();
588 if ( aToRemove.isEmpty() )
591 bool isRemoved = false;
594 for ( int i = 0; i < aToRemove.length(); ++i )
596 const QString& aProfileName = aToRemove.value( i );
598 int aProfileId = myProfiles.indexOf( aProfileName );
599 if ( aProfileId < 0 )
602 myProfiles.removeAt( aProfileId );
603 myProfileParams.removeAt( aProfileId );
617 void HYDROGUI_StreamOp::onDDZValueChanged( double d )
622 void HYDROGUI_StreamOp::onSSValueChanged( double d )
627 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
630 Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
631 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
633 // Prepare data for intersection check
635 HYDROData_Stream::BuildRefFace( aPlane );
637 // SUIT_MessageBox::critical( module()->getApp()->desktop(),
638 // tr( "BAD_SELECTED_POLYLINE_TLT" ),
639 // tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
640 // // To restore the old axis
641 // updatePanelData();
645 QStringList aNewProfiles;
646 QList<double> aNewProfileParams;
647 QStringList aHasNoIntersectionProfiles;
649 // Get list of profiles which do not intersect the axis
650 for ( int i = 0; i < myProfiles.length(); ++i )
652 QString aProfileName = myProfiles.value( i );
654 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
655 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
656 if ( aProfile.IsNull() )
659 Standard_Real aProfilePar = 0.0;
660 if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
662 // Insert profile in correct place
663 insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
667 aHasNoIntersectionProfiles << aProfile->GetName();
671 // If there are profiles which don't intersect the new axis - show confirmation message box
672 bool isConfirmed = true;
673 if ( !aHasNoIntersectionProfiles.isEmpty() )
675 SUIT_MessageBox::StandardButtons aButtons =
676 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
678 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
679 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
680 tr( "CONFIRMATION" ),
681 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
683 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
686 // Check if the user has confirmed axis change
689 // To restore the old axis
695 myHydAxis = theNewAxis;
696 myProfiles = aNewProfiles;
697 myProfileParams = aNewProfileParams;
707 void HYDROGUI_StreamOp::updatePanelData()
709 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
713 aPanel->setAxisName( myHydAxis );
714 aPanel->setProfiles( myProfiles );