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_UpdateFlags.h"
28 #include <HYDROData_Document.h>
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Profile.h>
32 #include <LightApp_Application.h>
33 #include <LightApp_SelectionMgr.h>
34 #include <LightApp_UpdateFlags.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_Desktop.h>
39 #include <OCCViewer_ViewManager.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewWindow.h>
49 #include <TopoDS_Face.hxx>
51 #include <BRepBuilderAPI_MakeFace.hxx>
53 void insertProfileInToOrder( const QString& theProfileName,
54 const double& theProfilePar,
55 QStringList& theProfiles,
56 QList<double>& theProfileParams )
58 bool anIsInserted = false;
59 for ( int k = 0; k < theProfileParams.length(); ++k )
61 const double& aParam = theProfileParams.value( k );
62 if ( theProfilePar < aParam )
64 theProfiles.insert( k, theProfileName );
65 theProfileParams.insert( k, theProfilePar );
73 theProfiles << theProfileName;
74 theProfileParams << theProfilePar;
78 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
79 : HYDROGUI_Operation( theModule ),
80 myIsEdit( theIsEdit ),
83 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
86 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
91 void HYDROGUI_StreamOp::startOperation()
93 HYDROGUI_Operation::startOperation();
95 if ( !myIsEdit || isApplyAndClose() )
96 myEditedObject.Nullify();
99 myProfileParams.clear();
101 // Get panel and reset its state
102 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
105 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
108 if ( isApplyAndClose() )
110 Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
111 if ( !myEditedObject.IsNull() )
113 anObjectName = myEditedObject->GetName();
116 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
117 if ( !aHydraulicAxis.IsNull() )
119 myHydAxis = aHydraulicAxis->GetName();
122 if ( HYDROData_Stream::BuildFace( aHydraulicAxis, aPlane ) )
125 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
126 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
128 Handle(HYDROData_Profile) aProfile =
129 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
130 if ( aProfile.IsNull() )
133 QString aProfileName = aProfile->GetName();
135 Standard_Real aProfilePar = 0.0;
136 HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
138 myProfiles << aProfileName;
139 myProfileParams << aProfilePar;
147 // set the edited object name
148 aPanel->setObjectName( anObjectName );
150 // set the existing 2D polylines names to the panel
151 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
153 // synchronize the panel state with the edited object state
160 void HYDROGUI_StreamOp::abortOperation()
163 HYDROGUI_Operation::abortOperation();
166 void HYDROGUI_StreamOp::commitOperation()
169 HYDROGUI_Operation::commitOperation();
172 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
174 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
176 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
177 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
178 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
179 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
180 this, SLOT( onAxisChanged( const QString& ) ) );
185 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
186 QString& theErrorMsg,
187 QStringList& theBrowseObjectsEntries )
189 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
193 // Check whether the object name is not empty
194 QString anObjectName = aPanel->getObjectName().simplified();
195 if ( anObjectName.isEmpty() )
197 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
201 // Check that there are no other objects with the same name in the document
202 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
204 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
205 if( !anObject.IsNull() )
207 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
212 if ( myEditedObject.IsNull() ) // Create new data model object
213 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
215 // Check if the axis is set
216 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
217 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
218 if ( aHydAxis.IsNull() )
220 theErrorMsg = tr( "AXIS_NOT_DEFINED" );
224 // Check if at least 2 profiles is set
225 HYDROData_SequenceOfObjects aRefProfiles;
226 for ( int i = 0; i < myProfiles.length(); ++i )
228 QString aProfileName = myProfiles.value( i );
230 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
231 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
232 if ( !aProfile.IsNull() )
233 aRefProfiles.Append( aProfile );
236 if ( aRefProfiles.Length() < 2 )
238 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
242 // Set the object name
243 myEditedObject->SetName( anObjectName );
245 myEditedObject->SetHydraulicAxis( aHydAxis );
246 myEditedObject->SetProfiles( aRefProfiles, false );
248 if ( myEditedObject->IsMustBeUpdated() )
249 myEditedObject->Update();
253 myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
254 myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
260 // Show the object in case of creation mode of the operation
262 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
263 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
264 theBrowseObjectsEntries.append( anEntry );
267 module()->setIsToUpdate( myEditedObject );
270 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
275 void HYDROGUI_StreamOp::createPreview()
277 LightApp_Application* anApp = module()->getApp();
278 if ( !getPreviewManager() )
280 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
281 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
284 OCCViewer_ViewManager* aViewManager = getPreviewManager();
285 if ( aViewManager && !myPreviewPrs )
287 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
289 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
290 if ( !aCtx.IsNull() )
292 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
294 QColor aFillingColor = myEditedObject->DefaultFillingColor();
295 QColor aBorderColor = myEditedObject->DefaultBorderColor();
296 if ( !myEditedObject.IsNull() )
298 aFillingColor = myEditedObject->GetFillingColor();
299 aBorderColor = myEditedObject->GetBorderColor();
302 myPreviewPrs->setFillingColor( aFillingColor, false, false );
303 myPreviewPrs->setBorderColor( aBorderColor, false, false );
308 if ( !aViewManager || !myPreviewPrs )
311 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
312 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
314 HYDROData_SequenceOfObjects aRefProfiles;
315 for ( int i = 0; i < myProfiles.length(); ++i )
317 QString aProfileName = myProfiles.value( i );
319 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
320 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
321 if ( !aProfile.IsNull() )
322 aRefProfiles.Append( aProfile );
325 HYDROData_Stream::PrsDefinition aPrsDef;
326 if ( !HYDROData_Stream::CreatePresentations( aHydAxis, aRefProfiles, aPrsDef ) )
332 myPreviewPrs->setShape( aPrsDef.myPrs2D );
335 void HYDROGUI_StreamOp::erasePreview()
344 void HYDROGUI_StreamOp::onAddProfiles()
346 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
347 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
348 if ( aHydAxis.IsNull() )
352 if ( !HYDROData_Stream::BuildFace( aHydAxis, aPlane ) )
355 // Get the current profiles list
356 QStringList aCurrentProfiles = myProfiles;
358 // Get the selected profiles ( in the Object Browser )
359 QStringList anInvalidProfiles;
360 QStringList anExistingProfiles;
361 QStringList aHasNoIntersectionProfiles;
362 QStringList aVerifiedProfiles;
364 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
366 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
368 Handle(HYDROData_Profile) aProfile =
369 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
370 if ( aProfile.IsNull() )
373 QString aProfileName = aProfile->GetName();
374 Standard_Real aProfilePar = 0.0;
376 // Check the profile, if all is ok - add it to the list
377 if ( !aProfile->IsValid() )
379 // check whether the profile is valid
380 anInvalidProfiles << aProfileName;
382 else if ( aCurrentProfiles.contains( aProfileName ) )
384 // check whether the profile is already added
385 anExistingProfiles << aProfileName;
387 else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
389 // check whether the profile has intersection
390 aHasNoIntersectionProfiles << aProfileName;
394 // Insert profile in correct place
395 insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
396 aVerifiedProfiles << aProfileName;
400 // Show message box with the ignored profiles
401 if ( !anInvalidProfiles.isEmpty() ||
402 !anExistingProfiles.isEmpty() ||
403 !aHasNoIntersectionProfiles.isEmpty() )
405 QString aMessage = tr( "IGNORED_PROFILES" );
406 if ( !anInvalidProfiles.isEmpty() )
408 aMessage.append( "\n\n" );
409 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
411 if ( !anExistingProfiles.isEmpty() )
413 aMessage.append( "\n\n" );
414 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
416 if ( !aHasNoIntersectionProfiles.isEmpty() )
418 aMessage.append( "\n\n" );
419 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
422 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
425 if ( aVerifiedProfiles.isEmpty() )
435 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
437 QStringList aToRemove = theProfilesToRemove;
439 aToRemove.removeDuplicates();
440 if ( aToRemove.isEmpty() )
443 bool isRemoved = false;
446 for ( int i = 0; i < aToRemove.length(); ++i )
448 const QString& aProfileName = aToRemove.value( i );
450 int aProfileId = myProfiles.indexOf( aProfileName );
451 if ( aProfileId < 0 )
454 myProfiles.removeAt( aProfileId );
455 myProfileParams.removeAt( aProfileId );
469 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
472 Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
473 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
475 // Prepare data for intersection check
477 if ( !HYDROData_Stream::BuildFace( aNewAxis, aPlane ) )
479 SUIT_MessageBox::critical( module()->getApp()->desktop(),
480 tr( "BAD_SELECTED_POLYLINE_TLT" ),
481 tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
482 // To restore the old axis
487 QStringList aNewProfiles;
488 QList<double> aNewProfileParams;
489 QStringList aHasNoIntersectionProfiles;
491 // Get list of profiles which do not intersect the axis
492 for ( int i = 0; i < myProfiles.length(); ++i )
494 QString aProfileName = myProfiles.value( i );
496 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
497 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
498 if ( aProfile.IsNull() )
501 Standard_Real aProfilePar = 0.0;
502 if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
504 // Insert profile in correct place
505 insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
509 aHasNoIntersectionProfiles << aProfile->GetName();
513 // If there are profiles which don't intersect the new axis - show confirmation message box
514 bool isConfirmed = true;
515 if ( !aHasNoIntersectionProfiles.isEmpty() )
517 SUIT_MessageBox::StandardButtons aButtons =
518 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
520 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
521 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
522 tr( "CONFIRMATION" ),
523 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
525 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
528 // Check if the user has confirmed axis change
531 // To restore the old axis
537 myHydAxis = theNewAxis;
538 myProfiles = aNewProfiles;
539 myProfileParams = aNewProfileParams;
549 void HYDROGUI_StreamOp::updatePanelData()
551 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
555 aPanel->setAxisName( myHydAxis );
556 aPanel->setProfiles( myProfiles );