Salome HOME
Corrections of examples path after install with scbi
[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   bool profile_mode = true;
72
73
74   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
75   if ( myIsEdit )
76   {
77     if ( isApplyAndClose() )
78       myEditedObject =
79         Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
80     if ( !myEditedObject.IsNull() )
81     {
82       anObjectName = myEditedObject->GetName();
83
84       Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
85       if ( !aRefGuideLine.IsNull() )
86         aSelectedGuideLine = aRefGuideLine->GetName();
87
88       Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
89       if ( !aRefProfile.IsNull() )
90         aSelectedProfile = aRefProfile->GetName();
91
92       profile_mode = myEditedObject->GetProfileMode();
93     }
94   }
95
96   // collect information about existing 3d polylines
97   QStringList aGuideLines;
98   HYDROData_Iterator aPolylinesIt( doc(), KIND_POLYLINE );
99   for ( ; aPolylinesIt.More(); aPolylinesIt.Next() )
100   {
101     Handle(HYDROData_Polyline3D) aPolyline3d = 
102       Handle(HYDROData_Polyline3D)::DownCast( aPolylinesIt.Current() );
103     if( !aPolyline3d.IsNull() )
104     {
105       TopoDS_Shape aShape = aPolyline3d->GetShape3D();
106       if( aShape.ShapeType()==TopAbs_WIRE )
107       {
108         TopoDS_Wire aWire = TopoDS::Wire( aShape );
109         if( !aWire.Closed() )
110           aGuideLines.append( aPolyline3d->GetName() );
111       }
112     }
113   }
114
115   // collect information about existing profiles
116   QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE, false );
117
118   aPanel->setObjectName( anObjectName );
119
120   aPanel->setGuideLineNames( aGuideLines );
121   aPanel->setProfileNames( aProfiles );
122
123   aPanel->setGuideLineName( aSelectedGuideLine );
124   aPanel->setProfileName( aSelectedProfile );
125
126   if( !myEditedObject.IsNull() )
127   {
128     aPanel->setEquiDistance( myEditedObject->GetEquiDistance() );
129     if (!profile_mode) //predef
130     {
131       aPanel->setLCValue( myEditedObject->GetLCValue() );
132       aPanel->setCoteZValue( myEditedObject->GetCoteZValue() );
133       aPanel->setDeltaZValue( myEditedObject->GetDeltaZValue() );
134     }
135   }
136   else
137     aPanel->setEquiDistance( 1.0 );
138
139   if( !myEditedObject.IsNull() )
140   {
141     bool invDirection = false;
142     Handle(HYDROData_IAltitudeObject) anObjAltitude = myEditedObject->GetAltitudeObject();
143     Handle(HYDROData_ChannelAltitude) aChannelAlt = Handle(HYDROData_ChannelAltitude)::DownCast(anObjAltitude);
144     if (!aChannelAlt.IsNull())
145       invDirection = aChannelAlt->GetInvertDirection();
146     aPanel->setInvertDirection( invDirection );
147   }
148   else
149     aPanel->setInvertDirection( false );
150
151   aPanel->blockSignals( false );
152   aPanel->setProfileMode(profile_mode);
153
154   onCreatePreview();
155 }
156
157 void HYDROGUI_ChannelOp::abortOperation()
158 {
159   erasePreview();
160   HYDROGUI_Operation::abortOperation();
161 }
162
163 void HYDROGUI_ChannelOp::commitOperation()
164 {
165   erasePreview();
166   HYDROGUI_Operation::commitOperation();
167 }
168
169 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
170 {
171   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
172   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
173   return aPanel;
174 }
175
176 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
177                                        QString& theErrorMsg,
178                                        QStringList& theBrowseObjectsEntries )
179 {
180   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
181   if ( !aPanel )
182     return false;
183
184   QString anObjectName = aPanel->getObjectName().simplified();
185   if ( anObjectName.isEmpty() )
186   {
187     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
188     return false;
189   }
190
191   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
192   {
193     // check that there are no other objects with the same name in the document
194     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
195     if( !anObject.IsNull() )
196     {
197       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
198       return false;
199     }
200   }
201
202   
203   if ( myEditedObject.IsNull() )
204     myEditedObject = createNewObject(); // Create new data model object
205
206   myEditedObject->SetName( anObjectName );
207
208   if ( !myIsEdit )
209   {
210     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
211     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
212   }
213
214   QString aGuideLineName = aPanel->getGuideLineName();
215
216   bool profileMode = aPanel->getProfileMode();
217   QString aProfileName;
218   double lc=0, deltaz=0, cotez=0;
219   
220   if (profileMode)
221   {
222     aProfileName = aPanel->getProfileName();
223   }
224   else
225   {
226     lc = aPanel->getLCValue();
227     deltaz = aPanel->getDeltaZValue();
228     cotez = aPanel->getCoteZValue();
229   }
230
231
232   /*if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
233   {
234     myEditedObject->RemoveGuideLine();
235     myEditedObject->RemoveProfile();
236   }
237   else*/
238   {
239     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
240       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
241     if ( aGuideLine.IsNull() )
242     {
243       theErrorMsg = tr( "GUIDE_LINE_IS_NOT_SELECTED" );
244       return false;
245     }
246
247     myEditedObject->RemoveGuideLine();
248     myEditedObject->SetGuideLine( aGuideLine );
249
250     myEditedObject->SetProfileMode(profileMode);
251
252     if (profileMode)
253     {
254       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
255         HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
256       if ( aProfile.IsNull() )
257       {
258         theErrorMsg = tr( "PROFILE_IS_NOT_SELECTED" );
259         return false;
260       }
261       myEditedObject->RemoveProfile();
262       myEditedObject->SetProfile( aProfile );
263       myEditedObject->SetLCValue(0);
264       myEditedObject->SetDeltaZValue(0);
265       myEditedObject->SetCoteZValue(0);
266     }
267     else
268     {
269       myEditedObject->RemoveProfile();
270       myEditedObject->SetLCValue(lc);
271       myEditedObject->SetDeltaZValue(deltaz);
272       myEditedObject->SetCoteZValue(cotez);
273     }
274
275     myEditedObject->SetEquiDistance( aPanel->getEquiDistance() );
276     ///
277     Handle(HYDROData_IAltitudeObject) anObjAltitude = myEditedObject->GetAltitudeObject();
278     Handle(HYDROData_ChannelAltitude) aChannelAlt = Handle(HYDROData_ChannelAltitude)::DownCast(anObjAltitude);
279     if (!aChannelAlt.IsNull())
280       aChannelAlt->SetInvertDirection(aPanel->getInvertDirection());
281   }
282
283   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
284     myEditedObject->Update();
285
286   erasePreview();
287
288   if( !myIsEdit )
289   {
290     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
291     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
292     theBrowseObjectsEntries.append( anEntry );
293   }
294
295   module()->setIsToUpdate( myEditedObject );
296
297   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
298
299   return true;
300 }
301
302 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
303 {
304   return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
305 }
306
307 void HYDROGUI_ChannelOp::onCreatePreview()
308 {
309   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
310   if ( !aPanel )
311     return;
312
313   QString aGuideLineName = aPanel->getGuideLineName();
314   QString aProfileName = aPanel->getProfileName();
315   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
316   {
317     erasePreview();
318     return;
319   }
320
321   LightApp_Application* anApp = module()->getApp();
322   
323   if ( !getPreviewManager() )
324   {
325     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
326                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
327   }
328
329   OCCViewer_ViewManager* aViewManager = getPreviewManager();
330   if ( aViewManager && !myPreviewPrs )
331   {
332     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
333     {
334       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
335       if ( !aCtx.IsNull() )
336       {
337         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
338
339         QColor aFillingColor = Qt::blue;
340         QColor aBorderColor = Qt::transparent;
341         if ( !myEditedObject.IsNull() )
342         {
343           aFillingColor = myEditedObject->GetFillingColor();
344           aBorderColor = myEditedObject->GetBorderColor();
345         }
346
347         myPreviewPrs->setFillingColor( aFillingColor, false, false );
348         myPreviewPrs->setBorderColor( aBorderColor, false, false );
349       }
350     }
351   }
352
353   if ( !aViewManager || !myPreviewPrs )
354     return;
355
356   Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
357     HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
358
359   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
360     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
361
362   bool aProfMode = aPanel->getProfileMode();
363   HYDROData_Channel::PrsDefinition aPrsDef;
364   bool stat = false;
365   double eqDist = aPanel->getEquiDistance();
366   if (aProfMode)
367     stat = HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef, eqDist);
368   else
369   {
370     double lc = aPanel->getLCValue();
371     double deltaz = aPanel->getDeltaZValue();
372     double cotez = aPanel->getCoteZValue();
373     stat = HYDROData_Channel::CreatePresentations( aGuideLine, lc, deltaz, cotez, aPrsDef, eqDist);
374   }
375   if ( !stat  )
376   {
377     erasePreview();
378     return;
379   }
380
381   myPreviewPrs->setShape( aPrsDef.myPrs2D );
382 }
383
384 void HYDROGUI_ChannelOp::erasePreview()
385 {
386   if( myPreviewPrs )
387   {
388     delete myPreviewPrs;
389     myPreviewPrs = 0;
390   }
391 }