Salome HOME
linear stream interpolator : insertPoints() : deny to add more points than needed
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Poly3DOp.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_Poly3DOp.h"
20
21 #include "HYDROGUI_Module.h"
22 #include <HYDROGUI_DataObject.h>
23 #include "HYDROGUI_Tool2.h"
24 #include "HYDROGUI_Poly3DDlg.h"
25 #include "HYDROGUI_UpdateFlags.h"
26
27 #include <HYDROData_Document.h>
28 #include <HYDROData_Polyline3D.h>
29 #include <HYDROData_Profile.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Bathymetry.h>
32
33 #include <HYDROData_OperationsFactory.h>
34
35 HYDROGUI_Poly3DOp::HYDROGUI_Poly3DOp( HYDROGUI_Module* theModule,
36                                             const bool theIsEdit )
37 : HYDROGUI_Operation( theModule ),
38   myIsEdit( theIsEdit ),
39   myEditedObject( 0 )
40 {
41   setName( theIsEdit ? tr( "EDIT_POLYLINE_3D" ) : tr( "CREATE_POLYLINE_3D" ) );
42 }
43
44 HYDROGUI_Poly3DOp::~HYDROGUI_Poly3DOp()
45 {
46 }
47
48 HYDROGUI_InputPanel* HYDROGUI_Poly3DOp::createInputPanel() const
49 {
50   return new HYDROGUI_Poly3DDlg( module(), getName() );
51 }
52
53 void HYDROGUI_Poly3DOp::startOperation()
54 {
55   HYDROGUI_Operation::startOperation();
56
57   HYDROGUI_Poly3DDlg* aPanel = (HYDROGUI_Poly3DDlg*)inputPanel();
58   aPanel->reset();
59   aPanel->setMode( myIsEdit );
60
61   QString aPoly3DName;
62   if( myIsEdit )
63   {
64     if ( isApplyAndClose() )
65       myEditedObject = Handle(HYDROData_Polyline3D)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
66     if( !myEditedObject.IsNull() )
67       aPoly3DName = myEditedObject->GetName();
68   }
69   else
70   {
71     aPoly3DName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_3D_NAME" ) );
72   }
73   aPanel->setResultName( aPoly3DName );
74
75   QString aPolyName, aProfileName, aBathName;
76   if( myIsEdit && !myEditedObject.IsNull() )
77   {
78     Handle(HYDROData_ProfileUZ) aProfileUZ = myEditedObject->GetProfileUZ();
79     if( !aProfileUZ.IsNull() )
80     {
81       Handle(HYDROData_Entity) aProfile = aProfileUZ->GetFatherObject();
82       if ( !aProfile.IsNull() )
83         aProfileName = aProfile->GetName();
84     }
85
86     Handle(HYDROData_Entity) aPoly = myEditedObject->GetPolylineXY();
87     if( !aPoly.IsNull() )
88       aPolyName = aPoly->GetName();
89
90     Handle(HYDROData_IAltitudeObject) anAltitudeObj = myEditedObject->GetAltitudeObject();
91     if( !anAltitudeObj.IsNull() )
92       aBathName = anAltitudeObj->GetName();
93
94     aPanel->setSelectedObjects( aPolyName, aProfileName, aBathName );
95   }
96   else if( !myIsEdit )
97   {
98     Handle(HYDROData_Profile) aSelectedProfile =
99       Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
100     if( !aSelectedProfile.IsNull() )
101     {
102       QString aSelectedName = aSelectedProfile->GetName();
103       aPanel->setPreselectedObject( aSelectedName );
104     }
105   }
106 }
107
108 bool HYDROGUI_Poly3DOp::processApply( int& theUpdateFlags,
109                                       QString& theErrorMsg,
110                                       QStringList& theBrowseObjectsEntries )
111 {
112   HYDROGUI_Poly3DDlg* aPanel = dynamic_cast<HYDROGUI_Poly3DDlg*>( inputPanel() );
113
114   QString aResultName = aPanel->getResultName();
115   if( aResultName.isEmpty() )
116     return false;
117
118   QString aPolyName, aProfileName, aBathName;
119   if( !aPanel->getSelectedObjects( aPolyName, aProfileName, aBathName ) )
120     return false;
121
122   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aResultName ) )
123   {
124     // check that there are no other objects with the same name in the document
125     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aResultName );
126     if( !anObject.IsNull() )
127     {
128       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aResultName );
129       return false;
130     }
131   }
132
133   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( 
134     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
135   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( 
136     HYDROGUI_Tool::FindObjectByName( module(), aPolyName, KIND_POLYLINEXY ) );
137   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( 
138     HYDROGUI_Tool::FindObjectByName( module(), aBathName, KIND_BATHYMETRY ) );
139
140   if ( aPolyline.IsNull() || ( aProfile.IsNull() && aBath.IsNull() ) )
141     return false;
142
143   Handle(HYDROData_Polyline3D) aResult;
144   if( myIsEdit )
145   {
146     aResult = myEditedObject;
147   }
148   else
149   {
150     aResult = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
151     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aResult );
152     theBrowseObjectsEntries.append( anEntry );
153   }
154
155   if( aResult.IsNull() )
156     return false;
157
158   aResult->SetName( aResultName );
159
160   aResult->SetPolylineXY( aPolyline, false );
161   if ( !aProfile.IsNull() )
162   {
163     Handle(HYDROData_ProfileUZ) aProfileUZ = aProfile->GetProfileUZ();
164     if( aProfileUZ.IsNull() )
165       return false;
166     
167     aResult->SetProfileUZ( aProfileUZ );
168   }
169   else
170   {
171     aResult->SetAltitudeObject( aBath );
172   }
173
174   if( !myIsEdit )
175   {
176     aResult->SetBorderColor( aResult->DefaultBorderColor() );
177   }
178
179   aResult->Update();
180
181   if( !myIsEdit )
182   {
183     size_t aViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
184     module()->setObjectVisible( aViewId, aPolyline, false );
185     module()->setObjectVisible( aViewId, aProfile, false );
186     module()->setObjectVisible( aViewId, aResult, true );
187   }
188   module()->setIsToUpdate( aResult );
189
190   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
191   return true;
192 }