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