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