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_Shape.h"
27 #include "HYDROGUI_StreamDlg.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
31 #include <HYDROData_Document.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_Profile.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Desktop.h>
42 #include <OCCViewer_ViewManager.h>
43 #include <OCCViewer_ViewModel.h>
44 #include <OCCViewer_ViewWindow.h>
52 #include <TopoDS_Face.hxx>
54 #include <BRepBuilderAPI_MakeFace.hxx>
55 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
56 : HYDROGUI_Operation( theModule ),
57 myIsEdit( theIsEdit ),
58 myViewManager( NULL ),
61 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
64 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
69 void HYDROGUI_StreamOp::startOperation()
71 HYDROGUI_Operation::startOperation();
73 // We start operation in the document
76 // Get panel and reset its state
77 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
80 // Get/create the edited object
82 myEditedObject = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
83 if ( !myEditedObject.IsNull() && myEditedObject->IsMustBeUpdated() ) {
84 myEditedObject->Update();
87 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
90 // Get the edited object name
93 anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
94 } else if ( !myEditedObject.IsNull() ) {
95 anObjectName = myEditedObject->GetName();
99 // set the edited object name
100 aPanel->setObjectName( anObjectName );
101 // set the existing 2D polylines names to the panel
102 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
103 // synchronize the panel state with the edited object state
110 void HYDROGUI_StreamOp::abortOperation()
115 HYDROGUI_Operation::abortOperation();
118 void HYDROGUI_StreamOp::commitOperation()
122 HYDROGUI_Operation::commitOperation();
125 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
127 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
129 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
130 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
131 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
132 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
133 this, SLOT( onAxisChanged( const QString& ) ) );
138 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
139 QString& theErrorMsg )
141 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
142 if ( !aPanel || myEditedObject.IsNull() ) {
146 // Check whether the object name is not empty
147 QString anObjectName = aPanel->getObjectName().simplified();
148 if ( anObjectName.isEmpty() ) {
149 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
153 // Check that there are no other objects with the same name in the document
154 if( myEditedObject->GetName() != anObjectName )
156 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
157 if( !anObject.IsNull() ) {
158 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
163 // Check if the axis is set
164 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
165 if ( aHydraulicAxis.IsNull() ) {
166 theErrorMsg = tr( "AXIS_NOT_DEFINED" );
170 // Check if at least 2 profiles is set
171 HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
172 if ( aProfiles.Length() < 2 ) {
173 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
177 // Set the object name
178 myEditedObject->SetName( anObjectName );
182 myEditedObject->SetFillingColor( HYDROData_Stream::DefaultFillingColor() );
183 myEditedObject->SetBorderColor( HYDROData_Stream::DefaultBorderColor() );
189 // Show the object in case of creation mode of the operation
191 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
194 module()->setIsToUpdate( myEditedObject );
197 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
202 void HYDROGUI_StreamOp::createPreview()
204 if ( myEditedObject.IsNull() ) {
208 LightApp_Application* anApp = module()->getApp();
209 if ( !myViewManager ) {
210 myViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
211 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
214 if ( myViewManager && !myPreviewPrs ) {
215 if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() ) {
216 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
217 if ( !aCtx.IsNull() ) {
218 myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject );
223 if ( myPreviewPrs ) {
224 myPreviewPrs->update( true, true );
228 void HYDROGUI_StreamOp::erasePreview()
236 void HYDROGUI_StreamOp::onAddProfiles()
238 if ( myEditedObject.IsNull() ) {
242 // Get the current profiles list
243 HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
245 // TODO: to be optimized
246 QStringList aCurrentProfiles;
247 for( int i = 1, n = aProfiles.Length(); i <= n; i++ ) {
248 Handle(HYDROData_Profile) aProfile =
249 Handle(HYDROData_Profile)::DownCast( aProfiles.Value( i ) );
250 if ( !aProfile.IsNull() ) {
251 aCurrentProfiles << aProfile->GetName();
255 // Get the selected profiles ( in the Object Browser )
256 QStringList anInvalidProfiles, anExistingProfiles, aHasNoIntersectionProfiles;
258 HYDROData_SequenceOfObjects aVerifiedProfiles;
259 HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
260 Handle(HYDROData_PolylineXY) aHydAxis = myEditedObject->GetHydraulicAxis();
261 if ( aHydAxis.IsNull() )
264 if(!myEditedObject->BuildFace(aHydAxis, aPlane))
266 Standard_Real aPar(.0);
267 for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ ) {
268 Handle(HYDROData_Profile) aProfile =
269 Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
270 if ( !aProfile.IsNull() ) {
271 QString aProfileName = aProfile->GetName();
273 // Check the profile, if all is ok - add it to the list
274 if ( !aProfile->IsValid() ) { // check whether the profile is valid
275 anInvalidProfiles << aProfileName;
276 } else if ( aCurrentProfiles.contains( aProfileName ) ) { // check whether the profile is already added
277 anExistingProfiles << aProfileName;
278 } else if ( !myEditedObject->HasIntersection( aProfile, aPlane, aPar ) ) { // check whether the profile has intersection
279 aHasNoIntersectionProfiles << aProfileName;
281 aVerifiedProfiles.Append( aProfile );
286 // Show message box with the ignored profiles
287 if ( !anInvalidProfiles.isEmpty() || !anExistingProfiles.isEmpty() ||
288 !aHasNoIntersectionProfiles.isEmpty() ) {
289 QString aMessage = tr( "IGNORED_PROFILES" );
290 if ( !anInvalidProfiles.isEmpty() ) {
291 aMessage.append( "\n\n" );
292 aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
294 if ( !anExistingProfiles.isEmpty() ) {
295 aMessage.append( "\n\n" );
296 aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
298 if ( !aHasNoIntersectionProfiles.isEmpty() ) {
299 aMessage.append( "\n\n" );
300 aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
303 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
306 // Update the stream object
307 for( int i = 1, n = aVerifiedProfiles.Length(); i <= n; i++ ) {
308 Handle(HYDROData_Profile) aProfile =
309 Handle(HYDROData_Profile)::DownCast( aVerifiedProfiles.Value( i ) );
310 myEditedObject->AddProfile( aProfile );
312 myEditedObject->UpdatePrs();
321 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
323 if ( myEditedObject.IsNull() ) {
327 bool isRemoved = false;
329 // Take the Object Browser selection into account
330 HYDROData_SequenceOfObjects aSelectedObjects = HYDROGUI_Tool::GetSelectedObjects( module() );
331 for( int i = 1, n = aSelectedObjects.Length(); i <= n; i++ ) {
332 Handle(HYDROData_Profile) aProfile =
333 Handle(HYDROData_Profile)::DownCast( aSelectedObjects.Value( i ) );
334 if ( !aProfile.IsNull() && !theProfilesToRemove.contains(aProfile->GetName()) ) {
335 if ( myEditedObject->RemoveProfile( aProfile ) ) {
342 foreach( const QString& aProfileName, theProfilesToRemove ) {
343 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
344 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
345 if ( myEditedObject->RemoveProfile( aProfile ) ) {
351 // Update the edited stream object
352 myEditedObject->UpdatePrs();
362 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
364 if ( myEditedObject.IsNull() ) {
369 Handle(HYDROData_PolylineXY) anAxis = Handle(HYDROData_PolylineXY)::DownCast(
370 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
372 // Prepare data for intersection check
374 if ( !myEditedObject->BuildFace(anAxis, aPlane) ) {
375 SUIT_MessageBox::critical( module()->getApp()->desktop(),
376 tr( "BAD_SELECTED_POLYLINE_TLT" ),
377 tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
378 // To restore the old axis
383 Standard_Real aPar(.0);
385 // Get list of profiles which do not intersect the axis
386 QStringList aHasNoIntersectionProfiles;
387 HYDROData_SequenceOfObjects aCurrentProfiles = myEditedObject->GetProfiles();
388 for( int anIndex = 1, aLength = aCurrentProfiles.Length(); anIndex <= aLength; anIndex++ ) {
389 Handle(HYDROData_Profile) aProfile =
390 Handle(HYDROData_Profile)::DownCast( aCurrentProfiles.Value( anIndex ) );
391 if ( !aProfile.IsNull() ) {
392 if ( !HYDROData_Stream::HasIntersection( anAxis, aProfile, aPlane, aPar ) ) {
393 aHasNoIntersectionProfiles << aProfile->GetName();
398 // If there are profiles which don't intersect the new axis - show confirmation message box
399 bool isConfirmed = true;
400 if ( !aHasNoIntersectionProfiles.isEmpty() ) {
401 SUIT_MessageBox::StandardButtons aButtons =
402 SUIT_MessageBox::Yes | SUIT_MessageBox::No;
404 QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
405 isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
406 tr( "CONFIRMATION" ),
407 tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
409 SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
412 // Check if the user has confirmed axis change
413 if ( !isConfirmed ) {
414 // To restore the old axis
418 myEditedObject->SetHydraulicAxis( anAxis );
419 myEditedObject->UpdatePrs();
429 void HYDROGUI_StreamOp::updatePanelData()
431 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
432 if ( !aPanel || myEditedObject.IsNull() ) {
437 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
438 if ( !aHydraulicAxis.IsNull() ) {
439 aPanel->setAxisName( aHydraulicAxis->GetName() );
441 aPanel->setAxisName( "" );
445 QStringList aProfiles;
447 HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
448 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i ) {
449 Handle(HYDROData_Profile) aProfile =
450 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
451 if ( !aProfile.IsNull() ) {
452 QString aProfileName = aProfile->GetName();
453 aProfiles << aProfileName;
457 aPanel->setProfiles( aProfiles );