]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_ChannelOp.cxx
Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[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
203   bool profileMode = aPanel->getProfileMode();
204   QString aProfileName;
205   double lc=0, deltaz=0, cotez=0;
206   
207   if (profileMode)
208   {
209     aProfileName = aPanel->getProfileName();
210   }
211   else
212   {
213     lc = aPanel->getLCValue();
214     deltaz = aPanel->getDeltaZValue();
215     cotez = aPanel->getCoteZValue();
216   }
217
218
219   /*if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
220   {
221     myEditedObject->RemoveGuideLine();
222     myEditedObject->RemoveProfile();
223   }
224   else*/
225   {
226     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
227       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
228     if ( aGuideLine.IsNull() )
229     {
230       theErrorMsg = tr( "GUIDE_LINE_IS_NOT_SELECTED" );
231       return false;
232     }
233
234     myEditedObject->RemoveGuideLine();
235     myEditedObject->SetGuideLine( aGuideLine );
236
237     myEditedObject->SetProfileMode(profileMode);
238
239     if (profileMode)
240     {
241       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
242         HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
243       if ( aProfile.IsNull() )
244       {
245         theErrorMsg = tr( "PROFILE_IS_NOT_SELECTED" );
246         return false;
247       }
248       myEditedObject->RemoveProfile();
249       myEditedObject->SetProfile( aProfile );
250       myEditedObject->SetLCValue(0);
251       myEditedObject->SetDeltaZValue(0);
252       myEditedObject->SetCoteZValue(0);
253     }
254     else
255     {
256       myEditedObject->RemoveProfile();
257       myEditedObject->SetLCValue(lc);
258       myEditedObject->SetDeltaZValue(deltaz);
259       myEditedObject->SetCoteZValue(cotez);
260     }
261
262     myEditedObject->SetEquiDistance( aPanel->getEquiDistance() );
263     ///
264     Handle(HYDROData_IAltitudeObject) anObjAltitude = myEditedObject->GetAltitudeObject();
265     Handle(HYDROData_ChannelAltitude) aChannelAlt = Handle(HYDROData_ChannelAltitude)::DownCast(anObjAltitude);
266     if (!aChannelAlt.IsNull())
267       aChannelAlt->SetInvertDirection(aPanel->getInvertDirection());
268   }
269
270   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
271     myEditedObject->Update();
272
273   erasePreview();
274
275   if( !myIsEdit )
276   {
277     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
278     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
279     theBrowseObjectsEntries.append( anEntry );
280   }
281
282   module()->setIsToUpdate( myEditedObject );
283
284   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
285
286   return true;
287 }
288
289 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
290 {
291   return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
292 }
293
294 void HYDROGUI_ChannelOp::onCreatePreview()
295 {
296   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
297   if ( !aPanel )
298     return;
299
300   QString aGuideLineName = aPanel->getGuideLineName();
301   QString aProfileName = aPanel->getProfileName();
302   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
303   {
304     erasePreview();
305     return;
306   }
307
308   LightApp_Application* anApp = module()->getApp();
309   
310   if ( !getPreviewManager() )
311   {
312     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
313                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
314   }
315
316   OCCViewer_ViewManager* aViewManager = getPreviewManager();
317   if ( aViewManager && !myPreviewPrs )
318   {
319     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
320     {
321       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
322       if ( !aCtx.IsNull() )
323       {
324         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
325
326         QColor aFillingColor = Qt::blue;
327         QColor aBorderColor = Qt::transparent;
328         if ( !myEditedObject.IsNull() )
329         {
330           aFillingColor = myEditedObject->GetFillingColor();
331           aBorderColor = myEditedObject->GetBorderColor();
332         }
333
334         myPreviewPrs->setFillingColor( aFillingColor, false, false );
335         myPreviewPrs->setBorderColor( aBorderColor, false, false );
336       }
337     }
338   }
339
340   if ( !aViewManager || !myPreviewPrs )
341     return;
342
343   Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
344     HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
345
346   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
347     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
348
349   bool aProfMode = aPanel->getProfileMode();
350   HYDROData_Channel::PrsDefinition aPrsDef;
351   bool stat = false;
352   double eqDist = aPanel->getEquiDistance();
353   if (aProfMode)
354     stat = HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef, eqDist);
355   else
356   {
357     double lc = aPanel->getLCValue();
358     double deltaz = aPanel->getDeltaZValue();
359     double cotez = aPanel->getCoteZValue();
360     stat = HYDROData_Channel::CreatePresentations( aGuideLine, lc, deltaz, cotez, aPrsDef, eqDist);
361   }
362   if ( !stat  )
363   {
364     erasePreview();
365     return;
366   }
367
368   myPreviewPrs->setShape( aPrsDef.myPrs2D );
369 }
370
371 void HYDROGUI_ChannelOp::erasePreview()
372 {
373   if( myPreviewPrs )
374   {
375     delete myPreviewPrs;
376     myPreviewPrs = 0;
377   }
378 }