]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ProfileOp.cxx
Salome HOME
copyrights are updated
[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     myEditedObject = Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
87
88   QString aProfileName;
89   if( !myEditedObject.IsNull() )
90   {
91     Handle(HYDROData_ProfileUZ) aProfileUZ = myEditedObject->GetProfileUZ( false );
92     if ( !aProfileUZ.IsNull() )
93     {
94       CurveCreator::Coordinates aCurveCoords;
95       CurveCreator::SectionsMap aSectionsMap;
96
97       HYDROData_ProfileUZ::PointsList aSectPointsList = aProfileUZ->GetPoints();
98       CurveCreator::PosPointsList aPoints;
99       for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
100       {
101         const HYDROData_ProfileUZ::Point& aSectPoint = aSectPointsList.Value( k );
102         aCurveCoords.clear();
103         aCurveCoords.push_back( aSectPoint.X() );
104         aCurveCoords.push_back( aSectPoint.Y() );
105
106         CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( k, aCurveCoords );
107         aPoints.push_back( aPosPoint );
108       }
109
110       aSectionsMap[0] = aPoints;
111       myProfile->addPointsInternal( aSectionsMap );
112
113       HYDROData_ProfileUZ::SectionType aSectType = aProfileUZ->GetSectionType( 0 );
114
115       CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
116       if( aSectType == HYDROData_ProfileUZ::SECTION_SPLINE )
117         aCurveType = CurveCreator::Spline;
118
119       myProfile->setSectionType( 0, aCurveType );
120     }
121
122     aProfileName = myEditedObject->GetName();
123   }
124   else
125   {
126     aProfileName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_PROFILE_NAME" ) );
127   }
128
129   aPanel->setProfileName( aProfileName );
130   aPanel->setProfile( myProfile );
131   displayPreview();
132 }
133
134 void HYDROGUI_ProfileOp::abortOperation()
135 {
136   erasePreview();
137
138   HYDROGUI_Operation::abortOperation();
139 }
140
141 void HYDROGUI_ProfileOp::commitOperation()
142 {
143   erasePreview();
144
145   HYDROGUI_Operation::commitOperation();
146 }
147
148 HYDROGUI_InputPanel* HYDROGUI_ProfileOp::createInputPanel() const
149 {
150   HYDROGUI_ProfileDlg* aDlg = new HYDROGUI_ProfileDlg( module(), getName() );
151   return aDlg;
152 }
153
154 bool HYDROGUI_ProfileOp::processApply( int& theUpdateFlags,
155                                        QString& theErrorMsg,
156                                        QStringList& theBrowseObjectsEntries )
157 {
158   HYDROGUI_ProfileDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
159   if ( !aPanel )
160     return false;
161
162   QString aProfileName = aPanel->getProfileName().simplified();
163   if ( aProfileName.isEmpty() )
164   {
165     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
166     return false;
167   }
168
169   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aProfileName ) )
170   {
171     // check that there are no other objects with the same name in the document
172     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aProfileName );
173     if( !anObject.IsNull() )
174     {
175       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aProfileName );
176       return false;
177     }
178   }
179
180   Handle(HYDROData_Profile) aProfileObj;
181   if( myIsEdit ){
182     aProfileObj = myEditedObject;
183   }
184   else{
185     aProfileObj = Handle(HYDROData_Profile)::DownCast( doc()->CreateObject( KIND_PROFILE ) );
186   }
187
188   if( aProfileObj.IsNull() )
189     return false;
190
191   Handle(HYDROData_ProfileUZ) aProfileUZ = aProfileObj->GetProfileUZ();
192   if ( aProfileUZ.IsNull() )
193     return false;
194
195   aProfileObj->SetName(aProfileName);
196
197   HYDROData_ProfileUZ::PointsList aProfileParamPoints;
198
199   CurveCreator::Coordinates aCurveCoords = myProfile->getPoints( 0 );
200   if ( aCurveCoords.size() <= 2 )
201   {
202     theErrorMsg = tr( "NUMBER_OF_PROFILE_POINTS_INCORRECT" );
203     return false;
204   }
205
206   for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
207   {
208     HYDROData_ProfileUZ::Point aProfileParamPoint;
209
210     aProfileParamPoint.SetX( aCurveCoords.at( k ) );
211     k++;
212     aProfileParamPoint.SetY( aCurveCoords.at( k ) );
213
214     aProfileParamPoints.Append( aProfileParamPoint );
215   }
216   aProfileObj->SetParametricPoints( aProfileParamPoints );
217
218   HYDROData_ProfileUZ::SectionType aSectType = HYDROData_ProfileUZ::SECTION_POLYLINE;
219   if ( myProfile->getSectionType( 0 ) == CurveCreator::Spline )
220     aSectType = HYDROData_ProfileUZ::SECTION_SPLINE;
221
222   aProfileUZ->SetSectionType( 0, aSectType );
223
224   if ( !myIsEdit )
225   {
226     aProfileObj->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
227   }
228
229   // At first we update the child u,z profile object
230   aProfileUZ->SetToUpdate( true );
231   aProfileUZ->Update();
232
233   // And now we update our edited object
234   aProfileObj->Update();
235   module()->setIsToUpdate( aProfileObj );
236
237   theUpdateFlags = UF_Model;
238   if ( myIsEdit )
239     theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
240   else
241   {
242     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aProfileObj );
243     theBrowseObjectsEntries.append( anEntry );
244   }
245
246   return true;
247 }
248
249 void HYDROGUI_ProfileOp::displayPreview()
250 {
251   HYDROGUI_ProfileDlg* aPanel = dynamic_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
252   if( aPanel )
253   {
254     Handle(AIS_InteractiveContext) aCtx = aPanel->getAISContext();
255     if( !aCtx.IsNull() )
256     {
257       CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
258       myProfile->setDisplayer( aDisplayer );
259       aDisplayer->display( myProfile->getAISObject( true ), true );
260     }
261   }
262 }
263
264 void HYDROGUI_ProfileOp::erasePreview()
265 {
266   HYDROGUI_ProfileDlg* aPanel = dynamic_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
267   CurveCreator_Displayer* aDisplayer = myProfile ? myProfile->getDisplayer() : 0;
268   if( aPanel && aDisplayer )
269   {
270     Handle(AIS_InteractiveContext) aCtx = aPanel->getAISContext();
271     if( !aCtx.IsNull() )
272     {
273       aDisplayer->eraseAll( true );
274     }
275   }
276 }