Salome HOME
merge trunk on BR_quadtree 20140820
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ProfileOp.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 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_ProfileOp.h"
24 #include "HYDROGUI_ProfileDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27 #include <HYDROGUI_DataObject.h>
28 #include "HYDROData_Document.h"
29 #include "HYDROData_Profile.h"
30 #include "CurveCreator_Profile.hxx"
31 #include "CurveCreator_Displayer.h"
32
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
36
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewWindow.h>
40
41 #include <OCCViewer_AISSelector.h>
42
43 #include <Precision.hxx>
44
45 //static int ZValueIncrement = 0;
46
47 HYDROGUI_ProfileOp::HYDROGUI_ProfileOp( HYDROGUI_Module* theModule, bool theIsEdit )
48 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myProfile(NULL)
49 {
50   setName( theIsEdit ? tr( "EDIT_PROFILE" ) : tr( "CREATE_PROFILE" ) );
51 }
52
53 HYDROGUI_ProfileOp::~HYDROGUI_ProfileOp()
54 {
55   erasePreview();
56 }
57
58 /**
59  * Redirect the delete action to input panel
60  */
61 void HYDROGUI_ProfileOp::deleteSelected()
62 {
63   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
64   aPanel->deleteSelected();
65 }
66
67 /**
68  * Checks whether there are some to delete
69  */
70 bool HYDROGUI_ProfileOp::deleteEnabled()
71 {
72   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
73   return aPanel->deleteEnabled();
74 }
75
76 void HYDROGUI_ProfileOp::startOperation()
77 {
78   if( myProfile )
79     delete myProfile;
80
81   myProfile = new CurveCreator_Profile();
82
83   HYDROGUI_Operation::startOperation();
84
85   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
86   aPanel->reset();
87
88   if( myIsEdit )
89     myEditedObject = Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
90
91   QString aProfileName;
92   if( !myEditedObject.IsNull() )
93   {
94     Handle(HYDROData_ProfileUZ) aProfileUZ = myEditedObject->GetProfileUZ( false );
95     if ( !aProfileUZ.IsNull() )
96     {
97       CurveCreator::Coordinates aCurveCoords;
98       CurveCreator::SectionsMap aSectionsMap;
99
100       HYDROData_ProfileUZ::PointsList aSectPointsList = aProfileUZ->GetPoints();
101       CurveCreator::PosPointsList aPoints;
102       for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
103       {
104         const HYDROData_ProfileUZ::Point& aSectPoint = aSectPointsList.Value( k );
105         aCurveCoords.clear();
106         aCurveCoords.push_back( aSectPoint.X() );
107         aCurveCoords.push_back( aSectPoint.Y() );
108
109         CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( k, aCurveCoords );
110         aPoints.push_back( aPosPoint );
111       }
112
113       aSectionsMap[0] = aPoints;
114       myProfile->addPointsInternal( aSectionsMap );
115
116       HYDROData_ProfileUZ::SectionType aSectType = aProfileUZ->GetSectionType( 0 );
117
118       CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
119       if( aSectType == HYDROData_ProfileUZ::SECTION_SPLINE )
120         aCurveType = CurveCreator::Spline;
121
122       myProfile->setSectionType( 0, aCurveType );
123     }
124
125     aProfileName = myEditedObject->GetName();
126   }
127   else
128   {
129     aProfileName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_PROFILE_NAME" ) );
130   }
131
132   aPanel->setProfileName( aProfileName );
133   aPanel->setProfile( myProfile );
134   displayPreview();
135 }
136
137 void HYDROGUI_ProfileOp::abortOperation()
138 {
139   erasePreview();
140
141   HYDROGUI_Operation::abortOperation();
142 }
143
144 void HYDROGUI_ProfileOp::commitOperation()
145 {
146   erasePreview();
147
148   HYDROGUI_Operation::commitOperation();
149 }
150
151 HYDROGUI_InputPanel* HYDROGUI_ProfileOp::createInputPanel() const
152 {
153   HYDROGUI_ProfileDlg* aDlg = new HYDROGUI_ProfileDlg( module(), getName() );
154   return aDlg;
155 }
156
157 bool HYDROGUI_ProfileOp::processApply( int& theUpdateFlags,
158                                        QString& theErrorMsg,
159                                        QStringList& theBrowseObjectsEntries )
160 {
161   HYDROGUI_ProfileDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
162   if ( !aPanel )
163     return false;
164
165   QString aProfileName = aPanel->getProfileName().simplified();
166   if ( aProfileName.isEmpty() )
167   {
168     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
169     return false;
170   }
171
172   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aProfileName ) )
173   {
174     // check that there are no other objects with the same name in the document
175     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aProfileName );
176     if( !anObject.IsNull() )
177     {
178       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aProfileName );
179       return false;
180     }
181   }
182
183   Handle(HYDROData_Profile) aProfileObj;
184   if( myIsEdit ){
185     aProfileObj = myEditedObject;
186   }
187   else{
188     aProfileObj = Handle(HYDROData_Profile)::DownCast( doc()->CreateObject( KIND_PROFILE ) );
189   }
190
191   if( aProfileObj.IsNull() )
192     return false;
193
194   Handle(HYDROData_ProfileUZ) aProfileUZ = aProfileObj->GetProfileUZ();
195   if ( aProfileUZ.IsNull() )
196     return false;
197
198   aProfileObj->SetName(aProfileName);
199
200   HYDROData_ProfileUZ::PointsList aProfileParamPoints;
201
202   CurveCreator::Coordinates aCurveCoords = myProfile->getPoints( 0 );
203   if ( aCurveCoords.size() <= 2 )
204   {
205     theErrorMsg = tr( "NUMBER_OF_PROFILE_POINTS_INCORRECT" );
206     return false;
207   }
208
209   for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
210   {
211     HYDROData_ProfileUZ::Point aProfileParamPoint;
212
213     aProfileParamPoint.SetX( aCurveCoords.at( k ) );
214     k++;
215     aProfileParamPoint.SetY( aCurveCoords.at( k ) );
216
217     aProfileParamPoints.Append( aProfileParamPoint );
218   }
219   aProfileObj->SetParametricPoints( aProfileParamPoints );
220
221   HYDROData_ProfileUZ::SectionType aSectType = HYDROData_ProfileUZ::SECTION_POLYLINE;
222   if ( myProfile->getSectionType( 0 ) == CurveCreator::Spline )
223     aSectType = HYDROData_ProfileUZ::SECTION_SPLINE;
224
225   aProfileUZ->SetSectionType( 0, aSectType );
226
227   if ( !myIsEdit )
228   {
229     aProfileObj->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
230   }
231
232   // At first we update the child u,z profile object
233   aProfileUZ->SetToUpdate( true );
234   aProfileUZ->Update();
235
236   // And now we update our edited object
237   aProfileObj->Update();
238   module()->setIsToUpdate( aProfileObj );
239
240   theUpdateFlags = UF_Model;
241   if ( myIsEdit )
242     theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
243   else
244   {
245     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aProfileObj );
246     theBrowseObjectsEntries.append( anEntry );
247   }
248
249   return true;
250 }
251
252 void HYDROGUI_ProfileOp::displayPreview()
253 {
254   HYDROGUI_ProfileDlg* aPanel = dynamic_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
255   if( aPanel )
256   {
257     Handle(AIS_InteractiveContext) aCtx = aPanel->getAISContext();
258     if( !aCtx.IsNull() )
259     {
260       CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
261       myProfile->setDisplayer( aDisplayer );
262       aDisplayer->display( myProfile->getAISObject( true ), true );
263     }
264   }
265 }
266
267 void HYDROGUI_ProfileOp::erasePreview()
268 {
269   HYDROGUI_ProfileDlg* aPanel = dynamic_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
270   CurveCreator_Displayer* aDisplayer = myProfile ? myProfile->getDisplayer() : 0;
271   if( aPanel && aDisplayer )
272   {
273     Handle(AIS_InteractiveContext) aCtx = aPanel->getAISContext();
274     if( !aCtx.IsNull() )
275     {
276       aDisplayer->eraseAll( true );
277     }
278   }
279 }