1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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 if ( !myIsEdit || isApplyAndClose() )
100 myEditedObject.Nullify();
103 myProfileParams.clear();
105 // Get panel and reset its state
106 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
109 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
112 if ( isApplyAndClose() )
114 Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
115 if ( !myEditedObject.IsNull() )
117 anObjectName = myEditedObject->GetName();
120 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
121 if ( !aHydraulicAxis.IsNull() )
123 myHydAxis = aHydraulicAxis->GetName();
126 if ( HYDROData_Stream::BuildFace( aHydraulicAxis, aPlane ) )
129 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
130 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
132 Handle(HYDROData_Profile) aProfile =
133 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
134 if ( aProfile.IsNull() )
137 QString aProfileName = aProfile->GetName();
139 Standard_Real aProfilePar = 0.0;
140 HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
142 myProfiles << aProfileName;
143 myProfileParams << aProfilePar;
151 // set the edited object name
152 aPanel->setObjectName( anObjectName );
154 // set the existing 2D polylines names to the panel
155 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
157 // synchronize the panel state with the edited object state
164 void HYDROGUI_StreamOp::abortOperation()
167 HYDROGUI_Operation::abortOperation();
170 void HYDROGUI_StreamOp::commitOperation()
173 HYDROGUI_Operation::commitOperation();
176 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
178 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
180 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
181 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
182 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
183 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
184 this, SLOT( onAxisChanged( const QString& ) ) );
189 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
190 QString& theErrorMsg,
191 QStringList& theBrowseObjectsEntries )
193 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
197 // Check whether the object name is not empty
198 QString anObjectName = aPanel->getObjectName().simplified();
199 if ( anObjectName.isEmpty() )
201 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
205 // Check that there are no other objects with the same name in the document
206 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
208 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
209 if( !anObject.IsNull() )
211 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
216 if ( myEditedObject.IsNull() ) // Create new data model object
217 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
219 // Check if the axis is set
220 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
221 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
222 if ( aHydAxis.IsNull() )
224 theErrorMsg = tr( "AXIS_NOT_DEFINED" );
228 // Check if at least 2 profiles is set
229 HYDROData_SequenceOfObjects aRefProfiles;
230 for ( int i = 0; i < myProfiles.length(); ++i )
232 QString aProfileName = myProfiles.value( i );
234 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
235 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
236 if ( !aProfile.IsNull() )
237 aRefProfiles.Append( aProfile );
240 if ( aRefProfiles.Length() < 2 )
242 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
246 // Set the object name
247 myEditedObject->SetName( anObjectName );
249 myEditedObject->SetHydraulicAxis( aHydAxis );
250 myEditedObject->SetProfiles( aRefProfiles, false );
252 if ( myEditedObject->IsMustBeUpdated() )
253 myEditedObject->Update();
257 myEditedObject->SetFillingColor( HYDROData_Stream::DefaultFillingColor() );
258 myEditedObject->SetBorderColor( HYDROData_Stream::DefaultBorderColor() );
264 // Show the object in case of creation mode of the operation
266 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
267 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
268 theBrowseObjectsEntries.append( anEntry );
271 module()->setIsToUpdate( myEditedObject );
274 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
279 void HYDROGUI_StreamOp::createPreview()
281 LightApp_Application* anApp = module()->getApp();
282 if ( !getPreviewManager() )
284 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
285 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
288 OCCViewer_ViewManager* aViewManager = getPreviewManager();
289 if ( aViewManager && !myPreviewPrs )
291 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
293 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
294 if ( !aCtx.IsNull() )
296 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
298 QColor aFillingColor = HYDROData_Stream::DefaultFillingColor();
299 QColor aBorderColor = HYDROData_Stream::DefaultBorderColor();
300 if ( !myEditedObject.IsNull() )
302 aFillingColor = myEditedObject->GetFillingColor();
303 aBorderColor = myEditedObject->GetBorderColor();
306 myPreviewPrs->setFillingColor( aFillingColor, false, false );
307 myPreviewPrs->setBorderColor( aBorderColor, false, false );
312 if ( !aViewManager || !myPreviewPrs )
315 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
316 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
318 HYDROData_SequenceOfObjects aRefProfiles;
319 for ( int i = 0; i < myProfiles.length(); ++i )
321 QString aProfileName = myProfiles.value( i );
323 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
324 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
325 if ( !aProfile.IsNull() )
326 aRefProfiles.Append( aProfile );
329 HYDROData_Stream::PrsDefinition aPrsDef;
330 if ( !HYDROData_Stream::CreatePresentations( aHydAxis, aRefProfiles, aPrsDef ) )
336 myPreviewPrs->setShape( aPrsDef.myPrs2D );
339 void HYDROGUI_StreamOp::erasePreview()
348 void HYDROGUI_StreamOp::onAddProfiles()
350 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
351 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
352 if ( aHydAxis.IsNull() )
356 if ( !HYDROData_Stream::BuildFace( aHydAxis, aPlane ) )
359 // Get the current profiles list
360 QStringList aCurrentProfiles = myProfiles;
362 // Get the selected profiles ( in the Object Browser )
363 QStringList anInvalidProfiles;
364 QStringList anExistingProfiles;
365 QStringList aHasNoIntersectionProfiles;
366 QStringList aVerifiedProfiles;
368 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
370 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
372 Handle(HYDROData_Profile) aProfile =
373 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
374 if ( aProfile.IsNull() )
377 QString aProfileName = aProfile->GetName();
378 Standard_Real aProfilePar = 0.0;
380 // Check the profile, if all is ok - add it to the list
381 if ( !aProfile->IsValid() )
383 // check whether the profile is valid
384 anInvalidProfiles << aProfileName;
386 else if ( aCurrentProfiles.contains( aProfileName ) )
388 // check whether the profile is already added
389 anExistingProfiles << aProfileName;
391 else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
393 // check whether the profile has intersection
394 aHasNoIntersectionProfiles << aProfileName;
398 // Insert profile in correct place
399 insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
400 aVerifiedProfiles << aProfileName;
404 // Show message box with the ignored profiles
405 if ( !anInvalidProfiles.isEmpty() ||
406 !anExistingProfiles.isEmpty() ||
407 !aHasNoIntersectionProfiles.isEmpty() )
409 QString aMessage = tr( "IGNORED_PROFILES" );
410 if ( !anInvalidProfiles.isEmpty() )
412 aMessage.append( "\n\n" );
413 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
415 if ( !anExistingProfiles.isEmpty() )
417 aMessage.append( "\n\n" );
418 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
420 if ( !aHasNoIntersectionProfiles.isEmpty() )
422 aMessage.append( "\n\n" );
423 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
426 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
429 if ( aVerifiedProfiles.isEmpty() )
439 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
441 QStringList aToRemove = theProfilesToRemove;
443 // Take the Object Browser selection into account
444 HYDROData_SequenceOfObjects aSelectedObjects = HYDROGUI_Tool::GetSelectedObjects( module() );
445 for( int i = 1, n = aSelectedObjects.Length(); i <= n; i++ )
447 Handle(HYDROData_Profile) aProfile =
448 Handle(HYDROData_Profile)::DownCast( aSelectedObjects.Value( i ) );
449 if ( aProfile.IsNull() )
452 QString aProfileName = aProfile->GetName();
453 aToRemove.append( aProfileName );
456 aToRemove.removeDuplicates();
457 if ( aToRemove.isEmpty() )
460 bool isRemoved = false;
463 for ( int i = 0; i < aToRemove.length(); ++i )
465 const QString& aProfileName = aToRemove.value( i );
467 int aProfileId = myProfiles.indexOf( aProfileName );
468 if ( aProfileId < 0 )
471 myProfiles.removeAt( aProfileId );
472 myProfileParams.removeAt( aProfileId );
486 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
489 Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
490 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
492 // Prepare data for intersection check
494 if ( !HYDROData_Stream::BuildFace( aNewAxis, aPlane ) )
496 SUIT_MessageBox::critical( module()->getApp()->desktop(),
497 tr( "BAD_SELECTED_POLYLINE_TLT" ),
498 tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
499 // To restore the old axis
504 QStringList aNewProfiles;
505 QList<double> aNewProfileParams;
506 QStringList aHasNoIntersectionProfiles;
508 // Get list of profiles which do not intersect the axis
509 for ( int i = 0; i < myProfiles.length(); ++i )
511 QString aProfileName = myProfiles.value( i );
513 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
514 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
515 if ( aProfile.IsNull() )
518 Standard_Real aProfilePar = 0.0;
519 if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
521 // Insert profile in correct place
522 insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
526 aHasNoIntersectionProfiles << aProfile->GetName();
530 // If there are profiles which don't intersect the new axis - show confirmation message box
531 bool isConfirmed = true;
532 if ( !aHasNoIntersectionProfiles.isEmpty() )
534 SUIT_MessageBox::StandardButtons aButtons =
535 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
537 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
538 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
539 tr( "CONFIRMATION" ),
540 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
542 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
545 // Check if the user has confirmed axis change
548 // To restore the old axis
554 myHydAxis = theNewAxis;
555 myProfiles = aNewProfiles;
556 myProfileParams = aNewProfileParams;
566 void HYDROGUI_StreamOp::updatePanelData()
568 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
572 aPanel->setAxisName( myHydAxis );
573 aPanel->setProfiles( myProfiles );