Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[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   aPanel->blockSignals( false );
122
123   onCreatePreview();
124 }
125
126 void HYDROGUI_ChannelOp::abortOperation()
127 {
128   erasePreview();
129   HYDROGUI_Operation::abortOperation();
130 }
131
132 void HYDROGUI_ChannelOp::commitOperation()
133 {
134   erasePreview();
135   HYDROGUI_Operation::commitOperation();
136 }
137
138 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
139 {
140   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
141   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
142   return aPanel;
143 }
144
145 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
146                                        QString& theErrorMsg,
147                                        QStringList& theBrowseObjectsEntries )
148 {
149   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
150   if ( !aPanel )
151     return false;
152
153   QString anObjectName = aPanel->getObjectName().simplified();
154   if ( anObjectName.isEmpty() )
155   {
156     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
157     return false;
158   }
159
160   if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
161   {
162     // check that there are no other objects with the same name in the document
163     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
164     if( !anObject.IsNull() )
165     {
166       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
167       return false;
168     }
169   }
170
171   
172   if ( myEditedObject.IsNull() )
173     myEditedObject = createNewObject(); // Create new data model object
174
175   myEditedObject->SetName( anObjectName );
176
177   if ( !myIsEdit )
178   {
179     myEditedObject->SetFillingColor( myEditedObject->DefaultFillingColor() );
180     myEditedObject->SetBorderColor( myEditedObject->DefaultBorderColor() );
181   }
182
183   QString aGuideLineName = aPanel->getGuideLineName();
184   QString aProfileName = aPanel->getProfileName();
185   /*if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
186   {
187     myEditedObject->RemoveGuideLine();
188     myEditedObject->RemoveProfile();
189   }
190   else*/
191   {
192     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
193       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
194     if ( aGuideLine.IsNull() )
195     {
196       theErrorMsg = tr( "GUIDE_LINE_IS_NOT_SELECTED" );
197       return false;
198     }
199
200     myEditedObject->RemoveGuideLine();
201     myEditedObject->SetGuideLine( aGuideLine );
202
203     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
204       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
205     if ( aProfile.IsNull() )
206     {
207       theErrorMsg = tr( "PROFILE_IS_NOT_SELECTED" );
208       return false;
209     }
210
211     myEditedObject->RemoveProfile();
212     myEditedObject->SetProfile( aProfile );
213   }
214
215   if ( myEditedObject->IsMustBeUpdated( HYDROData_Entity::Geom_2d ) )
216     myEditedObject->Update();
217
218   erasePreview();
219
220   if( !myIsEdit )
221   {
222     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
223     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
224     theBrowseObjectsEntries.append( anEntry );
225   }
226
227   module()->setIsToUpdate( myEditedObject );
228
229   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
230
231   return true;
232 }
233
234 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
235 {
236   return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
237 }
238
239 void HYDROGUI_ChannelOp::onCreatePreview()
240 {
241   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
242   if ( !aPanel )
243     return;
244
245   QString aGuideLineName = aPanel->getGuideLineName();
246   QString aProfileName = aPanel->getProfileName();
247   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
248   {
249     erasePreview();
250     return;
251   }
252
253   LightApp_Application* anApp = module()->getApp();
254   
255   if ( !getPreviewManager() )
256   {
257     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
258                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
259   }
260
261   OCCViewer_ViewManager* aViewManager = getPreviewManager();
262   if ( aViewManager && !myPreviewPrs )
263   {
264     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
265     {
266       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
267       if ( !aCtx.IsNull() )
268       {
269         myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
270
271         QColor aFillingColor = Qt::blue;
272         QColor aBorderColor = Qt::transparent;
273         if ( !myEditedObject.IsNull() )
274         {
275           aFillingColor = myEditedObject->GetFillingColor();
276           aBorderColor = myEditedObject->GetBorderColor();
277         }
278
279         myPreviewPrs->setFillingColor( aFillingColor, false, false );
280         myPreviewPrs->setBorderColor( aBorderColor, false, false );
281       }
282     }
283   }
284
285   if ( !aViewManager || !myPreviewPrs )
286     return;
287
288   Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
289     HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
290
291   Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
292     HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
293
294   HYDROData_Channel::PrsDefinition aPrsDef;
295   if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef ) )
296   {
297     erasePreview();
298     return;
299   }
300
301   myPreviewPrs->setShape( aPrsDef.myPrs2D );
302 }
303
304 void HYDROGUI_ChannelOp::erasePreview()
305 {
306   if( myPreviewPrs )
307   {
308     delete myPreviewPrs;
309     myPreviewPrs = 0;
310   }
311 }