Salome HOME
Names of objects can't be empty (Bug #149).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ProfileOp.cxx
1 // Copyright (C) 2007-2013  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.
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 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_ProfileOp.h"
24 #include "HYDROGUI_ProfileDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27
28 #include "HYDROData_Document.h"
29 #include "HYDROData_Profile.h"
30 #include "CurveCreator_Profile.hxx"
31 #include "CurveCreator_Displayer.h"
32
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
36
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewWindow.h>
40
41 #include <OCCViewer_AISSelector.h>
42
43 #include <Precision.hxx>
44
45 //static int ZValueIncrement = 0;
46
47 HYDROGUI_ProfileOp::HYDROGUI_ProfileOp( HYDROGUI_Module* theModule, bool theIsEdit )
48 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myProfile(NULL)
49 {
50   setName( theIsEdit ? tr( "EDIT_PROFILE" ) : tr( "CREATE_PROFILE" ) );
51 }
52
53 HYDROGUI_ProfileOp::~HYDROGUI_ProfileOp()
54 {
55   erasePreview();
56 }
57
58 /**
59  * Redirect the delete action to input panel
60  */
61 void HYDROGUI_ProfileOp::deleteSelected()
62 {
63   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
64   aPanel->deleteSelected();
65 }
66
67 /**
68  * Checks whether there are some to delete
69  */
70 bool HYDROGUI_ProfileOp::deleteEnabled()
71 {
72   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
73   return aPanel->deleteEnabled();
74 }
75
76 void HYDROGUI_ProfileOp::startOperation()
77 {
78   if( myProfile )
79     delete myProfile;
80
81   myProfile = new CurveCreator_Profile();
82
83   HYDROGUI_Operation::startOperation();
84
85   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
86   aPanel->reset();
87
88   if( myIsEdit )
89     myEditedObject = Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
90
91   QString aProfileName;
92   if( !myEditedObject.IsNull() )
93   {
94     CurveCreator::Coordinates aCurveCoords;
95     CurveCreator::SectionsMap aSectionsMap;
96
97     HYDROData_Profile::ProfilePoints aSectPointsList = myEditedObject->GetProfilePoints();
98     CurveCreator::PosPointsList aPoints;
99     for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
100     {
101       const HYDROData_Profile::ProfilePoint& aSectPoint = aSectPointsList.Value( k );
102       aCurveCoords.clear();
103       aCurveCoords.push_back( aSectPoint.X() );
104       aCurveCoords.push_back( aSectPoint.Z() );
105
106       CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( k, aCurveCoords );
107       aPoints.push_back( aPosPoint );
108     }
109     aSectionsMap[0] = aPoints;
110     myProfile->addPointsInternal( aSectionsMap );
111
112     aProfileName = myEditedObject->GetName();
113   }
114   else
115   {
116     aProfileName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_PROFILE_NAME" ) );
117   }
118
119   aPanel->setProfileName( aProfileName );
120   aPanel->setProfile( myProfile );
121   displayPreview();
122 }
123
124 void HYDROGUI_ProfileOp::abortOperation()
125 {
126   erasePreview();
127
128   HYDROGUI_Operation::abortOperation();
129 }
130
131 void HYDROGUI_ProfileOp::commitOperation()
132 {
133   erasePreview();
134
135   HYDROGUI_Operation::commitOperation();
136 }
137
138 HYDROGUI_InputPanel* HYDROGUI_ProfileOp::createInputPanel() const
139 {
140   HYDROGUI_ProfileDlg* aDlg = new HYDROGUI_ProfileDlg( module(), getName() );
141   return aDlg;
142 }
143
144 bool HYDROGUI_ProfileOp::processApply( int& theUpdateFlags,
145                                         QString& theErrorMsg )
146 {
147   HYDROGUI_ProfileDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
148   if ( !aPanel )
149     return false;
150
151   QString aProfileName = aPanel->getProfileName().simplified();
152   if ( aProfileName.isEmpty() )
153   {
154     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
155     return false;
156   }
157
158   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aProfileName ) )
159   {
160     // check that there are no other objects with the same name in the document
161     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aProfileName );
162     if( !anObject.IsNull() )
163     {
164       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aProfileName );
165       return false;
166     }
167   }
168
169   Handle(HYDROData_Profile) aProfileObj;
170   if( myIsEdit ){
171     aProfileObj = myEditedObject;
172   }
173   else{
174     aProfileObj = Handle(HYDROData_Profile)::DownCast( doc()->CreateObject( KIND_PROFILE ) );
175   }
176
177   if( aProfileObj.IsNull() )
178     return false;
179
180   aProfileObj->SetName(aProfileName);
181
182   HYDROData_ProfileUZ::PointsList aProfileParamPoints;
183
184   CurveCreator::Coordinates aCurveCoords = myProfile->getPoints( 0 );
185   for ( int k = 0 ; k + 1 < aCurveCoords.size() ; k++ )
186   {
187     HYDROData_ProfileUZ::Point aProfileParamPoint;
188
189     aProfileParamPoint.SetX( aCurveCoords.at( k ) );
190     k++;
191     aProfileParamPoint.SetY( aCurveCoords.at( k ) );
192
193     aProfileParamPoints.Append( aProfileParamPoint );
194   }
195   aProfileObj->SetParametricPoints( aProfileParamPoints );
196
197   if( !myIsEdit )
198     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aProfileObj, true );
199
200   theUpdateFlags = UF_Model;
201   return true;
202 }
203
204 void HYDROGUI_ProfileOp::displayPreview()
205 {
206   HYDROGUI_ProfileDlg* aPanel = dynamic_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
207   if( aPanel )
208   {
209     Handle(AIS_InteractiveContext) aCtx = aPanel->getAISContext();
210     if( !aCtx.IsNull() )
211     {
212       CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
213       myProfile->setDisplayer( aDisplayer );
214       aDisplayer->display( myProfile->constructWire(), true );
215     }
216   }
217 }
218
219 void HYDROGUI_ProfileOp::erasePreview()
220 {
221   HYDROGUI_ProfileDlg* aPanel = dynamic_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
222   CurveCreator_Displayer* aDisplayer = myProfile ? myProfile->getDisplayer() : 0;
223   if( aPanel && aDisplayer )
224   {
225     Handle(AIS_InteractiveContext) aCtx = aPanel->getAISContext();
226     if( !aCtx.IsNull() )
227     {
228       aDisplayer->erase( true );
229     }
230   }
231 }