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