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 myEditedObject.Nullify();
98 myProfileParams.clear();
100 // Get panel and reset its state
101 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
104 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
108 Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
109 if ( !myEditedObject.IsNull() )
111 anObjectName = myEditedObject->GetName();
114 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
115 if ( !aHydraulicAxis.IsNull() )
117 myHydAxis = aHydraulicAxis->GetName();
120 if ( HYDROData_Stream::BuildFace( aHydraulicAxis, aPlane ) )
123 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
124 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
126 Handle(HYDROData_Profile) aProfile =
127 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
128 if ( aProfile.IsNull() )
131 QString aProfileName = aProfile->GetName();
133 Standard_Real aProfilePar = 0.0;
134 HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
136 myProfiles << aProfileName;
137 myProfileParams << aProfilePar;
145 // set the edited object name
146 aPanel->setObjectName( anObjectName );
148 // set the existing 2D polylines names to the panel
149 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
151 // synchronize the panel state with the edited object state
158 void HYDROGUI_StreamOp::abortOperation()
161 HYDROGUI_Operation::abortOperation();
164 void HYDROGUI_StreamOp::commitOperation()
167 HYDROGUI_Operation::commitOperation();
170 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
172 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
174 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
175 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
176 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
177 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
178 this, SLOT( onAxisChanged( const QString& ) ) );
183 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
184 QString& theErrorMsg,
185 QStringList& theBrowseObjectsEntries )
187 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
191 // Check whether the object name is not empty
192 QString anObjectName = aPanel->getObjectName().simplified();
193 if ( anObjectName.isEmpty() )
195 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
199 // Check that there are no other objects with the same name in the document
200 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
202 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
203 if( !anObject.IsNull() )
205 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
210 if ( myEditedObject.IsNull() ) // Create new data model object
211 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
213 // Check if the axis is set
214 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
215 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
216 if ( aHydAxis.IsNull() )
218 theErrorMsg = tr( "AXIS_NOT_DEFINED" );
222 // Check if at least 2 profiles is set
223 HYDROData_SequenceOfObjects aRefProfiles;
224 for ( int i = 0; i < myProfiles.length(); ++i )
226 QString aProfileName = myProfiles.value( i );
228 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
229 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
230 if ( !aProfile.IsNull() )
231 aRefProfiles.Append( aProfile );
234 if ( aRefProfiles.Length() < 2 )
236 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
240 // Set the object name
241 myEditedObject->SetName( anObjectName );
243 myEditedObject->SetHydraulicAxis( aHydAxis );
244 myEditedObject->SetProfiles( aRefProfiles, false );
246 if ( myEditedObject->IsMustBeUpdated() )
247 myEditedObject->Update();
251 myEditedObject->SetFillingColor( HYDROData_Stream::DefaultFillingColor() );
252 myEditedObject->SetBorderColor( HYDROData_Stream::DefaultBorderColor() );
258 // Show the object in case of creation mode of the operation
260 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
261 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
262 theBrowseObjectsEntries.append( anEntry );
265 module()->setIsToUpdate( myEditedObject );
268 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
273 void HYDROGUI_StreamOp::createPreview()
275 LightApp_Application* anApp = module()->getApp();
276 if ( !getPreviewManager() )
278 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
279 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
282 OCCViewer_ViewManager* aViewManager = getPreviewManager();
283 if ( aViewManager && !myPreviewPrs )
285 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
287 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
288 if ( !aCtx.IsNull() )
290 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
292 QColor aFillingColor = HYDROData_Stream::DefaultFillingColor();
293 QColor aBorderColor = HYDROData_Stream::DefaultBorderColor();
294 if ( !myEditedObject.IsNull() )
296 aFillingColor = myEditedObject->GetFillingColor();
297 aBorderColor = myEditedObject->GetBorderColor();
300 myPreviewPrs->setFillingColor( aFillingColor, false, false );
301 myPreviewPrs->setBorderColor( aBorderColor, false, false );
306 if ( !aViewManager || !myPreviewPrs )
309 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
310 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
312 HYDROData_SequenceOfObjects aRefProfiles;
313 for ( int i = 0; i < myProfiles.length(); ++i )
315 QString aProfileName = myProfiles.value( i );
317 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
318 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
319 if ( !aProfile.IsNull() )
320 aRefProfiles.Append( aProfile );
323 HYDROData_Stream::PrsDefinition aPrsDef;
324 if ( !HYDROData_Stream::CreatePresentations( aHydAxis, aRefProfiles, aPrsDef ) )
330 myPreviewPrs->setShape( aPrsDef.myPrs2D );
333 void HYDROGUI_StreamOp::erasePreview()
342 void HYDROGUI_StreamOp::onAddProfiles()
344 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
345 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
346 if ( aHydAxis.IsNull() )
350 if ( !HYDROData_Stream::BuildFace( aHydAxis, aPlane ) )
353 // Get the current profiles list
354 QStringList aCurrentProfiles = myProfiles;
356 // Get the selected profiles ( in the Object Browser )
357 QStringList anInvalidProfiles;
358 QStringList anExistingProfiles;
359 QStringList aHasNoIntersectionProfiles;
360 QStringList aVerifiedProfiles;
362 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
364 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
366 Handle(HYDROData_Profile) aProfile =
367 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
368 if ( aProfile.IsNull() )
371 QString aProfileName = aProfile->GetName();
372 Standard_Real aProfilePar = 0.0;
374 // Check the profile, if all is ok - add it to the list
375 if ( !aProfile->IsValid() )
377 // check whether the profile is valid
378 anInvalidProfiles << aProfileName;
380 else if ( aCurrentProfiles.contains( aProfileName ) )
382 // check whether the profile is already added
383 anExistingProfiles << aProfileName;
385 else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
387 // check whether the profile has intersection
388 aHasNoIntersectionProfiles << aProfileName;
392 // Insert profile in correct place
393 insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
394 aVerifiedProfiles << aProfileName;
398 // Show message box with the ignored profiles
399 if ( !anInvalidProfiles.isEmpty() ||
400 !anExistingProfiles.isEmpty() ||
401 !aHasNoIntersectionProfiles.isEmpty() )
403 QString aMessage = tr( "IGNORED_PROFILES" );
404 if ( !anInvalidProfiles.isEmpty() )
406 aMessage.append( "\n\n" );
407 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
409 if ( !anExistingProfiles.isEmpty() )
411 aMessage.append( "\n\n" );
412 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
414 if ( !aHasNoIntersectionProfiles.isEmpty() )
416 aMessage.append( "\n\n" );
417 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
420 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
423 if ( aVerifiedProfiles.isEmpty() )
433 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
435 QStringList aToRemove = theProfilesToRemove;
437 // Take the Object Browser selection into account
438 HYDROData_SequenceOfObjects aSelectedObjects = HYDROGUI_Tool::GetSelectedObjects( module() );
439 for( int i = 1, n = aSelectedObjects.Length(); i <= n; i++ )
441 Handle(HYDROData_Profile) aProfile =
442 Handle(HYDROData_Profile)::DownCast( aSelectedObjects.Value( i ) );
443 if ( aProfile.IsNull() )
446 QString aProfileName = aProfile->GetName();
447 aToRemove.append( aProfileName );
450 aToRemove.removeDuplicates();
451 if ( aToRemove.isEmpty() )
454 bool isRemoved = false;
457 for ( int i = 0; i < aToRemove.length(); ++i )
459 const QString& aProfileName = aToRemove.value( i );
461 int aProfileId = myProfiles.indexOf( aProfileName );
462 if ( aProfileId < 0 )
465 myProfiles.removeAt( aProfileId );
466 myProfileParams.removeAt( aProfileId );
480 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
483 Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
484 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
486 // Prepare data for intersection check
488 if ( !HYDROData_Stream::BuildFace( aNewAxis, aPlane ) )
490 SUIT_MessageBox::critical( module()->getApp()->desktop(),
491 tr( "BAD_SELECTED_POLYLINE_TLT" ),
492 tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
493 // To restore the old axis
498 QStringList aNewProfiles;
499 QList<double> aNewProfileParams;
500 QStringList aHasNoIntersectionProfiles;
502 // Get list of profiles which do not intersect the axis
503 for ( int i = 0; i < myProfiles.length(); ++i )
505 QString aProfileName = myProfiles.value( i );
507 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
508 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
509 if ( aProfile.IsNull() )
512 Standard_Real aProfilePar = 0.0;
513 if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
515 // Insert profile in correct place
516 insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
520 aHasNoIntersectionProfiles << aProfile->GetName();
524 // If there are profiles which don't intersect the new axis - show confirmation message box
525 bool isConfirmed = true;
526 if ( !aHasNoIntersectionProfiles.isEmpty() )
528 SUIT_MessageBox::StandardButtons aButtons =
529 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
531 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
532 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
533 tr( "CONFIRMATION" ),
534 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
536 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
539 // Check if the user has confirmed axis change
542 // To restore the old axis
548 myHydAxis = theNewAxis;
549 myProfiles = aNewProfiles;
550 myProfileParams = aNewProfileParams;
560 void HYDROGUI_StreamOp::updatePanelData()
562 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
566 aPanel->setAxisName( myHydAxis );
567 aPanel->setProfiles( myProfiles );