Salome HOME
dumping-to-py of digue
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ProfileInterpolateOp.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROGUI_ProfileInterpolateOp.h>
20
21 #include "HYDROGUI_Tool.h"
22 #include "HYDROGUI_Tool2.h"
23 #include "HYDROGUI_Module.h"
24 #include "HYDROGUI_DataObject.h"
25 #include "HYDROGUI_UpdateFlags.h"
26 #include "HYDROGUI_ProfileInterpolateDlg.h"
27
28 #include <HYDROData_Stream.h>
29 #include <HYDROData_Profile.h>
30 #include <HYDROData_Document.h>
31 #include <HYDROData_ProfileUZ.h>
32 #include <HYDROData_IPolyline.h>
33 #include <HYDROData_PolylineXY.h>
34 #include <HYDROData_InterpolatorsFactory.h>
35 #include <HYDROData_IProfilesInterpolator.h>
36
37 #include <LightApp_Application.h>
38 #include <LightApp_SelectionMgr.h>
39 #include <LightApp_UpdateFlags.h>
40
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewPort3d.h>
43 #include <OCCViewer_ViewWindow.h>
44 #include <OCCViewer_ViewManager.h>
45 #include <OCCViewer_AISSelector.h>
46
47 #include <AIS_ListOfInteractive.hxx>
48 #include <AIS_ListIteratorOfListOfInteractive.hxx>
49
50 #include <BRep_Builder.hxx>
51 #include <TopoDS_Compound.hxx>
52
53 #include <QMessageBox>
54
55 HYDROGUI_ProfileInterpolateOp::HYDROGUI_ProfileInterpolateOp( HYDROGUI_Module* theModule )
56     : HYDROGUI_Operation( theModule )
57 {
58     setName( tr( "PROFILE_INTERPOLATION" ) );
59 }
60
61 HYDROGUI_ProfileInterpolateOp::~HYDROGUI_ProfileInterpolateOp()
62 {
63 }
64
65 void HYDROGUI_ProfileInterpolateOp::startOperation()
66 {
67     HYDROGUI_Operation::startOperation();
68
69     if ( isApplyAndClose() )
70     {
71       HYDROGUI_ProfileInterpolateDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
72       if ( aPanel )
73       {
74         aPanel->reset();
75         aPanel->setInterpolators( interpolators() );
76       }
77     }
78 }
79
80 void HYDROGUI_ProfileInterpolateOp::abortOperation()
81 {
82     if ( !myPreview.IsNull() )
83     {
84         HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
85         if ( aDlg && !aDlg->getAISContext().IsNull() )
86         {
87             aDlg->getAISContext()->Remove( myPreview, false );
88             myPreview.Nullify();
89         }
90     }
91
92     HYDROGUI_Operation::abortOperation();
93 }
94
95 void HYDROGUI_ProfileInterpolateOp::commitOperation()
96 {
97     if ( !myPreview.IsNull() )
98     {
99         HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
100         if ( aDlg && !aDlg->getAISContext().IsNull() )
101         {
102             aDlg->getAISContext()->Remove( myPreview, false );
103             myPreview.Nullify();
104         }
105     }
106
107     HYDROGUI_Operation::commitOperation();
108 }
109
110 HYDROGUI_InputPanel* HYDROGUI_ProfileInterpolateOp::createInputPanel() const
111 {
112     HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
113
114     connect( aDlg, SIGNAL( riverChanged( const QString& ) ), this, SLOT( onRiverChanged( const QString& ) ) );
115     connect( aDlg, SIGNAL( interpolatorChanged( const QString& ) ), this, SLOT( onInterpolatorChanged( const QString& ) ) );
116     connect( aDlg, SIGNAL( interpolatorParametersChanged( const QString& ) ), this, SLOT( updatePreview() ) );
117     connect( aDlg, SIGNAL( profileNumberChanged( int ) ), this, SLOT( updatePreview() ) );
118     connect( aDlg, SIGNAL( profileStartChanged( const QString& ) ), this, SLOT( updatePreview() ) );
119     connect( aDlg, SIGNAL( profileFinishChanged( const QString& ) ), this, SLOT( updatePreview() ) );
120
121     return aDlg;
122 }
123
124 bool HYDROGUI_ProfileInterpolateOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
125                                                   QStringList& theBrowseObjectsEntries )
126 {
127     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
128     if ( !aDlg )
129         return false;
130
131     QString errMessage;
132     HYDROData_IProfilesInterpolator* anIterp = interpolator( aDlg->interpolator() );
133     if ( anIterp )
134     {
135         updateInterpolator( anIterp );
136         Handle(HYDROData_Stream) aRiver = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), aDlg->river(), KIND_STREAM ) );
137         if ( !aRiver.IsNull() )
138         {
139             startDocOperation();
140             aRiver->Interpolate( anIterp );
141             if ( anIterp->GetErrorCode() == OK )
142                 commitDocOperation();
143             else
144             {
145                 errMessage = tr( "CALCULATE_ERROR" ), QString( HYDROGUI_Tool::ToQString( anIterp->GetErrorMessage() ) );
146                 abortDocOperation();
147             }
148         }
149         else
150             errMessage = tr( "CANT_GET_STREAM_OBJECT" ).arg( aDlg->river() );
151
152         if ( !errMessage.isEmpty() )
153             QMessageBox::critical( aDlg->topLevelWidget(), tr( "INTERPOLATION_ERROR" ), errMessage, QMessageBox::Ok );
154         else
155             commit();
156     }
157
158     bool res = errMessage.isEmpty();
159
160     if ( res )
161         theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
162
163     return res;
164 }
165
166 void HYDROGUI_ProfileInterpolateOp::updatePreview()
167 {
168     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
169     if ( !aDlg )
170         return;
171
172     Handle(AIS_InteractiveContext) aCtx = aDlg->getAISContext();
173     if ( aCtx.IsNull() )
174         return;
175
176     if ( !myPreview.IsNull() )
177         aCtx->Remove( myPreview, false );
178
179     myPreview.Nullify();
180
181     HYDROData_IProfilesInterpolator* anIterp = interpolator( aDlg->interpolator() );
182     if ( anIterp )
183     {
184         updateInterpolator( anIterp );
185         anIterp->Calculate();
186         if ( anIterp->GetErrorCode() == OK )
187         {
188             TopoDS_Shape aShape = previewShape( anIterp );
189             if ( !aShape.IsNull() )
190             {
191                 myPreview = new AIS_Shape( aShape );
192                 myPreview->SetColor( Quantity_NOC_RED );
193                 aCtx->Display( myPreview, 1, -1, false );
194             }
195         }
196     }
197
198     aCtx->UpdateCurrentViewer();
199 }
200
201 void HYDROGUI_ProfileInterpolateOp::updateInterpolator( HYDROData_IProfilesInterpolator* theInt )
202 {
203     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
204     if ( !aDlg || !theInt )
205         return;
206
207     theInt->Reset();
208     theInt->SetResultProfilesNumber( aDlg->profileNumber() );
209     theInt->SetProfiles( profile( aDlg->profileStart() ), profile( aDlg->profileFinish() ) );
210     ParamsList aParams = parameters( aDlg->interpolatorParameters() );
211     for ( ParamsList::Iterator it = aParams.begin(); it != aParams.end(); ++it ) {
212         theInt->SetParameter( HYDROGUI_Tool::ToAsciiString( (*it).first ), 
213                               HYDROGUI_Tool::ToAsciiString( (*it).second ) );
214     }
215 }
216
217 HYDROData_Profile::ProfilePoints HYDROGUI_ProfileInterpolateOp::profile( const QString& theName ) const
218 {
219     HYDROData_Profile::ProfilePoints aPoints;
220     Handle(HYDROData_Profile) aProf = Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theName, KIND_PROFILE ) );
221     if ( !aProf.IsNull() )
222     {
223         aPoints = aProf->GetProfilePoints();
224     }
225     return aPoints;
226 }
227
228 HYDROGUI_ProfileInterpolateOp::ParamsList HYDROGUI_ProfileInterpolateOp::parameters( const QString& theStr ) const
229 {
230     // Regular expression for parsing parameters string of kind: <param_name> <param_value> <param_name> <param_value> ..."
231     // QRegExp rx( "([A-Za-z_\\d]+)\\s+([A-Za-z_\\d]+)\\s*" );
232
233     // Regular expression for parsing parameters string of kind: <param_name> = <param_value> <param_name> = <param_value> ..."
234     QRegExp rx( "([A-Za-z_\\d]+)\\s*=\\s*([A-Za-z_\\d]+)\\s*" );
235
236     ParamsList aParamList;
237     for ( int i = 0; i >= 0; )
238     {
239         i = rx.indexIn( theStr, i );
240         if ( i >= 0  )
241         {
242             aParamList.append( QPair<QString, QString>( rx.cap( 1 ), rx.cap( 2 ) ) );
243             i += rx.matchedLength();
244         }
245     }
246
247     return aParamList;
248 }
249
250 QString HYDROGUI_ProfileInterpolateOp::parameters( const ParamsList& theParamList ) const
251 {
252     QStringList paramList;
253     for ( ParamsList::const_iterator it = theParamList.begin(); it != theParamList.end(); ++it )
254         paramList.append( QString( "%1 = %2" ).arg( (*it).first ).arg( (*it).second ) );
255     return paramList.join( " " );
256 }
257
258 QStringList HYDROGUI_ProfileInterpolateOp::interpolators() const
259 {
260     HYDROData_InterpolatorsFactory* anIFactory = 0;
261     Handle(HYDROData_Document) aDoc = doc();
262     if ( !aDoc.IsNull() )
263         anIFactory = aDoc->GetInterpolatorsFactory();
264
265     QStringList aNames;
266     if ( anIFactory )
267     {
268       NCollection_Sequence<TCollection_AsciiString> iNames = anIFactory->GetInterpolatorNames();
269       for ( int i = 1, n = iNames.Size(); i <= n; ++i ) {
270         const TCollection_AsciiString& anInterpName = iNames.Value( i );
271         aNames.append( HYDROGUI_Tool::ToQString( anInterpName ) );
272       }
273     }
274
275     return aNames;
276 }
277
278 HYDROData_IProfilesInterpolator* HYDROGUI_ProfileInterpolateOp::interpolator( const QString& theName ) const
279 {
280     HYDROData_InterpolatorsFactory* anIFactory = 0;
281     Handle(HYDROData_Document) aDoc = doc();
282     if ( !aDoc.IsNull() )
283         anIFactory = aDoc->GetInterpolatorsFactory();
284
285     HYDROData_IProfilesInterpolator* aRes = 0;
286     if ( anIFactory )
287         aRes = anIFactory->GetInterpolator( HYDROGUI_Tool::ToAsciiString( theName ) );
288     return aRes;
289 }
290
291 TopoDS_Shape HYDROGUI_ProfileInterpolateOp::previewShape( HYDROData_IProfilesInterpolator* theInterp ) const
292 {
293     TopoDS_Compound aPreviewShape;
294     if ( theInterp )
295     {
296         BRep_Builder aBuilder;
297         aBuilder.MakeCompound( aPreviewShape );
298         for ( int i = 0; i < theInterp->GetCalculatedProfilesNumber(); i++ )
299         {
300             NCollection_Sequence<gp_XYZ> pointSeq = theInterp->GetResultProfilePoints( i );
301
302             TopoDS_Shape aWire = HYDROData_PolylineXY::BuildWire( HYDROData_IPolyline::SECTION_SPLINE, false, pointSeq );
303             if ( !aWire.IsNull() )
304                 aBuilder.Add( aPreviewShape, aWire );
305         }
306     }
307
308     return aPreviewShape;
309 }
310
311 void HYDROGUI_ProfileInterpolateOp::onInterpolatorChanged( const QString& theInterpName )
312 {
313     HYDROGUI_ProfileInterpolateDlg* aPanel = dynamic_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
314     HYDROData_IProfilesInterpolator* anInterp = interpolator( theInterpName );
315     if ( !aPanel || !anInterp )
316         return;
317
318     aPanel->setInterpolatorDescription( HYDROGUI_Tool::ToQString( anInterp->GetDescription() ) );
319
320     updatePreview();
321 }
322
323 void HYDROGUI_ProfileInterpolateOp::onRiverChanged( const QString& theRiver )
324 {
325     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
326     if ( !aDlg )
327         return;
328
329     Handle(AIS_InteractiveContext) aCtx = aDlg->getAISContext();
330     if ( !aCtx.IsNull() )
331     {
332         AIS_ListOfInteractive aList;
333         aCtx->DisplayedObjects( aList );
334         for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
335         {
336             Handle(AIS_Shape) anObj = Handle(AIS_Shape)::DownCast( it.Value() );
337             if ( !anObj.IsNull() )
338                 aCtx->Remove( anObj, false );
339         }
340
341         Handle(HYDROData_Stream) aStream = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theRiver, KIND_STREAM ) );
342         if ( !aStream.IsNull() )
343         {
344             HYDROData_SequenceOfObjects aSeq = aStream->GetProfiles();
345             for ( int i = aSeq.Lower(); i <= aSeq.Upper(); i++ )
346             {
347                 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( aSeq.Value( i ) );
348                 if ( !aProfile.IsNull() )
349                 {
350                     Handle(AIS_Shape) aPrs = new AIS_Shape( aProfile->GetShape3D() );
351                     aPrs->SetOwner( aProfile );
352                     aPrs->SetColor( Quantity_NOC_BLACK );
353                     aCtx->Display( aPrs, 1, 0, false );
354                 }
355             }
356             OCCViewer_ViewManager* vm = aDlg->viewManager();
357             if ( vm )
358             {
359                 QVector<SUIT_ViewWindow*> winList = vm->getViews();
360                 for ( QVector<SUIT_ViewWindow*>::iterator it = winList.begin(); it != winList.end(); ++it )
361                 {
362                     OCCViewer_ViewWindow* occWin = ::qobject_cast<OCCViewer_ViewWindow*>( *it );
363                     if ( occWin )
364                     {
365                         OCCViewer_ViewPort3d* vp = occWin->getViewPort();
366                         if ( vp )
367                             vp->fitAll();
368                     }
369                 }
370             }
371         }
372     }
373     updatePreview();
374 }