1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_StreamOp.h"
25 #include "HYDROGUI_Module.h"
26 #include <HYDROGUI_DataObject.h>
27 #include "HYDROGUI_Shape.h"
28 #include "HYDROGUI_StreamDlg.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
32 #include <HYDROData_Document.h>
33 #include <HYDROData_PolylineXY.h>
34 #include <HYDROData_Profile.h>
36 #include <LightApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <LightApp_UpdateFlags.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_Desktop.h>
43 #include <OCCViewer_ViewManager.h>
44 #include <OCCViewer_ViewModel.h>
45 #include <OCCViewer_ViewWindow.h>
53 #include <TopoDS_Face.hxx>
55 #include <BRepBuilderAPI_MakeFace.hxx>
57 void insertProfileInToOrder( const QString& theProfileName,
58 const double& theProfilePar,
59 QStringList& theProfiles,
60 QList<double>& theProfileParams )
62 bool anIsInserted = false;
63 for ( int k = 0; k < theProfileParams.length(); ++k )
65 const double& aParam = theProfileParams.value( k );
66 if ( theProfilePar < aParam )
68 theProfiles.insert( k, theProfileName );
69 theProfileParams.insert( k, theProfilePar );
77 theProfiles << theProfileName;
78 theProfileParams << theProfilePar;
82 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
83 : HYDROGUI_Operation( theModule ),
84 myIsEdit( theIsEdit ),
87 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
90 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
95 void HYDROGUI_StreamOp::startOperation()
97 HYDROGUI_Operation::startOperation();
99 myEditedObject.Nullify();
102 myProfileParams.clear();
104 // Get panel and reset its state
105 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
108 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
112 Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
113 if ( !myEditedObject.IsNull() )
115 anObjectName = myEditedObject->GetName();
118 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
119 if ( !aHydraulicAxis.IsNull() )
121 myHydAxis = aHydraulicAxis->GetName();
124 if ( HYDROData_Stream::BuildFace( aHydraulicAxis, aPlane ) )
127 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
128 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
130 Handle(HYDROData_Profile) aProfile =
131 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
132 if ( aProfile.IsNull() )
135 QString aProfileName = aProfile->GetName();
137 Standard_Real aProfilePar = 0.0;
138 HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
140 myProfiles << aProfileName;
141 myProfileParams << aProfilePar;
149 // set the edited object name
150 aPanel->setObjectName( anObjectName );
152 // set the existing 2D polylines names to the panel
153 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
155 // synchronize the panel state with the edited object state
162 void HYDROGUI_StreamOp::abortOperation()
165 HYDROGUI_Operation::abortOperation();
168 void HYDROGUI_StreamOp::commitOperation()
171 HYDROGUI_Operation::commitOperation();
174 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
176 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
178 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
179 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
180 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
181 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
182 this, SLOT( onAxisChanged( const QString& ) ) );
187 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
188 QString& theErrorMsg,
189 QStringList& theBrowseObjectsEntries )
191 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
195 // Check whether the object name is not empty
196 QString anObjectName = aPanel->getObjectName().simplified();
197 if ( anObjectName.isEmpty() )
199 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
203 // Check that there are no other objects with the same name in the document
204 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
206 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
207 if( !anObject.IsNull() )
209 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
214 if ( myEditedObject.IsNull() ) // Create new data model object
215 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
217 // Check if the axis is set
218 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
219 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
220 if ( aHydAxis.IsNull() )
222 theErrorMsg = tr( "AXIS_NOT_DEFINED" );
226 // Check if at least 2 profiles is set
227 HYDROData_SequenceOfObjects aRefProfiles;
228 for ( int i = 0; i < myProfiles.length(); ++i )
230 QString aProfileName = myProfiles.value( i );
232 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
233 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
234 if ( !aProfile.IsNull() )
235 aRefProfiles.Append( aProfile );
238 if ( aRefProfiles.Length() < 2 )
240 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
244 // Set the object name
245 myEditedObject->SetName( anObjectName );
247 myEditedObject->SetHydraulicAxis( aHydAxis );
248 myEditedObject->SetProfiles( aRefProfiles, false );
250 if ( myEditedObject->IsMustBeUpdated() )
251 myEditedObject->Update();
255 myEditedObject->SetFillingColor( HYDROData_Stream::DefaultFillingColor() );
256 myEditedObject->SetBorderColor( HYDROData_Stream::DefaultBorderColor() );
262 // Show the object in case of creation mode of the operation
264 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
265 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
266 theBrowseObjectsEntries.append( anEntry );
269 module()->setIsToUpdate( myEditedObject );
272 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
277 void HYDROGUI_StreamOp::createPreview()
279 LightApp_Application* anApp = module()->getApp();
280 if ( !getPreviewManager() )
282 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
283 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
286 OCCViewer_ViewManager* aViewManager = getPreviewManager();
287 if ( aViewManager && !myPreviewPrs )
289 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
291 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
292 if ( !aCtx.IsNull() )
294 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
296 QColor aFillingColor = HYDROData_Stream::DefaultFillingColor();
297 QColor aBorderColor = HYDROData_Stream::DefaultBorderColor();
298 if ( !myEditedObject.IsNull() )
300 aFillingColor = myEditedObject->GetFillingColor();
301 aBorderColor = myEditedObject->GetBorderColor();
304 myPreviewPrs->setFillingColor( aFillingColor, false, false );
305 myPreviewPrs->setBorderColor( aBorderColor, false, false );
310 if ( !aViewManager || !myPreviewPrs )
313 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
314 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
316 HYDROData_SequenceOfObjects aRefProfiles;
317 for ( int i = 0; i < myProfiles.length(); ++i )
319 QString aProfileName = myProfiles.value( i );
321 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
322 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
323 if ( !aProfile.IsNull() )
324 aRefProfiles.Append( aProfile );
327 HYDROData_Stream::PrsDefinition aPrsDef;
328 if ( !HYDROData_Stream::CreatePresentations( aHydAxis, aRefProfiles, aPrsDef ) )
334 myPreviewPrs->setShape( aPrsDef.myPrs2D );
337 void HYDROGUI_StreamOp::erasePreview()
346 void HYDROGUI_StreamOp::onAddProfiles()
348 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
349 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
350 if ( aHydAxis.IsNull() )
354 if ( !HYDROData_Stream::BuildFace( aHydAxis, aPlane ) )
357 // Get the current profiles list
358 QStringList aCurrentProfiles = myProfiles;
360 // Get the selected profiles ( in the Object Browser )
361 QStringList anInvalidProfiles;
362 QStringList anExistingProfiles;
363 QStringList aHasNoIntersectionProfiles;
364 QStringList aVerifiedProfiles;
366 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
368 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
370 Handle(HYDROData_Profile) aProfile =
371 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
372 if ( aProfile.IsNull() )
375 QString aProfileName = aProfile->GetName();
376 Standard_Real aProfilePar = 0.0;
378 // Check the profile, if all is ok - add it to the list
379 if ( !aProfile->IsValid() )
381 // check whether the profile is valid
382 anInvalidProfiles << aProfileName;
384 else if ( aCurrentProfiles.contains( aProfileName ) )
386 // check whether the profile is already added
387 anExistingProfiles << aProfileName;
389 else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
391 // check whether the profile has intersection
392 aHasNoIntersectionProfiles << aProfileName;
396 // Insert profile in correct place
397 insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
398 aVerifiedProfiles << aProfileName;
402 // Show message box with the ignored profiles
403 if ( !anInvalidProfiles.isEmpty() ||
404 !anExistingProfiles.isEmpty() ||
405 !aHasNoIntersectionProfiles.isEmpty() )
407 QString aMessage = tr( "IGNORED_PROFILES" );
408 if ( !anInvalidProfiles.isEmpty() )
410 aMessage.append( "\n\n" );
411 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
413 if ( !anExistingProfiles.isEmpty() )
415 aMessage.append( "\n\n" );
416 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
418 if ( !aHasNoIntersectionProfiles.isEmpty() )
420 aMessage.append( "\n\n" );
421 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
424 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
427 if ( aVerifiedProfiles.isEmpty() )
437 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
439 QStringList aToRemove = theProfilesToRemove;
441 // Take the Object Browser selection into account
442 HYDROData_SequenceOfObjects aSelectedObjects = HYDROGUI_Tool::GetSelectedObjects( module() );
443 for( int i = 1, n = aSelectedObjects.Length(); i <= n; i++ )
445 Handle(HYDROData_Profile) aProfile =
446 Handle(HYDROData_Profile)::DownCast( aSelectedObjects.Value( i ) );
447 if ( aProfile.IsNull() )
450 QString aProfileName = aProfile->GetName();
451 aToRemove.append( aProfileName );
454 aToRemove.removeDuplicates();
455 if ( aToRemove.isEmpty() )
458 bool isRemoved = false;
461 for ( int i = 0; i < aToRemove.length(); ++i )
463 const QString& aProfileName = aToRemove.value( i );
465 int aProfileId = myProfiles.indexOf( aProfileName );
466 if ( aProfileId < 0 )
469 myProfiles.removeAt( aProfileId );
470 myProfileParams.removeAt( aProfileId );
484 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
487 Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
488 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
490 // Prepare data for intersection check
492 if ( !HYDROData_Stream::BuildFace( aNewAxis, aPlane ) )
494 SUIT_MessageBox::critical( module()->getApp()->desktop(),
495 tr( "BAD_SELECTED_POLYLINE_TLT" ),
496 tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
497 // To restore the old axis
502 QStringList aNewProfiles;
503 QList<double> aNewProfileParams;
504 QStringList aHasNoIntersectionProfiles;
506 // Get list of profiles which do not intersect the axis
507 for ( int i = 0; i < myProfiles.length(); ++i )
509 QString aProfileName = myProfiles.value( i );
511 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
512 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
513 if ( aProfile.IsNull() )
516 Standard_Real aProfilePar = 0.0;
517 if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
519 // Insert profile in correct place
520 insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
524 aHasNoIntersectionProfiles << aProfile->GetName();
528 // If there are profiles which don't intersect the new axis - show confirmation message box
529 bool isConfirmed = true;
530 if ( !aHasNoIntersectionProfiles.isEmpty() )
532 SUIT_MessageBox::StandardButtons aButtons =
533 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
535 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
536 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
537 tr( "CONFIRMATION" ),
538 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
540 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
543 // Check if the user has confirmed axis change
546 // To restore the old axis
552 myHydAxis = theNewAxis;
553 myProfiles = aNewProfiles;
554 myProfileParams = aNewProfileParams;
564 void HYDROGUI_StreamOp::updatePanelData()
566 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
570 aPanel->setAxisName( myHydAxis );
571 aPanel->setProfiles( myProfiles );