Salome HOME
OCC functionality moving out from the widget
[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   if( myIsEdit )
72     myEditedObject = Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
73   else 
74     myEditedObject = Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
75
76   QString aSelectedGuideLine, aSelectedProfile;
77
78   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
79   if ( myIsEdit && !myEditedObject.IsNull() )
80   {
81     anObjectName = myEditedObject->GetName();
82
83     Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
84     if ( !aRefGuideLine.IsNull() )
85       aSelectedGuideLine = aRefGuideLine->GetName();
86
87     Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
88     if ( !aRefProfile.IsNull() )
89       aSelectedProfile = aRefProfile->GetName();
90   }
91
92   // collect information about existing 3d polylines
93   QStringList aGuideLines = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINE );
94
95   // collect information about existing profiles
96   QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE );
97
98   aPanel->setObjectName( anObjectName );
99
100   aPanel->setGuideLineNames( aGuideLines );
101   aPanel->setProfileNames( aProfiles );
102
103   aPanel->setGuideLineName( aSelectedGuideLine );
104   aPanel->setProfileName( aSelectedProfile );
105
106   aPanel->blockSignals( false );
107
108   onCreatePreview( true );
109 }
110
111 void HYDROGUI_ChannelOp::abortOperation()
112 {
113   erasePreview();
114
115   HYDROGUI_Operation::abortOperation();
116 }
117
118 void HYDROGUI_ChannelOp::commitOperation()
119 {
120   erasePreview();
121
122   HYDROGUI_Operation::commitOperation();
123 }
124
125 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
126 {
127   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
128   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
129   return aPanel;
130 }
131
132 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
133                                               QString& theErrorMsg )
134 {
135   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
136   if ( !aPanel )
137     return false;
138
139   QString anObjectName = aPanel->getObjectName().simplified();
140   if ( anObjectName.isEmpty() )
141   {
142     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
143     return false;
144   }
145
146   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
147   {
148     // check that there are no other objects with the same name in the document
149     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
150     if( !anObject.IsNull() )
151     {
152       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
153       return false;
154     }
155   }
156
157   if ( myEditedObject.IsNull() )
158     return false;
159
160   myEditedObject->SetName( anObjectName );
161
162   if ( !myIsEdit ) {
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   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
173
174   return true;
175 }
176
177 void HYDROGUI_ChannelOp::onCreatePreview( const bool theIsInit )
178 {
179   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
180   if ( !aPanel || myEditedObject.IsNull() )
181     return;
182
183   QString aGuideLineName = aPanel->getGuideLineName();
184   QString aProfileName = aPanel->getProfileName();
185   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
186   {
187     if ( !theIsInit )
188     {
189       myEditedObject->RemoveGuideLine();
190       myEditedObject->RemoveProfile();
191       myEditedObject->Update();
192     }
193
194     erasePreview();
195     return;
196   }
197
198   // Update channel data
199   if ( !theIsInit )
200   {
201     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
202       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
203     myEditedObject->SetGuideLine( aGuideLine );
204
205     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
206       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
207     myEditedObject->SetProfile( aProfile );
208
209     if ( myEditedObject->IsMustBeUpdated() )
210       myEditedObject->Update();
211   }
212
213   LightApp_Application* anApp = module()->getApp();
214   if ( !myViewManager )
215     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
216       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
217
218   if ( myViewManager && !myPreviewPrs )
219   {
220     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
221     {
222       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
223       if ( !aCtx.IsNull() )
224         myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject );
225     }
226   }
227
228   if ( !myViewManager || !myPreviewPrs )
229     return;
230
231   myPreviewPrs->update();
232 }
233
234 void HYDROGUI_ChannelOp::erasePreview()
235 {
236   if( myPreviewPrs )
237   {
238     delete myPreviewPrs;
239     myPreviewPrs = 0;
240   }
241 }