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>
46 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
47 : HYDROGUI_Operation( theModule ),
48 myIsEdit( theIsEdit ),
49 myViewManager( NULL ),
52 setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
55 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
60 void HYDROGUI_StreamOp::startOperation()
62 HYDROGUI_Operation::startOperation();
64 // We start operation in the document
67 HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
70 myEditedObject = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
72 myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
74 QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
75 if ( myIsEdit && !myEditedObject.IsNull() ) {
76 anObjectName = myEditedObject->GetName();
80 aPanel->blockSignals( true );
82 aPanel->setObjectName( anObjectName );
83 aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
84 aPanel->blockSignals( false );
89 onAxisChanged( aPanel->getAxisName() );
96 void HYDROGUI_StreamOp::abortOperation()
101 HYDROGUI_Operation::abortOperation();
104 void HYDROGUI_StreamOp::commitOperation()
108 HYDROGUI_Operation::commitOperation();
111 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
113 HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
114 //TODO connect( aPanel, SIGNAL( CreatePreview() ), this, SLOT( onCreatePreview() ) );
115 connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
116 connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ),
117 this, SLOT( onRemoveProfiles( const QStringList& ) ) );
118 connect( aPanel, SIGNAL( AxisChanged( const QString& ) ),
119 this, SLOT( onAxisChanged( const QString& ) ) );
123 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
124 QString& theErrorMsg )
126 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
130 QString anObjectName = aPanel->getObjectName().simplified();
131 if ( anObjectName.isEmpty() )
133 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
137 if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
139 // check that there are no other objects with the same name in the document
140 Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
141 if( !anObject.IsNull() )
143 theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
148 if ( myEditedObject.IsNull() )
151 // Check if the axis is set
152 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
153 if ( aHydraulicAxis.IsNull() ) {
154 theErrorMsg = tr( "AXIS_NOT_DEFINED" );
158 // Check if the axis is set
159 HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
160 if ( aProfiles.Length() < 2 ) {
161 theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
165 myEditedObject->SetName( anObjectName );
170 module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
172 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
177 void HYDROGUI_StreamOp::onCreatePreview()
179 if ( myEditedObject.IsNull() )
182 LightApp_Application* anApp = module()->getApp();
183 if ( !myViewManager )
184 myViewManager = ::qobject_cast<OCCViewer_ViewManager*>(
185 anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
187 if ( myViewManager && !myPreviewPrs )
189 if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
191 Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
192 if ( !aCtx.IsNull() )
193 myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject );
197 if ( !myViewManager || !myPreviewPrs )
200 myPreviewPrs->update();
203 void HYDROGUI_StreamOp::erasePreview()
212 void HYDROGUI_StreamOp::onAddProfiles()
214 if ( myEditedObject.IsNull() ) {
218 // Get the current profiles list
219 HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
221 // TODO: to be optimized
222 QStringList aCurrentProfiles;
223 for( int anIndex = 1, aLength = aProfiles.Length(); anIndex <= aLength; anIndex++ ) {
224 Handle(HYDROData_Profile) aProfile =
225 Handle(HYDROData_Profile)::DownCast( aProfiles.Value( anIndex ) );
226 if ( !aProfile.IsNull() ) {
227 aCurrentProfiles << aProfile->GetName();
231 // Get the selected profiles ( in the Object Browser )
232 QStringList anInvalidProfiles, anExistingProfiles, aHasNoIntersectionProfiles;
234 HYDROData_SequenceOfObjects aSeqOfProfiles;
235 HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
236 for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
237 Handle(HYDROData_Profile) aProfile =
238 Handle(HYDROData_Profile)::DownCast( aSeq.Value( anIndex ) );
239 if ( !aProfile.IsNull() ) {
240 QString aProfileName = aProfile->GetName();
242 // Check the profile, if all is ok - add it to the list
243 if ( !aProfile->IsValid() ) { // check whether the profile is valid
244 anInvalidProfiles << aProfileName;
245 } else if ( aCurrentProfiles.contains( aProfileName ) ) { // check whether the profile is already added
246 anExistingProfiles << aProfileName;
247 } else if ( !myEditedObject->HasIntersection( aProfile ) ) { // check whether the profile has intersection
248 aHasNoIntersectionProfiles << aProfile->GetName();
250 aSeqOfProfiles.Append( aProfile );
255 // Show message box with the ignored profiles
256 QStringList anIgnoredProfiles;
257 anIgnoredProfiles << anInvalidProfiles << anExistingProfiles << aHasNoIntersectionProfiles;
258 if ( !anIgnoredProfiles.isEmpty() ) {
259 QString aMessage = tr( "IGNORED_PROFILES" ).arg( anIgnoredProfiles.join( "\n" ) );
260 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
263 // Update the stream object
264 for( int anIndex = 1, aLength = aSeqOfProfiles.Length(); anIndex <= aLength; anIndex++ ) {
265 Handle(HYDROData_Profile) aProfile =
266 Handle(HYDROData_Profile)::DownCast( aSeqOfProfiles.Value( anIndex ) );
267 myEditedObject->AddProfile( aProfile );
269 myEditedObject->Update();
278 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
280 if ( myEditedObject.IsNull() ) {
285 foreach( const QString& aProfileName, theProfilesToRemove ) {
286 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
287 HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
288 myEditedObject->RemoveProfile( aProfile );
290 myEditedObject->Update();
299 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
301 if ( myEditedObject.IsNull() ) {
306 Handle(HYDROData_PolylineXY) anAxis = Handle(HYDROData_PolylineXY)::DownCast(
307 HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
309 // Get list of profiles which do not intersect the axis
310 QStringList aHasNoIntersectionProfiles;
311 HYDROData_SequenceOfObjects aCurrentProfiles = myEditedObject->GetProfiles();
312 for( int anIndex = 1, aLength = aCurrentProfiles.Length(); anIndex <= aLength; anIndex++ ) {
313 Handle(HYDROData_Profile) aProfile =
314 Handle(HYDROData_Profile)::DownCast( aCurrentProfiles.Value( anIndex ) );
315 if ( !aProfile.IsNull() ) {
316 // TODO check intersection
320 // Show message box to confirm
321 bool isConfirmed = true;
322 if ( !aHasNoIntersectionProfiles.isEmpty() ) {
323 // TODO show message box
326 // Check if the user has confirmed axis change
327 if ( !isConfirmed ) {
331 myEditedObject->SetHydraulicAxis( anAxis );
332 myEditedObject->Update();
342 void HYDROGUI_StreamOp::updatePanel()
344 HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
350 Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
351 if ( !aHydraulicAxis.IsNull() ) {
352 aPanel->setAxisName( aHydraulicAxis->GetName() );
356 QStringList aSelectedProfiles;
358 HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
359 for ( int i = 1, n = aProfiles.Length(); i <= n; ++i ) {
360 Handle(HYDROData_Profile) aProfile =
361 Handle(HYDROData_Profile)::DownCast( aProfiles.Value( i ) );
362 if ( aProfile.IsNull() ) {
366 QString aProfileName = aProfile->GetName();
367 aSelectedProfiles << aProfileName;
370 aPanel->setSelectedProfiles( aSelectedProfiles );