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