Salome HOME
Fix for the bug #318: Different styles in folders' design in Object browser.
[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   myViewManager( 0 ),
49   myPreviewPrs( 0 )
50 {
51   setName( theIsEdit ? tr( "EDIT_CHANNEL" ) : tr( "CREATE_CHANNEL" ) );
52 }
53
54 HYDROGUI_ChannelOp::~HYDROGUI_ChannelOp()
55 {
56   erasePreview();
57 }
58
59 void HYDROGUI_ChannelOp::startOperation()
60 {
61   HYDROGUI_Operation::startOperation();
62
63   // We start operation in the document
64   startDocOperation();
65
66   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
67
68   aPanel->blockSignals( true );
69
70   aPanel->reset();
71
72   myEditedObject = getObjectToEdit();
73
74   QString aSelectedGuideLine, aSelectedProfile;
75
76   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
77   if ( myIsEdit && !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   // collect information about existing 3d polylines
91   QStringList aGuideLines;
92   QStringList anAllGuideLines = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINE );
93   foreach( QString aGuideLine, anAllGuideLines )
94   {
95     Handle( HYDROData_Polyline3D ) aPolyline3d = 
96       Handle( HYDROData_Polyline3D )::DownCast( 
97     HYDROGUI_Tool::FindObjectByName( module(), aGuideLine, KIND_POLYLINE ) );
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( aGuideLine );
106       }
107     }
108   }
109
110   // collect information about existing profiles
111   QStringList aProfiles = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_PROFILE );
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( true );
124 }
125
126
127 void HYDROGUI_ChannelOp::abortOperation()
128 {
129   erasePreview();
130   abortDocOperation();
131
132   HYDROGUI_Operation::abortOperation();
133 }
134
135 void HYDROGUI_ChannelOp::commitOperation()
136 {
137   erasePreview();
138
139   HYDROGUI_Operation::commitOperation();
140 }
141
142 HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
143 {
144   HYDROGUI_ChannelDlg* aPanel = new HYDROGUI_ChannelDlg( module(), getName() );
145   connect( aPanel, SIGNAL( CreatePreview() ), this,   SLOT( onCreatePreview() ) );
146   return aPanel;
147 }
148
149 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
150                                        QString& theErrorMsg )
151 {
152   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
153   if ( !aPanel )
154     return false;
155
156   QString anObjectName = aPanel->getObjectName().simplified();
157   if ( anObjectName.isEmpty() )
158   {
159     theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
160     return false;
161   }
162
163   if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
164   {
165     // check that there are no other objects with the same name in the document
166     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
167     if( !anObject.IsNull() )
168     {
169       theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
170       return false;
171     }
172   }
173
174   if ( myEditedObject.IsNull() )
175     return false;
176
177   myEditedObject->SetName( anObjectName );
178
179   if ( !myIsEdit )
180   {
181     myEditedObject->SetFillingColor( HYDROData_Channel::DefaultFillingColor() );
182     myEditedObject->SetBorderColor( HYDROData_Channel::DefaultBorderColor() );
183   }
184
185   erasePreview();
186
187   if( !myIsEdit )
188     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
189
190   module()->setIsToUpdate( myEditedObject );
191
192   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
193
194   return true;
195 }
196
197 Handle(HYDROData_Channel) HYDROGUI_ChannelOp::getObjectToEdit() const
198 {
199   return myIsEdit ? Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) ) :
200                     Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
201 }
202
203 void HYDROGUI_ChannelOp::onCreatePreview( const bool theIsInit )
204 {
205   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
206   if ( !aPanel || myEditedObject.IsNull() )
207     return;
208
209   QString aGuideLineName = aPanel->getGuideLineName();
210   QString aProfileName = aPanel->getProfileName();
211   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
212   {
213     if ( !theIsInit )
214     {
215       myEditedObject->RemoveGuideLine();
216       myEditedObject->RemoveProfile();
217       myEditedObject->Update();
218     }
219
220     erasePreview();
221     return;
222   }
223
224   // Update channel data
225   if ( !theIsInit )
226   {
227     Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
228       HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
229     myEditedObject->SetGuideLine( aGuideLine );
230
231     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
232       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
233     myEditedObject->SetProfile( aProfile );
234
235     if ( myEditedObject->IsMustBeUpdated() )
236       myEditedObject->Update();
237   }
238
239   LightApp_Application* anApp = module()->getApp();
240   if ( !myViewManager )
241     myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
242       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
243
244   if ( myViewManager && !myPreviewPrs )
245   {
246     if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
247     {
248       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
249       if ( !aCtx.IsNull() )
250         myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject );
251     }
252   }
253
254   if ( !myViewManager || !myPreviewPrs )
255     return;
256
257   myPreviewPrs->update( true, true );
258 }
259
260 void HYDROGUI_ChannelOp::erasePreview()
261 {
262   if( myPreviewPrs )
263   {
264     delete myPreviewPrs;
265     myPreviewPrs = 0;
266   }
267 }