Salome HOME
36a50ca716c99ddb49a88d8960c181cc736544c0
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_StreamOp.h"
24
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"
30
31 #include <HYDROData_Document.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_Profile.h>
34
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
38
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Desktop.h>
41
42 #include <OCCViewer_ViewManager.h>
43 #include <OCCViewer_ViewModel.h>
44 #include <OCCViewer_ViewWindow.h>
45
46 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
47 : HYDROGUI_Operation( theModule ), 
48   myIsEdit( theIsEdit ),
49   myViewManager( NULL ),
50   myPreviewPrs( NULL )
51 {
52   setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
53 }
54
55 HYDROGUI_StreamOp::~HYDROGUI_StreamOp()
56 {
57   erasePreview();
58 }
59
60 void HYDROGUI_StreamOp::startOperation()
61 {
62   HYDROGUI_Operation::startOperation();
63
64   // We start operation in the document
65   startDocOperation();
66
67   // Get panel and reset its state
68   HYDROGUI_StreamDlg* aPanel = (HYDROGUI_StreamDlg*)inputPanel();
69   aPanel->reset();
70
71   // Get/create the edited object
72   if( myIsEdit ) {
73     myEditedObject = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
74   } else {
75     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
76   }
77
78   // Get the edited object name
79   QString anObjectName; 
80   if ( !myIsEdit ) {
81     anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_STREAM_NAME" ) );
82   } else if ( !myEditedObject.IsNull() ) {
83     anObjectName = myEditedObject->GetName();
84   }
85
86   // Update the panel
87   // set the edited object name
88   aPanel->setObjectName( anObjectName );
89   // set the existing 2D polylines names to the panel
90   aPanel->setAxisNames( HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY ) );
91   // synchronize the panel state with the edited object state
92   updatePanelData();
93
94   // Create preview
95   createPreview();
96 }
97
98 void HYDROGUI_StreamOp::abortOperation()
99 {
100   erasePreview();
101   abortDocOperation();
102
103   HYDROGUI_Operation::abortOperation();
104 }
105
106 void HYDROGUI_StreamOp::commitOperation()
107 {
108   erasePreview();
109
110   HYDROGUI_Operation::commitOperation();
111 }
112
113 HYDROGUI_InputPanel* HYDROGUI_StreamOp::createInputPanel() const
114 {
115   HYDROGUI_StreamDlg* aPanel = new HYDROGUI_StreamDlg( module(), getName() );
116
117   connect( aPanel, SIGNAL( AddProfiles() ), this, SLOT( onAddProfiles() ) );
118   connect( aPanel, SIGNAL( RemoveProfiles( const QStringList& ) ), 
119            this, SLOT( onRemoveProfiles( const QStringList& ) ) );
120   connect( aPanel, SIGNAL( AxisChanged( const QString& ) ), 
121            this, SLOT( onAxisChanged( const QString& ) ) );
122
123   return aPanel;
124 }
125
126 bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
127                                       QString& theErrorMsg )
128 {
129   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
130   if ( !aPanel || myEditedObject.IsNull() ) {
131     return false;
132   }
133
134   // Check whether the object name is not empty
135   QString anObjectName = aPanel->getObjectName().simplified();
136   if ( anObjectName.isEmpty() ) {
137     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
138     return false;
139   }
140
141   // Check that there are no other objects with the same name in the document
142   if( myEditedObject->GetName() != anObjectName )
143   {
144     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
145     if( !anObject.IsNull() ) {
146       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
147       return false;
148     }
149   }
150
151   // Check if the axis is set
152   Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
153   if ( aHydraulicAxis.IsNull() ) {
154     theErrorMsg = tr( "AXIS_NOT_DEFINED" );
155     return false;
156   }
157
158   // Check if at least 2 profiles is set
159   HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
160   if ( aProfiles.Length() < 2 ) {
161     theErrorMsg = tr( "PROFILES_NOT_DEFINED" );
162     return false;
163   }
164
165   // Set the object name
166   myEditedObject->SetName( anObjectName );
167
168   // Erase preview
169   erasePreview();
170
171   // Show the object in case of creation mode of the operation
172   if( !myIsEdit ) {
173     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
174   }
175
176   // Set update flags
177   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
178
179   return true;
180 }
181
182 void HYDROGUI_StreamOp::createPreview()
183 {
184   if ( myEditedObject.IsNull() ) {
185     return;
186   }
187
188   LightApp_Application* anApp = module()->getApp();
189   if ( !myViewManager ) {
190     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
191       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
192   }
193
194   if ( myViewManager && !myPreviewPrs ) {
195     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() ) {
196       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
197       if ( !aCtx.IsNull() ) {
198         myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject );
199       }
200     }
201   }
202
203   if ( myPreviewPrs ) {
204     myPreviewPrs->update();  
205   }
206 }
207
208 void HYDROGUI_StreamOp::erasePreview()
209 {
210   if( myPreviewPrs ) {
211     delete myPreviewPrs;
212     myPreviewPrs = 0;
213   }
214 }
215
216 void HYDROGUI_StreamOp::onAddProfiles()
217 {
218   if ( myEditedObject.IsNull() ) {
219     return;
220   }
221
222   // Get the current profiles list
223   HYDROData_SequenceOfObjects aProfiles = myEditedObject->GetProfiles();
224
225   // TODO: to be optimized
226   QStringList aCurrentProfiles;
227   for( int i = 1, n = aProfiles.Length(); i <= n; i++ ) {
228     Handle(HYDROData_Profile) aProfile =
229       Handle(HYDROData_Profile)::DownCast( aProfiles.Value( i ) );
230     if ( !aProfile.IsNull() ) {
231       aCurrentProfiles << aProfile->GetName();
232     }
233   }
234     
235   // Get the selected profiles ( in the Object Browser )
236   QStringList anInvalidProfiles, anExistingProfiles, aHasNoIntersectionProfiles;
237
238   HYDROData_SequenceOfObjects aVerifiedProfiles;
239   HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
240   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ ) {
241     Handle(HYDROData_Profile) aProfile = 
242       Handle(HYDROData_Profile)::DownCast( aSelectedProfiles.Value( i ) );
243     if ( !aProfile.IsNull() ) {
244       QString aProfileName = aProfile->GetName();
245
246       // Check the profile, if all is ok - add it to the list
247       if ( !aProfile->IsValid() ) { // check whether the profile is valid
248         anInvalidProfiles << aProfileName;
249       } else if ( aCurrentProfiles.contains( aProfileName ) ) { // check whether the profile is already added
250         anExistingProfiles << aProfileName;
251       } else if ( !myEditedObject->HasIntersection( aProfile ) ) {  // check whether the profile has intersection
252         aHasNoIntersectionProfiles << aProfileName;
253       } else {
254         aVerifiedProfiles.Append( aProfile );
255       }
256     }
257   }
258  
259   // Show message box with the ignored profiles
260   if ( aVerifiedProfiles.Length() < aSelectedProfiles.Length() ) {
261     QString aMessage = tr( "IGNORED_PROFILES" );
262     if ( !anInvalidProfiles.isEmpty() ) {
263       aMessage.append( "\n\n" );
264       aMessage.append( tr("INVALID_PROFILES").arg( anInvalidProfiles.join( "\n" ) ) );
265     }
266     if ( !anExistingProfiles.isEmpty() ) {
267       aMessage.append( "\n\n" );
268       aMessage.append( tr("EXISTING_PROFILES").arg( anExistingProfiles.join( "\n" ) ) );
269     }
270     if ( !aHasNoIntersectionProfiles.isEmpty() ) {
271       aMessage.append( "\n\n" );
272       aMessage.append( tr("NOT_INTERSECTED_PROFILES").arg( aHasNoIntersectionProfiles.join( "\n" ) ) );
273     }
274
275     SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "WARNING" ), aMessage );
276   }
277
278   // Update the stream object
279   for( int i = 1, n = aVerifiedProfiles.Length(); i <= n; i++ ) {
280     Handle(HYDROData_Profile) aProfile =
281       Handle(HYDROData_Profile)::DownCast( aVerifiedProfiles.Value( i ) );
282     myEditedObject->AddProfile( aProfile );
283   }
284   myEditedObject->Update();
285
286   // Update the panel
287   updatePanelData();
288
289   // Update preview
290   createPreview();
291 }
292
293 void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove )
294 {
295   if ( myEditedObject.IsNull() ) {
296     return;
297   }
298
299   // Remove profiles
300   foreach( const QString& aProfileName, theProfilesToRemove ) {
301     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
302       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
303     myEditedObject->RemoveProfile( aProfile );
304   }
305   myEditedObject->Update();
306
307   // Update the panel
308   updatePanelData();
309
310   // Update preview
311   createPreview();
312 }
313
314 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
315 {
316   if ( myEditedObject.IsNull() ) {
317     return;
318   }
319
320   // Get axis object   
321   Handle(HYDROData_PolylineXY) anAxis = Handle(HYDROData_PolylineXY)::DownCast(
322     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
323
324   // Get list of profiles which do not intersect the axis
325   QStringList aHasNoIntersectionProfiles;
326   HYDROData_SequenceOfObjects aCurrentProfiles = myEditedObject->GetProfiles();
327   for( int anIndex = 1, aLength = aCurrentProfiles.Length(); anIndex <= aLength; anIndex++ ) {
328     Handle(HYDROData_Profile) aProfile =
329       Handle(HYDROData_Profile)::DownCast( aCurrentProfiles.Value( anIndex ) );
330     if ( !aProfile.IsNull() ) {
331       // TODO check intersection
332     }
333   }
334
335   // If there are profiles which don't intersect the new axis - show confirmation message box
336   bool isConfirmed = true;
337   if ( !aHasNoIntersectionProfiles.isEmpty() ) {
338     SUIT_MessageBox::StandardButtons aButtons = 
339       SUIT_MessageBox::Yes | SUIT_MessageBox::No;
340
341     QString aMsg = aHasNoIntersectionProfiles.join( "\n" );
342     isConfirmed = SUIT_MessageBox::question( module()->getApp()->desktop(),
343                                              tr( "CONFIRMATION" ),
344                                              tr( "CONFIRM_AXIS_CHANGE" ).arg( aMsg ),
345                                              aButtons, 
346                                              SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes;
347   }
348
349   // Check if the user has confirmed axis change
350   if ( !isConfirmed ) {
351     // To restore the old axis
352     updatePanelData();
353   } else {
354     // Set axis
355     myEditedObject->SetHydraulicAxis( anAxis );
356     myEditedObject->Update();
357
358     // Update the panel
359     updatePanelData();
360
361     // Update preview
362     createPreview();
363   }
364 }
365
366 void HYDROGUI_StreamOp::updatePanelData()
367 {
368   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
369   if ( !aPanel ) {
370     return;
371   }
372
373   // Hydraulic axis
374   Handle(HYDROData_PolylineXY) aHydraulicAxis = myEditedObject->GetHydraulicAxis();
375   if ( !aHydraulicAxis.IsNull() ) {
376     aPanel->setAxisName( aHydraulicAxis->GetName() );
377   } else {
378     aPanel->setAxisName( "" );
379   }
380
381   // Stream profiles
382   QStringList aProfiles;
383
384   HYDROData_SequenceOfObjects aStreamProfiles = myEditedObject->GetProfiles();
385   for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i ) {
386     Handle(HYDROData_Profile) aProfile = 
387       Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
388     if ( !aProfile.IsNull() ) {
389       QString aProfileName = aProfile->GetName();
390       aProfiles << aProfileName;      
391     }
392   }
393
394   aPanel->setProfiles( aProfiles );
395 }