Salome HOME
lot 10 - warnings for DTM - untested
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ChannelOp.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_ChannelOp.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include <HYDROGUI_DataObject.h>
23 #include "HYDROGUI_ChannelDlg.h"
24 #include "HYDROGUI_Module.h"
25 #include "HYDROGUI_Shape.h"
26 #include "HYDROGUI_Tool.h"
27 #include "HYDROGUI_Tool2.h"
28 #include "HYDROGUI_UpdateFlags.h"
29
30 #include <HYDROData_Iterator.h>
31 #include <HYDROData_Polyline3D.h>
32 #include <HYDROData_Profile.h>
33
34 #include <OCCViewer_ViewManager.h>
35 #include <OCCViewer_ViewModel.h>
36
37 #include <LightApp_Application.h>
38 #include <LightApp_UpdateFlags.h>
39 #include <HYDROData_ChannelAltitude.h>
40
41 #include <TopoDS.hxx>
42
43 HYDROGUI_ChannelOp::HYDROGUI_ChannelOp( HYDROGUI_Module* theModule,
44                                        const bool theIsEdit )
45 : HYDROGUI_Operation( theModule ),
46   myIsEdit( theIsEdit ),
47   myPreviewPrs( 0 )
48 {
49   setName( theIsEdit ? tr( "EDIT_CHANNEL" ) : tr( "CREATE_CHANNEL" ) );
50 }
51
52 HYDROGUI_ChannelOp::~HYDROGUI_ChannelOp()
53 {
54   erasePreview();
55 }
56
57 void HYDROGUI_ChannelOp::startOperation()
58 {
59   HYDROGUI_Operation::startOperation();
60
61   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
62
63   aPanel->blockSignals( true );
64
65   aPanel->reset();
66
67   if ( isApplyAndClose() )
68     myEditedObject.Nullify();
69
70   QString aSelectedGuideLine, aSelectedProfile;
71
72   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
73   if ( myIsEdit )
74   {
75     if ( isApplyAndClose() )
76       myEditedObject =
77         Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
78     if ( !myEditedObject.IsNull() )
79     {
80       anObjectName = myEditedObject->GetName();
81
82       Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
83       if ( !aRefGuideLine.IsNull() )
84         aSelectedGuideLine = aRefGuideLine->GetName();
85
86       Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
87       if ( !aRefProfile.IsNull() )
88         aSelectedProfile = aRefProfile->GetName();
89     }
90   }
91
92   // collect information about existing 3d polylines
93   QStringList aGuideLines;
94   HYDROData_Iterator aPolylinesIt( doc(), KIND_POLYLINE );
95   for ( ; aPolylinesIt.More(); aPolylinesIt.Next() )
96   {
97     Handle(HYDROData_Polyline3D) aPolyline3d = 
98       Handle(HYDROData_Polyline3D)::DownCast( aPolylinesIt.Current() );
99     if( !aPolyline3d.IsNull() )
100     {
101       TopoDS_Shape aShape = aPolyline3d->GetShape3D();
102       if( aShape.ShapeType()==TopAbs_WIRE )
103       {
104         TopoDS_Wire aWire = TopoDS::Wire( aShape );
105         if( !aWire.Closed() )
106           aGuideLines.append( aPolyline3d->GetName() );
107       }
108     }
109   }
110
111   // collect information about existing profiles
112   QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE, false );
113
114   aPanel->setObjectName( anObjectName );
115
116   aPanel->setGuideLineNames( aGuideLines );
117   aPanel->setProfileNames( aProfiles );
118
119   aPanel->setGuideLineName( aSelectedGuideLine );
120   aPanel->setProfileName( aSelectedProfile );
121
122   if( !myEditedObject.IsNull() )
123     aPanel->setEquiDistance( myEditedObject->GetEquiDistance() );
124   else
125     aPanel->setEquiDistance( 1.0 );
126
127   if( !myEditedObject.IsNull() )
128   {
129     bool invDirection = false;
130     Handle(HYDROData_IAltitudeObject) anObjAltitude = myEditedObject->GetAltitudeObject();
131     Handle(HYDROData_ChannelAltitude) aChannelAlt = Handle(HYDROData_ChannelAltitude)::DownCast(anObjAltitude);
132     if (!aChannelAlt.IsNull())
133       invDirection = aChannelAlt->GetInvertDirection();
134     aPanel->setInvertDirection( invDirection );
135   }
136   else
137     aPanel->setInvertDirection( false );
138
139   aPanel->blockSignals( false );
140
141   onCreatePreview();
142 }
143
144 void HYDROGUI_ChannelOp::abortOperation()
145 {
146   erasePreview();
147   HYDROGUI_Operation::abortOperation();
148 }
149
150 void HYDROGUI_ChannelOp::commitOperation()
151 {
152   erasePreview();
153   HYDROGUI_Operation::commitOperation();
154 }
155
156 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
157 {
158   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
159   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
160   return aPanel;
161 }
162
163 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
164                                        QString& theErrorMsg,
165                                        QStringList& theBrowseObjectsEntries )
166 {
167   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
168   if ( !aPanel )
169     return false;
170
171   QString anObjectName = aPanel->getObjectName().simplified();
172   if ( anObjectName.isEmpty() )
173   {
174     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
175     return false;
176   }
177
178   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
179   {
180     // check that there are no other objects with the same name in the document
181     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
182     if( !anObject.IsNull() )
183     {
184       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
185       return false;
186     }
187   }
188
189   
190   if ( myEditedObject.IsNull() )
191     myEditedObject = createNewObject(); // Create new data model object
192
193   myEditedObject->SetName( anObjectName );
194
195   if ( !myIsEdit )
196   {
197     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
198     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
199   }
200
201   QString aGuideLineName = aPanel->getGuideLineName();
202   QString aProfileName = aPanel->getProfileName();
203   /*if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
204   {
205     myEditedObject->RemoveGuideLine();
206     myEditedObject->RemoveProfile();
207   }
208   else*/
209   {
210     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
211       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
212     if ( aGuideLine.IsNull() )
213     {
214       theErrorMsg = tr( "GUIDE_LINE_IS_NOT_SELECTED" );
215       return false;
216     }
217
218     myEditedObject->RemoveGuideLine();
219     myEditedObject->SetGuideLine( aGuideLine );
220
221     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
222       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
223     if ( aProfile.IsNull() )
224     {
225       theErrorMsg = tr( "PROFILE_IS_NOT_SELECTED" );
226       return false;
227     }
228
229     myEditedObject->RemoveProfile();
230     myEditedObject->SetProfile( aProfile );
231     myEditedObject->SetEquiDistance( aPanel->getEquiDistance() );
232     ///
233     Handle(HYDROData_IAltitudeObject) anObjAltitude = myEditedObject->GetAltitudeObject();
234     Handle(HYDROData_ChannelAltitude) aChannelAlt = Handle(HYDROData_ChannelAltitude)::DownCast(anObjAltitude);
235     if (!aChannelAlt.IsNull())
236       aChannelAlt->SetInvertDirection(aPanel->getInvertDirection());
237   }
238
239   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
240     myEditedObject->Update();
241
242   erasePreview();
243
244   if( !myIsEdit )
245   {
246     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
247     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
248     theBrowseObjectsEntries.append( anEntry );
249   }
250
251   module()->setIsToUpdate( myEditedObject );
252
253   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
254
255   return true;
256 }
257
258 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
259 {
260   return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
261 }
262
263 void HYDROGUI_ChannelOp::onCreatePreview()
264 {
265   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
266   if ( !aPanel )
267     return;
268
269   QString aGuideLineName = aPanel->getGuideLineName();
270   QString aProfileName = aPanel->getProfileName();
271   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
272   {
273     erasePreview();
274     return;
275   }
276
277   LightApp_Application* anApp = module()->getApp();
278   
279   if ( !getPreviewManager() )
280   {
281     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
282                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
283   }
284
285   OCCViewer_ViewManager* aViewManager = getPreviewManager();
286   if ( aViewManager && !myPreviewPrs )
287   {
288     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
289     {
290       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
291       if ( !aCtx.IsNull() )
292       {
293         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
294
295         QColor aFillingColor = Qt::blue;
296         QColor aBorderColor = Qt::transparent;
297         if ( !myEditedObject.IsNull() )
298         {
299           aFillingColor = myEditedObject->GetFillingColor();
300           aBorderColor = myEditedObject->GetBorderColor();
301         }
302
303         myPreviewPrs->setFillingColor( aFillingColor, false, false );
304         myPreviewPrs->setBorderColor( aBorderColor, false, false );
305       }
306     }
307   }
308
309   if ( !aViewManager || !myPreviewPrs )
310     return;
311
312   Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
313     HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
314
315   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
316     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
317
318   HYDROData_Channel::PrsDefinition aPrsDef;
319   if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef, aPanel->getEquiDistance() ) )
320   {
321     erasePreview();
322     return;
323   }
324
325   myPreviewPrs->setShape( aPrsDef.myPrs2D );
326 }
327
328 void HYDROGUI_ChannelOp::erasePreview()
329 {
330   if( myPreviewPrs )
331   {
332     delete myPreviewPrs;
333     myPreviewPrs = 0;
334   }
335 }