Salome HOME
#refs 327 - Polyline is not shown during creation
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ChannelOp.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ChannelOp.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_ChannelDlg.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Shape.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Iterator.h>
33 #include <HYDROData_Polyline3D.h>
34 #include <HYDROData_Profile.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
41
42 #include <TopoDS.hxx>
43
44 HYDROGUI_ChannelOp::HYDROGUI_ChannelOp( HYDROGUI_Module* theModule,
45                                        const bool theIsEdit )
46 : HYDROGUI_Operation( theModule ),
47   myIsEdit( theIsEdit ),
48   myPreviewPrs( 0 )
49 {
50   setName( theIsEdit ? tr( "EDIT_CHANNEL" ) : tr( "CREATE_CHANNEL" ) );
51 }
52
53 HYDROGUI_ChannelOp::~HYDROGUI_ChannelOp()
54 {
55   erasePreview();
56 }
57
58 void HYDROGUI_ChannelOp::startOperation()
59 {
60   HYDROGUI_Operation::startOperation();
61
62   // We start operation in the document
63   startDocOperation();
64
65   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
66
67   aPanel->blockSignals( true );
68
69   aPanel->reset();
70
71   myEditedObject = getObjectToEdit();
72
73   QString aSelectedGuideLine, aSelectedProfile;
74
75   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
76   if ( myIsEdit && !myEditedObject.IsNull() )
77   {
78     anObjectName = myEditedObject->GetName();
79
80     Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
81     if ( !aRefGuideLine.IsNull() )
82       aSelectedGuideLine = aRefGuideLine->GetName();
83
84     Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
85     if ( !aRefProfile.IsNull() )
86       aSelectedProfile = aRefProfile->GetName();
87   }
88
89   // collect information about existing 3d polylines
90   QStringList aGuideLines;
91   QStringList anAllGuideLines = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINE );
92   foreach( QString aGuideLine, anAllGuideLines )
93   {
94     Handle( HYDROData_Polyline3D ) aPolyline3d = 
95       Handle( HYDROData_Polyline3D )::DownCast( 
96     HYDROGUI_Tool::FindObjectByName( module(), aGuideLine, KIND_POLYLINE ) );
97     if( !aPolyline3d.IsNull() )
98     {
99       TopoDS_Shape aShape = aPolyline3d->GetShape3D();
100       if( aShape.ShapeType()==TopAbs_WIRE )
101       {
102         TopoDS_Wire aWire = TopoDS::Wire( aShape );
103         if( !aWire.Closed() )
104           aGuideLines.append( aGuideLine );
105       }
106     }
107   }
108
109   // collect information about existing profiles
110   QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE );
111
112   aPanel->setObjectName( anObjectName );
113
114   aPanel->setGuideLineNames( aGuideLines );
115   aPanel->setProfileNames( aProfiles );
116
117   aPanel->setGuideLineName( aSelectedGuideLine );
118   aPanel->setProfileName( aSelectedProfile );
119
120   aPanel->blockSignals( false );
121
122   onCreatePreview( true );
123 }
124
125
126 void HYDROGUI_ChannelOp::abortOperation()
127 {
128   erasePreview();
129   abortDocOperation();
130
131   HYDROGUI_Operation::abortOperation();
132 }
133
134 void HYDROGUI_ChannelOp::commitOperation()
135 {
136   erasePreview();
137
138   HYDROGUI_Operation::commitOperation();
139 }
140
141 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
142 {
143   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
144   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
145   return aPanel;
146 }
147
148 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
149                                        QString& theErrorMsg )
150 {
151   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
152   if ( !aPanel )
153     return false;
154
155   QString anObjectName = aPanel->getObjectName().simplified();
156   if ( anObjectName.isEmpty() )
157   {
158     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
159     return false;
160   }
161
162   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
163   {
164     // check that there are no other objects with the same name in the document
165     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
166     if( !anObject.IsNull() )
167     {
168       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
169       return false;
170     }
171   }
172
173   if ( myEditedObject.IsNull() )
174     return false;
175
176   myEditedObject->SetName( anObjectName );
177
178   if ( !myIsEdit )
179   {
180     myEditedObject->SetFillingColor( HYDROData_Channel::DefaultFillingColor() );
181     myEditedObject->SetBorderColor( HYDROData_Channel::DefaultBorderColor() );
182   }
183
184   erasePreview();
185
186   if( !myIsEdit )
187     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
188
189   module()->setIsToUpdate( myEditedObject );
190
191   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
192
193   return true;
194 }
195
196 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::getObjectToEdit() const
197 {
198   return myIsEdit ? Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) ) :
199                     Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
200 }
201
202 void HYDROGUI_ChannelOp::onCreatePreview( const bool theIsInit )
203 {
204   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
205   if ( !aPanel || myEditedObject.IsNull() )
206     return;
207
208   QString aGuideLineName = aPanel->getGuideLineName();
209   QString aProfileName = aPanel->getProfileName();
210   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
211   {
212     if ( !theIsInit )
213     {
214       myEditedObject->RemoveGuideLine();
215       myEditedObject->RemoveProfile();
216       myEditedObject->Update();
217     }
218
219     erasePreview();
220     return;
221   }
222
223   // Update channel data
224   if ( !theIsInit )
225   {
226     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
227       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
228     myEditedObject->SetGuideLine( aGuideLine );
229
230     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
231       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
232     myEditedObject->SetProfile( aProfile );
233
234     if ( myEditedObject->IsMustBeUpdated() )
235       myEditedObject->Update();
236   }
237
238   LightApp_Application* anApp = module()->getApp();
239   
240   if ( !getPreviewManager() )
241     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
242                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
243
244   OCCViewer_ViewManager* aViewManager = getPreviewManager();
245   if ( aViewManager && !myPreviewPrs )
246   {
247     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
248     {
249       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
250       if ( !aCtx.IsNull() )
251         myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject, getPreviewZLayer() );
252     }
253   }
254
255   if ( !aViewManager || !myPreviewPrs )
256     return;
257
258   myPreviewPrs->update( true, true );
259 }
260
261 void HYDROGUI_ChannelOp::erasePreview()
262 {
263   if( myPreviewPrs )
264   {
265     delete myPreviewPrs;
266     myPreviewPrs = 0;
267   }
268 }