Salome HOME
7dc18267d1a9e5973e7f6ec9e55733f1aa394e32
[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_Tool.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     myEditedObject = Handle(HYDROData_Polyline3D)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
65     if( !myEditedObject.IsNull() )
66       aPoly3DName = myEditedObject->GetName();
67   }
68   else
69   {
70     aPoly3DName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_POLYLINE_3D_NAME" ) );
71   }
72   aPanel->setResultName( aPoly3DName );
73
74   QString aPolyName, aProfileName, aBathName;
75   if( myIsEdit && !myEditedObject.IsNull() )
76   {
77     Handle(HYDROData_ProfileUZ) aProfileUZ = myEditedObject->GetProfileUZ();
78     if( !aProfileUZ.IsNull() )
79     {
80       Handle(HYDROData_Entity) aProfile = aProfileUZ->GetFatherObject();
81       if ( !aProfile.IsNull() )
82         aProfileName = aProfile->GetName();
83     }
84
85     Handle(HYDROData_Entity) aPoly = myEditedObject->GetPolylineXY();
86     if( !aPoly.IsNull() )
87       aPolyName = aPoly->GetName();
88
89     Handle(HYDROData_IAltitudeObject) anAltitudeObj = myEditedObject->GetAltitudeObject();
90     if( !anAltitudeObj.IsNull() )
91       aBathName = anAltitudeObj->GetName();
92
93     aPanel->setSelectedObjects( aPolyName, aProfileName, aBathName );
94   }
95   else if( !myIsEdit )
96   {
97     Handle(HYDROData_Profile) aSelectedProfile =
98       Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
99     if( !aSelectedProfile.IsNull() )
100     {
101       QString aSelectedName = aSelectedProfile->GetName();
102       aPanel->setPreselectedObject( aSelectedName );
103     }
104   }
105 }
106
107 bool HYDROGUI_Poly3DOp::processApply( int& theUpdateFlags,
108                                       QString& theErrorMsg,
109                                       QStringList& theBrowseObjectsEntries )
110 {
111   HYDROGUI_Poly3DDlg* aPanel = dynamic_cast<HYDROGUI_Poly3DDlg*>( inputPanel() );
112
113   QString aResultName = aPanel->getResultName();
114   if( aResultName.isEmpty() )
115     return false;
116
117   QString aPolyName, aProfileName, aBathName;
118   if( !aPanel->getSelectedObjects( aPolyName, aProfileName, aBathName ) )
119     return false;
120
121   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != aResultName ) )
122   {
123     // check that there are no other objects with the same name in the document
124     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), aResultName );
125     if( !anObject.IsNull() )
126     {
127       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( aResultName );
128       return false;
129     }
130   }
131
132   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( 
133     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
134   Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( 
135     HYDROGUI_Tool::FindObjectByName( module(), aPolyName, KIND_POLYLINEXY ) );
136   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( 
137     HYDROGUI_Tool::FindObjectByName( module(), aBathName, KIND_BATHYMETRY ) );
138
139   if ( aPolyline.IsNull() || ( aProfile.IsNull() && aBath.IsNull() ) )
140     return false;
141
142   Handle(HYDROData_Polyline3D) aResult;
143   if( myIsEdit )
144   {
145     aResult = myEditedObject;
146   }
147   else
148   {
149     aResult = Handle(HYDROData_Polyline3D)::DownCast( doc()->CreateObject( KIND_POLYLINE ) );
150     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aResult );
151     theBrowseObjectsEntries.append( anEntry );
152   }
153
154   if( aResult.IsNull() )
155     return false;
156
157   aResult->SetName( aResultName );
158
159   aResult->SetPolylineXY( aPolyline, false );
160   if ( !aProfile.IsNull() )
161   {
162     Handle(HYDROData_ProfileUZ) aProfileUZ = aProfile->GetProfileUZ();
163     if( aProfileUZ.IsNull() )
164       return false;
165     
166     aResult->SetProfileUZ( aProfileUZ );
167   }
168   else
169   {
170     aResult->SetAltitudeObject( aBath );
171   }
172
173   if( !myIsEdit )
174   {
175     aResult->SetBorderColor( HYDROData_Polyline3D::DefaultBorderColor() );
176   }
177
178   aResult->Update();
179
180   if( !myIsEdit )
181   {
182     size_t aViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
183     module()->setObjectVisible( aViewId, aPolyline, false );
184     module()->setObjectVisible( aViewId, aProfile, false );
185     module()->setObjectVisible( aViewId, aResult, true );
186   }
187   module()->setIsToUpdate( aResult );
188
189   theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
190   return true;
191 }