Salome HOME
refs #529: use 'Apply and Close', 'Apply' and 'Close' buttons in HYDRO dialogs.
[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     if ( isApplyAndClose() )
91       myEditedObject = Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
92
93   QString aProfileName;
94   if( !myEditedObject.IsNull() )
95   {
96     Handle(HYDROData_ProfileUZ) aProfileUZ = myEditedObject->GetProfileUZ( false );
97     if ( !aProfileUZ.IsNull() )
98     {
99       CurveCreator::Coordinates aCurveCoords;
100       CurveCreator::SectionsMap aSectionsMap;
101
102       HYDROData_ProfileUZ::PointsList aSectPointsList = aProfileUZ->GetPoints();
103       CurveCreator::PosPointsList aPoints;
104       for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
105       {
106         const HYDROData_ProfileUZ::Point& aSectPoint = aSectPointsList.Value( k );
107         aCurveCoords.clear();
108         aCurveCoords.push_back( aSectPoint.X() );
109         aCurveCoords.push_back( aSectPoint.Y() );
110
111         CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( k, aCurveCoords );
112         aPoints.push_back( aPosPoint );
113       }
114
115       aSectionsMap[0] = aPoints;
116       myProfile->addPointsInternal( aSectionsMap );
117
118       HYDROData_ProfileUZ::SectionType aSectType = aProfileUZ->GetSectionType( 0 );
119
120       CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
121       if( aSectType == HYDROData_ProfileUZ::SECTION_SPLINE )
122         aCurveType = CurveCreator::Spline;
123
124       myProfile->setSectionType( 0, aCurveType );
125     }
126
127     aProfileName = myEditedObject->GetName();
128   }
129   else
130   {
131     aProfileName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_PROFILE_NAME" ) );
132   }
133
134   aPanel->setProfileName( aProfileName );
135   aPanel->setProfile( myProfile );
136   displayPreview();
137 }
138
139 void HYDROGUI_ProfileOp::abortOperation()
140 {
141   erasePreview();
142
143   HYDROGUI_Operation::abortOperation();
144 }
145
146 void HYDROGUI_ProfileOp::commitOperation()
147 {
148   erasePreview();
149
150   HYDROGUI_Operation::commitOperation();
151 }
152
153 HYDROGUI_InputPanel* HYDROGUI_ProfileOp::createInputPanel() const
154 {
155   HYDROGUI_ProfileDlg* aDlg = new HYDROGUI_ProfileDlg( module(), getName() );
156   return aDlg;
157 }
158
159 bool HYDROGUI_ProfileOp::processApply( int& theUpdateFlags,
160                                        QString& theErrorMsg,
161                                        QStringList& theBrowseObjectsEntries )
162 {
163   HYDROGUI_ProfileDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
164   if ( !aPanel )
165     return false;
166
167   QString aProfileName = aPanel->getProfileName().simplified();
168   if ( aProfileName.isEmpty() )
169   {
170     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
171     return false;
172   }
173
174   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aProfileName ) )
175   {
176     // check that there are no other objects with the same name in the document
177     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aProfileName );
178     if( !anObject.IsNull() )
179     {
180       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aProfileName );
181       return false;
182     }
183   }
184
185   Handle(HYDROData_Profile) aProfileObj;
186   if( myIsEdit ){
187     aProfileObj = myEditedObject;
188   }
189   else{
190     aProfileObj = Handle(HYDROData_Profile)::DownCast( doc()->CreateObject( KIND_PROFILE ) );
191   }
192
193   if( aProfileObj.IsNull() )
194     return false;
195
196   Handle(HYDROData_ProfileUZ) aProfileUZ = aProfileObj->GetProfileUZ();
197   if ( aProfileUZ.IsNull() )
198     return false;
199
200   aProfileObj->SetName(aProfileName);
201
202   HYDROData_ProfileUZ::PointsList aProfileParamPoints;
203
204   CurveCreator::Coordinates aCurveCoords = myProfile->getPoints( 0 );
205   if ( aCurveCoords.size() <= 2 )
206   {
207     theErrorMsg = tr( "NUMBER_OF_PROFILE_POINTS_INCORRECT" );
208     return false;
209   }
210
211   for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
212   {
213     HYDROData_ProfileUZ::Point aProfileParamPoint;
214
215     aProfileParamPoint.SetX( aCurveCoords.at( k ) );
216     k++;
217     aProfileParamPoint.SetY( aCurveCoords.at( k ) );
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( HYDROData_Profile::DefaultBorderColor() );
232   }
233
234   // At first we update the child u,z profile object
235   aProfileUZ->SetToUpdate( true );
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 }