Salome HOME
Fix for the bug #255: VTK viewer is not updated after modification of objects.
[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
43 HYDROGUI_ChannelOp::HYDROGUI_ChannelOp( HYDROGUI_Module* theModule,
44                                        const bool theIsEdit )
45 : HYDROGUI_Operation( theModule ),
46   myIsEdit( theIsEdit ),
47   myViewManager( 0 ),
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 = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINE );
91
92   // collect information about existing profiles
93   QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE );
94
95   aPanel->setObjectName( anObjectName );
96
97   aPanel->setGuideLineNames( aGuideLines );
98   aPanel->setProfileNames( aProfiles );
99
100   aPanel->setGuideLineName( aSelectedGuideLine );
101   aPanel->setProfileName( aSelectedProfile );
102
103   aPanel->blockSignals( false );
104
105   onCreatePreview( true );
106 }
107
108
109 void HYDROGUI_ChannelOp::abortOperation()
110 {
111   erasePreview();
112   abortDocOperation();
113
114   HYDROGUI_Operation::abortOperation();
115 }
116
117 void HYDROGUI_ChannelOp::commitOperation()
118 {
119   erasePreview();
120
121   HYDROGUI_Operation::commitOperation();
122 }
123
124 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
125 {
126   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
127   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
128   return aPanel;
129 }
130
131 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
132                                        QString& theErrorMsg )
133 {
134   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
135   if ( !aPanel )
136     return false;
137
138   QString anObjectName = aPanel->getObjectName().simplified();
139   if ( anObjectName.isEmpty() )
140   {
141     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
142     return false;
143   }
144
145   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
146   {
147     // check that there are no other objects with the same name in the document
148     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
149     if( !anObject.IsNull() )
150     {
151       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
152       return false;
153     }
154   }
155
156   if ( myEditedObject.IsNull() )
157     return false;
158
159   myEditedObject->SetName( anObjectName );
160
161   if ( !myIsEdit )
162   {
163     myEditedObject->SetFillingColor( HYDROData_Channel::DefaultFillingColor() );
164     myEditedObject->SetBorderColor( HYDROData_Channel::DefaultBorderColor() );
165   }
166
167   erasePreview();
168
169   if( !myIsEdit )
170     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
171
172   module()->setIsToUpdate( myEditedObject );
173
174   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
175
176   return true;
177 }
178
179 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::getObjectToEdit() const
180 {
181   return myIsEdit ? Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) ) :
182                     Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
183 }
184
185 void HYDROGUI_ChannelOp::onCreatePreview( const bool theIsInit )
186 {
187   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
188   if ( !aPanel || myEditedObject.IsNull() )
189     return;
190
191   QString aGuideLineName = aPanel->getGuideLineName();
192   QString aProfileName = aPanel->getProfileName();
193   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
194   {
195     if ( !theIsInit )
196     {
197       myEditedObject->RemoveGuideLine();
198       myEditedObject->RemoveProfile();
199       myEditedObject->Update();
200     }
201
202     erasePreview();
203     return;
204   }
205
206   // Update channel data
207   if ( !theIsInit )
208   {
209     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
210       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
211     myEditedObject->SetGuideLine( aGuideLine );
212
213     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
214       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
215     myEditedObject->SetProfile( aProfile );
216
217     if ( myEditedObject->IsMustBeUpdated() )
218       myEditedObject->Update();
219   }
220
221   LightApp_Application* anApp = module()->getApp();
222   if ( !myViewManager )
223     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
224       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
225
226   if ( myViewManager && !myPreviewPrs )
227   {
228     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
229     {
230       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
231       if ( !aCtx.IsNull() )
232         myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject );
233     }
234   }
235
236   if ( !myViewManager || !myPreviewPrs )
237     return;
238
239   myPreviewPrs->update();
240 }
241
242 void HYDROGUI_ChannelOp::erasePreview()
243 {
244   if( myPreviewPrs )
245   {
246     delete myPreviewPrs;
247     myPreviewPrs = 0;
248   }
249 }