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>
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_Desktop.h>
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
50 #include <TopoDS_Face.hxx>
52 #include <BRepBuilderAPI_MakeFace.hxx>
54 void insertProfileInToOrder( const QString& theProfileName,
55 const double& theProfilePar,
56 QStringList& theProfiles,
57 QList<double>& theProfileParams )
59 bool anIsInserted = false;
60 for ( int k = 0; k < theProfileParams.length(); ++k )
62 const double& aParam = theProfileParams.value( k );
63 if ( theProfilePar < aParam )
65 theProfiles.insert( k, theProfileName );
66 theProfileParams.insert( k, theProfilePar );
74 theProfiles << theProfileName;
75 theProfileParams << theProfilePar;
79 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
80 : HYDROGUI_Operation( theModule ),
81 myIsEdit( theIsEdit ),
84 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
87 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
92 void HYDROGUI_StreamOp::startOperation()
94 HYDROGUI_Operation::startOperation();
96 if ( !myIsEdit || isApplyAndClose() )
97 myEditedObject.Nullify();
100 myProfileParams.clear();
102 // Get panel and reset its state
103 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
106 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
109 if ( isApplyAndClose() )
111 Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
112 if ( !myEditedObject.IsNull() )
114 anObjectName = myEditedObject->GetName();
117 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
118 if ( !aHydraulicAxis.IsNull() )
120 myHydAxis = aHydraulicAxis->GetName();
123 if ( HYDROData_Stream::BuildFace( aHydraulicAxis, aPlane ) )
126 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
127 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
129 Handle(HYDROData_Profile) aProfile =
130 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
131 if ( aProfile.IsNull() )
134 QString aProfileName = aProfile->GetName();
136 Standard_Real aProfilePar = 0.0;
137 HYDROData_Stream::HasIntersection( aHydraulicAxis, aProfile, aPlane, aProfilePar );
139 myProfiles << aProfileName;
140 myProfileParams << aProfilePar;
148 // set the edited object name
149 aPanel->setObjectName( anObjectName );
151 // set the existing 2D polylines names to the panel
152 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
154 // synchronize the panel state with the edited object state
161 void HYDROGUI_StreamOp::abortOperation()
164 HYDROGUI_Operation::abortOperation();
167 void HYDROGUI_StreamOp::commitOperation()
170 HYDROGUI_Operation::commitOperation();
173 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
175 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
177 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
178 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
179 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
180 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
181 this, SLOT( onAxisChanged( const QString& ) ) );
186 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
187 QString& theErrorMsg,
188 QStringList& theBrowseObjectsEntries )
190 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
194 // Check whether the object name is not empty
195 QString anObjectName = aPanel->getObjectName().simplified();
196 if ( anObjectName.isEmpty() )
198 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
202 // Check that there are no other objects with the same name in the document
203 if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
205 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
206 if( !anObject.IsNull() )
208 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
213 if ( myEditedObject.IsNull() ) // Create new data model object
214 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
216 // Check if the axis is set
217 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
218 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
219 if ( aHydAxis.IsNull() )
221 theErrorMsg = tr( "AXIS_NOT_DEFINED" );
225 // Check if at least 2 profiles is set
226 HYDROData_SequenceOfObjects aRefProfiles;
227 for ( int i = 0; i < myProfiles.length(); ++i )
229 QString aProfileName = myProfiles.value( i );
231 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
232 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
233 if ( !aProfile.IsNull() )
234 aRefProfiles.Append( aProfile );
237 if ( aRefProfiles.Length() < 2 )
239 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
243 // Set the object name
244 myEditedObject->SetName( anObjectName );
246 myEditedObject->SetHydraulicAxis( aHydAxis );
247 myEditedObject->SetProfiles( aRefProfiles, false );
249 if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
250 myEditedObject->Update();
254 myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
255 myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
261 // Show the object in case of creation mode of the operation
263 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
264 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
265 theBrowseObjectsEntries.append( anEntry );
268 module()->setIsToUpdate( myEditedObject );
271 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
276 void HYDROGUI_StreamOp::createPreview()
278 LightApp_Application* anApp = module()->getApp();
279 if ( !getPreviewManager() )
281 setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>(
282 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
285 OCCViewer_ViewManager* aViewManager = getPreviewManager();
286 if ( aViewManager && !myPreviewPrs )
288 if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
290 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
291 if ( !aCtx.IsNull() )
293 myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
295 QColor aFillingColor = Qt::green;
296 QColor aBorderColor = Qt::transparent;
297 if ( !myEditedObject.IsNull() )
299 aFillingColor = myEditedObject->GetFillingColor();
300 aBorderColor = myEditedObject->GetBorderColor();
303 myPreviewPrs->setFillingColor( aFillingColor, false, false );
304 myPreviewPrs->setBorderColor( aBorderColor, false, false );
309 if ( !aViewManager || !myPreviewPrs )
312 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
313 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
315 HYDROData_SequenceOfObjects aRefProfiles;
316 for ( int i = 0; i < myProfiles.length(); ++i )
318 QString aProfileName = myProfiles.value( i );
320 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
321 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
322 if ( !aProfile.IsNull() )
323 aRefProfiles.Append( aProfile );
326 HYDROData_Stream::PrsDefinition aPrsDef;
327 if ( !HYDROData_Stream::CreatePresentations( aHydAxis, aRefProfiles, aPrsDef ) )
333 myPreviewPrs->setShape( aPrsDef.myPrs2D );
336 void HYDROGUI_StreamOp::erasePreview()
345 void HYDROGUI_StreamOp::onAddProfiles()
347 Handle(HYDROData_PolylineXY) aHydAxis = Handle(HYDROData_PolylineXY)::DownCast(
348 HYDROGUI_Tool::FindObjectByName( module(), myHydAxis, KIND_POLYLINEXY ) );
349 if ( aHydAxis.IsNull() )
353 if ( !HYDROData_Stream::BuildFace( aHydAxis, aPlane ) )
356 // Get the current profiles list
357 QStringList aCurrentProfiles = myProfiles;
359 // Get the selected profiles ( in the Object Browser )
360 QStringList anInvalidProfiles;
361 QStringList anExistingProfiles;
362 QStringList aHasNoIntersectionProfiles;
363 QStringList aVerifiedProfiles;
365 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
367 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ )
369 Handle(HYDROData_Profile) aProfile =
370 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
371 if ( aProfile.IsNull() )
374 QString aProfileName = aProfile->GetName();
375 Standard_Real aProfilePar = 0.0;
377 // Check the profile, if all is ok - add it to the list
378 if ( !aProfile->IsValid() )
380 // check whether the profile is valid
381 anInvalidProfiles << aProfileName;
383 else if ( aCurrentProfiles.contains( aProfileName ) )
385 // check whether the profile is already added
386 anExistingProfiles << aProfileName;
388 else if ( !HYDROData_Stream::HasIntersection( aHydAxis, aProfile, aPlane, aProfilePar ) )
390 // check whether the profile has intersection
391 aHasNoIntersectionProfiles << aProfileName;
395 // Insert profile in correct place
396 insertProfileInToOrder( aProfileName, aProfilePar, myProfiles, myProfileParams );
397 aVerifiedProfiles << aProfileName;
401 // Show message box with the ignored profiles
402 if ( !anInvalidProfiles.isEmpty() ||
403 !anExistingProfiles.isEmpty() ||
404 !aHasNoIntersectionProfiles.isEmpty() )
406 QString aMessage = tr( "IGNORED_PROFILES" );
407 if ( !anInvalidProfiles.isEmpty() )
409 aMessage.append( "\n\n" );
410 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
412 if ( !anExistingProfiles.isEmpty() )
414 aMessage.append( "\n\n" );
415 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
417 if ( !aHasNoIntersectionProfiles.isEmpty() )
419 aMessage.append( "\n\n" );
420 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
423 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
426 if ( aVerifiedProfiles.isEmpty() )
436 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
438 QStringList aToRemove = theProfilesToRemove;
440 aToRemove.removeDuplicates();
441 if ( aToRemove.isEmpty() )
444 bool isRemoved = false;
447 for ( int i = 0; i < aToRemove.length(); ++i )
449 const QString& aProfileName = aToRemove.value( i );
451 int aProfileId = myProfiles.indexOf( aProfileName );
452 if ( aProfileId < 0 )
455 myProfiles.removeAt( aProfileId );
456 myProfileParams.removeAt( aProfileId );
470 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
473 Handle(HYDROData_PolylineXY) aNewAxis = Handle(HYDROData_PolylineXY)::DownCast(
474 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
476 // Prepare data for intersection check
478 if ( !HYDROData_Stream::BuildFace( aNewAxis, aPlane ) )
480 SUIT_MessageBox::critical( module()->getApp()->desktop(),
481 tr( "BAD_SELECTED_POLYLINE_TLT" ),
482 tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
483 // To restore the old axis
488 QStringList aNewProfiles;
489 QList<double> aNewProfileParams;
490 QStringList aHasNoIntersectionProfiles;
492 // Get list of profiles which do not intersect the axis
493 for ( int i = 0; i < myProfiles.length(); ++i )
495 QString aProfileName = myProfiles.value( i );
497 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
498 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
499 if ( aProfile.IsNull() )
502 Standard_Real aProfilePar = 0.0;
503 if ( HYDROData_Stream::HasIntersection( aNewAxis, aProfile, aPlane, aProfilePar ) )
505 // Insert profile in correct place
506 insertProfileInToOrder( aProfileName, aProfilePar, aNewProfiles, aNewProfileParams );
510 aHasNoIntersectionProfiles << aProfile->GetName();
514 // If there are profiles which don't intersect the new axis - show confirmation message box
515 bool isConfirmed = true;
516 if ( !aHasNoIntersectionProfiles.isEmpty() )
518 SUIT_MessageBox::StandardButtons aButtons =
519 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
521 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
522 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
523 tr( "CONFIRMATION" ),
524 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
526 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
529 // Check if the user has confirmed axis change
532 // To restore the old axis
538 myHydAxis = theNewAxis;
539 myProfiles = aNewProfiles;
540 myProfileParams = aNewProfileParams;
550 void HYDROGUI_StreamOp::updatePanelData()
552 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
556 aPanel->setAxisName( myHydAxis );
557 aPanel->setProfiles( myProfiles );