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