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