Salome HOME
Merge branch 'master' into BR_RIVER
[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
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36 #include <LightApp_UpdateFlags.h>
37
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewManager.h>
40 #include <OCCViewer_ViewWindow.h>
41 #include <OCCViewer_AISSelector.h>
42
43 #include <AIS_ListOfInteractive.hxx>
44 #include <AIS_ListIteratorOfListOfInteractive.hxx>
45
46 HYDROGUI_ProfileInterpolateOp::HYDROGUI_ProfileInterpolateOp( HYDROGUI_Module* theModule )
47     : HYDROGUI_Operation( theModule )
48 {
49     setName( tr( "PROFILE_INTERPOLATION" ) );
50 }
51
52 HYDROGUI_ProfileInterpolateOp::~HYDROGUI_ProfileInterpolateOp()
53 {
54 }
55
56 void HYDROGUI_ProfileInterpolateOp::startOperation()
57 {
58     HYDROGUI_Operation::startOperation();
59
60     HYDROGUI_ProfileInterpolateDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
61     if ( aPanel )
62     {
63         aPanel->reset();
64         aPanel->setInterpolators( interpolators() );
65     }
66
67 /*
68     if( myIsEdit )
69         myEditedObject = Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
70
71     QString aProfileName;
72     if( !myEditedObject.IsNull() )
73     {
74         Handle(HYDROData_ProfileUZ) aProfileUZ = myEditedObject->GetProfileUZ( false );
75         if ( !aProfileUZ.IsNull() )
76         {
77             CurveCreator::Coordinates aCurveCoords;
78             CurveCreator::SectionsMap aSectionsMap;
79
80             HYDROData_ProfileUZ::PointsList aSectPointsList = aProfileUZ->GetPoints();
81             CurveCreator::PosPointsList aPoints;
82             for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
83             {
84                 const HYDROData_ProfileUZ::Point& aSectPoint = aSectPointsList.Value( k );
85                 aCurveCoords.clear();
86                 aCurveCoords.push_back( aSectPoint.X() );
87                 aCurveCoords.push_back( aSectPoint.Y() );
88
89                 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( k, aCurveCoords );
90                 aPoints.push_back( aPosPoint );
91             }
92
93             aSectionsMap[0] = aPoints;
94             myProfile->addPointsInternal( aSectionsMap );
95
96             HYDROData_ProfileUZ::SectionType aSectType = aProfileUZ->GetSectionType( 0 );
97
98             CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
99             if( aSectType == HYDROData_ProfileUZ::SECTION_SPLINE )
100                 aCurveType = CurveCreator::Spline;
101
102             myProfile->setSectionType( 0, aCurveType );
103         }
104
105         aProfileName = myEditedObject->GetName();
106     }
107     else
108     {
109         aProfileName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_PROFILE_NAME" ) );
110     }
111
112     aPanel->setProfileName( aProfileName );
113     aPanel->setProfile( myProfile );
114 */
115 //    displayPreview();
116 }
117
118 void HYDROGUI_ProfileInterpolateOp::abortOperation()
119 {
120     HYDROGUI_Operation::abortOperation();
121 }
122
123 void HYDROGUI_ProfileInterpolateOp::commitOperation()
124 {
125     HYDROGUI_Operation::commitOperation();
126 }
127
128 HYDROGUI_InputPanel* HYDROGUI_ProfileInterpolateOp::createInputPanel() const
129 {
130     HYDROGUI_ProfileInterpolateDlg* aDlg = new HYDROGUI_ProfileInterpolateDlg( module(), getName() );
131
132     connect( aDlg, SIGNAL( riverChanged( const QString& ) ), this, SLOT( onRiverChanged( const QString& ) ) );
133     connect( aDlg, SIGNAL( interpolatorChanged( const QString& ) ), this, SLOT( onInterpolatorChanged( const QString& ) ) );
134     connect( aDlg, SIGNAL( interpolatorParametersChanged( const QString& ) ), this, SLOT( updatePreview() ) );
135     connect( aDlg, SIGNAL( profileNumberChanged( int ) ), this, SLOT( updatePreview() ) );
136     connect( aDlg, SIGNAL( profileStartChanged( const QString& ) ), this, SLOT( updatePreview() ) );
137     connect( aDlg, SIGNAL( profileFinishChanged( const QString& ) ), this, SLOT( updatePreview() ) );
138
139     return aDlg;
140 }
141
142 bool HYDROGUI_ProfileInterpolateOp::processApply( int& theUpdateFlags, QString& theErrorMsg,
143                                                   QStringList& theBrowseObjectsEntries )
144 {
145     HYDROGUI_ProfileInterpolateDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
146     if ( !aPanel )
147         return false;
148 /*
149     QString aProfileName = aPanel->getProfileName().simplified();
150     if ( aProfileName.isEmpty() )
151     {
152         theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
153         return false;
154     }
155
156     if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aProfileName ) )
157     {
158         // check that there are no other objects with the same name in the document
159         Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aProfileName );
160         if( !anObject.IsNull() )
161         {
162             theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aProfileName );
163             return false;
164         }
165     }
166
167     Handle(HYDROData_Profile) aProfileObj;
168     if( myIsEdit ){
169         aProfileObj = myEditedObject;
170     }
171     else{
172         aProfileObj = Handle(HYDROData_Profile)::DownCast( doc()->CreateObject( KIND_PROFILE ) );
173     }
174
175     if( aProfileObj.IsNull() )
176         return false;
177
178     Handle(HYDROData_ProfileUZ) aProfileUZ = aProfileObj->GetProfileUZ();
179     if ( aProfileUZ.IsNull() )
180         return false;
181
182     aProfileObj->SetName(aProfileName);
183
184     HYDROData_ProfileUZ::PointsList aProfileParamPoints;
185
186     CurveCreator::Coordinates aCurveCoords = myProfile->getPoints( 0 );
187     if ( aCurveCoords.size() <= 2 )
188     {
189         theErrorMsg = tr( "NUMBER_OF_PROFILE_POINTS_INCORRECT" );
190         return false;
191     }
192
193     for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
194     {
195         HYDROData_ProfileUZ::Point aProfileParamPoint;
196
197         aProfileParamPoint.SetX( aCurveCoords.at( k ) );
198         k++;
199         aProfileParamPoint.SetY( aCurveCoords.at( k ) );
200
201         aProfileParamPoints.Append( aProfileParamPoint );
202     }
203     aProfileObj->SetParametricPoints( aProfileParamPoints );
204
205     HYDROData_ProfileUZ::SectionType aSectType = HYDROData_ProfileUZ::SECTION_POLYLINE;
206     if ( myProfile->getSectionType( 0 ) == CurveCreator::Spline )
207         aSectType = HYDROData_ProfileUZ::SECTION_SPLINE;
208
209     aProfileUZ->SetSectionType( 0, aSectType );
210
211     if ( !myIsEdit )
212     {
213         aProfileObj->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
214     }
215
216     // At first we update the child u,z profile object
217     aProfileUZ->SetToUpdate( true );
218     aProfileUZ->Update();
219
220     // And now we update our edited object
221     aProfileObj->Update();
222     module()->setIsToUpdate( aProfileObj );
223
224     theUpdateFlags = UF_Model;
225     if ( myIsEdit )
226         theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
227     else
228     {
229         QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aProfileObj );
230         theBrowseObjectsEntries.append( anEntry );
231     }
232
233     */
234     return true;
235 }
236
237 void HYDROGUI_ProfileInterpolateOp::updatePreview()
238 {
239     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
240     if ( !aDlg )
241         return;
242
243     Handle(AIS_InteractiveContext) aCtx = aDlg->getAISContext();
244     if ( aCtx.IsNull() )
245         return;
246
247     aCtx->UpdateCurrentViewer();
248 }
249
250 QStringList HYDROGUI_ProfileInterpolateOp::interpolators() const
251 {
252     return QStringList() << "Linear interpolator" << "Spline interpolator";
253 }
254
255 void HYDROGUI_ProfileInterpolateOp::onInterpolatorChanged( const QString& theInterpName )
256 {
257     HYDROGUI_ProfileInterpolateDlg* aPanel = dynamic_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
258     if ( !aPanel )
259         return;
260
261     aPanel->setInterpolatorDescription( QString( "Profile will be calculated used %1" ).arg( theInterpName ) );
262
263     updatePreview();
264 }
265
266 void HYDROGUI_ProfileInterpolateOp::onRiverChanged( const QString& theRiver )
267 {
268     HYDROGUI_ProfileInterpolateDlg* aDlg = ::qobject_cast<HYDROGUI_ProfileInterpolateDlg*>( inputPanel() );
269     if ( !aDlg )
270         return;
271
272     Handle(AIS_InteractiveContext) aCtx = aDlg->getAISContext();
273     if ( !aCtx.IsNull() )
274     {
275         AIS_ListOfInteractive aList;
276         aCtx->DisplayedObjects( aList );
277         for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
278         {
279             Handle(AIS_Shape) anObj = Handle(AIS_Shape)::DownCast( it.Value() );
280             if ( !anObj.IsNull() )
281                 aCtx->Remove( anObj, false );
282         }
283
284         Handle(HYDROData_Stream) aStream = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), theRiver, KIND_STREAM ) );
285         if ( !aStream.IsNull() )
286         {
287             HYDROData_SequenceOfObjects aSeq = aStream->GetProfiles();
288             for ( int i = aSeq.Lower(); i <= aSeq.Upper(); i++ )
289             {
290                 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( aSeq.Value( i ) );
291                 if ( !aProfile.IsNull() )
292                 {
293                     Handle(AIS_Shape) aPrs = new AIS_Shape( aProfile->GetShape3D() );
294                     aPrs->SetOwner( aProfile );
295                     aPrs->SetColor( Quantity_NOC_BLACK );
296                     aCtx->Display( aPrs, 0, 0, false );
297                 }
298             }
299         }
300     }
301     updatePreview();
302 }